Skip to content

Commit 0aa2b58

Browse files
Stel000gmessner
authored andcommitted
Add project milestones,issue notes and labels API. (#99)
* [Add]Add gitlab project milestone api. * [Add]Check projectId not null. * [Fix]Fix some bugs. * [Add]Add gitlab issue notes api. * [Add]Add gitlab labels api. * [Imp]Add attr-required to Param. * [Fix]Fix test error.
1 parent 3a9bd74 commit 0aa2b58

File tree

8 files changed

+441
-19
lines changed

8 files changed

+441
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>org.gitlab4j</groupId>
66
<artifactId>gitlab4j-api</artifactId>
77
<packaging>jar</packaging>
8-
<version>4.6.10-SNAPSHOT</version>
8+
<version>4.7.1-SNAPSHOT</version>
99
<name>GitLab API Java Client</name>
1010
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API</description>
1111
<url>http://www.messners.com/#gitlab4j-api/gitlab4j-api.html</url>

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

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

3-
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
4-
53
import com.fasterxml.jackson.annotation.JsonCreator;
64
import com.fasterxml.jackson.annotation.JsonValue;
5+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
76

87
public interface Constants {
98

@@ -214,6 +213,28 @@ public String toString() {
214213
}
215214
}
216215

216+
public enum MilestoneState {
217+
218+
ACTIVE, CLOSED, ACTIVATE, CLOSE;
219+
220+
private static JacksonJsonEnumHelper<MilestoneState> enumHelper = new JacksonJsonEnumHelper<>(MilestoneState.class);
221+
222+
@JsonCreator
223+
public static MilestoneState forValue(String value) {
224+
return enumHelper.forValue(value);
225+
}
226+
227+
@JsonValue
228+
public String toValue() {
229+
return (enumHelper.toString(this));
230+
}
231+
232+
@Override
233+
public String toString() {
234+
return (enumHelper.toString(this));
235+
}
236+
}
237+
217238
/** Enum to use for specifying the event action_type. */
218239
public enum ActionType {
219240

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

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

3-
import java.util.Map;
4-
5-
import javax.ws.rs.core.Response;
6-
73
import org.gitlab4j.api.Constants.TokenType;
84
import org.gitlab4j.api.models.Session;
95
import org.gitlab4j.api.models.User;
106
import org.gitlab4j.api.models.Version;
117

8+
import javax.ws.rs.core.Response;
9+
import java.util.Map;
10+
1211
/**
1312
* This class is provides a simplified interface to a GitLab API server, and divides the API up into
1413
* a separate API class for each concern.
@@ -35,6 +34,7 @@ public String getApiNamespace() {
3534
private GroupApi groupApi;
3635
private IssuesApi issuesApi;
3736
private MergeRequestApi mergeRequestApi;
37+
private MileStonesApi mileStonesApi;
3838
private NamespaceApi namespaceApi;
3939
private PipelineApi pipelineApi;
4040
private ProjectApi projectApi;
@@ -44,6 +44,7 @@ public String getApiNamespace() {
4444
private SessionApi sessoinApi;
4545
private UserApi userApi;
4646
private JobApi jobApi;
47+
private LabelsApi labelsApi;
4748
private NotesApi notesApi;
4849
private EventsApi eventsApi;
4950

@@ -321,7 +322,9 @@ public GitLabApi(ApiVersion apiVersion, String hostUrl, TokenType tokenType, Str
321322
groupApi = new GroupApi(this);
322323
issuesApi = new IssuesApi(this);
323324
jobApi = new JobApi(this);
325+
labelsApi = new LabelsApi(this);
324326
mergeRequestApi = new MergeRequestApi(this);
327+
mileStonesApi = new MileStonesApi(this);
325328
namespaceApi = new NamespaceApi(this);
326329
notesApi = new NotesApi(this);
327330
pipelineApi = new PipelineApi(this);
@@ -530,6 +533,10 @@ public JobApi getJobApi() {
530533
return (jobApi);
531534
}
532535

536+
public LabelsApi getLabelsApi() {
537+
return labelsApi;
538+
}
539+
533540
/**
534541
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
535542
* to perform all merge request related API calls.
@@ -540,6 +547,10 @@ public MergeRequestApi getMergeRequestApi() {
540547
return (mergeRequestApi);
541548
}
542549

550+
public MileStonesApi getMileStonesApi() {
551+
return mileStonesApi;
552+
}
553+
543554
/**
544555
* Gets the NamespaceApi instance owned by this GitLabApi instance. The NamespaceApi is used
545556
* to perform all namespace related API calls.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.Label;
4+
5+
import javax.ws.rs.core.GenericType;
6+
import javax.ws.rs.core.Response;
7+
import java.util.List;
8+
9+
public class LabelsApi extends AbstractApi {
10+
public LabelsApi(GitLabApi gitLabApi) {
11+
super(gitLabApi);
12+
}
13+
14+
public List<Label> getLabels(Integer projectId) throws GitLabApiException {
15+
if (projectId == null) {
16+
throw new RuntimeException("projectId cannot be null");
17+
}
18+
Response response = get(javax.ws.rs.core.Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "labels");
19+
return (response.readEntity(new GenericType<List<Label>>() {
20+
}));
21+
}
22+
23+
public List<Label> getLabels(Integer projectId, int page, int perPage) throws GitLabApiException {
24+
if (projectId == null) {
25+
throw new RuntimeException("projectId cannot be null");
26+
}
27+
Response response = get(javax.ws.rs.core.Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "labels");
28+
return (response.readEntity(new GenericType<List<Label>>() {
29+
}));
30+
}
31+
32+
public Label createLabel(Integer projectId, String name, String color, String description) throws GitLabApiException {
33+
return (createLabel(projectId, name, color, description, null));
34+
}
35+
36+
public Label createLabel(Integer projectId, String name, String color) throws GitLabApiException {
37+
return (createLabel(projectId, name, color, null, null));
38+
}
39+
40+
public Label createLabel(Integer projectId, String name, String color, Integer priority) throws GitLabApiException {
41+
return (createLabel(projectId, name, color, null, priority));
42+
}
43+
44+
public Label createLabel(Integer projectId, String name, String color, String description, Integer priority) throws GitLabApiException {
45+
if (projectId == null) {
46+
throw new RuntimeException("projectId cannot be null");
47+
}
48+
GitLabApiForm formData = new GitLabApiForm()
49+
.withParam("name", name, true)
50+
.withParam("color", color, true)
51+
.withParam("description", description)
52+
.withParam("priority", priority);
53+
Response response = post(Response.Status.OK, formData, "projects", projectId, "labels");
54+
return (response.readEntity(Label.class));
55+
}
56+
57+
public Label updateLabelName(Integer projectId, String name, String newName, String description, Integer priority) throws GitLabApiException {
58+
return (updateLabel(projectId, name, newName, null, description, priority));
59+
}
60+
61+
public Label updateLabelColor(Integer projectId, String name, String color, String description, Integer priority) throws GitLabApiException {
62+
return (updateLabel(projectId, name, null, color, description, priority));
63+
}
64+
65+
public Label updateLabel(Integer projectId, String name, String newName, String color, String description, Integer priority) throws GitLabApiException {
66+
if (projectId == null) {
67+
throw new RuntimeException("projectId cannot be null");
68+
}
69+
GitLabApiForm formData = new GitLabApiForm()
70+
.withParam("name", name, true)
71+
.withParam("new_name", newName)
72+
.withParam("color", color)
73+
.withParam("description", description)
74+
.withParam("priority", priority);
75+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "labels");
76+
return (response.readEntity(Label.class));
77+
}
78+
79+
public void deleteLabel(Integer projectId, String name) throws GitLabApiException {
80+
if (projectId == null) {
81+
throw new RuntimeException("projectId cannot be null");
82+
}
83+
GitLabApiForm formData = new GitLabApiForm()
84+
.withParam("name", name, true);
85+
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
86+
delete(expectedStatus, formData.asMap(), "projects", projectId, "labels");
87+
}
88+
89+
public Label subscribeLabel(Integer projectId, Integer labelId) throws GitLabApiException {
90+
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), "projects", projectId, "labels", labelId, "subscribe");
91+
return (response.readEntity(Label.class));
92+
}
93+
94+
public Label unsubscribeLabel(Integer projectId, Integer labelId) throws GitLabApiException {
95+
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), "projects", projectId, "labels", labelId, "unsubscribe");
96+
return (response.readEntity(Label.class));
97+
}
98+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.Issue;
4+
import org.gitlab4j.api.models.MergeRequest;
5+
import org.gitlab4j.api.models.Milestone;
6+
7+
import javax.ws.rs.core.Form;
8+
import javax.ws.rs.core.GenericType;
9+
import javax.ws.rs.core.Response;
10+
import java.util.Date;
11+
import java.util.List;
12+
13+
/**
14+
* This class implements the client side API for the GitLab groups calls.
15+
*/
16+
public class MileStonesApi extends AbstractApi {
17+
18+
public MileStonesApi(GitLabApi gitLabApi) {
19+
super(gitLabApi);
20+
}
21+
22+
public List<Milestone> getMilestones(Integer projectId) throws GitLabApiException {
23+
if (projectId == null) {
24+
throw new RuntimeException("projectId cannot be null");
25+
}
26+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones");
27+
return (response.readEntity(new GenericType<List<Milestone>>() {
28+
}));
29+
}
30+
31+
public List<Milestone> getMilestones(Integer projectId, int page, int perPage) throws GitLabApiException {
32+
if (projectId == null) {
33+
throw new RuntimeException("projectId cannot be null");
34+
}
35+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "milestones");
36+
return (response.readEntity(new GenericType<List<Milestone>>() {
37+
}));
38+
}
39+
40+
public List<Milestone> getMilestones(Integer projectId, MilestoneState state) throws GitLabApiException {
41+
if (projectId == null) {
42+
throw new RuntimeException("projectId cannot be null");
43+
}
44+
Form formData = new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage());
45+
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
46+
return (response.readEntity(new GenericType<List<Milestone>>() {
47+
}));
48+
}
49+
50+
public List<Milestone> getMilestones(Integer projectId, String search) throws GitLabApiException {
51+
if (projectId == null) {
52+
throw new RuntimeException("projectId cannot be null");
53+
}
54+
Form formData = new GitLabApiForm().withParam("search", search).withParam(PER_PAGE_PARAM, getDefaultPerPage());
55+
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
56+
return (response.readEntity(new GenericType<List<Milestone>>() {
57+
}));
58+
}
59+
60+
public List<Milestone> getMilestones(Integer projectId, MilestoneState state, String search) throws GitLabApiException {
61+
if (projectId == null) {
62+
throw new RuntimeException("projectId cannot be null");
63+
}
64+
Form formData = new GitLabApiForm().withParam("state", state).withParam("search", search).withParam(PER_PAGE_PARAM, getDefaultPerPage());
65+
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
66+
return (response.readEntity(new GenericType<List<Milestone>>() {
67+
}));
68+
}
69+
70+
public Milestone getMilestone(Integer projectId, int milestoneId) throws GitLabApiException {
71+
if (projectId == null) {
72+
throw new RuntimeException("projectId cannot be null");
73+
}
74+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId);
75+
return (response.readEntity(Milestone.class));
76+
}
77+
78+
public List<Issue> getIssues(Integer projectId, Integer milestoneId) throws GitLabApiException {
79+
if (projectId == null) {
80+
throw new RuntimeException("projectId cannot be null");
81+
}
82+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId, "issues");
83+
return (response.readEntity(new GenericType<List<Issue>>() {
84+
}));
85+
}
86+
87+
public List<MergeRequest> getMergeRequest(Integer projectId, Integer milestoneId) throws GitLabApiException {
88+
if (projectId == null) {
89+
throw new RuntimeException("projectId cannot be null");
90+
}
91+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId, "merge_requests");
92+
return (response.readEntity(new GenericType<List<MergeRequest>>() {
93+
}));
94+
}
95+
96+
public Milestone createMilestone(Integer projectId, String title, String description, Date dueDate, Date startDate) throws GitLabApiException {
97+
if (projectId == null) {
98+
throw new RuntimeException("projectId cannot be null");
99+
}
100+
GitLabApiForm formData = new GitLabApiForm()
101+
.withParam("title", title, true)
102+
.withParam("description", description)
103+
.withParam("due_date", dueDate)
104+
.withParam("start_date", startDate);
105+
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "milestones");
106+
return (response.readEntity(Milestone.class));
107+
}
108+
109+
public Milestone closeMilestone(Integer projectId, Integer milestoneId) throws GitLabApiException {
110+
if (projectId == null) {
111+
throw new RuntimeException("projectId cannot be null");
112+
}
113+
if (milestoneId == null) {
114+
throw new RuntimeException("milestoneId cannot be null");
115+
}
116+
GitLabApiForm formData = new GitLabApiForm().withParam("state_event", MilestoneState.CLOSE);
117+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
118+
return (response.readEntity(Milestone.class));
119+
}
120+
121+
public Milestone activateMilestone(Integer projectId, Integer milestoneId) throws GitLabApiException {
122+
if (projectId == null) {
123+
throw new RuntimeException("projectId cannot be null");
124+
}
125+
if (milestoneId == null) {
126+
throw new RuntimeException("milestoneId cannot be null");
127+
}
128+
GitLabApiForm formData = new GitLabApiForm().withParam("state_event", MilestoneState.ACTIVATE);
129+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
130+
return (response.readEntity(Milestone.class));
131+
}
132+
133+
public Milestone updateMilestone(Integer projectId, Integer milestoneId, String title, String description, Date dueDate, Date startDate, MilestoneState milestoneState) throws GitLabApiException {
134+
if (projectId == null) {
135+
throw new RuntimeException("projectId cannot be null");
136+
}
137+
if (milestoneId == null) {
138+
throw new RuntimeException("milestoneId cannot be null");
139+
}
140+
GitLabApiForm formData = new GitLabApiForm()
141+
.withParam("title", title, true)
142+
.withParam("description", description)
143+
.withParam("due_date", dueDate)
144+
.withParam("start_date", startDate)
145+
.withParam("state_event", milestoneState);
146+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
147+
return (response.readEntity(Milestone.class));
148+
}
149+
}

0 commit comments

Comments
 (0)