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

Commit 01e52d7

Browse files
authored
small refactor (#51)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI Refactor: This pull request adds filename to inputs, removes an unused variable, and adds try-catch blocks for getting file contents and diff in `src/review-comment.ts`. Inputs are updated with file content and diff. <!-- end of auto-generated comment: release notes by openai -->
1 parent d6f4471 commit 01e52d7

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

dist/index.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/review-comment.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
6464
!comment.body.includes(COMMENT_REPLY_TAG)
6565
) {
6666
const pull_number = context.payload.pull_request.number
67-
const diff = comment.diff_hunk
6867

6968
inputs.comment = `${comment.user.login}: ${comment.body}`
70-
inputs.diff = diff
69+
inputs.diff = comment.diff_hunk
70+
inputs.filename = comment.path
7171

7272
const {chain: comment_chain, topLevelComment} =
7373
await commenter.get_conversation_chain(pull_number, comment)
@@ -79,8 +79,6 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
7979
comment_chain.includes(COMMENT_REPLY_TAG) ||
8080
comment.body.startsWith(ASK_BOT)
8181
) {
82-
let file_content = ''
83-
let file_diff = ''
8482
try {
8583
const contents = await octokit.repos.getContent({
8684
owner: repo.owner,
@@ -91,13 +89,17 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
9189
if (contents.data) {
9290
if (!Array.isArray(contents.data)) {
9391
if (contents.data.type === 'file' && contents.data.content) {
94-
file_content = Buffer.from(
92+
inputs.file_content = Buffer.from(
9593
contents.data.content,
9694
'base64'
9795
).toString()
9896
}
9997
}
10098
}
99+
} catch (error) {
100+
core.warning(`Failed to get file contents: ${error}, skipping.`)
101+
}
102+
try {
101103
// get diff for this file by comparing the base and head commits
102104
const diffAll = await octokit.repos.compareCommits({
103105
owner: repo.owner,
@@ -110,12 +112,12 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
110112
if (files) {
111113
const file = files.find(f => f.filename === comment.path)
112114
if (file && file.patch) {
113-
file_diff = file.patch
115+
inputs.file_diff = file.patch
114116
}
115117
}
116118
}
117119
} catch (error) {
118-
core.warning(`Failed to get file contents: ${error}, skipping.`)
120+
core.warning(`Failed to get file diff: ${error}, skipping.`)
119121
}
120122

121123
// get summary of the PR
@@ -127,18 +129,16 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
127129
inputs.summary = summary.body
128130
}
129131

130-
inputs.filename = comment.path
131-
inputs.file_content = file_content
132-
inputs.file_diff = file_diff
133-
134132
// begin comment generation
135133
const [, comment_begin_ids] = await bot.chat(
136134
prompts.render_comment_beginning(inputs),
137135
{}
138136
)
139137
let next_comment_ids = comment_begin_ids
140-
if (file_content.length > 0) {
141-
const file_content_tokens = tokenizer.get_token_count(file_content)
138+
if (inputs.file_content.length > 0) {
139+
const file_content_tokens = tokenizer.get_token_count(
140+
inputs.file_content
141+
)
142142
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
143143
const [file_content_resp, file_content_ids] = await bot.chat(
144144
prompts.render_comment_file(inputs),
@@ -150,8 +150,8 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
150150
}
151151
}
152152

153-
if (file_diff.length > 0) {
154-
const file_diff_tokens = tokenizer.get_token_count(file_diff)
153+
if (inputs.file_diff.length > 0) {
154+
const file_diff_tokens = tokenizer.get_token_count(inputs.file_diff)
155155
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
156156
const [file_diff_resp, file_diff_ids] = await bot.chat(
157157
prompts.render_comment_file_diff(inputs),

0 commit comments

Comments
 (0)