Skip to content

Commit fd371e0

Browse files
committed
GH Actions: fix PR comment conundrum
As described in issue 42, the PR comment to review the pre-processed wiki files was not being posted on PRs coming from forks, while it is all the more relevant for those, as outside contributors may not be aware of the artifact with the wiki files being available. I've now done some more research into this and believe the changes in this PR will fix this. The most important take-away from my research is as follows: * Workflows triggered by a PR from a fork only have "read" access via the `secrets.GITHUB_TOKEN` and no access to Personal Access Tokens. * However, workflows can trigger other workflows and those "follow-on" workflows **_can_** have "write" access via the `secrets.GITHUB_TOKEN` as they are run in the context of the `main` branch, not in the context of the PR branch (which is also why I had to briefly use `main` to test this PR). So, with that in mind, I've moved the "comment on PR" step to a separate workflow which is triggered once the "Publish wiki" workflow has completed and will only run for pull requests. This new workflow had access to the artifacts created by the triggering workflow, but doesn't have access to much else, while it needs access to the PR number of the PR which triggered the workflow cascade + access to the download URL for the wiki files artifact. Those pieces of information are both available in the "triggering workflow", so we need to store them in - you guessed it - an artifact, so that info can then be retrieved by the second (PR comment) workflow. Other notes: * The "Deploy to wiki" dry-run was failing for PRs from forks (though this was hidden by the earlier step for the comment failing) with the following error: "fatal: could not read Username for 'https://github.com': No such device or address". As the "Deploy to wiki" step needs a Personal Access Token for the push to the wiki, this is no great surprise, what with PRs from forks not having access to the PAT. Switching to the `secrets.GITHUB_TOKEN` just and only for the dry-run, unfortunately would still fail for PRs from works, so I've now made the "Deploy to wiki" step conditional on the workflow either not being triggered by a PR _or_ the PR not coming from a fork. Fixes 42 Refs: * https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#using-data-from-the-triggering-workflow
1 parent c923cac commit fd371e0

File tree

2 files changed

+122
-18
lines changed

2 files changed

+122
-18
lines changed

.github/workflows/pr-comment.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Pull Request Comment
2+
3+
on:
4+
workflow_run:
5+
workflows: ['Publish wiki']
6+
types:
7+
- completed
8+
9+
# Cancels all previous workflow runs for pull requests that have not completed.
10+
concurrency:
11+
# The concurrency group contains the workflow name and the branch name for pull requests
12+
# or the commit hash for any other events.
13+
group: ${{ github.workflow }}-${{ github.sha }}
14+
15+
jobs:
16+
artifact-review-comment:
17+
if: >
18+
github.repository == 'PHPCSStandards/PHP_CodeSniffer-documentation' &&
19+
github.event.workflow_run.event == 'pull_request' &&
20+
github.event.workflow_run.conclusion == 'success'
21+
22+
runs-on: ubuntu-latest
23+
24+
permissions:
25+
# Needed for the PR comment.
26+
pull-requests: write
27+
28+
name: Comment on a pull request
29+
30+
steps:
31+
- name: Download PR info artifact
32+
uses: actions/github-script@v7
33+
with:
34+
script: |
35+
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
36+
owner: context.repo.owner,
37+
repo: context.repo.repo,
38+
run_id: context.payload.workflow_run.id,
39+
});
40+
41+
const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
42+
return artifact.name == "pr_info"
43+
})[0];
44+
45+
if ( ! matchArtifact ) {
46+
core.setFailed( 'No artifact found!' );
47+
return;
48+
}
49+
50+
const download = await github.rest.actions.downloadArtifact({
51+
owner: context.repo.owner,
52+
repo: context.repo.repo,
53+
artifact_id: matchArtifact.id,
54+
archive_format: 'zip',
55+
});
56+
57+
const fs = require( 'fs' );
58+
fs.writeFileSync( '${{ github.workspace }}/pr_info.zip', Buffer.from( download.data ) )
59+
60+
- name: Unzip PR info artifact
61+
run: unzip pr_info.zip
62+
63+
- name: Read PR number
64+
id: pr_number
65+
shell: bash
66+
run: |
67+
value=$(<"pr_number")
68+
echo "PR_NR=$value" >> "$GITHUB_OUTPUT"
69+
70+
- name: Read Wiki artifact URL
71+
id: artifact_url
72+
shell: bash
73+
run: |
74+
value=$(<"artifact_url")
75+
echo "URL=$value" >> "$GITHUB_OUTPUT"
76+
77+
- name: "Post comment to review artifact"
78+
uses: mshick/add-pr-comment@v2
79+
with:
80+
issue: ${{ steps.pr_number.outputs.PR_NR }}
81+
repo-token: ${{ secrets.GITHUB_TOKEN }}
82+
message: |
83+
**_=== This is an auto-generated comment ===_**
84+
85+
Thank you for your PR.
86+
A dry-run has been executed on your PR, executing all markdown pre-processing for the wiki files.
87+
88+
Please review the resulting final markdown files via the [created artifact](${{ steps.artifact_url.outputs.URL }}).
89+
This is especially important when adding new pages or updating auto-generated output blocks.
90+
91+
_N.B.: the above link will automatically be updated when this PR is updated._

