Skip to content

Commit ce92dcf

Browse files
committed
chore: when diffing, update existing comments
LW-10047
1 parent 1cc7134 commit ce92dcf

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

.github/workflows/std.yml

+69-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,55 @@ concurrency:
4444
group: std-${{ github.workflow }}-${{ github.ref }}
4545
cancel-in-progress: true
4646
jobs:
47+
48+
tmp-comment:
49+
name: Diff & Comment
50+
if: github.event_name == 'pull_request' && (github.base_ref == 'master')
51+
permissions:
52+
contents: read
53+
pull-requests: write
54+
runs-on: ubuntu-22.04
55+
steps:
56+
- uses: actions/checkout@v4
57+
- name: Stub the Diff
58+
run: |
59+
echo "Hello, world! take 3" >pr-comment.md
60+
- name: Post Comment on the PR
61+
env:
62+
GH_TOKEN: ${{ github.token }}
63+
COMMENT_MARKER: "nix-helm-diff-777f3796-c80d-4d68-bf4f-8faad564f03f"
64+
run: |
65+
set -x
66+
prNumber=$(cut -d/ -f1 <<<'${{ github.ref_name }}')
67+
# The `gh` command doesn’t return numeric comment ids, but instead node_ids, which don’t work with the regular API
68+
# Why the regular API? Because the `gh` command doesn’t support editing comments:
69+
existingCommentId=$(
70+
curl --fail-with-body -sSL \
71+
-X GET \
72+
-H "Authorization: Bearer $GH_TOKEN" \
73+
-H "Accept: application/vnd.github+json" \
74+
-H "X-GitHub-Api-Version: 2022-11-28" \
75+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${prNumber}/comments" \
76+
| jq -r --arg commentMarker "$COMMENT_MARKER" 'first(.[] | select(.body | contains($commentMarker)) | .id)'
77+
)
78+
if [ -z "$existingCommentId" ]; then
79+
gh pr comment "$prNumber" --body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->"
80+
else
81+
curl --fail-with-body -sSL \
82+
-X PATCH \
83+
-H "Authorization: Bearer $GH_TOKEN" \
84+
-H "Accept: application/vnd.github+json" \
85+
-H "X-GitHub-Api-Version: 2022-11-28" \
86+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${existingCommentId}" \
87+
-d "$(jq --null-input --arg body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->" '{body: $body}')"
88+
fi
89+
- name: Fail
90+
run: |
91+
exit 77
92+
93+
4794
discover:
95+
needs: tmp-comment
4896
outputs:
4997
hits: ${{ steps.discovery.outputs.hits }}
5098
deployment-matrix: ${{ steps.deployment-matrix.outputs.deployment-matrix }}
@@ -218,9 +266,29 @@ jobs:
218266
- name: Post Comment on the PR
219267
env:
220268
GH_TOKEN: ${{ github.token }}
269+
COMMENT_MARKER: "nix-helm-diff-777f3796-c80d-4d68-bf4f-8faad564f03f"
221270
run: |
222271
prNumber=$(cut -d/ -f1 <<<'${{ github.ref_name }}')
223-
gh pr comment "$prNumber" --body "$(cat pr-comment.md)"
272+
273+
echo ==================================================================
274+
gh pr view "$prNumber" --json comments | tee comments.json
275+
echo ==================================================================
276+
277+
existingCommentId=$(cat comments.json \
278+
| jq -r --arg commentMarker "$COMMENT_MARKER" 'first(.comments[] | select(.body | contains($commentMarker)) | .id)')
279+
if [ -z "$existingCommentId" ]; then
280+
echo "; new comment needed"
281+
gh pr comment "$prNumber" --body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->"
282+
else
283+
# The `gh` command doesn’t have anything like `edit-comment`…
284+
curl --fail-with-body -sSL \
285+
-X PATCH \
286+
-H "Authorization: Bearer $GH_TOKEN" \
287+
-H "Accept: application/vnd.github+json" \
288+
-H "X-GitHub-Api-Version: 2022-11-28" \
289+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/comments/${existingCommentId}" \
290+
-d "$(jq --null-input --arg body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->" '{body: $body}')"
291+
fi
224292
225293
226294
deploy:

0 commit comments

Comments
 (0)