2525
2626 - name : Check that PR SHA matches workflow SHA
2727 run : |
28- PR="$(grep -o '^[0-9]\+$' pr .txt)"
28+ PR="$(grep -o '^[0-9]\+$' pr_number .txt)"
2929 PR_HEAD_SHA="$(gh api "/repos/${GITHUB_REPOSITORY}/pulls/${PR}" --jq .head.sha)"
3030 # Check that the pull-request head SHA matches the head SHA of the workflow run
3131 if [ "${WORKFLOW_RUN_HEAD_SHA}" != "${PR_HEAD_SHA}" ]; then
@@ -36,38 +36,22 @@ jobs:
3636 GITHUB_TOKEN : ${{ github.token }}
3737 WORKFLOW_RUN_HEAD_SHA : ${{ github.event.workflow_run.head_commit.id }}
3838
39- # create-or-update-comment doesn't support fetching the comment directly
40- # from a file, so we have to output it from a step.
41- - id : get-comment-body
42- run : |
43- body="$(cat comment.txt)"
44- # Escape newlines - sadly required for some reason:
45- # https://github.community/t/set-output-truncates-multiline-strings/16852/3
46- body="${body//'%'/'%25'}"
47- body="${body//$'\n'/'%0A'}"
48- body="${body//$'\r'/'%0D'}"
49- echo "::set-output name=body::$body"
50-
51- - id : get-pr-number
39+ - name : Create or update comment
5240 run : |
53- pr="$(cat pr.txt)"
54- echo "::set-output name=pr-number::$pr"
55-
56- - name : Find existing comment, if it exists
57- uses : peter-evans/find-comment@v2
58- id : find-comment
59- with :
60- issue-number : ${{ steps.get-pr-number.outputs.pr-number }}
61- comment-author : ' github-actions[bot]'
62- body-includes : QHelp previews
63- direction : last
64- token : ${{ github.token }}
41+ PR_NUMBER="$(grep -o '^[0-9]\+$' pr_number.txt)"
42+ # comment_id.txt may be empty if there is no existing comment
43+ if [ -s comment_id.txt ]
44+ then
45+ COMMENT_ID="$(grep -o '^[0-9]\+$' comment_id.txt)"
46+ fi
6547
66- - name : Create or update comment
67- uses : peter-evans/create-or-update-comment@v2
68- with :
69- comment-id : ${{ steps.find-comment.outputs.comment-id }}
70- issue-number : ${{ steps.get-pr-number.outputs.pr-number }}
71- edit-mode : replace
72- token : ${{ github.token }}
73- body : ${{ steps.get-comment-body.outputs.body }}
48+ if [ -z "$COMMENT_ID" ]
49+ then
50+ # Create new comment
51+ jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" -X POST --input -
52+ else
53+ # Update existing comment
54+ jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${{ github.repository }}/issues/comments/${COMMENT_ID}" -X PATCH --input -
55+ fi
56+ env :
57+ GITHUB_TOKEN : ${{ github.token }}
0 commit comments