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

Commit

Permalink
handle errors when posting comments (#274)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by openai -->
### Summary by OpenAI

Release Notes:

Bug fix: Added error handling and logging for updating or creating
review comments in `commenter.ts`. This improves the reliability of the
code by catching errors and providing warnings.

> "Bugs can be pesky, 
> But we won't let them get too risky. 
> With error handling in place, 
> Our code is now a safer space."
<!-- end of auto-generated comment: release notes by openai -->
  • Loading branch information
harjotgill committed May 5, 2023
1 parent 5d9174b commit 3e70a18
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 85 deletions.
80 changes: 43 additions & 37 deletions dist/index.js

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

98 changes: 50 additions & 48 deletions src/commenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,68 +194,70 @@ ${COMMENT_TAG}`
info(
`Submitting review for PR #${pullNumber}, total comments: ${this.reviewCommentsBuffer.length}`
)
try {
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,
comment.startLine,
comment.endLine
)
for (const c of comments) {
if (c.body.includes(COMMENT_TAG)) {
info(
`Updating review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
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,
comment.startLine,
comment.endLine
)
for (const c of comments) {
if (c.body.includes(COMMENT_TAG)) {
info(
`Updating review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
try {
await octokit.pulls.updateReviewComment({
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
comment_id: c.id,
body: comment.message
})
found = true
break
} catch (e) {
warning(`Failed to update review comment: ${e}`)
}
found = true
break
}
}

if (!found) {
info(
`Creating new review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
const commentData: any = {
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber,
// eslint-disable-next-line camelcase
commit_id: commitId,
body: comment.message,
path: comment.path,
line: comment.endLine
}

if (comment.startLine !== comment.endLine) {
// eslint-disable-next-line camelcase
commentData.start_side = 'RIGHT'
// eslint-disable-next-line camelcase
commentData.start_line = comment.startLine
}
if (!found) {
info(
`Creating new review comment for ${comment.path}:${comment.startLine}-${comment.endLine}: ${comment.message}`
)
const commentData: any = {
owner: repo.owner,
repo: repo.repo,
// eslint-disable-next-line camelcase
pull_number: pullNumber,
// eslint-disable-next-line camelcase
commit_id: commitId,
body: comment.message,
path: comment.path,
line: comment.endLine
}

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}`)
}

commentCounter++
info(
`Comment ${commentCounter}/${this.reviewCommentsBuffer.length} posted`
)
}
} catch (e) {
warning(`Failed to submit review: ${e}`)
throw e

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

Expand Down

0 comments on commit 3e70a18

Please sign in to comment.