Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9116dbd

Browse files
committed
Update QHelp PR comment if it already exists
If we've already commented on a PR with a preview of the QHelp changes, then update the existing comment instead of creating a new one.
1 parent 50dc382 commit 9116dbd

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
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
26
on:
37
workflow_run:
4-
workflows: ["Query help preview"]
8+
workflows: [Render QHelp changes]
59
types:
610
- completed
711

812
permissions:
913
pull-requests: write
14+
actions: read
1015

1116
jobs:
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 }}

.github/workflows/qhelp-pr-preview.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
name: Query help preview
1+
# This workflow checks for any changes in .qhelp files in pull requests.
2+
# For any changed files, it renders them to HTML in a file called `comment.txt`.
3+
# It also writes the PR number to `pr.txt`.
4+
# These two files are uploaded as an artifact.
5+
6+
# When this workflow completes, the workflow "Add PR comment with QHelp preview" runs.
7+
# It downloads the artifact and adds a comment to the PR with the rendered
8+
# QHelp.
9+
10+
# The task is split like this because creating PR comments requires extra
11+
# permissions that we don't want to expose to PRs from external forks.
12+
13+
# For more info see:
14+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
15+
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
16+
name: Render QHelp changes
217

318
permissions:
419
contents: read

0 commit comments

Comments
 (0)