Skip to content

Commit 3ea6ba5

Browse files
authored
Merge pull request github#8618 from hmac/hmac/qlhelp-comment-workflow
Update existing qhelp comment, if it exists
2 parents 9927a82 + 942388e commit 3ea6ba5

File tree

2 files changed

+93
-11
lines changed

2 files changed

+93
-11
lines changed

.github/workflows/post-pr-comment.yml

Lines changed: 48 additions & 5 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: Post PR comment
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,53 @@ jobs:
1722
env:
1823
GITHUB_TOKEN: ${{ github.token }}
1924
WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
20-
- run: |
21-
PR="$(grep -o '^[0-9]\+$' pr.txt)"
25+
26+
- name: Check that PR SHA matches workflow SHA
27+
run: |
28+
PR="$(grep -o '^[0-9]\+$' pr_number.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+
- name: Create or update comment
40+
run: |
41+
COMMENT_PREFIX="QHelp previews"
42+
COMMENT_AUTHOR="github-actions[bot]"
43+
PR_NUMBER="$(grep -o '^[0-9]\+$' pr_number.txt)"
44+
45+
# If there is no existing comment, comment_id.txt will contain just a
46+
# newline (due to jq & gh behaviour). This will cause grep to fail, so
47+
# we catch that.
48+
RAW_COMMENT_ID=$(grep -o '^[0-9]\+$' comment_id.txt || true)
49+
50+
if [ $RAW_COMMENT_ID ]
51+
then
52+
# Fetch existing comment, and validate:
53+
# - comment belongs to the PR with number $PR_NUMBER
54+
# - comment starts with the expected prefix ("QHelp previews")
55+
# - comment author is github-actions[bot]
56+
FILTER='select(.issue_url | endswith($repo+"/issues/"+$pr))
57+
| select(.body | startswith($prefix))
58+
| select(.user.login == $author)
59+
| .id'
60+
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${RAW_COMMENT_ID}" | jq --arg repo "${GITHUB_REPOSITORY}" --arg pr "${PR_NUMBER}" --arg prefix "${COMMENT_PREFIX}" --arg author "${COMMENT_AUTHOR}" "${FILTER}")
61+
if [ $COMMENT_ID ]
62+
then
63+
# Update existing comment
64+
jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -X PATCH --input -
65+
else
66+
echo "Comment ${RAW_COMMENT_ID} did not pass validations: not editing." >&2
67+
exit 1
68+
fi
69+
else
70+
# Create new comment
71+
jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input -
72+
fi
73+
env:
74+
GITHUB_TOKEN: ${{ github.token }}

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
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 markdown in a file called `comment_body.txt`.
3+
# It then checks if there's an existing comment on the pull request generated by
4+
# this workflow, and writes the comment ID to `comment_id.txt`.
5+
# It also writes the PR number to `pr_number.txt`.
6+
# These three files are uploaded as an artifact.
7+
8+
# When this workflow completes, the workflow "Post PR comment" runs.
9+
# It downloads the artifact and adds a comment to the PR with the rendered
10+
# QHelp.
11+
12+
# The task is split like this because creating PR comments requires extra
13+
# permissions that we don't want to expose to PRs from external forks.
14+
15+
# For more info see:
16+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
17+
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
18+
name: Render QHelp changes
219

320
permissions:
421
contents: read
22+
pull-requests: read
523

624
on:
725
pull_request:
@@ -15,12 +33,16 @@ jobs:
1533
qhelp:
1634
runs-on: ubuntu-latest
1735
steps:
18-
- run: echo "${{ github.event.number }}" > pr.txt
36+
- run: echo "${PR_NUMBER}" > pr_number.txt
37+
env:
38+
PR_NUMBER: ${{ github.event.number }}
1939
- uses: actions/upload-artifact@v2
2040
with:
2141
name: comment
22-
path: pr.txt
42+
path: pr_number.txt
43+
if-no-files-found: error
2344
retention-days: 1
45+
2446
- uses: actions/checkout@v2
2547
with:
2648
fetch-depth: 2
@@ -36,7 +58,7 @@ jobs:
3658
- name: QHelp preview
3759
run: |
3860
EXIT_CODE=0
39-
echo "QHelp previews:" > comment.txt
61+
echo "QHelp previews:" > comment_body.txt
4062
while read -r -d $'\0' path; do
4163
if [ ! -f "${path}" ]; then
4264
exit 1
@@ -52,12 +74,29 @@ jobs:
5274
echo '```'
5375
fi
5476
echo "</details>"
55-
done < "${RUNNER_TEMP}/paths.txt" >> comment.txt
77+
done < "${RUNNER_TEMP}/paths.txt" >> comment_body.txt
5678
exit "${EXIT_CODE}"
5779
5880
- if: always()
5981
uses: actions/upload-artifact@v2
6082
with:
6183
name: comment
62-
path: comment.txt
84+
path: comment_body.txt
85+
if-no-files-found: error
86+
retention-days: 1
87+
88+
- name: Save ID of existing QHelp comment (if it exists)
89+
run: |
90+
# Find the latest comment starting with "QHelp previews"
91+
COMMENT_PREFIX="QHelp previews"
92+
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate | jq --arg prefix "${COMMENT_PREFIX}" '[.[] | select(.body|startswith($prefix)) | .id] | max' > comment_id.txt
93+
env:
94+
GITHUB_TOKEN: ${{ github.token }}
95+
PR_NUMBER: ${{ github.event.number }}
96+
97+
- uses: actions/upload-artifact@v2
98+
with:
99+
name: comment
100+
path: comment_id.txt
101+
if-no-files-found: error
63102
retention-days: 1

0 commit comments

Comments
 (0)