From e2ee6feff9f2404c1986b25c30b7ebd4d7a46ab2 Mon Sep 17 00:00:00 2001 From: eutkin <40791167+eutkin@users.noreply.github.com> Date: Thu, 5 Jul 2018 10:21:16 +0300 Subject: [PATCH] Added createRelease(), updateRelease() (#211) --- .../java/org/gitlab4j/api/CommitsApi.java | 59 ++++++++++++++---- .../java/org/gitlab4j/api/RepositoryApi.java | 62 +++++++++++++++---- .../org/gitlab4j/api/models/CommitRef.java | 57 +++++++++++++++++ .../java/org/gitlab4j/api/TestCommitsApi.java | 33 +++++++--- 4 files changed, 177 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/gitlab4j/api/models/CommitRef.java diff --git a/src/main/java/org/gitlab4j/api/CommitsApi.java b/src/main/java/org/gitlab4j/api/CommitsApi.java index 818be9aa..bdcacd2b 100644 --- a/src/main/java/org/gitlab4j/api/CommitsApi.java +++ b/src/main/java/org/gitlab4j/api/CommitsApi.java @@ -1,22 +1,24 @@ package org.gitlab4j.api; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.Date; -import java.util.List; -import java.util.Optional; - -import javax.ws.rs.core.Form; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.Response; - import org.gitlab4j.api.models.Comment; import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.CommitAction; import org.gitlab4j.api.models.CommitPayload; +import org.gitlab4j.api.models.CommitRef; import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.utils.ISO8601; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +import static org.gitlab4j.api.models.CommitRef.RefType.all; + /** * This class implements the client side API for the GitLab commits calls. */ @@ -251,6 +253,41 @@ public class CommitsApi extends AbstractApi { } } + /** + * Get a specific commit identified by the commit hash or name of a branch or tag as an Optional instance + * + * GET /projects/:id/repository/commits/:sha/refs + * + * @param projectId the project ID that the commit belongs to + * @param sha a commit hash or name of a branch or tag + * @return Get all references (from branches or tags) a commit is pushed to + * @throws GitLabApiException GitLabApiException if any exception occurs during execution + * @since Gitlab 10.6 + */ + public List getCommitRefs(int projectId, String sha) throws GitLabApiException { + return getCommitRefs(projectId, sha, all); + } + + /** + * Get a specific commit identified by the commit hash or name of a branch or tag as an Optional instance + * + * GET /projects/:id/repository/commits/:sha/refs?type=:refType + * + * @param projectId the project ID that the commit belongs to + * @param sha a commit hash or name of a branch or tag + * @param refType the scope of commits. Possible values branch, tag, all. Default is all. + * @return Get all references (from branches or tags) a commit is pushed to + * @throws GitLabApiException GitLabApiException if any exception occurs during execution + * @since Gitlab 10.6 + */ + public List getCommitRefs(int projectId, String sha, CommitRef.RefType refType) throws GitLabApiException { + Form form = new GitLabApiForm() + .withParam("type", refType) + .withParam(PER_PAGE_PARAM, getDefaultPerPage()); + Response response = get(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "commits", sha, "refs"); + return (response.readEntity(new GenericType>(){})); + } + /** * Get the list of diffs of a commit in a project. * @@ -302,7 +339,7 @@ public class CommitsApi extends AbstractApi { Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "comments"); return (response.readEntity(new GenericType>() {})); } - + /** * Get a Pager of the comments of a commit in a project. * diff --git a/src/main/java/org/gitlab4j/api/RepositoryApi.java b/src/main/java/org/gitlab4j/api/RepositoryApi.java index 21867fcb..2d0e3685 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryApi.java @@ -1,5 +1,18 @@ package org.gitlab4j.api; +import org.gitlab4j.api.GitLabApi.ApiVersion; +import org.gitlab4j.api.models.Branch; +import org.gitlab4j.api.models.CompareResults; +import org.gitlab4j.api.models.Contributor; +import org.gitlab4j.api.models.Release; +import org.gitlab4j.api.models.Tag; +import org.gitlab4j.api.models.TreeItem; +import org.gitlab4j.api.utils.FileUtils; + +import javax.ws.rs.core.Form; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -9,19 +22,6 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.List; -import javax.ws.rs.core.Form; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.gitlab4j.api.GitLabApi.ApiVersion; -import org.gitlab4j.api.models.Branch; -import org.gitlab4j.api.models.CompareResults; -import org.gitlab4j.api.models.Contributor; -import org.gitlab4j.api.models.Tag; -import org.gitlab4j.api.models.TreeItem; -import org.gitlab4j.api.utils.FileUtils; - /** * This class provides an entry point to all the GitLab API repository calls. */ @@ -225,6 +225,42 @@ public class RepositoryApi extends AbstractApi { return (response.readEntity(Tag.class)); } + /** + * Add release notes to the existing git tag. + * + * POST /projects/:id/repository/tags/:tagName/release + * + * @param projectId the ID of the project + * @param tagName the name of a tag + * @param releaseNotes release notes with markdown support + * @return a Tag instance containing info on the newly created tag + * @throws GitLabApiException if any exception occurs + */ + public Release createRelease(Integer projectId, String tagName, String releaseNotes) throws GitLabApiException { + Form formData = new GitLabApiForm() + .withParam("description", releaseNotes, false); + Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "tags", tagName, "release"); + return (response.readEntity(Release.class)); + } + + /** + * Updates the release notes of a given release. + * + * PUT /projects/:id/repository/tags/:tagName/release + * + * @param projectId the ID of the project + * @param tagName the name of a tag + * @param releaseNotes release notes with markdown support + * @return a Tag instance containing info on the newly created tag + * @throws GitLabApiException if any exception occurs + */ + public Release updateRelease(Integer projectId, String tagName, String releaseNotes) throws GitLabApiException { + Form formData = new GitLabApiForm() + .withParam("description", releaseNotes, false); + Response response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "tags", tagName, "release"); + return (response.readEntity(Release.class)); + } + /** * Creates a tag on a particular ref of a given project. A message and a File instance containing the * release notes are optional. This method is the same as {@link #createTag(Integer, String, String, String, String)}, diff --git a/src/main/java/org/gitlab4j/api/models/CommitRef.java b/src/main/java/org/gitlab4j/api/models/CommitRef.java new file mode 100644 index 00000000..39c7f806 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/CommitRef.java @@ -0,0 +1,57 @@ +package org.gitlab4j.api.models; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import org.gitlab4j.api.utils.JacksonJsonEnumHelper; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author Евгений Уткин (evgeny.utkin@mediascope.net) + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class CommitRef { + + private RefType type; + private String name; + + public enum RefType { + BRANCH, TAG, ALL; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(RefType.class); + + @JsonCreator + public static RefType forValue(String value) { + return enumHelper.forValue(value); + } + + @JsonValue + public String toValue() { + return (enumHelper.toString(this)); + } + + @Override + public String toString() { + return (enumHelper.toString(this)); + } + } + + public RefType getType() { + return type; + } + + public void setType(RefType type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/gitlab4j/api/TestCommitsApi.java b/src/test/java/org/gitlab4j/api/TestCommitsApi.java index 1499de19..c2c362b7 100644 --- a/src/test/java/org/gitlab4j/api/TestCommitsApi.java +++ b/src/test/java/org/gitlab4j/api/TestCommitsApi.java @@ -1,18 +1,9 @@ package org.gitlab4j.api; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; - -import java.util.Date; -import java.util.List; - -import javax.ws.rs.core.Response; - import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.models.Comment; import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.CommitRef; import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Project; import org.junit.Before; @@ -21,6 +12,15 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; +import javax.ws.rs.core.Response; +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + /** * In order for these tests to run you must set the following properties in test-gitlab4j.properties * @@ -156,6 +156,19 @@ public class TestCommitsApi { assertTrue(pager.getTotalItems() > 0); } + @Test + public void testCommitRefs() throws GitLabApiException { + assertNotNull(testProject); + + List commits = gitLabApi.getCommitsApi().getCommits(testProject.getId()); + assertNotNull(commits); + assertTrue(commits.size() > 0); + + List commitRefs = gitLabApi.getCommitsApi().getCommitRefs(testProject.getId(), commits.get(0).getId()); + assertNotNull(commits); + assertTrue(commits.size() > 0); + } + @Test public void testCommitsSinceWithPath() throws GitLabApiException { -- GitLab