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

Commit

Permalink
Provide file diff when replying to whole file review comments (#64)
Browse files Browse the repository at this point in the history
<!-- 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 -->
  • Loading branch information
harjotgill authored Mar 19, 2023
1 parent df2286c commit 50b4b93
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 45 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Example:

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

![PR Conversation Request](./docs/images/openai-review-request.png)

#### Environment variables

- `GITHUB_TOKEN`: This should already be available to the GitHub Action
Expand Down
52 changes: 33 additions & 19 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ inputs:
required: false
description: 'System message to be sent to OpenAI'
default: |
You are a highly experienced software engineer with a strong ability to
review code changes thoroughly.You can uncover issues such as logic
errors, syntax errors, out of bound errors, potential data races,
You are `@openai`, a highly experienced software engineer with a strong
ability to review code changes thoroughly.You can uncover issues such as
logic errors, syntax errors, out of bound errors, potential data races,
livelocks, starvation, suspension, order violation, atomicity violation,
consistency, complexity, error handling, and more.
We will be conducting code reviews today.
You will be conducting code reviews today and write code if asked to do so.
summarize_beginning:
required: false
description: 'The prompt for the whole pull request'
Expand Down Expand Up @@ -156,7 +156,7 @@ inputs:
$summary
```
Reply "OK" to confirm that you are ready for further instructions.
Reply "OK" to confirm.
review_file:
required: false
description: 'The prompt for each file'
Expand All @@ -167,6 +167,8 @@ inputs:
```
$file_content
```
Reply "OK" to confirm.
review_file_diff:
required: false
description: 'The prompt for each file diff'
Expand All @@ -177,6 +179,8 @@ inputs:
```diff
$file_diff
```
Reply "OK" to confirm.
review_patch_begin:
required: false
description: 'The prompt for each file diff'
Expand All @@ -187,7 +191,9 @@ inputs:
"LGTM!" with a short comment.
Your responses will be recorded as review comments on the pull request.
Markdown format is preferred for your responses. Reply "OK" to confirm.
Markdown format is preferred for your responses.
Reply "OK" to confirm.
review_patch:
required: false
description: 'The prompt for each chunks/patches'
Expand All @@ -211,11 +217,13 @@ inputs:
$system_message
A comment was made on a review for a diff patch on file
`$filename`. You will be replying directly to that. If possible, I
will provide you the file and the entire diff to help
provide overall context.
`$filename`. I would like you to follow the instructions
in that comment.
For context, the pull request has the title "$title" and the following
If possible, I will provide you the file and the entire diff
to help provide overall context for your response.
The pull request has the title "$title" and the following
description:
```
Expand All @@ -238,6 +246,8 @@ inputs:
```
$file_content
```
Reply "OK" to confirm.
comment_file_diff:
required: false
description: 'Prompt for file diff'
Expand All @@ -247,12 +257,14 @@ inputs:
```diff
$file_diff
```
Reply "OK" to confirm.
comment:
required: false
description: 'Prompt for comment'
default: |
I would like you to reply to the new comment made on
a conversation chain on a code review diff.
I would like you to follow the instructions in the new
comment made on a conversation chain on a code review diff.
Diff being commented on:
Expand All @@ -269,19 +281,21 @@ inputs:
$comment_chain
```
Please reply directly to the new comment in the conversation
chain (instead of suggesting a reply) and your reply will be
posted as-is.
Please reply directly to the new comment (instead of suggesting
a reply) and your reply will be posted as-is.
If the comment contains instructions/request for you, please comply.
For example, if the comment is asking you to generate documentation
comments on the code, in your reply please generate the required code.
In your reply, please make sure to begin the reply by
tagging the user with "@user".
In your reply, please make sure to begin the reply by tagging the user
with "@user".
The comment that you need to directly reply to:
The comment/request that you need to directly reply to:
```
$comment
```
runs:
using: 'node16'
main: 'dist/index.js'
46 changes: 34 additions & 12 deletions dist/index.js

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

Binary file added docs/images/openai-review-request.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 26 additions & 8 deletions src/review-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
comment_chain.includes(COMMENT_REPLY_TAG) ||
comment.body.startsWith(ASK_BOT)
) {
let file_content = ''
try {
const contents = await octokit.repos.getContent({
owner: repo.owner,
Expand All @@ -89,7 +90,7 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
if (contents.data) {
if (!Array.isArray(contents.data)) {
if (contents.data.type === 'file' && contents.data.content) {
inputs.file_content = Buffer.from(
file_content = Buffer.from(
contents.data.content,
'base64'
).toString()
Expand All @@ -99,6 +100,8 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
} catch (error) {
core.warning(`Failed to get file contents: ${error}, skipping.`)
}

let file_diff = ''
try {
// get diff for this file by comparing the base and head commits
const diffAll = await octokit.repos.compareCommits({
Expand All @@ -112,7 +115,7 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
if (files) {
const file = files.find(f => f.filename === comment.path)
if (file && file.patch) {
inputs.file_diff = file.patch
file_diff = file.patch
}
}
}
Expand All @@ -135,10 +138,9 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
{}
)
let next_comment_ids = comment_begin_ids
if (inputs.file_content.length > 0) {
const file_content_tokens = tokenizer.get_token_count(
inputs.file_content
)
if (file_content.length > 0) {
inputs.file_content = file_content
const file_content_tokens = tokenizer.get_token_count(file_content)
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
const [file_content_resp, file_content_ids] = await bot.chat(
prompts.render_comment_file(inputs),
Expand All @@ -150,8 +152,13 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
}
}

if (inputs.file_diff.length > 0) {
const file_diff_tokens = tokenizer.get_token_count(inputs.file_diff)
if (file_diff.length > 0) {
inputs.file_diff = file_diff
// use file diff if no diff was found in the comment
if (inputs.diff.length === 0) {
inputs.diff = file_diff
}
const file_diff_tokens = tokenizer.get_token_count(file_diff)
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
const [file_diff_resp, file_diff_ids] = await bot.chat(
prompts.render_comment_file_diff(inputs),
Expand Down Expand Up @@ -198,6 +205,17 @@ ${COMMENT_REPLY_TAG}
})
} catch (error) {
core.warning(`Failed to reply to the top-level comment`)
try {
await octokit.pulls.createReplyForReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: `Could not post the reply to the top-level comment due to the following error: ${error}`,
comment_id: topLevelCommentId
})
} catch (error) {
core.warning(`Failed to reply to the top-level comment`)
}
}
} else {
core.warning(`Failed to find the top-level comment to reply to`)
Expand Down
15 changes: 9 additions & 6 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ export const codeReview = async (
): Promise<[string, string] | null> => {
const ins = inputs.clone()
ins.filename = filename
ins.file_content = file_content
ins.file_diff = file_diff

if (file_content.length > 0) {
ins.file_content = file_content
}
if (file_diff.length > 0) {
ins.file_diff = file_diff
const file_diff_tokens = tokenizer.get_token_count(file_diff)
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
// summarize diff
Expand Down Expand Up @@ -225,8 +228,8 @@ ${filename}: ${summary}
---
Tips:
- You can reply on the review comment left by this bot to ask follow-up questions.
- You can invite the bot into a review conversation by typing \`@openai\` in the beginning of the comment.
- Reply on the review comment left by this bot to ask follow-up questions.
- Invite the bot into a review conversation by typing \`@openai\` in the beginning of the comment.
`

next_summarize_ids = summarize_final_response_ids
Expand Down Expand Up @@ -263,10 +266,9 @@ Tips:
const ins: Inputs = inputs.clone()

ins.filename = filename
ins.file_content = file_content
ins.file_diff = file_diff

if (file_content.length > 0) {
ins.file_content = file_content
const file_content_tokens = tokenizer.get_token_count(file_content)
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
try {
Expand All @@ -291,6 +293,7 @@ Tips:
}

if (file_diff.length > 0) {
ins.file_diff = file_diff
const file_diff_tokens = tokenizer.get_token_count(file_diff)
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
try {
Expand Down

0 comments on commit 50b4b93

Please sign in to comment.