Skip to content

Commit

Permalink
error handling (coderabbitai#56)
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**

This pull request includes improvements to error handling in the
`review.ts` file. The changes made aim to improve the reliability and
robustness of the code by handling potential errors more gracefully.
This is a "Bug fix" that increases the `limit` variable and adds
try-catch blocks around various sections of code to handle errors from
OpenAI and the GitHub API.
<!-- end of auto-generated comment: release notes by openai -->
  • Loading branch information
harjotgill authored Mar 19, 2023
1 parent b3818f4 commit 3bfec53
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 84 deletions.
89 changes: 55 additions & 34 deletions dist/index.js

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

121 changes: 71 additions & 50 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const token = core.getInput('token')
const octokit = new Octokit({auth: `token ${token}`})
const context = github.context
const repo = context.repo
const limit = pLimit(2)
const limit = pLimit(4)

const MAX_TOKENS_FOR_EXTRA_CONTENT = 2500

Expand Down Expand Up @@ -170,15 +170,20 @@ export const codeReview = async (
const file_diff_tokens = tokenizer.get_token_count(file_diff)
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
// summarize diff
const [summarize_resp] = await bot.chat(
prompts.render_summarize_file_diff(ins),
summarize_begin_ids
)
if (!summarize_resp) {
core.info('summarize: nothing obtained from openai')
try {
const [summarize_resp] = await bot.chat(
prompts.render_summarize_file_diff(ins),
summarize_begin_ids
)
if (!summarize_resp) {
core.info('summarize: nothing obtained from openai')
return null
} else {
return [filename, summarize_resp]
}
} catch (error) {
core.warning(`summarize: error from openai: ${error}`)
return null
} else {
return [filename, summarize_resp]
}
}
}
Expand Down Expand Up @@ -262,15 +267,19 @@ Tips:
if (file_content.length > 0) {
const file_content_tokens = tokenizer.get_token_count(file_content)
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
// review file
const [resp, review_file_ids] = await bot.chat(
prompts.render_review_file(ins),
next_review_ids
)
if (!resp) {
core.info('review: nothing obtained from openai')
} else {
next_review_ids = review_file_ids
try {
// review file
const [resp, review_file_ids] = await bot.chat(
prompts.render_review_file(ins),
next_review_ids
)
if (!resp) {
core.info('review: nothing obtained from openai')
} else {
next_review_ids = review_file_ids
}
} catch (error) {
core.warning(`review: error from openai: ${error}`)
}
} else {
core.info(
Expand All @@ -282,15 +291,19 @@ Tips:
if (file_diff.length > 0) {
const file_diff_tokens = tokenizer.get_token_count(file_diff)
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
// review diff
const [resp, review_diff_ids] = await bot.chat(
prompts.render_review_file_diff(ins),
next_review_ids
)
if (!resp) {
core.info('review: nothing obtained from openai')
} else {
next_review_ids = review_diff_ids
try {
// review diff
const [resp, review_diff_ids] = await bot.chat(
prompts.render_review_file_diff(ins),
next_review_ids
)
if (!resp) {
core.info('review: nothing obtained from openai')
} else {
next_review_ids = review_diff_ids
}
} catch (error) {
core.warning(`review: error from openai: ${error}`)
}
} else {
core.info(
Expand All @@ -313,33 +326,41 @@ Tips:
core.warning('No pull request found, skipping.')
continue
}
// get existing comments on the line
const all_chains = await commenter.get_conversation_chains_at_line(
context.payload.pull_request.number,
filename,
line,
COMMENT_REPLY_TAG
)

if (all_chains.length > 0) {
ins.comment_chain = all_chains
} else {
ins.comment_chain = 'no previous comments'
try {
// get existing comments on the line
const all_chains =
await commenter.get_conversation_chains_at_line(
context.payload.pull_request.number,
filename,
line,
COMMENT_REPLY_TAG
)

if (all_chains.length > 0) {
ins.comment_chain = all_chains
} else {
ins.comment_chain = 'no previous comments'
}
} catch (e: any) {
core.warning(
`Failed to get comments: ${e}, skipping. backtrace: ${e.stack}`
)
}

const [response, patch_ids] = await bot.chat(
prompts.render_review_patch(ins),
next_review_ids
)
if (!response) {
core.info('review: nothing obtained from openai')
continue
}
next_review_ids = patch_ids
if (!options.review_comment_lgtm && response.includes('LGTM')) {
continue
}
try {
const [response, patch_ids] = await bot.chat(
prompts.render_review_patch(ins),
next_review_ids
)
if (!response) {
core.info('review: nothing obtained from openai')
continue
}
next_review_ids = patch_ids
if (!options.review_comment_lgtm && response.includes('LGTM')) {
continue
}
await commenter.review_comment(
context.payload.pull_request.number,
commits[commits.length - 1].sha,
Expand Down

0 comments on commit 3bfec53

Please sign in to comment.