Skip to content

Commit e4b6b37

Browse files
committed
chore: address review comments
1 parent fe5bd23 commit e4b6b37

6 files changed

Lines changed: 242 additions & 86 deletions

File tree

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,9 @@ public PagedIterable<GHPullRequestFileDetail> listFiles() {
531531
* Obtains all the review comments associated with this pull request.
532532
*
533533
* <p>
534-
* This method uses the pull request comments endpoint which returns complete comment data including
535-
* {@link GHPullRequestReviewComment#getLine() line}, {@link GHPullRequestReviewComment#getOriginalLine()
536-
* originalLine}, {@link GHPullRequestReviewComment#getSide() side}, and other position-related fields.
537-
*
538-
* <p>
539-
* If you need line number information, prefer this method over {@link GHPullRequestReview#listReviewComments()},
540-
* which uses a different API endpoint that does not return line-related fields.
534+
* Unlike {@link GHPullRequestReview#listReviewComments()}, this method returns full
535+
* {@link GHPullRequestReviewComment} objects including line-related fields such as
536+
* {@link GHPullRequestReviewComment#getLine() line}, {@link GHPullRequestReviewComment#getSide() side}, etc.
541537
*
542538
* @return the paged iterable
543539
* @see GHPullRequestReview#listReviewComments()

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

Lines changed: 191 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,190 @@
4343
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
4444
public class GHPullRequestReview extends GHObject {
4545

46+
/**
47+
* Represents a review comment as returned by the review comments endpoint. This is a limited view that does not
48+
* include line-related fields such as {@code line}, {@code originalLine}, {@code side}, etc.
49+
*
50+
* <p>
51+
* To obtain the full {@link GHPullRequestReviewComment} with all fields, call
52+
* {@link #readPullRequestReviewComment()}.
53+
*
54+
* @see GHPullRequest#listReviewComments()
55+
*/
56+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
57+
public static class ReviewComment extends GHObject {
58+
59+
private GHCommentAuthorAssociation authorAssociation;
60+
private String body;
61+
private String commitId;
62+
private String diffHunk;
63+
private String htmlUrl;
64+
private String originalCommitId;
65+
private int originalPosition = -1;
66+
private String path;
67+
private int position = -1;
68+
private Long pullRequestReviewId = -1L;
69+
private String pullRequestUrl;
70+
private GHPullRequestReviewCommentReactions reactions;
71+
private GHUser user;
72+
73+
GHPullRequest owner;
74+
75+
/**
76+
* Create default ReviewComment instance
77+
*/
78+
public ReviewComment() {
79+
}
80+
81+
/**
82+
* Gets the author association to the project.
83+
*
84+
* @return the author association to the project
85+
*/
86+
public GHCommentAuthorAssociation getAuthorAssociation() {
87+
return authorAssociation;
88+
}
89+
90+
/**
91+
* The comment itself.
92+
*
93+
* @return the body
94+
*/
95+
public String getBody() {
96+
return body;
97+
}
98+
99+
/**
100+
* Gets commit id.
101+
*
102+
* @return the commit id
103+
*/
104+
public String getCommitId() {
105+
return commitId;
106+
}
107+
108+
/**
109+
* Gets diff hunk.
110+
*
111+
* @return the diff hunk
112+
*/
113+
public String getDiffHunk() {
114+
return diffHunk;
115+
}
116+
117+
/**
118+
* Gets the html url.
119+
*
120+
* @return the html url
121+
*/
122+
public URL getHtmlUrl() {
123+
return GitHubClient.parseURL(htmlUrl);
124+
}
125+
126+
/**
127+
* Gets commit id.
128+
*
129+
* @return the original commit id
130+
*/
131+
public String getOriginalCommitId() {
132+
return originalCommitId;
133+
}
134+
135+
/**
136+
* Gets original position.
137+
*
138+
* @return the original position
139+
*/
140+
public int getOriginalPosition() {
141+
return originalPosition;
142+
}
143+
144+
/**
145+
* Gets path.
146+
*
147+
* @return the path
148+
*/
149+
public String getPath() {
150+
return path;
151+
}
152+
153+
/**
154+
* Gets position.
155+
*
156+
* @return the position
157+
*/
158+
public int getPosition() {
159+
return position;
160+
}
161+
162+
/**
163+
* Gets The ID of the pull request review to which the comment belongs.
164+
*
165+
* @return {@link Long} the ID of the pull request review
166+
*/
167+
public Long getPullRequestReviewId() {
168+
return pullRequestReviewId != null ? pullRequestReviewId : -1;
169+
}
170+
171+
/**
172+
* Gets URL for the pull request that the review comment belongs to.
173+
*
174+
* @return {@link URL} the URL of the pull request
175+
*/
176+
public URL getPullRequestUrl() {
177+
return GitHubClient.parseURL(pullRequestUrl);
178+
}
179+
180+
/**
181+
* Gets the Reaction Rollup.
182+
*
183+
* @return {@link GHPullRequestReviewCommentReactions} the reaction rollup
184+
*/
185+
public GHPullRequestReviewCommentReactions getReactions() {
186+
return reactions;
187+
}
188+
189+
/**
190+
* Gets the user who posted this comment.
191+
*
192+
* @return the user
193+
* @throws IOException
194+
* the io exception
195+
*/
196+
public GHUser getUser() throws IOException {
197+
return owner.root().getUser(user.getLogin());
198+
}
199+
200+
/**
201+
* Fetches the full {@link GHPullRequestReviewComment} from the API, which includes all fields such as
202+
* {@link GHPullRequestReviewComment#getLine() line}, {@link GHPullRequestReviewComment#getOriginalLine()
203+
* originalLine}, {@link GHPullRequestReviewComment#getSide() side}, etc.
204+
*
205+
* @return the full {@link GHPullRequestReviewComment}
206+
* @throws IOException
207+
* if an I/O error occurs
208+
*/
209+
public GHPullRequestReviewComment readPullRequestReviewComment() throws IOException {
210+
return owner.root()
211+
.createRequest()
212+
.withUrlPath("/repos/" + owner.getRepository().getFullName() + "/pulls/comments/" + getId())
213+
.fetch(GHPullRequestReviewComment.class)
214+
.wrapUp(owner);
215+
}
216+
217+
/**
218+
* Wrap up.
219+
*
220+
* @param owner
221+
* the owner
222+
* @return the review comment
223+
*/
224+
ReviewComment wrapUp(GHPullRequest owner) {
225+
this.owner = owner;
226+
return this;
227+
}
228+
}
229+
46230
private String body;
47231

48232
private String commitId;
@@ -175,24 +359,19 @@ public GHUser getUser() throws IOException {
175359
* Obtains all the review comments associated with this pull request review.
176360
*
177361
* <p>
178-
* <strong>Note:</strong> The GitHub API endpoint used by this method does not return line-related fields such as
179-
* {@link GHPullRequestReviewComment#getLine() line}, {@link GHPullRequestReviewComment#getOriginalLine()
180-
* originalLine}, {@link GHPullRequestReviewComment#getSide() side},
181-
* {@link GHPullRequestReviewComment#getStartLine() startLine}, etc. These fields will return their default values
182-
* (-1 or UNKNOWN).
183-
*
184-
* <p>
185-
* If you need line number information, use {@link GHPullRequest#listReviewComments()} instead and filter by
186-
* {@link GHPullRequestReviewComment#getPullRequestReviewId()} if needed.
362+
* The GitHub API endpoint used by this method returns a limited set of fields. To obtain full comment data
363+
* including line numbers, use {@link ReviewComment#readPullRequestReviewComment()} on individual comments, or use
364+
* {@link GHPullRequest#listReviewComments()} instead.
187365
*
188-
* @return the paged iterable
366+
* @return the paged iterable of {@link ReviewComment} objects
189367
* @see GHPullRequest#listReviewComments()
368+
* @see ReviewComment#readPullRequestReviewComment()
190369
*/
191-
public PagedIterable<GHPullRequestReviewComment> listReviewComments() {
370+
public PagedIterable<ReviewComment> listReviewComments() {
192371
return owner.root()
193372
.createRequest()
194373
.withUrlPath(getApiRoute() + "/comments")
195-
.toIterable(GHPullRequestReviewComment[].class, item -> item.wrapUp(owner));
374+
.toIterable(ReviewComment[].class, item -> item.wrapUp(owner));
196375
}
197376

198377
/**

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

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @see GHPullRequest#createReviewComment(String, String, String, int) GHPullRequest#createReviewComment(String, String,
4141
* String, int)
4242
*/
43-
public class GHPullRequestReviewComment extends GHObject implements Reactable, Refreshable {
43+
public class GHPullRequestReviewComment extends GHObject implements Reactable {
4444

4545
/**
4646
* The side of the diff to which the comment applies
@@ -219,12 +219,12 @@ public long getInReplyToId() {
219219
* Gets The line of the blob to which the comment applies. The last line of the range for a multi-line comment.
220220
*
221221
* <p>
222-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
223-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
224-
* this method returns -1 due to limitations in the GitHub API.
222+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
223+
* {@link GHPullRequestReview#listReviewComments()}. Use
224+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
225+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
225226
*
226227
* @return the line to which the comment applies, or -1 if not available
227-
* @see GHPullRequest#listReviewComments()
228228
*/
229229
public int getLine() {
230230
return line;
@@ -243,12 +243,12 @@ public String getOriginalCommitId() {
243243
* Gets The line of the blob to which the comment applies. The last line of the range for a multi-line comment.
244244
*
245245
* <p>
246-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
247-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
248-
* this method returns -1 due to limitations in the GitHub API.
246+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
247+
* {@link GHPullRequestReview#listReviewComments()}. Use
248+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
249+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
249250
*
250251
* @return the line to which the comment applies, or -1 if not available
251-
* @see GHPullRequest#listReviewComments()
252252
*/
253253
public int getOriginalLine() {
254254
return originalLine;
@@ -267,12 +267,12 @@ public int getOriginalPosition() {
267267
* Gets The first line of the range for a multi-line comment.
268268
*
269269
* <p>
270-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
271-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
272-
* this method returns -1 due to limitations in the GitHub API.
270+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
271+
* {@link GHPullRequestReview#listReviewComments()}. Use
272+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
273+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
273274
*
274275
* @return the original start line, or -1 if not available or not a multi-line comment
275-
* @see GHPullRequest#listReviewComments()
276276
*/
277277
public int getOriginalStartLine() {
278278
return originalStartLine != null ? originalStartLine : -1;
@@ -339,12 +339,12 @@ public GHPullRequestReviewCommentReactions getReactions() {
339339
* comment.
340340
*
341341
* <p>
342-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
343-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
344-
* this method returns {@link Side#UNKNOWN} due to limitations in the GitHub API.
342+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
343+
* {@link GHPullRequestReview#listReviewComments()}. Use
344+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
345+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
345346
*
346347
* @return {@link Side} the side of the diff to which the comment applies, or {@link Side#UNKNOWN} if not available
347-
* @see GHPullRequest#listReviewComments()
348348
*/
349349
public Side getSide() {
350350
return Side.from(side);
@@ -354,12 +354,12 @@ public Side getSide() {
354354
* Gets The first line of the range for a multi-line comment.
355355
*
356356
* <p>
357-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
358-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
359-
* this method returns -1 due to limitations in the GitHub API.
357+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
358+
* {@link GHPullRequestReview#listReviewComments()}. Use
359+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
360+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
360361
*
361362
* @return the start line, or -1 if not available or not a multi-line comment
362-
* @see GHPullRequest#listReviewComments()
363363
*/
364364
public int getStartLine() {
365365
return startLine != null ? startLine : -1;
@@ -369,12 +369,12 @@ public int getStartLine() {
369369
* Gets The side of the first line of the range for a multi-line comment.
370370
*
371371
* <p>
372-
* <strong>Note:</strong> This field is only populated when comments are retrieved via
373-
* {@link GHPullRequest#listReviewComments()}. When retrieved via {@link GHPullRequestReview#listReviewComments()},
374-
* this method returns {@link Side#UNKNOWN} due to limitations in the GitHub API.
372+
* This field is not available on {@link GHPullRequestReview.ReviewComment} objects returned by
373+
* {@link GHPullRequestReview#listReviewComments()}. Use
374+
* {@link GHPullRequestReview.ReviewComment#readPullRequestReviewComment()} or
375+
* {@link GHPullRequest#listReviewComments()} to obtain this value.
375376
*
376377
* @return {@link Side} the side of the first line, or {@link Side#UNKNOWN} if not available
377-
* @see GHPullRequest#listReviewComments()
378378
*/
379379
public Side getStartSide() {
380380
return Side.from(startSide);
@@ -403,22 +403,6 @@ public PagedIterable<GHReaction> listReactions() {
403403
.toIterable(GHReaction[].class, item -> owner.root());
404404
}
405405

406-
/**
407-
* Refreshes this comment by fetching the full data from the API.
408-
*
409-
* <p>
410-
* This is useful when the comment was obtained via {@link GHPullRequestReview#listReviewComments()}, which uses a
411-
* GitHub API endpoint that does not return line-related fields. After calling this method, fields like
412-
* {@link #getLine()}, {@link #getOriginalLine()}, {@link #getSide()}, etc. will return their actual values.
413-
*
414-
* @throws IOException
415-
* if an I/O error occurs
416-
* @see GHPullRequest#listReviewComments()
417-
*/
418-
public void refresh() throws IOException {
419-
owner.root().createRequest().withUrlPath(getApiRoute()).fetchInto(this).wrapUp(owner);
420-
}
421-
422406
/**
423407
* Create a new comment that replies to this comment.
424408
*

0 commit comments

Comments
 (0)