diff --git a/.vale/templates/bot-comment-output.tmpl b/.vale/templates/bot-comment-output.tmpl index 96fceebbd2cd..4b1ae38871c1 100644 --- a/.vale/templates/bot-comment-output.tmpl +++ b/.vale/templates/bot-comment-output.tmpl @@ -29,7 +29,12 @@ {{- /* Variables setup */ -}} {{- $loc := printf "%d" .Line -}} {{- $check := printf "%s" .Check -}} - {{- $message := printf "%s" .Message -}} + {{- /* Escape special characters for valid JSON */ -}} + {{- $message := replace "\\" "\\\\" (printf "%s" .Message) -}} + {{- $message = replace "\"" "\\\"" $message -}} + {{- $message = replace "\n" "\\n" $message -}} + {{- $message = replace "\r" "\\r" $message -}} + {{- $message = replace "\t" "\\t" $message -}} {{- /* Only add a link for RedHat rule errors */ -}} {{- $link := "" -}} {{- if contains "RedHat." .Check -}} @@ -45,4 +50,5 @@ } {{end -}} {{end -}} -] \ No newline at end of file +] + diff --git a/scripts/prow-vale-review.sh b/scripts/prow-vale-review.sh index e5be79d731eb..4338b0ebf872 100755 --- a/scripts/prow-vale-review.sh +++ b/scripts/prow-vale-review.sh @@ -23,7 +23,18 @@ function post_review_comment { BODY=$1 FILENAME=$2 echo "Sending review comment curl request..." - curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_AUTH_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/openshift/openshift-docs/pulls/$PULL_NUMBER/comments -d '{"body":"'"$BODY"'","commit_id":"'"$COMMIT_ID"'","path":"'"$FILENAME"'","line":'"$LINE_NUMBER"',"side":"RIGHT"}' + # Use jq to excruciatingly craft JSON payload + # jq -n because we're constructing from scratch per https://jqlang.org/manual/ + # --arg for string, --argjson for integer + # body constructed from https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#create-a-review-comment-for-a-pull-request + payload=$(jq -n \ + --arg body "$BODY" \ + --arg commit_id "$COMMIT_ID" \ + --arg path "$FILENAME" \ + --argjson line "$LINE_NUMBER" \ + '{body: $body, commit_id: $commit_id, path: $path, line: $line, side: "RIGHT"}') + echo "DEBUG payload:" "$payload" + curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_AUTH_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/openshift/openshift-docs/pulls/$PULL_NUMBER/comments -d "$payload" } @@ -132,4 +143,5 @@ do done -done \ No newline at end of file +done +