Skip to content

Commit 54e58dc

Browse files
committed
Added support for merge request notes (#140).
1 parent f5729ca commit 54e58dc

File tree

2 files changed

+243
-13
lines changed

2 files changed

+243
-13
lines changed

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

Lines changed: 216 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public Pager<Note> getIssueNotes(Integer projectId, Integer issueIid, int itemsP
113113
* Get the specified issues's note.
114114
*
115115
* @param projectId the project ID to get the issues for
116-
* @param issueIid the issue IID to get the notes for
117-
* @param noteId the ID of the Note to get
116+
* @param issueIid the issue IID to get the notes for
117+
* @param noteId the ID of the Note to get
118118
* @return a Note instance for the specified IDs
119119
* @throws GitLabApiException if any exception occurs
120120
*/
@@ -127,8 +127,8 @@ public Note getIssueNote(Integer projectId, Integer issueIid, Integer noteId) th
127127
* Create a issues's note.
128128
*
129129
* @param projectId the project ID to create the issues for
130-
* @param issueIid the issue IID to create the notes for
131-
* @param body the content of note
130+
* @param issueIid the issue IID to create the notes for
131+
* @param body the content of note
132132
* @return the created Note instance
133133
* @throws GitLabApiException if any exception occurs
134134
*/
@@ -140,16 +140,18 @@ public Note createIssueNote(Integer projectId, Integer issueIid, String body) th
140140
* Create a issues's note.
141141
*
142142
* @param projectId the project ID to create the issues for
143-
* @param issueIid the issue IID to create the notes for
144-
* @param body the content of note
143+
* @param issueIid the issue IID to create the notes for
144+
* @param body the content of note
145145
* @param createdAt the created time of note
146146
* @return the created Note instance
147147
* @throws GitLabApiException if any exception occurs
148148
*/
149149
public Note createIssueNote(Integer projectId, Integer issueIid, String body, Date createdAt) throws GitLabApiException {
150+
150151
if (projectId == null) {
151152
throw new RuntimeException("projectId cannot be null");
152153
}
154+
153155
GitLabApiForm formData = new GitLabApiForm()
154156
.withParam("body", body, true)
155157
.withParam("created_at", createdAt);
@@ -161,19 +163,20 @@ public Note createIssueNote(Integer projectId, Integer issueIid, String body, Da
161163
* Update the specified issues's note.
162164
*
163165
* @param projectId the project ID to update the issues for
164-
* @param issueIid the issue IID to update the notes for
165-
* @param nodeId the ID of the node to update
166-
* @param body the update content for the Note
166+
* @param issueIid the issue IID to update the notes for
167+
* @param noteId the ID of the node to update
168+
* @param body the update content for the Note
167169
* @return the modified Note instance
168170
* @throws GitLabApiException if any exception occurs
169171
*/
170-
public Note updateIssueNote(Integer projectId, Integer issueIid, Integer nodeId, String body) throws GitLabApiException {
172+
public Note updateIssueNote(Integer projectId, Integer issueIid, Integer noteId, String body) throws GitLabApiException {
173+
171174
if (projectId == null) {
172175
throw new RuntimeException("projectId cannot be null");
173176
}
174-
GitLabApiForm formData = new GitLabApiForm()
175-
.withParam("body", body, true);
176-
Response response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "notes", nodeId);
177+
178+
GitLabApiForm formData = new GitLabApiForm().withParam("body", body, true);
179+
Response response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "notes", noteId);
177180
return (response.readEntity(Note.class));
178181
}
179182

@@ -186,16 +189,216 @@ public Note updateIssueNote(Integer projectId, Integer issueIid, Integer nodeId,
186189
* @throws GitLabApiException if any exception occurs
187190
*/
188191
public void deleteIssueNote(Integer projectId, Integer issueIid, Integer noteId) throws GitLabApiException {
192+
189193
if (projectId == null) {
190194
throw new RuntimeException("projectId cannot be null");
191195
}
196+
192197
if (issueIid == null) {
193198
throw new RuntimeException("issueIid cannot be null");
194199
}
200+
195201
if (noteId == null) {
196202
throw new RuntimeException("noteId cannot be null");
197203
}
204+
198205
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
199206
delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "issues", issueIid, "notes", noteId);
200207
}
208+
209+
/**
210+
* Gets a list of all notes for a single merge request. Only returns the first page
211+
*
212+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
213+
*
214+
* @param projectId the project ID to get the issues for
215+
* @param mergeRequestIid the issue ID to get the notes for
216+
* @return a list of the merge request's notes
217+
* @throws GitLabApiException if any exception occurs
218+
*/
219+
public List<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid) throws GitLabApiException {
220+
return (getMergeRequestNotes(projectId, mergeRequestIid, null, null, 1, getDefaultPerPage()));
221+
}
222+
223+
/**
224+
* Gets a list of all notes for a single merge request. Only returns the first page
225+
*
226+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
227+
*
228+
* @param projectId the project ID to get the issues for
229+
* @param mergeRequestIid the issue ID to get the notes for
230+
* @param sortOrder return merge request notes sorted in the specified sort order, default is DESC
231+
* @param orderBy return merge request notes ordered by CREATED_AT or UPDATED_AT, default is CREATED_AT
232+
* @return a list of the merge request's notes
233+
* @throws GitLabApiException if any exception occurs
234+
*/
235+
public List<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid, SortOrder sortOrder, Note.OrderBy orderBy) throws GitLabApiException {
236+
return (getMergeRequestNotes(projectId, mergeRequestIid, sortOrder, orderBy, 1, getDefaultPerPage()));
237+
}
238+
239+
/**
240+
* Gets a list of all notes for a single merge request using the specified page and per page settings.
241+
*
242+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
243+
*
244+
* @param projectId the project ID to get the issues for
245+
* @param mergeRequestIid the merge request IID to get the notes for
246+
* @param page the page to get
247+
* @param perPage the number of notes per page
248+
* @return the list of notes in the specified range
249+
* @throws GitLabApiException if any exception occurs
250+
*/
251+
public List<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid, int page, int perPage) throws GitLabApiException {
252+
return (getMergeRequestNotes(projectId, mergeRequestIid, null, null, page, perPage));
253+
}
254+
255+
/**
256+
* Gets a list of all notes for a single merge request using the specified page and per page settings.
257+
*
258+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
259+
*
260+
* @param projectId the project ID to get the issues for
261+
* @param mergeRequestIid the merge request IID to get the notes for
262+
* @param sortOrder return merge request notes sorted in the specified sort order, default is DESC
263+
* @param orderBy return merge request notes ordered by CREATED_AT or UPDATED_AT, default is CREATED_AT
264+
* @param page the page to get
265+
* @param perPage the number of notes per page
266+
* @return the list of notes in the specified range
267+
* @throws GitLabApiException if any exception occurs
268+
*/
269+
public List<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid,
270+
SortOrder sortOrder, Note.OrderBy orderBy, int page, int perPage) throws GitLabApiException {
271+
272+
GitLabApiForm formData = new GitLabApiForm()
273+
.withParam("sort", sortOrder)
274+
.withParam("order_by", orderBy)
275+
.withParam(PAGE_PARAM, page)
276+
.withParam(PER_PAGE_PARAM, perPage);
277+
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid, "notes");
278+
return (response.readEntity(new GenericType<List<Note>>() {}));
279+
}
280+
281+
/**
282+
* Get a Pager of all notes for a single merge request
283+
*
284+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
285+
*
286+
* @param projectId the project ID to get the issues for
287+
* @param mergeRequestIid the merge request IID to get the notes for
288+
* @param itemsPerPage the number of notes per page
289+
* @return the list of notes in the specified range
290+
* @throws GitLabApiException if any exception occurs
291+
*/
292+
public Pager<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid, int itemsPerPage) throws GitLabApiException {
293+
return (getMergeRequestNotes(projectId, mergeRequestIid, null, null, itemsPerPage));
294+
}
295+
296+
/**
297+
* Get a Pager of all notes for a single merge request
298+
*
299+
* GET /projects/:id/merge_requests/:merge_request_iid/notes
300+
*
301+
* @param projectId the project ID to get the issues for
302+
* @param mergeRequestIid the merge request IID to get the notes for
303+
* @param sortOrder return merge request notes sorted in the specified sort order, default is DESC
304+
* @param orderBy return merge request notes ordered by CREATED_AT or UPDATED_AT, default is CREATED_AT
305+
* @param itemsPerPage the number of notes per page
306+
* @return the list of notes in the specified range
307+
* @throws GitLabApiException if any exception occurs
308+
*/
309+
public Pager<Note> getMergeRequestNotes(Integer projectId, Integer mergeRequestIid,
310+
SortOrder sortOrder, Note.OrderBy orderBy, int itemsPerPage) throws GitLabApiException {
311+
312+
GitLabApiForm formData = new GitLabApiForm()
313+
.withParam("sort", sortOrder)
314+
.withParam("order_by", orderBy)
315+
.withParam(PAGE_PARAM, 1)
316+
.withParam(PER_PAGE_PARAM, itemsPerPage);
317+
return (new Pager<Note>(this, Note.class, itemsPerPage, formData.asMap(),
318+
"projects", projectId, "merge_requests", mergeRequestIid, "notes"));
319+
}
320+
321+
/**
322+
* Get the specified merge request's note.
323+
*
324+
* @param projectId the project ID to get the issues for
325+
* @param mergeRequestIid the merge request IID to get the notes for
326+
* @param noteId the ID of the Note to get
327+
* @return a Note instance for the specified IDs
328+
* @throws GitLabApiException if any exception occurs
329+
*/
330+
public Note getMergeRequestNote(Integer projectId, Integer mergeRequestIid, Integer noteId) throws GitLabApiException {
331+
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
332+
"projects", projectId, "merge_requests", mergeRequestIid, "notes", noteId);
333+
return (response.readEntity(Note.class));
334+
}
335+
336+
/**
337+
* Create a merge request's note.
338+
*
339+
* @param projectId the project ID to create the issues for
340+
* @param mergeRequestIid the merge request IID to create the notes for
341+
* @param body the content of note
342+
* @return the created Note instance
343+
* @throws GitLabApiException if any exception occurs
344+
*/
345+
public Note createMergeRequestNote(Integer projectId, Integer mergeRequestIid, String body) throws GitLabApiException {
346+
347+
if (projectId == null) {
348+
throw new RuntimeException("projectId cannot be null");
349+
}
350+
351+
GitLabApiForm formData = new GitLabApiForm().withParam("body", body, true);
352+
Response response = post(Response.Status.CREATED, formData,
353+
"projects", projectId, "merge_requests", mergeRequestIid, "notes");
354+
return (response.readEntity(Note.class));
355+
}
356+
357+
/**
358+
* Update the specified merge request's note.
359+
*
360+
* @param projectId the project ID to update the issues for
361+
* @param mergeRequestIid the merge request IID to update the notes for
362+
* @param noteId the ID of the node to update
363+
* @param body the update content for the Note
364+
* @return the modified Note instance
365+
* @throws GitLabApiException if any exception occurs
366+
*/
367+
public Note updateMergeRequestNote(Integer projectId, Integer mergeRequestIid, Integer noteId, String body) throws GitLabApiException {
368+
369+
if (projectId == null) {
370+
throw new RuntimeException("projectId cannot be null");
371+
}
372+
373+
GitLabApiForm formData = new GitLabApiForm().withParam("body", body, true);
374+
Response response = put(Response.Status.CREATED, formData.asMap(),
375+
"projects", projectId, "merge_requests", mergeRequestIid, "notes", noteId);
376+
return (response.readEntity(Note.class));
377+
}
378+
379+
/**
380+
* Delete the specified merge request's note.
381+
*
382+
* @param projectId the project ID to delete the issues for
383+
* @param mergeRequestIid the merge request IID to delete the notes for
384+
* @param noteId the ID of the node to delete
385+
* @throws GitLabApiException if any exception occurs
386+
*/
387+
public void deleteMergeRequestNote(Integer projectId, Integer mergeRequestIid, Integer noteId) throws GitLabApiException {
388+
389+
if (projectId == null) {
390+
throw new RuntimeException("projectId cannot be null");
391+
}
392+
393+
if (mergeRequestIid == null) {
394+
throw new RuntimeException("mergeRequestIid cannot be null");
395+
}
396+
397+
if (noteId == null) {
398+
throw new RuntimeException("noteId cannot be null");
399+
}
400+
401+
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
402+
delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "merge_requests", mergeRequestIid, "notes", noteId);
403+
}
201404
}

