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

Skip to content

Commit e349111

Browse files
committed
Use gh api instead of third-party actions
Also move more steps to the unprivileged workflow.
1 parent 9116dbd commit e349111

2 files changed

Lines changed: 44 additions & 42 deletions

File tree

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
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 }}

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# 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.
2+
# For any changed files, it renders them to HTML 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.
57

68
# When this workflow completes, the workflow "Add PR comment with QHelp preview" runs.
79
# It downloads the artifact and adds a comment to the PR with the rendered
@@ -17,6 +19,7 @@ name: Render QHelp changes
1719

1820
permissions:
1921
contents: read
22+
pull-requests: read
2023

2124
on:
2225
pull_request:
@@ -30,12 +33,13 @@ jobs:
3033
qhelp:
3134
runs-on: ubuntu-latest
3235
steps:
33-
- run: echo "${{ github.event.number }}" > pr.txt
36+
- run: echo "${{ github.event.number }}" > pr_number.txt
3437
- uses: actions/upload-artifact@v2
3538
with:
3639
name: comment
37-
path: pr.txt
40+
path: pr_number.txt
3841
retention-days: 1
42+
3943
- uses: actions/checkout@v2
4044
with:
4145
fetch-depth: 2
@@ -51,7 +55,7 @@ jobs:
5155
- name: QHelp preview
5256
run: |
5357
EXIT_CODE=0
54-
echo "QHelp previews:" > comment.txt
58+
echo "QHelp previews:" > comment_body.txt
5559
while read -r -d $'\0' path; do
5660
if [ ! -f "${path}" ]; then
5761
exit 1
@@ -67,12 +71,26 @@ jobs:
6771
echo '```'
6872
fi
6973
echo "</details>"
70-
done < "${RUNNER_TEMP}/paths.txt" >> comment.txt
74+
done < "${RUNNER_TEMP}/paths.txt" >> comment_body.txt
7175
exit "${EXIT_CODE}"
7276
7377
- if: always()
7478
uses: actions/upload-artifact@v2
7579
with:
7680
name: comment
77-
path: comment.txt
81+
path: comment_body.txt
82+
retention-days: 1
83+
84+
- name: Save ID of existing QHelp comment (if it exists)
85+
run: |
86+
# Find the latest comment starting with "QHelp previews"
87+
COMMENT_PREFIX="QHelp previews"
88+
gh api "repos/${{ github.repository }}/issues/${{ github.event.number }}/comments?per_page=100" --jq "[.[] | select(.body|startswith(\"${COMMENT_PREFIX}\")) | .id] | max" > comment_id.txt
89+
env:
90+
GITHUB_TOKEN: ${{ github.token }}
91+
92+
- uses: actions/upload-artifact@v2
93+
with:
94+
name: comment
95+
path: comment_id.txt
7896
retention-days: 1

0 commit comments

Comments
 (0)