.github/workflows/publish-wiki.yml

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ on:
55
- 'main'
66
paths:
77
- .github/workflows/publish-wiki.yml
8+
- .github/workflows/pr-comment.yml
89
- build/wiki-code-samples/**
910
- build/wiki-command-replacer.sh
1011
- wiki/**
1112
# Do a dry-run (check, no deploy) for PRs.
1213
pull_request:
1314
paths:
1415
- .github/workflows/publish-wiki.yml
16+
- .github/workflows/pr-comment.yml
1517
- build/wiki-code-samples/**
1618
- build/wiki-command-replacer.sh
1719
- wiki/**
@@ -38,8 +40,6 @@ jobs:
3840
permissions:
3941
# Needed for the commit to the wiki.
4042
contents: write
41-
# Needed for the PR comment.
42-
pull-requests: write
4343

4444
steps:
4545
- name: Checkout code
@@ -126,22 +126,6 @@ jobs:
126126
if-no-files-found: error
127127
retention-days: 10
128128

129-
- name: "[PR only] Post comment to review artifact"
130-
if: ${{ github.event_name == 'pull_request' }}
131-
uses: mshick/add-pr-comment@v2
132-
with:
133-
repo-token: ${{ secrets.COMMENT_ON_PRS_TOKEN }}
134-
message: |
135-
**_=== This is an auto-generated comment ===_**
136-
137-
Thank you for your PR.
138-
A dry-run has been executed on your PR, executing all markdown pre-processing for the wiki files.
139-
140-
Please review the resulting final markdown files via the [created artifact](${{ steps.artifact.outputs.artifact-url }}).
141-
This is especially important when adding new pages or updating auto-generated output blocks.
142-
143-
_N.B.: the above link will automatically be updated when this PR is updated._
144-
145129

146130
# ################################################################################
147131
# Deploy to the wiki in the PHPCS repo.
@@ -153,6 +137,7 @@ jobs:
153137
git_threshold: partial_outage
154138

155139
- name: Deploy to wiki
140+
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
156141
uses: Andrew-Chen-Wang/[email protected]
157142
env:
158143
COMMIT_MSG: ${{ github.event.head_commit.message }}
@@ -166,3 +151,31 @@ jobs:
166151
dry-run: ${{ github.event_name == 'pull_request' }}
167152
disable-empty-commits: true
168153
preprocess: false
154+
155+
156+
# ################################################################################
157+
# Dry-run/PRs: save PR info for use in Pull Request Comment workflow.
158+
# ################################################################################
159+
160+
- name: Create temporary directory
161+
if: ${{ github.event_name == 'pull_request' }}
162+
run: mkdir -p ./pr
163+
164+
- name: Save PR number
165+
if: ${{ github.event_name == 'pull_request' }}
166+
env:
167+
PR_NUMBER: ${{ github.event.number }}
168+
run: echo "${PR_NUMBER}" > ./pr/pr_number
169+
170+
- name: Save artifact URL
171+
if: ${{ github.event_name == 'pull_request' }}
172+
env:
173+
ARTIFACT_URL: ${{ steps.artifact.outputs.artifact-url }}
174+
run: echo "${ARTIFACT_URL}" > ./pr/artifact_url
175+
176+
- name: Upload PR info as artifact
177+
if: ${{ github.event_name == 'pull_request' }}
178+
uses: actions/upload-artifact@v4
179+
with:
180+
name: pr_info
181+
path: pr/

0 commit comments

Comments
 (0)