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

Commit e54dc76

Browse files
authored
Revert "create review instead of single comments" (#187)
1 parent 4bf1cd4 commit e54dc76

File tree

3 files changed

+109
-70
lines changed

3 files changed

+109
-70
lines changed

dist/index.js

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

src/commenter.ts

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,9 @@ ${tag}`
129129
}
130130
}
131131

132-
private reviewCommentsBuffer: {
133-
path: string
134-
start_line: number
135-
end_line: number
136-
message: string
137-
}[] = []
138-
139-
async buffer_review_comment(
132+
async review_comment(
133+
pull_number: number,
134+
commit_id: string,
140135
path: string,
141136
start_line: number,
142137
end_line: number,
@@ -148,36 +143,66 @@ ${tag}`
148143
${message}
149144
150145
${tag}`
151-
152-
this.reviewCommentsBuffer.push({
153-
path,
154-
start_line,
155-
end_line,
156-
message
157-
})
158-
}
159-
160-
async submit_review(pull_number: number, commit_id: string) {
146+
// replace comment made by this action
161147
try {
162-
if (this.reviewCommentsBuffer.length > 0) {
163-
await octokit.pulls.createReview({
164-
owner: repo.owner,
165-
repo: repo.repo,
166-
pull_number,
167-
commit_id,
168-
event: 'COMMENT',
169-
comments: this.reviewCommentsBuffer.map(comment => ({
170-
path: comment.path,
171-
body: comment.message,
172-
line: comment.end_line,
173-
start_line: comment.start_line,
148+
let found = false
149+
const comments = await this.get_comments_at_range(
150+
pull_number,
151+
path,
152+
start_line,
153+
end_line
154+
)
155+
for (const comment of comments) {
156+
if (comment.body.includes(tag)) {
157+
core.info(
158+
`Updating review comment for ${path}:${start_line}-${end_line}: ${message}`
159+
)
160+
await octokit.pulls.updateReviewComment({
161+
owner: repo.owner,
162+
repo: repo.repo,
163+
comment_id: comment.id,
164+
body: message
165+
})
166+
found = true
167+
break
168+
}
169+
}
170+
171+
if (!found) {
172+
core.info(
173+
`Creating new review comment for ${path}:${start_line}-${end_line}: ${message}`
174+
)
175+
// if start_line is same as end_line, it's a single line comment
176+
// otherwise it's a multi-line comment
177+
if (start_line === end_line) {
178+
await octokit.pulls.createReviewComment({
179+
owner: repo.owner,
180+
repo: repo.repo,
181+
pull_number,
182+
body: message,
183+
commit_id,
184+
path,
185+
line: end_line
186+
})
187+
} else {
188+
await octokit.pulls.createReviewComment({
189+
owner: repo.owner,
190+
repo: repo.repo,
191+
pull_number,
192+
body: message,
193+
commit_id,
194+
path,
195+
line: end_line,
196+
start_line,
174197
start_side: 'RIGHT'
175-
}))
176-
})
177-
this.reviewCommentsBuffer = []
198+
})
199+
}
178200
}
179201
} catch (e) {
180-
core.warning(`Failed to submit review: ${e}`)
202+
core.warning(
203+
`Failed to post review comment, for ${path}:${start_line}-${end_line}: ${e}`
204+
)
205+
// throw error
181206
throw e
182207
}
183208
}

src/review.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ ${comment_chain}
610610
continue
611611
}
612612
try {
613-
await commenter.buffer_review_comment(
613+
await commenter.review_comment(
614+
context.payload.pull_request.number,
615+
commits[commits.length - 1].sha,
614616
filename,
615617
review.start_line,
616618
review.end_line,
@@ -750,12 +752,6 @@ ${
750752

751753
// post the final summary comment
752754
await commenter.comment(`${summarize_comment}`, SUMMARIZE_TAG, 'replace')
753-
754-
// post the review
755-
await commenter.submit_review(
756-
context.payload.pull_request.number,
757-
commits[commits.length - 1].sha
758-
)
759755
}
760756

761757
const split_patch = (patch: string | null | undefined): string[] => {

0 commit comments

Comments
 (0)