-
Notifications
You must be signed in to change notification settings - Fork 301
337 lines (296 loc) · 11.9 KB
/
audit-api-spec.yaml
File metadata and controls
337 lines (296 loc) · 11.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
name: Audit API Spec
on:
pull_request:
branches:
- master
- rel/**
env:
STATIC_ANALYSIS_BOT_APP_ID: 1819979
jobs:
generate-head-api-spec:
name: Generate head API spec
runs-on: ubuntu-latest
steps:
- name: Checkout PR
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 22
- name: Cache npm dependencies
id: node-modules-cache
uses: actions/cache@v5
with:
path: |
node_modules
modules/*/node_modules
key: ${{ runner.os }}-node18-${{ hashFiles('yarn.lock') }}-${{ hashFiles('tsconfig.packages.json') }}-${{ hashFiles('**/package.json') }}
- name: Install Dependencies
if: steps.node-modules-cache.outputs.cache-hit != 'true'
run: yarn install --with-frozen-lockfile --ignore-scripts
- name: Build packages
env:
DISABLE_V8_COMPILE_CACHE: '1'
run: yarn run postinstall
- name: Generate API spec
run: |
./node_modules/.bin/openapi-generator \
--codec-file modules/express/openapi-generator.rc.js \
modules/express/src/typedRoutes/api/index.ts \
> generated.json
- name: Remove unknown tags from generated spec
run: |
jq '(.paths[] | .[]? | select(. != null)) |= del(."x-unknown-tags")' generated.json > openapi.json
- name: Convert merged spec to YAML
run: yq -P < openapi.json > openapi-head.yaml
- name: Upload API spec to artifact
uses: actions/upload-artifact@v6
with:
name: openapi-head.yaml
path: openapi-head.yaml
generate-merge-base-api-spec:
name: Generate merge base API spec
runs-on: ubuntu-latest
steps:
- name: Checkout PR
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Find and checkout merge base
run: |
git fetch origin ${{ github.event.pull_request.base.ref }}
MERGE_BASE=$(git merge-base HEAD origin/${{ github.event.pull_request.base.ref }})
echo "Merge base commit: $MERGE_BASE"
git checkout $MERGE_BASE
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 22
- name: Cache npm dependencies
id: node-modules-cache
uses: actions/cache@v5
with:
path: |
node_modules
modules/*/node_modules
key: ${{ runner.os }}-node18-${{ hashFiles('yarn.lock') }}-${{ hashFiles('tsconfig.packages.json') }}-${{ hashFiles('**/package.json') }}
- name: Install Dependencies
if: steps.node-modules-cache.outputs.cache-hit != 'true'
run: yarn install --with-frozen-lockfile --ignore-scripts
- name: Build packages
env:
DISABLE_V8_COMPILE_CACHE: '1'
run: yarn run postinstall
- name: Generate API spec
run: |
./node_modules/.bin/openapi-generator \
--codec-file modules/express/openapi-generator.rc.js \
modules/express/src/typedRoutes/api/index.ts \
> generated.json
- name: Remove unknown tags from generated spec
run: |
jq '(.paths[] | .[]? | select(. != null)) |= del(."x-unknown-tags")' generated.json > openapi.json
- name: Convert merged spec to YAML
run: yq -P < openapi.json > openapi-merge-base.yaml
- name: Upload API spec to artifact
uses: actions/upload-artifact@v6
with:
name: openapi-merge-base.yaml
path: openapi-merge-base.yaml
check-specs-identical:
name: Check specs identical
runs-on: ubuntu-latest
needs: [generate-head-api-spec, generate-merge-base-api-spec]
outputs:
specs-identical: ${{ steps.check-specs-identical.outputs.identical }}
steps:
- name: Download head API spec artifact
uses: actions/download-artifact@v7
with:
name: openapi-head.yaml
- name: Download merge base API spec artifact
uses: actions/download-artifact@v7
with:
name: openapi-merge-base.yaml
- name: Check specs identical
id: check-specs-identical
run: |
if diff -q openapi-head.yaml openapi-merge-base.yaml > /dev/null 2>&1; then
echo "identical=true" >> $GITHUB_OUTPUT
echo "✅ Specs are identical - no changes detected, skipping subsequent checks"
else
echo "identical=false" >> $GITHUB_OUTPUT
echo "📝 Specs differ - proceeding with audit checks"
fi
generate-vacuum-reports:
name: Generate vacuum reports for API spec
runs-on: ubuntu-latest
needs: [check-specs-identical]
if: needs.check-specs-identical.outputs.specs-identical != 'true'
steps:
- name: Checkout PR head for ruleset
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Download head API spec artifact
uses: actions/download-artifact@v7
with:
name: openapi-head.yaml
- name: Download and install vacuum v0.18.1
run: |
curl -L \
--output vacuum.tar.gz \
--silent \
--show-error \
--fail \
https://github.com/daveshanley/vacuum/releases/download/v0.18.1/vacuum_0.18.1_linux_x86_64.tar.gz
tar -xzf vacuum.tar.gz
chmod u+x vacuum
sudo mv vacuum /usr/local/bin/
vacuum version
- name: Audit head API spec with Vacuum
run: |
vacuum report \
--no-style \
--stdout \
--ruleset modules/express/ruleset.yaml \
openapi-head.yaml > vacuum-report.json
jq '.resultSet.results // []' vacuum-report.json > vacuum-results.json
ERROR_COUNT=$(jq '[.[] | select(.ruleSeverity == "error")] | length' vacuum-results.json)
WARNING_COUNT=$(jq '[.[] | select(.ruleSeverity == "warn")] | length' vacuum-results.json)
echo "Found $ERROR_COUNT error(s) and $WARNING_COUNT warning(s)"
if [ "$ERROR_COUNT" -gt 0 ]; then
echo "API specification audit failed with $ERROR_COUNT error(s)"
echo ""
echo "Errors:"
jq -r '.[] | select(.ruleSeverity == "error") | " - [\(.ruleId)] \(.message) at \(.path)"' vacuum-results.json
exit 1
else
echo "API specification audit passed!"
fi
check-breaking-changes:
name: Check breaking changes
needs: [check-specs-identical]
if: needs.check-specs-identical.outputs.specs-identical != 'true'
runs-on: ubuntu-latest
steps:
- name: Download head API spec artifact
uses: actions/download-artifact@v7
with:
name: openapi-head.yaml
- name: Download merge base API spec artifact
uses: actions/download-artifact@v7
with:
name: openapi-merge-base.yaml
- name: Create static-analysis config file
run: |
cat <<EOF > .static-analysis.yaml
---
api_version: static-analysis.bitgo/v1alpha1
rules:
- path: rules/openapi/breaking-changes
severity: error
options:
before_spec_path: openapi-merge-base.yaml
after_spec_path: openapi-head.yaml
EOF
- name: Generate GitHub App Token
id: generate-github-app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ env.STATIC_ANALYSIS_BOT_APP_ID }}
private-key: ${{ secrets.STATIC_ANALYSIS_BOT_PRIVATE_KEY }}
owner: bitgo
repositories: |
static-analysis
- name: Install BitGo/static-analysis/static-analysis@v1
uses: BitGo/install-github-release-binary@v2
with:
targets: BitGo/static-analysis/static-analysis@v1
token: ${{ steps.generate-github-app-token.outputs.token }}
- name: Check breaking changes
run: |
if ! static-analysis; then
echo "
## ⚠️ Breaking Changes Detected
The OpenAPI spec changes in this PR contain breaking changes that could affect API consumers.
What to do next:
1. Review the breaking changes in the workflow run.
2. If these breaking changes are intentional and necessary, contact the DevEx team in #notify-dev-ex on Slack for a manual override.
3. If not intentional, please revise your changes to maintain backward compatibility.
"
exit 1
fi
manual-linter-override:
name: Linter Override
needs: [check-breaking-changes]
if: always() && needs.check-breaking-changes.result == 'failure'
environment: breaking-changes-override
runs-on: ubuntu-latest
steps:
- name: Override Breaking Changes Check
run: |
echo "⚠️ Manual override requested for breaking changes check"
echo "Breaking changes check failed but was manually approved to proceed"
echo "This override was approved by the reviewer"
api-spec-check:
name: API Spec Check
needs:
[
generate-head-api-spec,
generate-merge-base-api-spec,
check-specs-identical,
generate-vacuum-reports,
check-breaking-changes,
manual-linter-override,
]
runs-on: ubuntu-latest
if: always()
steps:
- name: Check generate-head-api-spec
env:
GENERATE_HEAD_API_SPEC_RESULT: ${{ needs.generate-head-api-spec.result }}
run: |
if [ "$GENERATE_HEAD_API_SPEC_RESULT" != "success" ]; then
echo "❌ generate-head-api-spec: ${{ needs.generate-head-api-spec.result }}"
exit 1
fi
- name: Check generate-merge-base-api-spec
env:
GENERATE_MERGE_BASE_API_SPEC_RESULT: ${{ needs.generate-merge-base-api-spec.result }}
run: |
if [ "$GENERATE_MERGE_BASE_API_SPEC_RESULT" != "success" ]; then
echo "❌ generate-merge-base-api-spec: ${{ needs.generate-merge-base-api-spec.result }}"
exit 1
fi
- name: Check generate-vacuum-reports
env:
GENERATE_VACUUM_REPORTS_RESULT: ${{ needs.generate-vacuum-reports.result }}
SPECS_IDENTICAL: ${{ needs.check-specs-identical.outputs.specs-identical }}
run: |
if [ "$GENERATE_VACUUM_REPORTS_RESULT" = "skipped" ] && [ "$SPECS_IDENTICAL" = "true" ]; then
echo "⏭️ generate-vacuum-reports: skipped (specs are identical)"
elif [ "$GENERATE_VACUUM_REPORTS_RESULT" != "success" ]; then
echo "❌ generate-vacuum-reports: ${{ needs.generate-vacuum-reports.result }}"
exit 1
fi
- name: Check check-breaking-changes
env:
CHECK_BREAKING_CHANGES_RESULT: ${{ needs.check-breaking-changes.result }}
MANUAL_LINTER_OVERRIDE_RESULT: ${{ needs.manual-linter-override.result }}
SPECS_IDENTICAL: ${{ needs.check-specs-identical.outputs.specs-identical }}
run: |
if [ "$CHECK_BREAKING_CHANGES_RESULT" = "skipped" ] && [ "$SPECS_IDENTICAL" = "true" ]; then
echo "⏭️ check-breaking-changes: skipped (specs are identical)"
elif [ "$CHECK_BREAKING_CHANGES_RESULT" != "success" ] && [ "$MANUAL_LINTER_OVERRIDE_RESULT" = "success" ]; then
echo "⚠️ Manual linter override requested for breaking changes check"
echo "Breaking changes check failed but was manually approved to proceed"
elif [ "$CHECK_BREAKING_CHANGES_RESULT" != "success" ]; then
echo "❌ check-breaking-changes: ${{ needs.check-breaking-changes.result }}"
exit 1
fi
- name: Verify API Spec Check Passed
run: echo "✅ All API specification checks passed successfully!"