Skip to content

Commit a0903c3

Browse files
committed
Use pull_request + workflow_run instead of pull_request_target
1 parent 3fb0139 commit a0903c3

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Post pull-request comment
2+
on:
3+
workflow_run:
4+
workflows: ["Query help preview"]
5+
types:
6+
- completed
7+
8+
permissions:
9+
pull-requests: write
10+
11+
jobs:
12+
post_comment:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Download artifact
16+
uses: dawidd6/action-download-artifact@af92a8455a59214b7b932932f2662fdefbd78126
17+
with:
18+
workflow: ${{ github.event.workflow_run.workflow_id }}
19+
workflow_conclusion: success
20+
name: comment
21+
- run: |
22+
PR=$(grep -o '^[0-9]\+$' pr.txt)
23+
PR_HEAD_SHA=$(gh api "/repos/${{ github.repository }}/pulls/${PR}" --jq .head.sha)
24+
# Check that the pull-request head SHA matches the head SHA of the workflow run
25+
if [ "${WORKFLOW_RUN_HEAD_SHA}" != "${PR_HEAD_SHA}" ]; then
26+
exit 1
27+
fi
28+
cat comment.txt | gh pr comment "${PR}" --repo "${{ github.repository }}" -F -
29+
env:
30+
GITHUB_TOKEN: ${{ github.token }}
31+
WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_commit.id }}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ permissions:
44
contents: read
55

66
on:
7-
pull_request_target:
7+
pull_request:
88
branches:
99
- main
1010
- "rc/*"
@@ -17,10 +17,16 @@ jobs:
1717
contents: read
1818
runs-on: ubuntu-latest
1919
steps:
20+
- run: echo "${{ github.event.number }}" > pr.txt
21+
- uses: actions/upload-artifact@v2
22+
with:
23+
name: comment
24+
path: pr.txt
25+
retention-days: 1
26+
2027
- uses: github/codeql/.github/actions/fetch-codeql@main
2128
- uses: actions/checkout@v2
2229
with:
23-
ref: refs/pull/${{ github.event.number }}/merge
2430
fetch-depth: 2
2531
persist-credentials: false
2632

@@ -33,6 +39,7 @@ jobs:
3339
3440
- name: QHelp preview
3541
run: |
42+
echo "QHelp previews:" > comment.txt
3643
cat "${{ runner.temp }}/paths.txt" | while read path; do
3744
if [ ! -f "${path}" ]; then
3845
exit 1
@@ -41,23 +48,10 @@ jobs:
4148
echo
4249
codeql generate query-help --format=markdown -- "./${path}"
4350
echo "</details>"
44-
done > comment.txt
51+
done >> comment.txt
4552
4653
- uses: actions/upload-artifact@v2
4754
with:
48-
name: comment.txt
55+
name: comment
4956
path: comment.txt
50-
51-
post_comment:
52-
permissions:
53-
pull-requests: write
54-
runs-on: ubuntu-latest
55-
needs: qhelp
56-
steps:
57-
- uses: actions/download-artifact@v2
58-
with:
59-
name: comment.txt
60-
- run: |
61-
(echo "QHelp previews:"; cat comment.txt) | gh pr comment "${{ github.event.pull_request.number }}" --repo "${{ github.repository }}" -F -
62-
env:
63-
GITHUB_TOKEN: ${{ github.token }}
57+
retention-days: 1

0 commit comments

Comments
 (0)