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

Commit a7ae0bc

Browse files
authored
create review instead of single comments (#188)
1 parent e54dc76 commit a7ae0bc

File tree

3 files changed

+96
-109
lines changed

3 files changed

+96
-109
lines changed

dist/index.js

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

src/commenter.ts

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

132-
async review_comment(
133-
pull_number: number,
134-
commit_id: string,
132+
private reviewCommentsBuffer: {
133+
path: string
134+
start_line: number
135+
end_line: number
136+
message: string
137+
}[] = []
138+
139+
async buffer_review_comment(
135140
path: string,
136141
start_line: number,
137142
end_line: number,
@@ -143,66 +148,36 @@ ${tag}`
143148
${message}
144149
145150
${tag}`
146-
// replace comment made by this action
147-
try {
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-
}
170151

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,
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) {
161+
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,
197174
start_side: 'RIGHT'
198-
})
199-
}
175+
}))
176+
})
177+
this.reviewCommentsBuffer = []
200178
}
201179
} catch (e) {
202-
core.warning(
203-
`Failed to post review comment, for ${path}:${start_line}-${end_line}: ${e}`
204-
)
205-
// throw error
180+
core.warning(`Failed to submit review: ${e}`)
206181
throw e
207182
}
208183
}

src/review.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,34 @@ ${comment_chain}
609609
core.warning('No pull request found, skipping.')
610610
continue
611611
}
612+
// sanitize review's start_line and end_line
613+
// with patches' start_line and end_line
614+
// if needed adjust start_line and end_line
615+
// to make it fit within a closest patch
616+
let within_patch = false
617+
let closest_start_line = patches[0][0]
618+
let closest_end_line = patches[0][1]
619+
for (const [start_line, end_line] of patches) {
620+
// see if review is within some patch
621+
if (review.start_line >= start_line) {
622+
closest_start_line = start_line
623+
closest_end_line = end_line
624+
if (review.end_line <= end_line) {
625+
within_patch = true
626+
break
627+
}
628+
}
629+
}
630+
if (!within_patch) {
631+
// map the review to the closest patch
632+
review.comment = `> Note: This review was outside of the patch, so it was mapped it to the closest patch. Original lines [${review.start_line}-${review.end_line}]
633+
${review.comment}`
634+
review.start_line = closest_start_line
635+
review.end_line = closest_end_line
636+
}
637+
612638
try {
613-
await commenter.review_comment(
614-
context.payload.pull_request.number,
615-
commits[commits.length - 1].sha,
639+
await commenter.buffer_review_comment(
616640
filename,
617641
review.start_line,
618642
review.end_line,
@@ -752,6 +776,12 @@ ${
752776

753777
// post the final summary comment
754778
await commenter.comment(`${summarize_comment}`, SUMMARIZE_TAG, 'replace')
779+
780+
// post the review
781+
await commenter.submit_review(
782+
context.payload.pull_request.number,
783+
commits[commits.length - 1].sha
784+
)
755785
}
756786

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

0 commit comments

Comments
 (0)