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

sanitize entire response before parsing comments #416

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 8 additions & 21 deletions dist/index.js

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

11 changes: 5 additions & 6 deletions src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ consisting of review sections. Each review section must have a line number range
and a review comment for that range. Use separator after each review section.
Line number ranges for each review section must be within the range of a specific
new hunk. Start line number must belong to the same hunk as the end line number.
Provide the exact line number range (inclusive) for each issue.
Provide the exact line number range (inclusive) for each review comment. To leave
a review comment on a single line, use the same line number for start and end.

Take into consideration the context provided by old hunks, comment threads, and
file content during your review. Remember, the hunk under review is a fragment of a
Expand Down Expand Up @@ -171,7 +172,7 @@ text \`LGTM!\` for that line range in the review section.
18: return a + b
19:
20: def add(x, y):
21: z = x - y
21: z = x + y
harjotgill marked this conversation as resolved.
Show resolved Hide resolved
22: retrn z
23:
24: def multiply(x, y):
Expand Down Expand Up @@ -219,11 +220,9 @@ def complex_function(x, y):
+ return c / 2
\`\`\`
---
20-22:
There's a logic error and a syntax error in the add function.
22-22:
There's a syntax error in the add function.
\`\`\`suggestion
def add(x, y):
z = x + y
return z
\`\`\`
---
Expand Down
18 changes: 4 additions & 14 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,21 +868,21 @@ function parseReview(
): Review[] {
const reviews: Review[] = []

response = sanitizeResponse(response.trim())

const lines = response.split('\n')
const lineNumberRangeRegex = /(?:^|\s)(\d+)-(\d+):\s*$/
const lineNumberSingleRegex = /(?:^|\s)(\d+):\s*$/ // New single line regex
const commentSeparator = '---'

let currentStartLine: number | null = null
let currentEndLine: number | null = null
let currentComment = ''
function storeReview(): void {
if (currentStartLine !== null && currentEndLine !== null) {
const sanitizedComment = sanitizeComment(currentComment.trim())
const review: Review = {
startLine: currentStartLine,
endLine: currentEndLine,
comment: sanitizedComment.trim()
comment: currentComment
harjotgill marked this conversation as resolved.
Show resolved Hide resolved
}

let withinPatch = false
Expand Down Expand Up @@ -971,15 +971,14 @@ ${review.comment}`
return comment
}

function sanitizeComment(comment: string): string {
function sanitizeResponse(comment: string): string {
comment = sanitizeCodeBlock(comment, 'suggestion')
comment = sanitizeCodeBlock(comment, 'diff')
return comment
harjotgill marked this conversation as resolved.
Show resolved Hide resolved
}

for (const line of lines) {
const lineNumberRangeMatch = line.match(lineNumberRangeRegex)
const lineNumberSingleMatch = line.match(lineNumberSingleRegex) // Check for single line match

if (lineNumberRangeMatch != null) {
storeReview()
Expand All @@ -990,15 +989,6 @@ ${review.comment}`
info(`Found line number range: ${currentStartLine}-${currentEndLine}`)
}
continue
} else if (lineNumberSingleMatch != null) {
storeReview()
currentStartLine = parseInt(lineNumberSingleMatch[1], 10)
currentEndLine = currentStartLine // For single line comments, start and end are the same
currentComment = ''
if (debug) {
info(`Found single line comment: ${currentStartLine}`)
}
continue
}

if (line.trim() === commentSeparator) {
harjotgill marked this conversation as resolved.
Show resolved Hide resolved
Expand Down