Skip to content

Commit 068b4e6

Browse files
maximevwMaxime Wiewiorabitwiseman
authored
Add support for multiline review comments (#1833)
* Add support for multi-line review comments On review comments or during the creation of a new review, support single and multi-line comments using `line` and `start_line` attributes. See: https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#create-a-review-comment-for-a-pull-request and https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request * Add tests for multi-line review comments * Fix tests for multi-line comments on pull requests * Add builder to create review comments * Improve PR review comments implementation * Update src/main/java/org/kohsuke/github/GHPullRequestReviewCommentBuilder.java * Update src/main/java/org/kohsuke/github/GHPullRequestReviewCommentBuilder.java * Update src/main/java/org/kohsuke/github/GHPullRequestReviewCommentBuilder.java * Fix code formatting * Update src/test/java/org/kohsuke/github/GHPullRequestTest.java * Update src/test/java/org/kohsuke/github/GHPullRequestTest.java --------- Co-authored-by: Maxime Wiewiora <[email protected]> Co-authored-by: Liam Newman <[email protected]>
1 parent 3e478c2 commit 068b4e6

File tree

148 files changed

+5459
-3539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+5459
-3539
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@
190190
<exclude>org.kohsuke.github.GHCompare.User</exclude>
191191

192192
<!-- TODO: Some coverage, but more needed -->
193-
<exclude>org.kohsuke.github.GHPullRequestReviewBuilder.DraftReviewComment</exclude>
194193
<exclude>org.kohsuke.github.GHIssue.PullRequest</exclude>
195194
<exclude>org.kohsuke.github.GHCommitSearchBuilder</exclude>
196195
<exclude>org.kohsuke.github.GHRepositorySearchBuilder</exclude>

src/main/java/org/kohsuke/github/GHPullRequest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,15 @@ public GHPullRequestReviewBuilder createReview() {
522522
return new GHPullRequestReviewBuilder(this);
523523
}
524524

525+
/**
526+
* Create gh pull request review comment builder.
527+
*
528+
* @return the gh pull request review comment builder.
529+
*/
530+
public GHPullRequestReviewCommentBuilder createReviewComment() {
531+
return new GHPullRequestReviewCommentBuilder(this);
532+
}
533+
525534
/**
526535
* Create review comment gh pull request review comment.
527536
*
@@ -536,18 +545,12 @@ public GHPullRequestReviewBuilder createReview() {
536545
* @return the gh pull request review comment
537546
* @throws IOException
538547
* the io exception
548+
* @deprecated use {@link #createReviewComment()}
539549
*/
550+
@Deprecated
540551
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position)
541552
throws IOException {
542-
return root().createRequest()
543-
.method("POST")
544-
.with("body", body)
545-
.with("commit_id", sha)
546-
.with("path", path)
547-
.with("position", position)
548-
.withUrlPath(getApiRoute() + COMMENTS_ACTION)
549-
.fetch(GHPullRequestReviewComment.class)
550-
.wrapUp(this);
553+
return createReviewComment().body(body).commitId(sha).path(path).position(position).create();
551554
}
552555

553556
/**

src/main/java/org/kohsuke/github/GHPullRequestReviewBuilder.java

Lines changed: 136 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class GHPullRequestReviewBuilder {
1515
private final GHPullRequest pr;
1616
private final Requester builder;
17-
private final List<DraftReviewComment> comments = new ArrayList<DraftReviewComment>();
17+
private final List<ReviewComment> comments = new ArrayList<>();
1818

1919
/**
2020
* Instantiates a new GH pull request review builder.
@@ -75,19 +75,53 @@ public GHPullRequestReviewBuilder event(GHPullRequestReviewEvent event) {
7575
* Comment gh pull request review builder.
7676
*
7777
* @param body
78-
* The relative path to the file that necessitates a review comment.
78+
* Text of the review comment.
7979
* @param path
80+
* The relative path to the file that necessitates a review comment.
81+
* @param position
8082
* The position in the diff where you want to add a review comment. Note this value is not the same as
8183
* the line number in the file. For help finding the position value, read the note below.
82-
* @param position
83-
* Text of the review comment.
8484
* @return the gh pull request review builder
8585
*/
8686
public GHPullRequestReviewBuilder comment(String body, String path, int position) {
8787
comments.add(new DraftReviewComment(body, path, position));
8888
return this;
8989
}
9090

