Skip to content

Commit 6ab8321

Browse files
committed
switch to more secure split PR comment approach
1 parent c36e2a5 commit 6ab8321

7 files changed

Lines changed: 265 additions & 93 deletions

File tree

.github/workflows/branch.yml

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ name: nf-core branch protection
22
# This workflow is triggered on PRs to main branch on the repository
33
# It fails when someone tries to make a PR against the nf-core `main` branch instead of `dev`
44
on:
5-
pull_request_target:
6-
branches: [main]
5+
pull_request:
6+
branches:
7+
- main
8+
9+
permissions: {}
710

811
jobs:
912
test:
@@ -12,32 +15,47 @@ jobs:
1215
# PRs to the nf-core repo main branch are only ok if coming from the nf-core repo `dev` or any `patch` branches
1316
- name: Check PRs
1417
if: github.repository == 'nf-core/tools'
18+
env:
19+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
1520
run: |
16-
{ [[ ${{github.event.pull_request.head.repo.full_name}} == nf-core/tools ]] && [[ $GITHUB_HEAD_REF == "dev" ]]; } || [[ $GITHUB_HEAD_REF == "patch" ]]
21+
{ [[ "$HEAD_REPO" == "nf-core/tools" ]] && [[ "$GITHUB_HEAD_REF" == "dev" ]]; } || [[ "$GITHUB_HEAD_REF" == "patch" ]]
1722
18-
# If the above check failed, post a comment on the PR explaining the failure
19-
- name: Post PR comment
23+
# If the above check failed, build a comment to be posted by the shared poster workflow
24+
- name: Build PR comment
2025
if: failure()
21-
uses: mshick/add-pr-comment@8e4927817251f1ff60c001f04568532b38e0b4a0 # v3
22-
with:
23-
message: |
24-
## This PR is against the `main` branch :x:
26+
env:
27+
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
BASE_REF: ${{ github.event.pull_request.base.ref }}
29+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
30+
PR_USER: ${{ github.event.pull_request.user.login }}
31+
run: |
32+
mkdir -p pr-comment
33+
echo "$PR_NUMBER" > pr-comment/pr_number.txt
34+
echo "branch" > pr-comment/header.txt
35+
cat > pr-comment/comment.md <<EOF
36+
## This PR is against the \`${BASE_REF}\` branch :x:
2537
26-
* Do not close this PR
27-
* Click _Edit_ and change the `base` to `dev`
28-
* This CI test will remain failed until you push a new commit
38+
* Do not close this PR
39+
* Click _Edit_ and change the \`base\` to \`dev\`
40+
* This CI test will remain failed until you push a new commit
2941
30-
---
42+
---
3143
32-
Hi @${{ github.event.pull_request.user.login }},
44+
Hi @${PR_USER},
3345
34-
It looks like this pull-request is has been made against the [${{github.event.pull_request.head.repo.full_name }}](https://github.com/${{github.event.pull_request.head.repo.full_name }}) `main` branch.
35-
The `main` branch on nf-core repositories should always contain code from the latest release.
36-
Because of this, PRs to `main` are only allowed if they come from the [${{github.event.pull_request.head.repo.full_name }}](https://github.com/${{github.event.pull_request.head.repo.full_name }}) `dev` branch.
46+
It looks like this pull-request is has been made against the [${HEAD_REPO}](https://github.com/${HEAD_REPO}) ${BASE_REF} branch.
47+
The ${BASE_REF} branch on nf-core repositories should always contain code from the latest release.
48+
Because of this, PRs to ${BASE_REF} are only allowed if they come from the [${HEAD_REPO}](https://github.com/${HEAD_REPO}) \`dev\` branch.
3749
38-
You do not need to close this PR, you can change the target branch to `dev` by clicking the _"Edit"_ button at the top of this page.
39-
Note that even after this, the test will continue to show as failing until you push a new commit.
50+
You do not need to close this PR, you can change the target branch to \`dev\` by clicking the _"Edit"_ button at the top of this page.
51+
Note that even after this, the test will continue to show as failing until you push a new commit.
4052
41-
Thanks again for your contribution!
42-
repo-token: ${{ secrets.GITHUB_TOKEN }}
43-
allow-repeats: false
53+
Thanks again for your contribution!
54+
EOF
55+
56+
- name: Upload PR comment artifact
57+
if: failure()
58+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
59+
with:
60+
name: pr-comment
61+
path: pr-comment/

.github/workflows/pr-comment.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Post PR comment
2+
# Shared, privileged comment poster.
3+
#
4+
# This is the single workflow that runs with a write token. It is triggered
5+
# after any of the listed "producer" workflows complete on a pull request.
6+
# Each producer runs untrusted PR code (if any) with a read-only token and
7+
# uploads a `pr-comment` artifact describing the comment to post; this workflow
8+
# only ever reads that plain-text artifact, so no PR code is executed here.
9+
#
10+
# Artifact contract (uploaded by producers under the name `pr-comment`):
11+
# pr_number.txt - the pull request number
12+
# header.txt - sticky-comment identifier (keeps comment types separate)
13+
# comment.md - the Markdown body (omit the file to post nothing)
14+
15+
on:
16+
workflow_run:
17+
workflows:
18+
- "nf-core branch protection"
19+
20+
permissions:
21+
actions: read
22+
contents: read
23+
pull-requests: write
24+
25+
jobs:
26+
post-comment:
27+
runs-on: ubuntu-latest
28+
# Only act on runs that were triggered by a pull request.
29+
if: github.event.workflow_run.event == 'pull_request'
30+
steps:
31+
- name: Download PR comment artifact
32+
uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21
33+
with:
34+
run_id: ${{ github.event.workflow_run.id }}
35+
name: pr-comment
36+
if_no_artifact_found: ignore
37+
38+
- name: Read comment metadata
39+
id: meta
40+
run: |
41+
# No comment body means there is nothing to post.
42+
[ -f pr-comment/comment.md ] || exit 0
43+
44+
pr_number=$(cat pr-comment/pr_number.txt)
45+
header=$(cat pr-comment/header.txt)
46+
47+
# Guard against anything unexpected ending up in the PR number.
48+
case "$pr_number" in
49+
''|*[!0-9]*)
50+
echo "Invalid PR number: '$pr_number'"
51+
exit 1
52+
;;
53+
esac
54+
55+
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
56+
echo "header=$header" >> "$GITHUB_OUTPUT"
57+
echo "post=true" >> "$GITHUB_OUTPUT"
58+
59+
- name: Post PR comment
60+
if: steps.meta.outputs.post == 'true'
61+
uses: marocchino/sticky-pull-request-comment@70d2764d1a7d5d9560b100cbea0077fc8f633987 # v3
62+
with:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
number: ${{ steps.meta.outputs.pr_number }}
65+
header: ${{ steps.meta.outputs.header }}
66+
path: pr-comment/comment.md

nf_core/pipeline-template/.github/workflows/branch.yml

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,61 @@ name: nf-core branch protection
22
# This workflow is triggered on PRs to `main`/`master` branch on the repository
33
# It fails when someone tries to make a PR against the nf-core `main`/`master` branch instead of `dev`
44
on:
5-
pull_request_target:
5+
pull_request:
66
branches:
77
- main
88
- master
99

10+
permissions: {}
11+
1012
jobs:
1113
test:
1214
runs-on: ubuntu-latest
1315
steps:
1416
# PRs to the nf-core repo main/master branch are only ok if coming from the nf-core repo `dev` or any `patch` branches
1517
- name: Check PRs
1618
if: github.repository == '{{ name }}'
19+
env:
20+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
1721
run: |
18-
{ [[ {% raw %}${{github.event.pull_request.head.repo.full_name }}{% endraw %} == {{ name }} ]] && [[ $GITHUB_HEAD_REF == "dev" ]]; } || [[ $GITHUB_HEAD_REF == "patch" ]]
22+
{ [[ "$HEAD_REPO" == {{ name }} ]] && [[ $GITHUB_HEAD_REF == "dev" ]]; } || [[ $GITHUB_HEAD_REF == "patch" ]]
1923
20-
# If the above check failed, post a comment on the PR explaining the failure {%- raw %}
21-
# NOTE - this doesn't currently work if the PR is coming from a fork, due to limitations in GitHub actions secrets
22-
- name: Post PR comment
24+
# If the above check failed, build a comment to be posted by the shared poster workflow
25+
- name: Build PR comment
2326
if: failure()
24-
uses: mshick/add-pr-comment@8e4927817251f1ff60c001f04568532b38e0b4a0 # v3
25-
with:
26-
message: |
27-
## This PR is against the `${{github.event.pull_request.base.ref}}` branch :x:
27+
env:
28+
PR_NUMBER: ${{ github.event.pull_request.number }}
29+
BASE_REF: ${{ github.event.pull_request.base.ref }}
30+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
31+
PR_USER: ${{ github.event.pull_request.user.login }}
32+
run: |
33+
mkdir -p pr-comment
34+
echo "$PR_NUMBER" > pr-comment/pr_number.txt
35+
echo "branch" > pr-comment/header.txt
36+
cat > pr-comment/comment.md <<EOF
37+
## This PR is against the \`${BASE_REF}\` branch :x:
2838
29-
* Do not close this PR
30-
* Click _Edit_ and change the `base` to `dev`
31-
* This CI test will remain failed until you push a new commit
39+
* Do not close this PR
40+
* Click _Edit_ and change the \`base\` to \`dev\`
41+
* This CI test will remain failed until you push a new commit
3242
33-
---
43+
---
3444
35-
Hi @${{ github.event.pull_request.user.login }},
45+
Hi @${PR_USER},
3646
37-
It looks like this pull-request is has been made against the [${{github.event.pull_request.head.repo.full_name }}](https://github.com/${{github.event.pull_request.head.repo.full_name }}) ${{github.event.pull_request.base.ref}} branch.
38-
The ${{github.event.pull_request.base.ref}} branch on nf-core repositories should always contain code from the latest release.
39-
Because of this, PRs to ${{github.event.pull_request.base.ref}} are only allowed if they come from the [${{github.event.pull_request.head.repo.full_name }}](https://github.com/${{github.event.pull_request.head.repo.full_name }}) `dev` branch.
47+
It looks like this pull-request is has been made against the [${HEAD_REPO}](https://github.com/${HEAD_REPO}) ${BASE_REF} branch.
48+
The ${BASE_REF} branch on nf-core repositories should always contain code from the latest release.
49+
Because of this, PRs to ${BASE_REF} are only allowed if they come from the [${HEAD_REPO}](https://github.com/${HEAD_REPO}) \`dev\` branch.
4050
41-
You do not need to close this PR, you can change the target branch to `dev` by clicking the _"Edit"_ button at the top of this page.
42-
Note that even after this, the test will continue to show as failing until you push a new commit.
51+
You do not need to close this PR, you can change the target branch to \`dev\` by clicking the _"Edit"_ button at the top of this page.
52+
Note that even after this, the test will continue to show as failing until you push a new commit.
4353
44-
Thanks again for your contribution!
45-
repo-token: ${{ secrets.GITHUB_TOKEN }}
46-
allow-repeats: false {%- endraw %}
54+
Thanks again for your contribution!
55+
EOF
56+
57+
- name: Upload PR comment artifact
58+
if: failure()
59+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
60+
with:
61+
name: pr-comment
62+
path: pr-comment/

nf_core/pipeline-template/.github/workflows/linting.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,21 @@ jobs:
7474
lint_log.txt
7575
lint_results.md
7676
PR_number.txt {%- endraw %}
77+
78+
# Build a comment for the shared pr-comment.yml poster to publish on the PR
79+
- name: Prepare PR comment
80+
if: ${{ always() }}
81+
env:
82+
PR_NUMBER: ${{ github.event.pull_request.number }}
83+
run: |
84+
mkdir -p pr-comment
85+
echo "$PR_NUMBER" > pr-comment/pr_number.txt
86+
echo "lint" > pr-comment/header.txt
87+
[ -f lint_results.md ] && cp lint_results.md pr-comment/comment.md || true
88+
89+
- name: Upload PR comment artifact
90+
if: ${{ always() }}
91+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
92+
with:
93+
name: pr-comment
94+
path: pr-comment/

nf_core/pipeline-template/.github/workflows/linting_comment.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Post PR comment
2+
# Shared, privileged comment poster.
3+
#
4+
# This is the single workflow that runs with a write token. It is triggered
5+
# after any of the listed "producer" workflows complete on a pull request.
6+
# Each producer runs untrusted PR code (if any) with a read-only token and
7+
# uploads a `pr-comment` artifact describing the comment to post; this workflow
8+
# only ever reads that plain-text artifact, so no PR code is executed here.
9+
#
10+
# Artifact contract (uploaded by producers under the name `pr-comment`):
11+
# pr_number.txt - the pull request number
12+
# header.txt - sticky-comment identifier (keeps comment types separate)
13+
# comment.md - the Markdown body (omit the file to post nothing)
14+
15+
on:
16+
workflow_run:
17+
workflows:
18+
- "nf-core linting"
19+
- "nf-core template version comment"
20+
- "nf-core branch protection"
21+
22+
permissions:
23+
actions: read
24+
contents: read
25+
pull-requests: write
26+
27+
jobs:
28+
post-comment:
29+
runs-on: ubuntu-latest
30+
# Only act on runs that were triggered by a pull request.
31+
if: github.event.workflow_run.event == 'pull_request'
32+
steps:
33+
- name: Download PR comment artifact
34+
uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21
35+
with:
36+
run_id: ${{ github.event.workflow_run.id }}
37+
name: pr-comment
38+
if_no_artifact_found: ignore
39+
40+
- name: Read comment metadata
41+
id: meta
42+
run: |
43+
# No comment body means there is nothing to post.
44+
[ -f pr-comment/comment.md ] || exit 0
45+
46+
pr_number=$(cat pr-comment/pr_number.txt)
47+
header=$(cat pr-comment/header.txt)
48+
49+
# Guard against anything unexpected ending up in the PR number.
50+
case "$pr_number" in
51+
''|*[!0-9]*)
52+
echo "Invalid PR number: '$pr_number'"
53+
exit 1
54+
;;
55+
esac
56+
57+
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
58+
echo "header=$header" >> "$GITHUB_OUTPUT"
59+
echo "post=true" >> "$GITHUB_OUTPUT"
60+
61+
- name: Post PR comment
62+
if: steps.meta.outputs.post == 'true'
63+
uses: marocchino/sticky-pull-request-comment@70d2764d1a7d5d9560b100cbea0077fc8f633987 # v3
64+
with:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66+
number: ${{ steps.meta.outputs.pr_number }}
67+
header: ${{ steps.meta.outputs.header }}
68+
path: pr-comment/comment.md

0 commit comments

Comments
 (0)