|
| 1 | +#!/usr/bin/env sh |
| 2 | +# Make a check's suggested-change review comments on a PR match a review.json |
| 3 | +# ({comments: [{path, line, start_line?, body}]}): post the new ones, update |
| 4 | +# the ones whose text changed, and delete the ones whose finding is gone. |
| 5 | +# GitHub sets line to null on comments it could not carry to the new revision, |
| 6 | +# so those are deleted too. Comments are matched by file, line, and the |
| 7 | +# marker comment on their first line, e.g. "<!-- check-links-finding: <url> -->". |
| 8 | +# |
| 9 | +# Usage: dev/sync-review-comments.sh '<!-- check-links-finding:' review.json |
| 10 | +# Needs GH_TOKEN, GITHUB_REPOSITORY, and PR_NUMBER. |
| 11 | +set -eu |
| 12 | +marker=$1 |
| 13 | +review=$2 |
| 14 | +key='(.path + ":" + (.line | tostring) + ":" + (.body | split("\n")[0]))' |
| 15 | +pulls="repos/$GITHUB_REPOSITORY/pulls" |
| 16 | + |
| 17 | +posted=$(gh api "$pulls/$PR_NUMBER/comments" --paginate \ |
| 18 | + --jq ".[] | select(.body | startswith(\"$marker\")) | {id, body, key: $key}" | jq -s .) |
| 19 | + |
| 20 | +printf '%s' "$posted" | jq -c --slurpfile review "$review" ".[] |
| 21 | + | .key as \$key |
| 22 | + | (\$review[0].comments | map(select($key == \$key)) | first) as \$wanted |
| 23 | + | if \$wanted == null then {id, method: \"DELETE\"} |
| 24 | + elif \$wanted.body != .body then {id, method: \"PATCH\", body: \$wanted.body} |
| 25 | + else empty end" \ |
| 26 | +| while read -r change; do |
| 27 | + comment="$pulls/comments/$(printf '%s' "$change" | jq -r .id)" |
| 28 | + if [ "$(printf '%s' "$change" | jq -r .method)" = DELETE ]; then |
| 29 | + gh api --method DELETE "$comment" < /dev/null |
| 30 | + else |
| 31 | + printf '%s' "$change" | jq '{body}' | gh api --method PATCH "$comment" --input - > /dev/null |
| 32 | + fi |
| 33 | +done |
| 34 | + |
| 35 | +fresh=$(jq --argjson posted "$posted" \ |
| 36 | + ".comments |= map(select($key as \$key | \$posted | any(.key == \$key) | not))" "$review") |
| 37 | +if [ "$(printf '%s' "$fresh" | jq '.comments | length')" -gt 0 ]; then |
| 38 | + printf '%s' "$fresh" | gh api --method POST "$pulls/$PR_NUMBER/reviews" --input - > /dev/null \ |
| 39 | + || echo "::warning::Could not post the suggested fixes; they are in the report above" |
| 40 | +fi |
0 commit comments