Skip to content

Commit d2b4785

Browse files
authored
endpoint to list pr review comments (#242)
1 parent 92bb6ea commit d2b4785

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public class GitHubClient {
9797
new TypeReference<>() {};
9898
static final TypeReference<List<PullRequestItem>> LIST_PR_TYPE_REFERENCE =
9999
new TypeReference<>() {};
100+
static final TypeReference<List<com.spotify.github.v3.prs.Comment>>
101+
LIST_PR_COMMENT_TYPE_REFERENCE = new TypeReference<>() {};
100102
static final TypeReference<List<Branch>> LIST_BRANCHES = new TypeReference<>() {};
101103
static final TypeReference<List<Reference>> LIST_REFERENCES = new TypeReference<>() {};
102104
static final TypeReference<List<RepositoryInvitation>> LIST_REPOSITORY_INVITATION =
@@ -109,8 +111,7 @@ public class GitHubClient {
109111
static final TypeReference<List<TeamInvitation>> LIST_PENDING_TEAM_INVITATIONS =
110112
new TypeReference<>() {};
111113

112-
static final TypeReference<List<FileItem>> LIST_FILE_ITEMS =
113-
new TypeReference<>() {};
114+
static final TypeReference<List<FileItem>> LIST_FILE_ITEMS = new TypeReference<>() {};
114115

115116
private static final String GET_ACCESS_TOKEN_URL = "app/installations/%s/access_tokens";
116117

@@ -1090,7 +1091,8 @@ private CompletableFuture<AccessToken> generateInstallationToken(
10901091
if (response.bodyString() == null) {
10911092
throw new RuntimeException(
10921093
String.format(
1093-
"Got empty response body when getting an access token from GitHub, HTTP status was: %s",
1094+
"Got empty response body when getting an access token from GitHub, HTTP"
1095+
+ " status was: %s",
10941096
response.statusMessage()));
10951097
}
10961098
final String text = response.bodyString();

src/main/java/com/spotify/github/v3/clients/PullRequestClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
2424
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_FILE_ITEMS;
26+
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_COMMENT_TYPE_REFERENCE;
2627
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2728
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_REQUEST_TYPE_REFERENCE;
2829
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_TYPE_REFERENCE;
@@ -63,6 +64,7 @@ public class PullRequestClient {
6364
private static final String PR_NUMBER_TEMPLATE = "/repos/%s/%s/pulls/%s";
6465
private static final String PR_COMMITS_TEMPLATE = "/repos/%s/%s/pulls/%s/commits";
6566
private static final String PR_REVIEWS_TEMPLATE = "/repos/%s/%s/pulls/%s/reviews";
67+
private static final String PR_COMMENTS_TEMPLATE = "/repos/%s/%s/pulls/%s/comments";
6668
private static final String PR_CHANGED_FILES_TEMPLATE = "/repos/%s/%s/pulls/%s/files";
6769
private static final String PR_REVIEW_REQUESTS_TEMPLATE =
6870
"/repos/%s/%s/pulls/%s/requested_reviewers";
@@ -467,6 +469,17 @@ private CompletableFuture<List<PullRequestItem>> list(final String parameterPath
467469
return github.request(path, LIST_PR_TYPE_REFERENCE);
468470
}
469471

472+
/**
473+
* List pull request review comments.
474+
*
475+
* @param prNumber pull request number
476+
* @return iterator of comments
477+
*/
478+
public Iterator<AsyncPage<Comment>> listComments(final long prNumber) {
479+
final String path = String.format(PR_COMMENTS_TEMPLATE, owner, repo, prNumber);
480+
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_PR_COMMENT_TYPE_REFERENCE));
481+
}
482+
470483
/**
471484
* Creates a reply to a pull request review comment.
472485
*

src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,30 @@ void testChangedFiles() throws IOException {
438438
assertEquals(actualFiles.get(0).filename(), expectedFiles.get(0).filename());
439439
assertEquals(actualFiles.get(1).filename(), expectedFiles.get(1).filename());
440440
}
441+
442+
@Test
443+
public void testListComments() throws Throwable {
444+
final String expectedBody = "[" + getFixture("pull_request_review_comment_reply.json") + "]";
445+
446+
final String pageLink =
447+
"<https://github.com/api/v3/repos/owner/repo/pulls/1/comments>; rel=\"first\"";
448+
449+
final HttpResponse firstPageResponse = createMockResponse(pageLink, expectedBody);
450+
451+
when(mockGithub.request("/repos/owner/repo/pulls/1/comments"))
452+
.thenReturn(completedFuture(firstPageResponse));
453+
454+
when(mockGithub.json()).thenReturn(github.json());
455+
456+
final PullRequestClient pullRequestClient =
457+
PullRequestClient.create(mockGithub, "owner", "repo");
458+
459+
final Iterable<AsyncPage<Comment>> pageIterator = () -> pullRequestClient.listComments(1L);
460+
List<Comment> comments = Async.streamFromPaginatingIterable(pageIterator).collect(toList());
461+
462+
assertEquals(1, comments.size());
463+
assertThat(comments.get(0).body(), is("Great stuff!"));
464+
assertThat(comments.get(0).id(), is(10L));
465+
assertThat(comments.get(0).user().login(), is("octocat"));
466+
}
441467
}

0 commit comments

Comments
 (0)