forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (136 loc) · 5.78 KB
/
comment-build-results.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
name: Comment build results
# We are not using using a single workflow on `pull_request_target`, and have two workflows on `pull_request` -> `workflow_run`:
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
on:
workflow_run:
workflows:
- Build types
types:
- completed
jobs:
check-current-build:
runs-on: ubuntu-latest
permissions:
actions: read # to list the workflow runs
if: >-
${{
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
}}
outputs:
run-id: ${{ fromJSON(steps.current-workflow-run.outputs.result).id }}
steps:
- name: Check current types build workflow run
id: current-workflow-run
uses: actions/github-script@v6
with:
script: |
const { data: { workflow_runs: [workflowRun] } } = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
branch: 'dev', // TODO: Get the PR target branch to support branches other than dev
workflow_id: 'build-types.yml',
event: 'push',
status: 'success',
});
return {
id: workflowRun?.id || -1,
}
comment-build-results:
runs-on: ubuntu-latest
permissions:
actions: read # to download artifacts
pull-requests: write # to comment to the PR
needs: check-current-build
if: ${{ needs.check-current-build.outputs.run-id != -1 }}
steps:
- name: Download the current types build
uses: actions/github-script@v6
env:
CURRENT_RUN_ID: ${{ needs.check-current-build.outputs.run-id }}
with:
script: |
const { writeFile, unlink } = require('node:fs/promises');
const { resolve } = require('node:path');
const __dirname = process.env.GITHUB_WORKSPACE;
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: process.env.CURRENT_RUN_ID,
});
const artifact = artifacts.find(({ name }) => name === 'types-build');
const { data: artifactData } = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const zipPath = resolve(__dirname, './current-build.zip');
await writeFile(zipPath, Buffer.from(artifactData));
await exec.exec('unzip', [zipPath, '-d', './current-build']);
await unlink(zipPath);
- name: Download the PR information artifact
id: pr-info
uses: actions/github-script@v6
with:
script: |
const { readFile, writeFile, unlink } = require('node:fs/promises');
const { resolve } = require('node:path');
const __dirname = process.env.GITHUB_WORKSPACE;
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const artifact = artifacts.find(({ name }) => name === 'types-build');
const { data: artifactData } = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const zipPath = resolve(__dirname, './pr-info.zip');
await writeFile(zipPath, Buffer.from(artifactData));
await exec.exec('unzip', [zipPath, '-d', './pr-build']);
await unlink(zipPath);
const prNumberPath = resolve(__dirname, './pr-build/.pr-number');
const prNumberString = await readFile(prNumberPath, { encoding: 'utf-8' })
await unlink(prNumberPath);
return {
number: Number(prNumberString)
}
- name: Prepare build results
run: |
echo "# Build Results" >> build-results.md
echo "" >> build-results.md
echo "## JSON API" >> build-results.md
echo "" >> build-results.md
# git diff exits with 1 if there is no diff, which evaluates to true
if $(git diff --no-index ./current-build/api ./pr-build/api > ./api.diff); then
echo "📜 No changes detected." >> build-results.md
else
echo "📜 Changes detected:" >> build-results.md
echo '```diff' >> build-results.md
cat ./api.diff >> build-results.md
echo '```' >> build-results.md
fi
echo "" >> build-results.md
echo "## Types" >> build-results.md
echo "" >> build-results.md
# git diff exits with 1 if there is no diff, which evaluates to true
if $(git diff --no-index ./current-build/types ./pr-build/types > ./types.diff); then
echo "📜 No changes detected." >> build-results.md
else
echo "📜 Changes detected:" >> build-results.md
echo '```diff' >> build-results.md
cat ./types.diff >> build-results.md
echo '```' >> build-results.md
fi
echo "" >> build-results.md
- uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ fromJSON(steps.pr-info.outputs.result).number }}
hide_and_recreate: true
hide_classify: 'OUTDATED'
path: build-results.md
- run: cat build-results.md >> $GITHUB_STEP_SUMMARY