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

Commit

Permalink
create review instead of single comments (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
harjotgill authored Apr 18, 2023
1 parent e54dc76 commit a7ae0bc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 109 deletions.
76 changes: 29 additions & 47 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 34 additions & 59 deletions src/commenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ ${tag}`
}
}

async review_comment(
pull_number: number,
commit_id: string,
private reviewCommentsBuffer: {
path: string
start_line: number
end_line: number
message: string
}[] = []

async buffer_review_comment(
path: string,
start_line: number,
end_line: number,
Expand All @@ -143,66 +148,36 @@ ${tag}`
${message}
${tag}`
// replace comment made by this action
try {
let found = false
const comments = await this.get_comments_at_range(
pull_number,
path,
start_line,
end_line
)
for (const comment of comments) {
if (comment.body.includes(tag)) {
core.info(
`Updating review comment for ${path}:${start_line}-${end_line}: ${message}`
)
await octokit.pulls.updateReviewComment({
owner: repo.owner,
repo: repo.repo,
comment_id: comment.id,
body: message
})
found = true
break
}
}

if (!found) {
core.info(
`Creating new review comment for ${path}:${start_line}-${end_line}: ${message}`
)
// if start_line is same as end_line, it's a single line comment
// otherwise it's a multi-line comment
if (start_line === end_line) {
await octokit.pulls.createReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: message,
commit_id,
path,
line: end_line
})
} else {
await octokit.pulls.createReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: message,
commit_id,
path,
line: end_line,
start_line,
this.reviewCommentsBuffer.push({
path,
start_line,
end_line,
message
})
}

async submit_review(pull_number: number, commit_id: string) {
try {
if (this.reviewCommentsBuffer.length > 0) {
await octokit.pulls.createReview({
owner: repo.owner,
repo: repo.repo,
pull_number,
commit_id,
event: 'COMMENT',
comments: this.reviewCommentsBuffer.map(comment => ({
path: comment.path,
body: comment.message,
line: comment.end_line,
start_line: comment.start_line,
start_side: 'RIGHT'
})
}
}))
})
this.reviewCommentsBuffer = []
}
} catch (e) {
core.warning(
`Failed to post review comment, for ${path}:${start_line}-${end_line}: ${e}`
)
// throw error
core.warning(`Failed to submit review: ${e}`)
throw e
}
}
Expand Down
36 changes: 33 additions & 3 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,34 @@ ${comment_chain}
core.warning('No pull request found, skipping.')
continue
}
// sanitize review's start_line and end_line
// with patches' start_line and end_line
// if needed adjust start_line and end_line
// to make it fit within a closest patch
let within_patch = false
let closest_start_line = patches[0][0]
let closest_end_line = patches[0][1]
for (const [start_line, end_line] of patches) {
// see if review is within some patch
if (review.start_line >= start_line) {
closest_start_line = start_line
closest_end_line = end_line
if (review.end_line <= end_line) {
within_patch = true
break
}
}
}
if (!within_patch) {
// map the review to the closest patch
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}]
${review.comment}`
review.start_line = closest_start_line
review.end_line = closest_end_line
}

try {
await commenter.review_comment(
context.payload.pull_request.number,
commits[commits.length - 1].sha,
await commenter.buffer_review_comment(
filename,
review.start_line,
review.end_line,
Expand Down Expand Up @@ -752,6 +776,12 @@ ${

// post the final summary comment
await commenter.comment(`${summarize_comment}`, SUMMARIZE_TAG, 'replace')

// post the review
await commenter.submit_review(
context.payload.pull_request.number,
commits[commits.length - 1].sha
)
}

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

0 comments on commit a7ae0bc

Please sign in to comment.