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

Commit 50b4b93

Browse files
authored
Provide file diff when replying to whole file review comments (#64)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI New Feature: Users can now view file diffs when replying to whole file review comments. The default system message has been updated and prompts for confirmation have been added. Additionally, the code now checks for file_content length before setting it, and updates the Tips section. <!-- end of auto-generated comment: release notes by openai -->
1 parent df2286c commit 50b4b93

File tree

6 files changed

+104
-45
lines changed

6 files changed

+104
-45
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Example:
8484

8585
![PR Conversation](./docs/images/openai-review-conversation.png)
8686

87+
![PR Conversation Request](./docs/images/openai-review-request.png)
88+
8789
#### Environment variables
8890

8991
- `GITHUB_TOKEN`: This should already be available to the GitHub Action

action.yml

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ inputs:
7070
required: false
7171
description: 'System message to be sent to OpenAI'
7272
default: |
73-
You are a highly experienced software engineer with a strong ability to
74-
review code changes thoroughly.You can uncover issues such as logic
75-
errors, syntax errors, out of bound errors, potential data races,
73+
You are `@openai`, a highly experienced software engineer with a strong
74+
ability to review code changes thoroughly.You can uncover issues such as
75+
logic errors, syntax errors, out of bound errors, potential data races,
7676
livelocks, starvation, suspension, order violation, atomicity violation,
7777
consistency, complexity, error handling, and more.
7878
79-
We will be conducting code reviews today.
79+
You will be conducting code reviews today and write code if asked to do so.
8080
summarize_beginning:
8181
required: false
8282
description: 'The prompt for the whole pull request'
@@ -156,7 +156,7 @@ inputs:
156156
$summary
157157
```
158158
159-
Reply "OK" to confirm that you are ready for further instructions.
159+
Reply "OK" to confirm.
160160
review_file:
161161
required: false
162162
description: 'The prompt for each file'
@@ -167,6 +167,8 @@ inputs:
167167
```
168168
$file_content
169169
```
170+
171+
Reply "OK" to confirm.
170172
review_file_diff:
171173
required: false
172174
description: 'The prompt for each file diff'
@@ -177,6 +179,8 @@ inputs:
177179
```diff
178180
$file_diff
179181
```
182+
183+
Reply "OK" to confirm.
180184
review_patch_begin:
181185
required: false
182186
description: 'The prompt for each file diff'
@@ -187,7 +191,9 @@ inputs:
187191
"LGTM!" with a short comment.
188192
189193
Your responses will be recorded as review comments on the pull request.
190-
Markdown format is preferred for your responses. Reply "OK" to confirm.
194+
Markdown format is preferred for your responses.
195+
196+
Reply "OK" to confirm.
191197
review_patch:
192198
required: false
193199
description: 'The prompt for each chunks/patches'
@@ -211,11 +217,13 @@ inputs:
211217
$system_message
212218
213219
A comment was made on a review for a diff patch on file
214-
`$filename`. You will be replying directly to that. If possible, I
215-
will provide you the file and the entire diff to help
216-
provide overall context.
220+
`$filename`. I would like you to follow the instructions
221+
in that comment.
217222
218-
For context, the pull request has the title "$title" and the following
223+
If possible, I will provide you the file and the entire diff
224+
to help provide overall context for your response.
225+
226+
The pull request has the title "$title" and the following
219227
description:
220228
221229
```
@@ -238,6 +246,8 @@ inputs:
238246
```
239247
$file_content
240248
```
249+
250+
Reply "OK" to confirm.
241251
comment_file_diff:
242252
required: false
243253
description: 'Prompt for file diff'
@@ -247,12 +257,14 @@ inputs:
247257
```diff
248258
$file_diff
249259
```
260+
261+
Reply "OK" to confirm.
250262
comment:
251263
required: false
252264
description: 'Prompt for comment'
253265
default: |
254-
I would like you to reply to the new comment made on
255-
a conversation chain on a code review diff.
266+
I would like you to follow the instructions in the new
267+
comment made on a conversation chain on a code review diff.
256268
257269
Diff being commented on:
258270
@@ -269,19 +281,21 @@ inputs:
269281
$comment_chain
270282
```
271283
272-
Please reply directly to the new comment in the conversation
273-
chain (instead of suggesting a reply) and your reply will be
274-
posted as-is.
284+
Please reply directly to the new comment (instead of suggesting
285+
a reply) and your reply will be posted as-is.
286+
287+
If the comment contains instructions/request for you, please comply.
288+
For example, if the comment is asking you to generate documentation
289+
comments on the code, in your reply please generate the required code.
275290
276-
In your reply, please make sure to begin the reply by
277-
tagging the user with "@user".
291+
In your reply, please make sure to begin the reply by tagging the user
292+
with "@user".
278293
279-
The comment that you need to directly reply to:
294+
The comment/request that you need to directly reply to:
280295
281296
```
282297
$comment
283298
```
284-
285299
runs:
286300
using: 'node16'
287301
main: 'dist/index.js'

dist/index.js

Lines changed: 34 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
276 KB
Loading

src/review-comment.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ 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 = ''
8283
try {
8384
const contents = await octokit.repos.getContent({
8485
owner: repo.owner,
@@ -89,7 +90,7 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
8990
if (contents.data) {
9091
if (!Array.isArray(contents.data)) {
9192
if (contents.data.type === 'file' && contents.data.content) {
92-
inputs.file_content = Buffer.from(
93+
file_content = Buffer.from(
9394
contents.data.content,
9495
'base64'
9596
).toString()
@@ -99,6 +100,8 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
99100
} catch (error) {
100101
core.warning(`Failed to get file contents: ${error}, skipping.`)
101102
}
103+
104+
let file_diff = ''
102105
try {
103106
// get diff for this file by comparing the base and head commits
104107
const diffAll = await octokit.repos.compareCommits({
@@ -112,7 +115,7 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
112115
if (files) {
113116
const file = files.find(f => f.filename === comment.path)
114117
if (file && file.patch) {
115-
inputs.file_diff = file.patch
118+
file_diff = file.patch
116119
}
117120
}
118121
}
@@ -135,10 +138,9 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
135138
{}
136139
)
137140
let next_comment_ids = comment_begin_ids
138-
if (inputs.file_content.length > 0) {
139-
const file_content_tokens = tokenizer.get_token_count(
140-
inputs.file_content
141-
)
141+
if (file_content.length > 0) {
142+
inputs.file_content = file_content
143+
const file_content_tokens = tokenizer.get_token_count(file_content)
142144
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
143145
const [file_content_resp, file_content_ids] = await bot.chat(
144146
prompts.render_comment_file(inputs),
@@ -150,8 +152,13 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
150152
}
151153
}
152154

153-
if (inputs.file_diff.length > 0) {
154-
const file_diff_tokens = tokenizer.get_token_count(inputs.file_diff)
155+
if (file_diff.length > 0) {
156+
inputs.file_diff = file_diff
157+
// use file diff if no diff was found in the comment
158+
if (inputs.diff.length === 0) {
159+
inputs.diff = file_diff
160+
}
161+
const file_diff_tokens = tokenizer.get_token_count(file_diff)
155162
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
156163
const [file_diff_resp, file_diff_ids] = await bot.chat(
157164
prompts.render_comment_file_diff(inputs),
@@ -198,6 +205,17 @@ ${COMMENT_REPLY_TAG}
198205
})
199206
} catch (error) {
200207
core.warning(`Failed to reply to the top-level comment`)
208+
try {
209+
await octokit.pulls.createReplyForReviewComment({
210+
owner: repo.owner,
211+
repo: repo.repo,
212+
pull_number,
213+
body: `Could not post the reply to the top-level comment due to the following error: ${error}`,
214+
comment_id: topLevelCommentId
215+
})
216+
} catch (error) {
217+
core.warning(`Failed to reply to the top-level comment`)
218+
}
201219
}
202220
} else {
203221
core.warning(`Failed to find the top-level comment to reply to`)

src/review.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,12 @@ export const codeReview = async (
164164
): Promise<[string, string] | null> => {
165165
const ins = inputs.clone()
166166
ins.filename = filename
167-
ins.file_content = file_content
168-
ins.file_diff = file_diff
167+
168+
if (file_content.length > 0) {
169+
ins.file_content = file_content
170+
}
169171
if (file_diff.length > 0) {
172+
ins.file_diff = file_diff
170173
const file_diff_tokens = tokenizer.get_token_count(file_diff)
171174
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
172175
// summarize diff
@@ -225,8 +228,8 @@ ${filename}: ${summary}
225228
---
226229
227230
Tips:
228-
- You can reply on the review comment left by this bot to ask follow-up questions.
229-
- You can invite the bot into a review conversation by typing \`@openai\` in the beginning of the comment.
231+
- Reply on the review comment left by this bot to ask follow-up questions.
232+
- Invite the bot into a review conversation by typing \`@openai\` in the beginning of the comment.
230233
`
231234

232235
next_summarize_ids = summarize_final_response_ids
@@ -263,10 +266,9 @@ Tips:
263266
const ins: Inputs = inputs.clone()
264267

265268
ins.filename = filename
266-
ins.file_content = file_content
267-
ins.file_diff = file_diff
268269

269270
if (file_content.length > 0) {
271+
ins.file_content = file_content
270272
const file_content_tokens = tokenizer.get_token_count(file_content)
271273
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
272274
try {
@@ -291,6 +293,7 @@ Tips:
291293
}
292294

293295
if (file_diff.length > 0) {
296+
ins.file_diff = file_diff
294297
const file_diff_tokens = tokenizer.get_token_count(file_diff)
295298
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
296299
try {

0 commit comments

Comments
 (0)