-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
205 lines (184 loc) · 9.42 KB
/
Copy pathoutput-diffs.yaml
File metadata and controls
205 lines (184 loc) · 9.42 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
name: Generated output diff
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
concurrency:
group: output-diff-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true
env:
OUTPUT_SNAPSHOT_CACHE_PREFIX: quicktype-output-snapshot-v1
jobs:
cache-master-snapshot:
name: Cache master output snapshot
if: github.event_name == 'push'
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Restore exact snapshot
id: snapshot-cache
uses: actions/cache/restore@v4
with:
path: .output-diffs/base
key: ${{ env.OUTPUT_SNAPSHOT_CACHE_PREFIX }}-${{ runner.os }}-${{ github.sha }}
- uses: ./.github/workflows/setup
if: steps.snapshot-cache.outputs.cache-hit != 'true'
- name: Generate snapshot
if: steps.snapshot-cache.outputs.cache-hit != 'true'
env:
ALL_FIXTURES: "1"
CPUs: "4"
ONLY_OUTPUT: "1"
OUTPUT_SNAPSHOT_DIR: ${{ github.workspace }}/.output-diffs/base
run: npm run test:output-snapshot
- name: Save exact snapshot
if: steps.snapshot-cache.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: .output-diffs/base
key: ${{ env.OUTPUT_SNAPSHOT_CACHE_PREFIX }}-${{ runner.os }}-${{ github.sha }}
compare-pull-request:
name: Compare generated output
if: github.event_name == 'pull_request'
runs-on: ubuntu-22.04
steps:
- name: Check out tested PR merge
uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
path: head-source
fetch-depth: 2
- name: Resolve tested base
id: tested-base
working-directory: head-source
shell: bash
run: |
read -ra commit <<< "$(git rev-list --parents -n 1 HEAD)"
if [[ "${#commit[@]}" -ne 3 ]]; then
echo "Expected the tested PR commit to have two parents" >&2
exit 1
fi
echo "sha=${commit[1]}" >> "$GITHUB_OUTPUT"
- name: Restore exact base snapshot
id: base-cache
uses: actions/cache/restore@v4
with:
path: .output-diffs/base
key: ${{ env.OUTPUT_SNAPSHOT_CACHE_PREFIX }}-${{ runner.os }}-${{ steps.tested-base.outputs.sha }}
- name: Check out base after a cache miss
if: steps.base-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
ref: ${{ steps.tested-base.outputs.sha }}
path: base-source
- name: Check snapshot compatibility
id: compatibility
shell: bash
run: |
if [[ "${{ steps.base-cache.outputs.cache-hit }}" == "true" ]]; then
echo "supported=true" >> "$GITHUB_OUTPUT"
elif node -e 'const p=require("./base-source/package.json"); process.exit(p.scripts?.["test:output-snapshot"] ? 0 : 1)'; then
echo "supported=true" >> "$GITHUB_OUTPUT"
else
echo "supported=false" >> "$GITHUB_OUTPUT"
echo "The base commit predates generated-output snapshot support; this bootstrap comparison is skipped." >> "$GITHUB_STEP_SUMMARY"
fi
- name: Set up Node
if: steps.compatibility.outputs.supported == 'true'
uses: actions/setup-node@v4
with:
node-version-file: head-source/.nvmrc
cache: npm
cache-dependency-path: head-source/package-lock.json
- name: Build base after a cache miss
id: base-build
if: steps.compatibility.outputs.supported == 'true' && steps.base-cache.outputs.cache-hit != 'true'
continue-on-error: true
working-directory: base-source
run: npm ci && npm run build
- name: Generate base snapshot after a cache miss
id: base-snapshot
if: steps.compatibility.outputs.supported == 'true' && steps.base-cache.outputs.cache-hit != 'true' && steps.base-build.outcome == 'success'
continue-on-error: true
working-directory: base-source
env:
ALL_FIXTURES: "1"
CPUs: "4"
ONLY_OUTPUT: "1"
OUTPUT_SNAPSHOT_DIR: ${{ github.workspace }}/.output-diffs/base
run: npm run test:output-snapshot
- name: Save PR-scoped base snapshot after a cache miss
if: steps.compatibility.outputs.supported == 'true' && steps.base-cache.outputs.cache-hit != 'true' && steps.base-snapshot.outcome == 'success'
uses: actions/cache/save@v4
with:
path: .output-diffs/base
key: ${{ env.OUTPUT_SNAPSHOT_CACHE_PREFIX }}-${{ runner.os }}-${{ steps.tested-base.outputs.sha }}
- name: Build tested PR merge
if: steps.compatibility.outputs.supported == 'true' && (steps.base-cache.outputs.cache-hit == 'true' || steps.base-snapshot.outcome == 'success')
working-directory: head-source
run: npm ci && npm run build
- name: Generate tested PR merge snapshot
if: steps.compatibility.outputs.supported == 'true' && (steps.base-cache.outputs.cache-hit == 'true' || steps.base-snapshot.outcome == 'success')
working-directory: head-source
env:
ALL_FIXTURES: "1"
CPUs: "4"
ONLY_OUTPUT: "1"
OUTPUT_SNAPSHOT_DIR: ${{ github.workspace }}/.output-diffs/head
run: npm run test:output-snapshot
- name: Compare snapshots
if: steps.compatibility.outputs.supported == 'true' && (steps.base-cache.outputs.cache-hit == 'true' || steps.base-snapshot.outcome == 'success')
run: |
mkdir -p .output-diffs/result
head-source/node_modules/.bin/tsx head-source/script/output-diff.ts compare \
.output-diffs/base .output-diffs/head \
.output-diffs/result/result.json .output-diffs/result/output.diff
- name: Write unavailable comparison result
if: steps.compatibility.outputs.supported != 'true' || (steps.base-cache.outputs.cache-hit != 'true' && steps.base-snapshot.outcome != 'success')
run: |
mkdir -p .output-diffs/result
printf '%s\n' '{"version":1,"hasDifferences":false,"files":[],"summary":{"added":0,"changedLines":0,"deleted":0,"deletions":0,"files":0,"insertions":0,"modified":0}}' > .output-diffs/result/result.json
: > .output-diffs/result/output.diff
echo "The base commit could not generate an output snapshot, so this comparison is unavailable." >> "$GITHUB_STEP_SUMMARY"
- name: Write comparison metadata
env:
BASE_SHA: ${{ steps.tested-base.outputs.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_SHA: ${{ github.sha }}
PR_URL: ${{ github.event.pull_request.html_url }}
SUPPORTED: ${{ steps.compatibility.outputs.supported == 'true' && (steps.base-cache.outputs.cache-hit == 'true' || steps.base-snapshot.outcome == 'success') }}
run: |
node - <<'NODE'
const fs = require("node:fs");
const metadata = {
baseSha: process.env.BASE_SHA,
headSha: process.env.HEAD_SHA,
prNumber: Number(process.env.PR_NUMBER),
prSha: process.env.PR_SHA,
prUrl: process.env.PR_URL,
supported: process.env.SUPPORTED === "true",
};
fs.writeFileSync(".output-diffs/result/metadata.json", `${JSON.stringify(metadata, null, 2)}\n`);
NODE
- name: Summarize comparison
run: |
node - <<'NODE'
const fs = require("node:fs");
const result = JSON.parse(fs.readFileSync(".output-diffs/result/result.json", "utf8"));
const s = result.summary;
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY,
result.hasDifferences
? `Generated output differs in **${s.files} files** (${s.modified} modified, ${s.added} new, ${s.deleted} deleted), with **${s.changedLines} changed lines** (+${s.insertions}/-${s.deletions}).\n`
: "Generated output is unchanged.\n");
NODE
- name: Upload comparison data
uses: actions/upload-artifact@v4
with:
name: generated-output-diff
path: .output-diffs/result
if-no-files-found: error
retention-days: 14