Skip to content

Commit 31c00dc

Browse files
authored
Refactor amalgamation workflow to avoid dangerous use of pull_request_target (#3969)
1 parent 6cec5ae commit 31c00dc

File tree

2 files changed

+91
-38
lines changed

2 files changed

+91
-38
lines changed

.github/workflows/check_amalgamation.yml

+16-38
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
name: "Check amalgamation"
22

33
on:
4-
pull_request_target:
4+
pull_request:
5+
6+
permissions: read-all
57

68
jobs:
9+
save:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Save PR number
13+
run: |
14+
mkdir -p ./pr
15+
echo ${{ github.event.number }} > ./pr/number
16+
echo ${{ github.event.pull_request.user.login }} > ./pr/author
17+
- uses: actions/upload-artifact@v2
18+
with:
19+
name: pr
20+
path: pr/
21+
722
check:
823
runs-on: ubuntu-latest
924
env:
@@ -53,40 +68,3 @@ jobs:
5368
astyle $ASTYLE_FLAGS $(find docs/examples include tests -type f \( -name '*.hpp' -o -name '*.cpp' -o -name '*.cu' \) -not -path 'tests/thirdparty/*' -not -path 'tests/abi/include/nlohmann/*' | sort)
5469
echo Check
5570
find $MAIN_DIR -name '*.orig' -exec false {} \+
56-
57-
- name: Comment on pull request
58-
if: failure()
59-
uses: actions/github-script@v6
60-
with:
61-
github-token: ${{secrets.GITHUB_TOKEN}}
62-
script: |
63-
const author = context.payload.pull_request.user.login
64-
const opts = github.rest.issues.listForRepo.endpoint.merge({
65-
owner: context.repo.owner,
66-
repo: context.repo.repo,
67-
creator: author,
68-
state: 'all'
69-
})
70-
71-
let first = true
72-
const issues = await github.paginate(opts)
73-
for (const issue of issues) {
74-
if (issue.number === context.issue.number) {
75-
continue
76-
}
77-
78-
if (issue.pull_request) {
79-
first = false
80-
break
81-
}
82-
}
83-
84-
await github.rest.issues.createComment({
85-
issue_number: context.issue.number,
86-
owner: context.repo.owner,
87-
repo: context.repo.repo,
88-
body: '## 🔴 Amalgamation check failed! 🔴\nThe source code has not been amalgamated.'
89-
+ (first ? ' @' + author + ' Please read and follow the [Contribution Guidelines]'
90-
+ '(https://github.com/nlohmann/json/blob/develop/.github/CONTRIBUTING.md#files-to-change).'
91-
: '')
92-
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Comment Check Amalgamation
2+
on:
3+
workflow_run:
4+
workflows: ["Check amalgamation"]
5+
types:
6+
- completed
7+
8+
permissions: {}
9+
10+
jobs:
11+
comment:
12+
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
actions: read
17+
issues: read
18+
pull-requests: write
19+
steps:
20+
- name: 'Download artifact'
21+
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975 # v6.4.0
22+
with:
23+
script: |
24+
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo,
27+
run_id: ${{github.event.workflow_run.id }},
28+
});
29+
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
30+
return artifact.name == "pr"
31+
})[0];
32+
var download = await github.rest.actions.downloadArtifact({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
artifact_id: matchArtifact.id,
36+
archive_format: 'zip',
37+
});
38+
var fs = require('fs');
39+
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
40+
- run: unzip pr.zip
41+
42+
- name: 'Comment on PR'
43+
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975 # v6.4.0
44+
with:
45+
github-token: ${{ secrets.GITHUB_TOKEN }}
46+
script: |
47+
var fs = require('fs');
48+
const author = fs.readFileSync('./author')
49+
const issue_number = Number(fs.readFileSync('./number'));
50+
const opts = github.rest.issues.listForRepo.endpoint.merge({
51+
owner: context.repo.owner,
52+
repo: context.repo.repo,
53+
creator: author,
54+
state: 'all'
55+
})
56+
let first = true
57+
const issues = await github.paginate(opts)
58+
for (const issue of issues) {
59+
if (issue.number === issue_number) {
60+
continue
61+
}
62+
if (issue.pull_request) {
63+
first = false
64+
break
65+
}
66+
}
67+
await github.rest.issues.createComment({
68+
issue_number: issue_number,
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
body: '## 🔴 Amalgamation check failed! 🔴\nThe source code has not been amalgamated.'
72+
+ (first ? ' @' + author + ' Please read and follow the [Contribution Guidelines]'
73+
+ '(https://github.com/nlohmann/json/blob/develop/.github/CONTRIBUTING.md#files-to-change).'
74+
: '')
75+
})

0 commit comments

Comments
 (0)