Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 0a7b5ff

Browse files
authored
fix comment conflicts (#140)
1 parent 86f3a37 commit 0a7b5ff

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ inputs:
202202
section in GitHub. Markdown format is preferred for text. Fenced code
203203
blocks should be used for complete code suggestions that replace code
204204
at those exact line number ranges. It's important that the code
205-
suggestions are complete and correctly formatted so that they can be
206-
committed as-is.
205+
suggestions are complete, correctly formatted and without line number
206+
annotations so that they can be committed as-is.
207207
208208
<start_line_number>-<end_line_number>:
209209
<review markdown>

dist/index.js

Lines changed: 25 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commenter.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ ${tag}`
156156
for (const comment of comments) {
157157
if (comment.body.includes(tag)) {
158158
core.info(
159-
`Updating review comment for ${path}:${end_line}: ${message}`
159+
`Updating review comment for ${path}:${start_line}-${end_line}: ${message}`
160160
)
161161
await octokit.pulls.updateReviewComment({
162162
owner: repo.owner,
@@ -261,7 +261,7 @@ ${COMMENT_REPLY_TAG}
261261
}
262262
}
263263

264-
async get_comments_at_range(
264+
async get_comments_within_range(
265265
pull_number: number,
266266
path: string,
267267
start_line: number,
@@ -279,14 +279,32 @@ ${COMMENT_REPLY_TAG}
279279
)
280280
}
281281

282-
async get_conversation_chains_at_range(
282+
async get_comments_at_range(
283+
pull_number: number,
284+
path: string,
285+
start_line: number,
286+
end_line: number
287+
) {
288+
const comments = await this.list_review_comments(pull_number)
289+
return comments.filter(
290+
(comment: any) =>
291+
comment.path === path &&
292+
comment.body !== '' &&
293+
((comment.start_line !== undefined &&
294+
comment.start_line === start_line &&
295+
comment.line === end_line) ||
296+
comment.line === end_line)
297+
)
298+
}
299+
300+
async get_conversation_chains_within_range(
283301
pull_number: number,
284302
path: string,
285303
start_line: number,
286304
end_line: number,
287305
tag = ''
288306
) {
289-
const existing_comments = await this.get_comments_at_range(
307+
const existing_comments = await this.get_comments_within_range(
290308
pull_number,
291309
path,
292310
start_line,

src/review.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ ${
400400

401401
let comment_chain = ''
402402
try {
403-
// get existing comments on the line
404-
const all_chains = await commenter.get_conversation_chains_at_range(
403+
const all_chains = await commenter.get_conversation_chains_within_range(
405404
context.payload.pull_request.number,
406405
filename,
407406
start_line,
@@ -457,8 +456,8 @@ ${comment_chain}
457456
return
458457
}
459458
// parse review
460-
const reviewMap = parseOpenAIReview(response, options.debug)
461-
for (const [, review] of reviewMap) {
459+
const reviews = parseReview(response, options.debug)
460+
for (const review of reviews) {
462461
// check for LGTM
463462
if (
464463
!options.review_comment_lgtm &&
@@ -649,11 +648,9 @@ type Review = {
649648
comment: string
650649
}
651650

652-
function parseOpenAIReview(
653-
response: string,
654-
debug = false
655-
): Map<string, Review> {
656-
const reviews = new Map<string, Review>()
651+
function parseReview(response: string, debug = false): Review[] {
652+
// instantiate an array of reviews
653+
const reviews: Review[] = []
657654

658655
// Split the response into lines
659656
const lines = response.split('\n')
@@ -671,9 +668,9 @@ function parseOpenAIReview(
671668
const lineNumberRangeMatch = line.match(lineNumberRangeRegex)
672669

673670
if (lineNumberRangeMatch) {
674-
// If there is a previous comment, store it in the reviews Map
671+
// If there is a previous comment, store it in the reviews
675672
if (currentStartLine !== null && currentEndLine !== null) {
676-
reviews.set(`${currentStartLine}-${currentEndLine}`, {
673+
reviews.push({
677674
start_line: currentStartLine,
678675
end_line: currentEndLine,
679676
comment: currentComment.trim()
@@ -697,9 +694,9 @@ function parseOpenAIReview(
697694

698695
// Check if the line is a comment separator
699696
if (line.trim() === commentSeparator) {
700-
// If there is a previous comment, store it in the reviews Map
697+
// If there is a previous comment, store it in the reviews
701698
if (currentStartLine !== null && currentEndLine !== null) {
702-
reviews.set(`${currentStartLine}-${currentEndLine}`, {
699+
reviews.push({
703700
start_line: currentStartLine,
704701
end_line: currentEndLine,
705702
comment: currentComment.trim()
@@ -724,9 +721,9 @@ function parseOpenAIReview(
724721
}
725722
}
726723

727-
// If there is a comment at the end of the response, store it in the reviews Map
724+
// If there is a comment at the end of the response, store it in the reviews
728725
if (currentStartLine !== null && currentEndLine !== null) {
729-
reviews.set(`${currentStartLine}-${currentEndLine}`, {
726+
reviews.push({
730727
start_line: currentStartLine,
731728
end_line: currentEndLine,
732729
comment: currentComment.trim()

0 commit comments

Comments
 (0)