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

change diff format #132

Merged
merged 20 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,30 +161,37 @@ inputs:
$summary
```

Here is the content of file `$filename` -
Here is the content of file `$filename` with
line numbers -
```
$file_content
```

Format for changes and review comments (if any)
on line ranges (line numbers in new file) -
<start_line-end_line>:
```diff
<diff_hunk>
Format for changes and review comments (if any) -

old_hunk:
```
<old code with line numbers>
```
new_hunk:
```
<new code with line numbers>
```
```text
<existing review comments>
```
---
<start_line-end_line>:
```diff
<diff_hunk>
old_hunk:
```
<old code with line numbers>
```
new_hunk:
```
<new code with line numbers>
```
---
...

The <diff_hunk> is in the unidiff format.

Changes for review -
$patches

Expand All @@ -193,13 +200,16 @@ inputs:
line ranges and review comments applicable for that line range.
Any other commentary outside of this format will be ignored
and will not be read by the parser -
<review_start_line-review_end_line>:

<start_line_number_new_code>-<end_line_number_new_code>:
<your review>
---
...

Make sure <review_start_line> and <review_end_line> for each review
must be within line ranges in the changes above. Don't echo back the
It's important that <start_line_number_new_code> and
<end_line_number_new_code> for each review must be within
line ranges of new_hunks as you cannot make comments
on line numbers of old_hunks. Don't echo back the
code provided to you as the line number range is sufficient to map
your comment to the relevant code section in GitHub. Your responses
will be recorded as multi-line review comments on the GitHub pull
Expand Down
95 changes: 82 additions & 13 deletions dist/index.js

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

4 changes: 2 additions & 2 deletions src/commenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ ${COMMENT_REPLY_TAG}
return comments.filter(
(comment: any) =>
comment.path === path &&
comment.start_line === start_line &&
comment.line === end_line &&
comment.start_line >= start_line &&
comment.line <= end_line &&
comment.body !== ''
)
}
Expand Down
108 changes: 94 additions & 14 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,28 @@ export const codeReview = async (
const patches: [number, number, string][] = []
for (const patch of split_patch(file.patch)) {
const patch_lines = patch_start_end_line(patch)
if (
!patch_lines ||
patch_lines.start_line === -1 ||
patch_lines.end_line === -1
) {
if (!patch_lines) {
continue
}
const hunks = parse_hunk(patch)
if (!hunks) {
continue
}
patches.push([patch_lines.start_line, patch_lines.end_line, patch])
const hunks_str = `old_hunk:
\`\`\`
${hunks.old_hunk}
\`\`\`
---
new_hunk:
\`\`\`
${hunks.new_hunk}
\`\`\`
`
patches.push([
patch_lines.new_hunk.start_line,
patch_lines.new_hunk.end_line,
hunks_str
])
}
if (patches.length > 0) {
return [file.filename, file_content, file_diff, patches]
Expand Down Expand Up @@ -319,7 +333,17 @@ ${
if (
file_content_tokens < options.heavy_token_limits.extra_content_tokens
) {
// rewrite file_content to preprend line numbers and colon before each line
const lines = file_content.split('\n')
let line_number = 1
file_content = ''
for (const line of lines) {
file_content += `${line_number}: ${line}
`
line_number += 1
}
ins.file_content = file_content
core.info(`file_content: ${file_content}`)
} else {
core.info(
`skip sending content of file: ${ins.filename} due to token count: ${file_content_tokens}`
Expand Down Expand Up @@ -348,6 +372,17 @@ ${
} else {
comment_chain = ''
}
// check comment_chain tokens and skip if too long
const comment_chain_tokens = tokenizer.get_token_count(comment_chain)
if (
comment_chain_tokens >
options.heavy_token_limits.extra_content_tokens
) {
core.info(
`skip sending comment chain of file: ${ins.filename} due to token count: ${comment_chain_tokens}`
)
comment_chain = ''
}
} catch (e: unknown) {
if (e instanceof ChatGPTError) {
core.warning(
Expand All @@ -356,10 +391,7 @@ ${
}
}
ins.patches += `
${start_line}-${end_line}:
\`\`\`diff
${patch}
\`\`\`
`
if (comment_chain !== '') {
ins.patches += `
Expand Down Expand Up @@ -484,21 +516,69 @@ const split_patch = (patch: string | null | undefined): string[] => {

const patch_start_end_line = (
patch: string
): {start_line: number; end_line: number} | null => {
): {
old_hunk: {start_line: number; end_line: number}
new_hunk: {start_line: number; end_line: number}
} | null => {
const pattern = /(^@@ -(\d+),(\d+) \+(\d+),(\d+) @@)/gm
const match = pattern.exec(patch)
if (match) {
const begin = parseInt(match[4])
const diff = parseInt(match[5])
const old_begin = parseInt(match[2])
const old_diff = parseInt(match[3])
const new_begin = parseInt(match[4])
const new_diff = parseInt(match[5])
return {
start_line: begin,
end_line: begin + diff - 1
old_hunk: {
start_line: old_begin,
end_line: old_begin + old_diff - 1
},
new_hunk: {
start_line: new_begin,
end_line: new_begin + new_diff - 1
}
}
} else {
return null
}
}

const parse_hunk = (
hunk: string
): {old_hunk: string; new_hunk: string} | null => {
const hunkInfo = patch_start_end_line(hunk)
if (!hunkInfo) {
return null
}

const old_hunk_lines: string[] = []
const new_hunk_lines: string[] = []

let old_line = hunkInfo.old_hunk.start_line
let new_line = hunkInfo.new_hunk.start_line

const lines = hunk.split('\n').slice(1) // Skip the @@ line

lines.forEach(line => {
if (line.startsWith('-')) {
old_hunk_lines.push(`${old_line}: ${line.substring(1)}`)
old_line++
} else if (line.startsWith('+')) {
new_hunk_lines.push(`${new_line}: ${line.substring(1)}`)
new_line++
} else {
old_hunk_lines.push(`${old_line}: ${line}`)
new_hunk_lines.push(`${new_line}: ${line}`)
old_line++
new_line++
}
})

return {
old_hunk: old_hunk_lines.join('\n'),
new_hunk: new_hunk_lines.join('\n')
}
}

type Review = {
start_line: number
end_line: number
Expand Down