src/main/java/org/gitlab4j/api/models/Note.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,37 @@
66
import javax.xml.bind.annotation.XmlAccessorType;
77
import javax.xml.bind.annotation.XmlRootElement;
88

9+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
10+
11+
import com.fasterxml.jackson.annotation.JsonCreator;
12+
import com.fasterxml.jackson.annotation.JsonValue;
13+
914
@XmlRootElement
1015
@XmlAccessorType(XmlAccessType.FIELD)
1116
public class Note {
1217

18+
/** Enum to use for ordering the results. */
19+
public static enum OrderBy {
20+
21+
CREATED_AT, UPDATED_AT;
22+
private static JacksonJsonEnumHelper<OrderBy> enumHelper = new JacksonJsonEnumHelper<>(OrderBy.class);
23+
24+
@JsonCreator
25+
public static OrderBy forValue(String value) {
26+
return enumHelper.forValue(value);
27+
}
28+
29+
@JsonValue
30+
public String toValue() {
31+
return (enumHelper.toString(this));
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return (enumHelper.toString(this));
37+
}
38+
}
39+
1340
public static enum NotableType {
1441
ISSUE("Issue"), MERGE_REQUEST("MergeRequest"), SNIPPET("Snippet");
1542

0 commit comments

Comments
 (0)