91+
/**
92+
* Add a multi-line comment to the gh pull request review builder.
93+
*
94+
* @param body
95+
* Text of the review comment.
96+
* @param path
97+
* The relative path to the file that necessitates a review comment.
98+
* @param startLine
99+
* The first line in the pull request diff that the multi-line comment applies to.
100+
* @param endLine
101+
* The last line of the range that the comment applies to.
102+
* @return the gh pull request review builder
103+
*/
104+
public GHPullRequestReviewBuilder multiLineComment(String body, String path, int startLine, int endLine) {
105+
this.comments.add(new MultilineDraftReviewComment(body, path, startLine, endLine));
106+
return this;
107+
}
108+
109+
/**
110+
* Add a single line comment to the gh pull request review builder.
111+
*
112+
* @param body
113+
* Text of the review comment.
114+
* @param path
115+
* The relative path to the file that necessitates a review comment.
116+
* @param line
117+
* The line of the blob in the pull request diff that the comment applies to.
118+
* @return the gh pull request review builder
119+
*/
120+
public GHPullRequestReviewBuilder singleLineComment(String body, String path, int line) {
121+
this.comments.add(new SingleLineDraftReviewComment(body, path, line));
122+
return this;
123+
}
124+
91125
/**
92126
* Create gh pull request review.
93127
*
@@ -103,7 +137,29 @@ public GHPullRequestReview create() throws IOException {
103137
.wrapUp(pr);
104138
}
105139

106-
private static class DraftReviewComment {
140+
/**
141+
* Common properties of the review comments, regardless of how the comment is positioned on the gh pull request.
142+
*/
143+
private interface ReviewComment {
144+
/**
145+
* Gets body.
146+
*
147+
* @return the body.
148+
*/
149+
String getBody();
150+
151+
/**
152+
* Gets path.
153+
*
154+
* @return the path.
155+
*/
156+
String getPath();
157+
}
158+
159+
/**
160+
* Single line comment using the relative position in the diff.
161+
*/
162+
static class DraftReviewComment implements ReviewComment {
107163
private String body;
108164
private String path;
109165
private int position;
@@ -114,20 +170,10 @@ private static class DraftReviewComment {
114170
this.position = position;
115171
}
116172

117-
/**
118-
* Gets body.
119-
*
120-
* @return the body
121-
*/
122173
public String getBody() {
123174
return body;
124175
}
125176

126-
/**
127-
* Gets path.
128-
*
129-
* @return the path
130-
*/
131177
public String getPath() {
132178
return path;
133179
}
@@ -141,4 +187,79 @@ public int getPosition() {
141187
return position;
142188
}
143189
}
190+
191+
/**
192+
* Multi-line comment.
193+
*/
194+
static class MultilineDraftReviewComment implements ReviewComment {
195+
private final String body;
196+
private final String path;
197+
private final int line;
198+
private final int start_line;
199+
200+
MultilineDraftReviewComment(final String body, final String path, final int startLine, final int line) {
201+
this.body = body;
202+
this.path = path;
203+
this.line = line;
204+
this.start_line = startLine;
205+
}
206+
207+
public String getBody() {
208+
return this.body;
209+
}
210+
211+
public String getPath() {
212+
return this.path;
213+
}
214+
215+
/**
216+
* Gets end line of the comment.
217+
*
218+
* @return the end line of the comment.
219+
*/
220+
public int getLine() {
221+
return line;
222+
}
223+
224+
/**
225+
* Gets start line of the comment.
226+
*
227+
* @return the start line of the comment.
228+
*/
229+
public int getStartLine() {
230+
return start_line;
231+
}
232+
}
233+
234+
/**
235+
* Single line comment.
236+
*/
237+
static class SingleLineDraftReviewComment implements ReviewComment {
238+
private final String body;
239+
private final String path;
240+
private final int line;
241+
242+
SingleLineDraftReviewComment(final String body, final String path, final int line) {
243+
this.body = body;
244+
this.path = path;
245+
this.line = line;
246+
}
247+
248+
public String getBody() {
249+
return this.body;
250+
}
251+
252+
public String getPath() {
253+
return this.path;
254+
}
255+
256+
/**
257+
* Gets line of the comment.
258+
*
259+
* @return the line of the comment.
260+
*/
261+
public int getLine() {
262+
return line;
263+
}
264+
}
144265
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
// TODO: Auto-generated Javadoc
6+
7+
/**
8+
* Builds up a creation of new {@link GHPullRequestReviewComment}.
9+
*
10+
* @see GHPullRequest#createReviewComment()
11+
*/
12+
public class GHPullRequestReviewCommentBuilder {
13+
private final GHPullRequest pr;
14+
private final Requester builder;
15+
16+
/**
17+
* Instantiates a new GH pull request review comment builder.
18+
*
19+
* @param pr
20+
* the pr
21+
*/
22+
GHPullRequestReviewCommentBuilder(GHPullRequest pr) {
23+
this.pr = pr;
24+
this.builder = pr.root().createRequest();
25+
}
26+
27+
/**
28+
* The SHA of the commit that needs a review. Not using the latest commit SHA may render your review comment
29+
* outdated if a subsequent commit modifies the line you specify as the position. Defaults to the most recent commit
30+
* in the pull request when you do not specify a value.
31+
*
32+
* @param commitId
33+
* the commit id
34+
* @return the gh pull request review comment builder
35+
*/
36+
public GHPullRequestReviewCommentBuilder commitId(String commitId) {
37+
builder.with("commit_id", commitId);
38+
return this;
39+
}
40+
41+
/**
42+
* The text of the pull request review comment.
43+
*
44+
* @param body
45+
* the body
46+
* @return the gh pull request review comment builder
47+
*/
48+
public GHPullRequestReviewCommentBuilder body(String body) {
49+
builder.with("body", body);
50+
return this;
51+
}
52+
53+
/**
54+
* The relative path to the file that necessitates a comment.
55+
*
56+
* @param path
57+
* the path
58+
* @return the gh pull request review comment builder
59+
*/
60+
public GHPullRequestReviewCommentBuilder path(String path) {
61+
builder.with("path", path);
62+
return this;
63+
}
64+
65+
/**
66+
* The position in the diff where you want to add a review comment.
67+
*
68+
* @param position
69+
* the position
70+
* @return the gh pull request review comment builder
71+
* @implNote As position is deprecated in GitHub API, only keep this for internal usage (for retro-compatibility
72+
* with {@link GHPullRequest#createReviewComment(String, String, String, int)}).
73+
*/
74+
GHPullRequestReviewCommentBuilder position(int position) {
75+
builder.with("position", position);
76+
return this;
77+
}
78+
79+
/**
80+
* A single line of the blob in the pull request diff that the comment applies to.
81+
* <p>
82+
* {@link #line(int)} and {@link #lines(int, int)} will overwrite each other's values.
83+
* </p>
84+
*
85+
* @param line
86+
* the line number
87+
* @return the gh pull request review comment builder
88+
*/
89+
public GHPullRequestReviewCommentBuilder line(int line) {
90+
builder.with("line", line);
91+
builder.remove("start_line");
92+
return this;
93+
}
94+
95+
/**
96+
* The range of lines in the pull request diff that this comment applies to.
97+
* <p>
98+
* {@link #line(int)} and {@link #lines(int, int)} will overwrite each other's values.
99+
* </p>
100+
*
101+
* @param startLine
102+
* the start line number of the comment
103+
* @param endLine
104+
* the end line number of the comment
105+
* @return the gh pull request review comment builder
106+
*/
107+
public GHPullRequestReviewCommentBuilder lines(int startLine, int endLine) {
108+
builder.with("start_line", startLine);
109+
builder.with("line", endLine);
110+
return this;
111+
}
112+
113+
/**
114+
* Create gh pull request review comment.
115+
*
116+
* @return the gh pull request review comment builder
117+
* @throws IOException
118+
* the io exception
119+
*/
120+
public GHPullRequestReviewComment create() throws IOException {
121+
return builder.method("POST")
122+
.withUrlPath(pr.getApiRoute() + "/comments")
123+
.fetch(GHPullRequestReviewComment.class)
124+
.wrapUp(pr);
125+
}
126+
127+
}

0 commit comments

Comments
 (0)