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

Commit 4e128be

Browse files
authored
sanitize entire response before parsing comments (#416)
<!-- This is an auto-generated comment: release notes by OSS CodeRabbit --> ### Summary by CodeRabbit ``` ### Bug Fixes: - Fixed a logic error in the `add` function in `prompts.ts`. The operation has been corrected from subtraction to addition. - Enhanced security in `review.ts` by sanitizing the entire response before parsing comments. ### Refactor: - Renamed `sanitizeComment` function to `sanitizeResponse` in `review.ts`, and expanded its functionality to sanitize code blocks for suggestions and diffs. ### Removed: - Removed single line comment functionality as it was deemed unnecessary. ``` > 🎉 Here's to bugs that are no more, > To logic errors shown the door. > With sanitized responses, we stand tall, > In the face of threats, big or small. > So here's to code that's clean and neat, > Making our victory oh so sweet! 🥳 <!-- end of auto-generated comment: release notes by OSS CodeRabbit -->
1 parent 9e60266 commit 4e128be

File tree

3 files changed

+17
-41
lines changed

3 files changed

+17
-41
lines changed

dist/index.js

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

src/prompts.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ consisting of review sections. Each review section must have a line number range
113113
and a review comment for that range. Use separator after each review section.
114114
Line number ranges for each review section must be within the range of a specific
115115
new hunk. Start line number must belong to the same hunk as the end line number.
116-
Provide the exact line number range (inclusive) for each issue.
116+
Provide the exact line number range (inclusive) for each review comment. To leave
117+
a review comment on a single line, use the same line number for start and end.
117118
118119
Take into consideration the context provided by old hunks, comment threads, and
119120
file content during your review. Remember, the hunk under review is a fragment of a
@@ -171,7 +172,7 @@ text \`LGTM!\` for that line range in the review section.
171172
18: return a + b
172173
19:
173174
20: def add(x, y):
174-
21: z = x - y
175+
21: z = x + y
175176
22: retrn z
176177
23:
177178
24: def multiply(x, y):
@@ -219,11 +220,9 @@ def complex_function(x, y):
219220
+ return c / 2
220221
\`\`\`
221222
---
222-
20-22:
223-
There's a logic error and a syntax error in the add function.
223+
22-22:
224+
There's a syntax error in the add function.
224225
\`\`\`suggestion
225-
def add(x, y):
226-
z = x + y
227226
return z
228227
\`\`\`
229228
---

src/review.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -868,21 +868,21 @@ function parseReview(
868868
): Review[] {
869869
const reviews: Review[] = []
870870

871+
response = sanitizeResponse(response.trim())
872+
871873
const lines = response.split('\n')
872874
const lineNumberRangeRegex = /(?:^|\s)(\d+)-(\d+):\s*$/
873-
const lineNumberSingleRegex = /(?:^|\s)(\d+):\s*$/ // New single line regex
874875
const commentSeparator = '---'
875876

876877
let currentStartLine: number | null = null
877878
let currentEndLine: number | null = null
878879
let currentComment = ''
879880
function storeReview(): void {
880881
if (currentStartLine !== null && currentEndLine !== null) {
881-
const sanitizedComment = sanitizeComment(currentComment.trim())
882882
const review: Review = {
883883
startLine: currentStartLine,
884884
endLine: currentEndLine,
885-
comment: sanitizedComment.trim()
885+
comment: currentComment
886886
}
887887

888888
let withinPatch = false
@@ -971,15 +971,14 @@ ${review.comment}`
971971
return comment
972972
}
973973

974-
function sanitizeComment(comment: string): string {
974+
function sanitizeResponse(comment: string): string {
975975
comment = sanitizeCodeBlock(comment, 'suggestion')
976976
comment = sanitizeCodeBlock(comment, 'diff')
977977
return comment
978978
}
979979

980980
for (const line of lines) {
981981
const lineNumberRangeMatch = line.match(lineNumberRangeRegex)
982-
const lineNumberSingleMatch = line.match(lineNumberSingleRegex) // Check for single line match
983982

984983
if (lineNumberRangeMatch != null) {
985984
storeReview()
@@ -990,15 +989,6 @@ ${review.comment}`
990989
info(`Found line number range: ${currentStartLine}-${currentEndLine}`)
991990
}
992991
continue
993-
} else if (lineNumberSingleMatch != null) {
994-
storeReview()
995-
currentStartLine = parseInt(lineNumberSingleMatch[1], 10)
996-
currentEndLine = currentStartLine // For single line comments, start and end are the same
997-
currentComment = ''
998-
if (debug) {
999-
info(`Found single line comment: ${currentStartLine}`)
1000-
}
1001-
continue
1002992
}
1003993

1004994
if (line.trim() === commentSeparator) {

0 commit comments

Comments
 (0)