Skip to content

Commit e2ee6fe

Browse files
eutkingmessner
authored andcommitted
Added createRelease(), updateRelease() (#211)
1 parent 7569887 commit e2ee6fe

File tree

4 files changed

+177
-34
lines changed

4 files changed

+177
-34
lines changed

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package org.gitlab4j.api;
22

3-
import java.io.UnsupportedEncodingException;
4-
import java.net.URLEncoder;
5-
import java.util.Date;
6-
import java.util.List;
7-
import java.util.Optional;
8-
9-
import javax.ws.rs.core.Form;
10-
import javax.ws.rs.core.GenericType;
11-
import javax.ws.rs.core.Response;
12-
133
import org.gitlab4j.api.models.Comment;
144
import org.gitlab4j.api.models.Commit;
155
import org.gitlab4j.api.models.CommitAction;
166
import org.gitlab4j.api.models.CommitPayload;
7+
import org.gitlab4j.api.models.CommitRef;
178
import org.gitlab4j.api.models.Diff;
189
import org.gitlab4j.api.utils.ISO8601;
1910

11+
import javax.ws.rs.core.Form;
12+
import javax.ws.rs.core.GenericType;
13+
import javax.ws.rs.core.Response;
14+
import java.io.UnsupportedEncodingException;
15+
import java.net.URLEncoder;
16+
import java.util.Date;
17+
import java.util.List;
18+
import java.util.Optional;
19+
20+
import static org.gitlab4j.api.models.CommitRef.RefType.all;
21+
2022
/**
2123
* This class implements the client side API for the GitLab commits calls.
2224
*/
@@ -251,6 +253,41 @@ public Optional<Commit> getOptionalCommit(int projectId, String sha) {
251253
}
252254
}
253255

256+
/**
257+
* Get a specific commit identified by the commit hash or name of a branch or tag as an Optional instance
258+
*
259+
* GET /projects/:id/repository/commits/:sha/refs
260+
*
261+
* @param projectId the project ID that the commit belongs to
262+
* @param sha a commit hash or name of a branch or tag
263+
* @return Get all references (from branches or tags) a commit is pushed to
264+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
265+
* @since Gitlab 10.6
266+
*/
267+
public List<CommitRef> getCommitRefs(int projectId, String sha) throws GitLabApiException {
268+
return getCommitRefs(projectId, sha, all);
269+
}
270+
271+
/**
272+
* Get a specific commit identified by the commit hash or name of a branch or tag as an Optional instance
273+
*
274+
* GET /projects/:id/repository/commits/:sha/refs?type=:refType
275+
*
276+
* @param projectId the project ID that the commit belongs to
277+
* @param sha a commit hash or name of a branch or tag
278+
* @param refType the scope of commits. Possible values branch, tag, all. Default is all.
279+
* @return Get all references (from branches or tags) a commit is pushed to
280+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
281+
* @since Gitlab 10.6
282+
*/
283+
public List<CommitRef> getCommitRefs(int projectId, String sha, CommitRef.RefType refType) throws GitLabApiException {
284+
Form form = new GitLabApiForm()
285+
.withParam("type", refType)
286+
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
287+
Response response = get(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "commits", sha, "refs");
288+
return (response.readEntity(new GenericType<List<CommitRef>>(){}));
289+
}
290+
254291
/**
255292
* Get the list of diffs of a commit in a project.
256293
*
@@ -302,7 +339,7 @@ public List<Comment> getComments(int projectId, String sha) throws GitLabApiExce
302339
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "comments");
303340
return (response.readEntity(new GenericType<List<Comment>>() {}));
304341
}
305-
342+
306343
/**
307344
* Get a Pager of the comments of a commit in a project.
308345
*

src/main/java/org/gitlab4j/api/RepositoryApi.java

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package org.gitlab4j.api;
22

3+
import org.gitlab4j.api.GitLabApi.ApiVersion;
4+
import org.gitlab4j.api.models.Branch;
5+
import org.gitlab4j.api.models.CompareResults;
6+
import org.gitlab4j.api.models.Contributor;
7+
import org.gitlab4j.api.models.Release;
8+
import org.gitlab4j.api.models.Tag;
9+
import org.gitlab4j.api.models.TreeItem;
10+
import org.gitlab4j.api.utils.FileUtils;
11+
12+
import javax.ws.rs.core.Form;
13+
import javax.ws.rs.core.GenericType;
14+
import javax.ws.rs.core.MediaType;
15+
import javax.ws.rs.core.Response;
316
import java.io.File;
417
import java.io.IOException;
518
import java.io.InputStream;
@@ -9,19 +22,6 @@
922
import java.nio.file.StandardCopyOption;
1023
import java.util.List;
1124

12-
import javax.ws.rs.core.Form;
13-
import javax.ws.rs.core.GenericType;
14-
import javax.ws.rs.core.MediaType;
15-
import javax.ws.rs.core.Response;
16-
17-
import org.gitlab4j.api.GitLabApi.ApiVersion;
18-
import org.gitlab4j.api.models.Branch;
19-
import org.gitlab4j.api.models.CompareResults;
20-
import org.gitlab4j.api.models.Contributor;
21-
import org.gitlab4j.api.models.Tag;
22-
import org.gitlab4j.api.models.TreeItem;
23-
import org.gitlab4j.api.utils.FileUtils;
24-
2525
/**
2626
* This class provides an entry point to all the GitLab API repository calls.
2727
*/
@@ -225,6 +225,42 @@ public Tag createTag(Integer projectId, String tagName, String ref, String messa
225225
return (response.readEntity(Tag.class));
226226
}
227227

