Skip to content

Commit 34b9687

Browse files
lucaslsjmini
andcommitted
DiscussionApi: Retrieve a single discussion from a Merge Request (gitlab4j#1284)
--------- Co-authored-by: Jeremie Bresson <[email protected]> (cherry picked from commit fbefbe4)
1 parent 63f3523 commit 34b9687

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/DiscussionsApi.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,50 @@ public Stream<Discussion> getMergeRequestDiscussionsStream(Object projectIdOrPat
361361
return (pager.stream());
362362
}
363363

364+
/**
365+
* Get a single discussion for the specified merge request.
366+
*
367+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id</code></pre>
368+
*
369+
* @param projectIdOrPath projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
370+
* @param mergeRequestIid the internal ID of the merge request
371+
* @param discussionId the ID of the discussion
372+
* @return the Discussion instance specified by discussionId for the specified merge request
373+
* @throws GitLabApiException if any exception occurs during execution
374+
*/
375+
public Discussion getMergeRequestDiscussion(Object projectIdOrPath, Long mergeRequestIid, String discussionId)
376+
throws GitLabApiException {
377+
Response response = get(
378+
Response.Status.OK,
379+
null,
380+
"projects",
381+
getProjectIdOrPath(projectIdOrPath),
382+
"merge_requests",
383+
mergeRequestIid,
384+
"discussions",
385+
discussionId);
386+
return (response.readEntity(Discussion.class));
387+
}
388+
389+
/**
390+
* Get an Optional instance of a single discussion for the specified merge request.
391+
*
392+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id</code></pre>
393+
*
394+
* @param projectIdOrPath projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
395+
* @param mergeRequestIid the internal ID of the merge request
396+
* @param discussionId the ID of the discussion
397+
* @return an Optional instance with the specified Discussion instance as a value
398+
*/
399+
public Optional<Discussion> getOptionalMergeRequestDiscussion(
400+
Object projectIdOrPath, Long mergeRequestIid, String discussionId) throws GitLabApiException {
401+
try {
402+
return (Optional.ofNullable(getMergeRequestDiscussion(projectIdOrPath, mergeRequestIid, discussionId)));
403+
} catch (GitLabApiException glae) {
404+
return (GitLabApi.createOptionalFromException(glae));
405+
}
406+
}
407+
364408
/**
365409
* Creates a new discussion to a single project merge request. This is similar to creating
366410
* a note but other comments (replies) can be added to it later.

gitlab4j-api/src/test/java/org/gitlab4j/api/TestMergeRequestDiscussionsApi.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.mockito.MockitoAnnotations.openMocks;
99

1010
import java.util.List;
11+
import java.util.Optional;
1112
import java.util.stream.Collectors;
1213
import java.util.stream.Stream;
1314

@@ -38,7 +39,8 @@ public class TestMergeRequestDiscussionsApi implements Constants {
3839
@BeforeEach
3940
public void setUp() throws Exception {
4041
openMocks(this);
41-
response = new MockResponse(Discussion.class, null, "merge-request-discussions.json");
42+
response =
43+
new MockResponse(Discussion.class, "merge-request-discussion.json", "merge-request-discussions.json");
4244
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
4345
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
4446
when(gitLabApiClient.get(attributeCaptor.capture(), Mockito.any(Object[].class)))
@@ -73,4 +75,18 @@ public void testGetMergeRequestDiscussionsByStream() throws Exception {
7375
List<Discussion> discussions = stream.collect(Collectors.toList());
7476
assertTrue(compareJson(discussions, "merge-request-discussions.json"));
7577
}
78+
79+
@Test
80+
public void testGetMergeRequestDiscussion() throws Exception {
81+
Discussion discussion = new DiscussionsApi(gitLabApi).getMergeRequestDiscussion(1L, 1L, "1");
82+
assertNotNull(discussion);
83+
assertTrue(compareJson(discussion, "merge-request-discussion.json"));
84+
}
85+
86+
@Test
87+
public void testGetOptionalMergeRequestDiscussion() throws Exception {
88+
Optional<Discussion> discussion = new DiscussionsApi(gitLabApi).getOptionalMergeRequestDiscussion(1L, 1L, "");
89+
assertTrue(discussion.isPresent());
90+
assertTrue(compareJson(discussion.get(), "merge-request-discussion.json"));
91+
}
7692
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"id": "6a9c1750b37d513a43987b574953fceb50b03ce7",
3+
"individual_note": false,
4+
"notes": [
5+
{
6+
"id": 1126,
7+
"type": "DiscussionNote",
8+
"author": {
9+
"id": 1,
10+
"name": "root",
11+
"username": "root",
12+
"state": "active",
13+
"avatar_url": "https://www.gravatar.com/avatar/00afb8fb6ab07c3ee3e9c1f38777e2f4?s=80&d=identicon",
14+
"web_url": "http://localhost:3000/root"
15+
},
16+
"created_at": "2018-03-03T21:54:39.668Z",
17+
"updated_at": "2018-03-03T21:54:39.668Z",
18+
"system": false,
19+
"noteable_id": 3,
20+
"noteable_type": "Merge request",
21+
"resolved": false,
22+
"resolvable": true
23+
},
24+
{
25+
"id": 1129,
26+
"type": "DiscussionNote",
27+
"body": "reply to the discussion",
28+
"author": {
29+
"id": 1,
30+
"name": "root",
31+
"username": "root",
32+
"state": "active",
33+
"avatar_url": "https://www.gravatar.com/avatar/00afb8fb6ab07c3ee3e9c1f38777e2f4?s=80&d=identicon",
34+
"web_url": "http://localhost:3000/root"
35+
},
36+
"created_at": "2018-03-04T13:38:02.127Z",
37+
"updated_at": "2018-03-04T13:38:02.127Z",
38+
"system": false,
39+
"noteable_id": 3,
40+
"noteable_type": "Merge request",
41+
"resolved": false,
42+
"resolvable": true
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)