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

Use GitHub Review API for comments #373

Merged
merged 10 commits into from
Jul 22, 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
110 changes: 83 additions & 27 deletions dist/index.js

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

132 changes: 101 additions & 31 deletions src/commenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,43 @@ ${COMMENT_TAG}`
})
}

async deletePendingReview(pullNumber: number) {
try {
const reviews = await octokit.pulls.listReviews({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber
})

const pendingReview = reviews.data.find(
review => review.state === 'PENDING'
)

if (pendingReview) {
info(
`Deleting pending review for PR #${pullNumber} id: ${pendingReview.id}`
)
try {
await octokit.pulls.deletePendingReview({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber,
// eslint-disable-next-line camelcase
review_id: pendingReview.id
})
} catch (e) {
warning(`Failed to delete pending review: ${e}`)
}
}
} catch (e) {
warning(`Failed to list reviews: ${e}`)
}
}

async submitReview(pullNumber: number, commitId: string) {
info(
`Submitting review for PR #${pullNumber}, total comments: ${this.reviewCommentsBuffer.length}`
)
let commentCounter = 0
for (const comment of this.reviewCommentsBuffer) {
info(`Posting comment: ${comment.message}`)
let found = false
const comments = await this.getCommentsAtRange(
pullNumber,
comment.path,
Expand All @@ -207,25 +236,74 @@ ${COMMENT_TAG}`
for (const c of comments) {
if (c.body.includes(COMMENT_TAG)) {
info(
`Updating review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
`Deleting review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
try {
await octokit.pulls.updateReviewComment({
await octokit.pulls.deleteReviewComment({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
comment_id: c.id,
body: comment.message
comment_id: c.id
})
} catch (e) {
warning(`Failed to update review comment: ${e}`)
warning(`Failed to delete review comment: ${e}`)
}
found = true
break
}
}
}

await this.deletePendingReview(pullNumber)

const generateCommentData = (comment: any) => {
const commentData: any = {
path: comment.path,
body: comment.message,
line: comment.endLine
}

if (comment.startLine !== comment.endLine) {
// eslint-disable-next-line camelcase
commentData.start_line = comment.startLine
// eslint-disable-next-line camelcase
commentData.start_side = 'RIGHT'
}

return commentData
}

if (!found) {
try {
const review = await octokit.pulls.createReview({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber,
// eslint-disable-next-line camelcase
commit_id: commitId,
comments: this.reviewCommentsBuffer.map(comment =>
generateCommentData(comment)
)
})

info(
`Submitting review for PR #${pullNumber}, total comments: ${this.reviewCommentsBuffer.length}, review id: ${review.data.id}`
)

await octokit.pulls.submitReview({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber,
// eslint-disable-next-line camelcase
review_id: review.data.id,
event: 'COMMENT'
})
} catch (e) {
warning(
`Failed to create review: ${e}. Falling back to individual comments.`
)
await this.deletePendingReview(pullNumber)
let commentCounter = 0
for (const comment of this.reviewCommentsBuffer) {
info(
`Creating new review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
Expand All @@ -236,28 +314,20 @@ ${COMMENT_TAG}`
pull_number: pullNumber,
// eslint-disable-next-line camelcase
commit_id: commitId,
body: comment.message,
path: comment.path,
line: comment.endLine
...generateCommentData(comment)
}

if (comment.startLine !== comment.endLine) {
// eslint-disable-next-line camelcase
commentData.start_side = 'RIGHT'
// eslint-disable-next-line camelcase
commentData.start_line = comment.startLine
}
try {
await octokit.pulls.createReviewComment(commentData)
} catch (e) {
warning(`Failed to create review comment: ${e}`)
} catch (ee) {
warning(`Failed to create review comment: ${ee}`)
}
}

commentCounter++
info(
`Comment ${commentCounter}/${this.reviewCommentsBuffer.length} posted`
)
commentCounter++
info(
`Comment ${commentCounter}/${this.reviewCommentsBuffer.length} posted`
)
}
}
}

Expand Down Expand Up @@ -483,7 +553,7 @@ ${chain}

async create(body: string, target: number) {
try {
// get commend ID from the response
// get comment ID from the response
await octokit.issues.createComment({
owner: repo.owner,
repo: repo.repo,
Expand Down
2 changes: 1 addition & 1 deletion src/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Retry count: ${retryCount}
},
retry: {
doNotRetry: ['429'],
maxRetries: 10
maxRetries: 3
}
})