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

create review instead of single comments #185

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 7 additions & 3 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,7 @@ ${comment_chain}
continue
}
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 +750,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
)
harjotgill marked this conversation as resolved.
Show resolved Hide resolved
}

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