228+
/**
229+
* Add release notes to the existing git tag.
230+
*
231+
* POST /projects/:id/repository/tags/:tagName/release
232+
*
233+
* @param projectId the ID of the project
234+
* @param tagName the name of a tag
235+
* @param releaseNotes release notes with markdown support
236+
* @return a Tag instance containing info on the newly created tag
237+
* @throws GitLabApiException if any exception occurs
238+
*/
239+
public Release createRelease(Integer projectId, String tagName, String releaseNotes) throws GitLabApiException {
240+
Form formData = new GitLabApiForm()
241+
.withParam("description", releaseNotes, false);
242+
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "tags", tagName, "release");
243+
return (response.readEntity(Release.class));
244+
}
245+
246+
/**
247+
* Updates the release notes of a given release.
248+
*
249+
* PUT /projects/:id/repository/tags/:tagName/release
250+
*
251+
* @param projectId the ID of the project
252+
* @param tagName the name of a tag
253+
* @param releaseNotes release notes with markdown support
254+
* @return a Tag instance containing info on the newly created tag
255+
* @throws GitLabApiException if any exception occurs
256+
*/
257+
public Release updateRelease(Integer projectId, String tagName, String releaseNotes) throws GitLabApiException {
258+
Form formData = new GitLabApiForm()
259+
.withParam("description", releaseNotes, false);
260+
Response response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "tags", tagName, "release");
261+
return (response.readEntity(Release.class));
262+
}
263+
228264
/**
229265
* Creates a tag on a particular ref of a given project. A message and a File instance containing the
230266
* release notes are optional. This method is the same as {@link #createTag(Integer, String, String, String, String)},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.gitlab4j.api.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
6+
7+
import javax.xml.bind.annotation.XmlAccessType;
8+
import javax.xml.bind.annotation.XmlAccessorType;
9+
import javax.xml.bind.annotation.XmlRootElement;
10+
11+
/**
12+
* @author Евгений Уткин (evgeny.utkin@mediascope.net)
13+
*/
14+
@XmlRootElement
15+
@XmlAccessorType(XmlAccessType.FIELD)
16+
public class CommitRef {
17+
18+
private RefType type;
19+
private String name;
20+
21+
public enum RefType {
22+
BRANCH, TAG, ALL;
23+
24+
private static JacksonJsonEnumHelper<RefType> enumHelper = new JacksonJsonEnumHelper<>(RefType.class);
25+
26+
@JsonCreator
27+
public static RefType forValue(String value) {
28+
return enumHelper.forValue(value);
29+
}
30+
31+
@JsonValue
32+
public String toValue() {
33+
return (enumHelper.toString(this));
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return (enumHelper.toString(this));
39+
}
40+
}
41+
42+
public RefType getType() {
43+
return type;
44+
}
45+
46+
public void setType(RefType type) {
47+
this.type = type;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public void setName(String name) {
55+
this.name = name;
56+
}
57+
}

src/test/java/org/gitlab4j/api/TestCommitsApi.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package org.gitlab4j.api;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.assertTrue;
6-
import static org.junit.Assume.assumeTrue;
7-
8-
import java.util.Date;
9-
import java.util.List;
10-
11-
import javax.ws.rs.core.Response;
12-
133
import org.gitlab4j.api.GitLabApi.ApiVersion;
144
import org.gitlab4j.api.models.Comment;
155
import org.gitlab4j.api.models.Commit;
6+
import org.gitlab4j.api.models.CommitRef;
167
import org.gitlab4j.api.models.Diff;
178
import org.gitlab4j.api.models.Project;
189
import org.junit.Before;
@@ -21,6 +12,15 @@
2112
import org.junit.Test;
2213
import org.junit.runners.MethodSorters;
2314

15+
import javax.ws.rs.core.Response;
16+
import java.util.Date;
17+
import java.util.List;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assume.assumeTrue;
23+
2424
/**
2525
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
2626
*
@@ -156,6 +156,19 @@ public void testCommitsSince() throws GitLabApiException {
156156
assertTrue(pager.getTotalItems() > 0);
157157
}
158158

159+
@Test
160+
public void testCommitRefs() throws GitLabApiException {
161+
assertNotNull(testProject);
162+
163+
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId());
164+
assertNotNull(commits);
165+
assertTrue(commits.size() > 0);
166+
167+
List<CommitRef> commitRefs = gitLabApi.getCommitsApi().getCommitRefs(testProject.getId(), commits.get(0).getId());
168+
assertNotNull(commits);
169+
assertTrue(commits.size() > 0);
170+
}
171+
159172
@Test
160173
public void testCommitsSinceWithPath() throws GitLabApiException {
161174

0 commit comments

Comments
 (0)