1- name : Post pull-request comment
1+ # This workflow is the second part of the process described in
2+ # .github/workflows/qhelp-pr-preview.yml
3+ # See that file for more info.
4+
5+ name : Add PR comment with QHelp preview
26on :
37 workflow_run :
4- workflows : ["Query help preview" ]
8+ workflows : [Render QHelp changes ]
59 types :
610 - completed
711
812permissions :
913 pull-requests : write
14+ actions : read
1015
1116jobs :
1217 post_comment :
@@ -17,15 +22,52 @@ jobs:
1722 env :
1823 GITHUB_TOKEN : ${{ github.token }}
1924 WORKFLOW_RUN_ID : ${{ github.event.workflow_run.id }}
20- - run : |
25+
26+ - name : Check that PR SHA matches workflow SHA
27+ run : |
2128 PR="$(grep -o '^[0-9]\+$' pr.txt)"
2229 PR_HEAD_SHA="$(gh api "/repos/${GITHUB_REPOSITORY}/pulls/${PR}" --jq .head.sha)"
2330 # Check that the pull-request head SHA matches the head SHA of the workflow run
2431 if [ "${WORKFLOW_RUN_HEAD_SHA}" != "${PR_HEAD_SHA}" ]; then
2532 echo "PR head SHA ${PR_HEAD_SHA} does not match workflow_run event SHA ${WORKFLOW_RUN_HEAD_SHA}. Stopping." 1>&2
2633 exit 1
2734 fi
28- gh pr comment "${PR}" --repo "${GITHUB_REPOSITORY}" -F comment.txt
2935 env :
3036 GITHUB_TOKEN : ${{ github.token }}
3137 WORKFLOW_RUN_HEAD_SHA : ${{ github.event.workflow_run.head_commit.id }}
38+
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
52+ 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 }}
65+
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 }}
0 commit comments