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

santize suggestions #199

Merged
merged 1 commit into from
Apr 19, 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
25 changes: 24 additions & 1 deletion dist/index.js

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

41 changes: 40 additions & 1 deletion src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,11 @@ function parseReview(
let currentComment = ''
function storeReview(): void {
if (currentStartLine !== null && currentEndLine !== null) {
const sanitizedComment = sanitizeComment(currentComment.trim())
const review: Review = {
start_line: currentStartLine,
end_line: currentEndLine,
comment: currentComment.trim()
comment: sanitizedComment.trim()
}

let within_patch = false
Expand Down Expand Up @@ -915,6 +916,44 @@ ${review.comment}`
}
}

function sanitizeComment(comment: string): string {
const suggestionStart = '```suggestion'
const suggestionEnd = '```'
const lineNumberRegex = /^ *(\d+): /gm

let suggestionStartIndex = comment.indexOf(suggestionStart)

while (suggestionStartIndex !== -1) {
const suggestionEndIndex = comment.indexOf(
suggestionEnd,
suggestionStartIndex + suggestionStart.length
)

if (suggestionEndIndex === -1) break

const suggestionBlock = comment.substring(
suggestionStartIndex + suggestionStart.length,
suggestionEndIndex
)
const sanitizedBlock = suggestionBlock.replace(lineNumberRegex, '')

comment =
comment.slice(0, suggestionStartIndex + suggestionStart.length) +
sanitizedBlock +
comment.slice(suggestionEndIndex)

suggestionStartIndex = comment.indexOf(
suggestionStart,
suggestionStartIndex +
suggestionStart.length +
sanitizedBlock.length +
suggestionEnd.length
)
}

return comment
}

for (const line of lines) {
const lineNumberRangeMatch = line.match(lineNumberRangeRegex)

Expand Down