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

Commit b73fb59

Browse files
authored
santize suggestions (#199)
1 parent a5a1bf3 commit b73fb59

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

dist/index.js

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/review.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,11 @@ function parseReview(
869869
let currentComment = ''
870870
function storeReview(): void {
871871
if (currentStartLine !== null && currentEndLine !== null) {
872+
const sanitizedComment = sanitizeComment(currentComment.trim())
872873
const review: Review = {
873874
start_line: currentStartLine,
874875
end_line: currentEndLine,
875-
comment: currentComment.trim()
876+
comment: sanitizedComment.trim()
876877
}
877878

878879
let within_patch = false
@@ -915,6 +916,44 @@ ${review.comment}`
915916
}
916917
}
917918

919+
function sanitizeComment(comment: string): string {
920+
const suggestionStart = '```suggestion'
921+
const suggestionEnd = '```'
922+
const lineNumberRegex = /^ *(\d+): /gm
923+
924+
let suggestionStartIndex = comment.indexOf(suggestionStart)
925+
926+
while (suggestionStartIndex !== -1) {
927+
const suggestionEndIndex = comment.indexOf(
928+
suggestionEnd,
929+
suggestionStartIndex + suggestionStart.length
930+
)
931+
932+
if (suggestionEndIndex === -1) break
933+
934+
const suggestionBlock = comment.substring(
935+
suggestionStartIndex + suggestionStart.length,
936+
suggestionEndIndex
937+
)
938+
const sanitizedBlock = suggestionBlock.replace(lineNumberRegex, '')
939+
940+
comment =
941+
comment.slice(0, suggestionStartIndex + suggestionStart.length) +
942+
sanitizedBlock +
943+
comment.slice(suggestionEndIndex)
944+
945+
suggestionStartIndex = comment.indexOf(
946+
suggestionStart,
947+
suggestionStartIndex +
948+
suggestionStart.length +
949+
sanitizedBlock.length +
950+
suggestionEnd.length
951+
)
952+
}
953+
954+
return comment
955+
}
956+
918957
for (const line of lines) {
919958
const lineNumberRangeMatch = line.match(lineNumberRangeRegex)
920959

0 commit comments

Comments
 (0)