Skip to content

Commit 47d142e

Browse files
check-links: move review-comment sync to dev/sync-review-comments.sh; update comments whose text changed
Amp-Thread-ID: https://ampcode.com/threads/T-01a08fee-74b4-76dc-aaf9-d1245d68fdc9 Co-authored-by: Amp <amp@ampcode.com>
1 parent a47d2e7 commit 47d142e

2 files changed

Lines changed: 43 additions & 24 deletions

File tree

.github/workflows/check-links.yml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,13 @@ jobs:
9797
fi
9898
9999
- name: Suggest fixes as review comments
100-
# One suggested change per finding with a fix. Suggestions already on the
101-
# PR (same file, line, and link) are not posted again; suggestions for
102-
# findings that are gone are deleted. GitHub sets line to null on comments
103-
# it could not carry to the new revision, so those are deleted too.
100+
# One suggested change per finding with a fix, kept in sync with the
101+
# findings; see dev/sync-review-comments.sh
104102
if: github.event.pull_request.head.repo.full_name == github.repository
105103
env:
106104
GH_TOKEN: ${{ github.token }}
107105
PR_NUMBER: ${{ github.event.pull_request.number }}
108-
run: |
109-
key='(.path + ":" + (.line | tostring) + ":" + (.body | split("\n")[0]))'
110-
gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/comments" --paginate \
111-
--jq ".[] | select(.body | startswith(\"<!-- check-links-finding:\")) | {id, key: $key}" \
112-
| jq -s . > "$RUNNER_TEMP/posted.json"
113-
jq --slurpfile posted "$RUNNER_TEMP/posted.json" \
114-
".comments |= map(select($key as \$key | \$posted[0] | any(.key == \$key) | not))" \
115-
"$RUNNER_TEMP/review.json" > "$RUNNER_TEMP/review-new.json"
116-
117-
jq -r --slurpfile review "$RUNNER_TEMP/review.json" \
118-
".[] | select(.key as \$key | \$review[0].comments | any($key == \$key) | not) | .id" \
119-
"$RUNNER_TEMP/posted.json" | while read -r comment_id; do
120-
gh api --method DELETE "repos/$GITHUB_REPOSITORY/pulls/comments/$comment_id"
121-
done
122-
123-
if [ "$(jq '.comments | length' "$RUNNER_TEMP/review-new.json")" -gt 0 ]; then
124-
gh api --method POST "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews" \
125-
--input "$RUNNER_TEMP/review-new.json" > /dev/null \
126-
|| echo "::warning::Could not post the suggested fixes; they are in the report above"
127-
fi
106+
run: dev/sync-review-comments.sh '<!-- check-links-finding:' "$RUNNER_TEMP/review.json"
128107

129108
- name: Fail when this PR introduces broken links
130109
if: steps.check.outputs.broken == 'true'

dev/sync-review-comments.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

Comments
 (0)