-
Notifications
You must be signed in to change notification settings - Fork 0
465 lines (401 loc) · 16.5 KB
/
api-spec-sync.yml
File metadata and controls
465 lines (401 loc) · 16.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# Canvas Learning System - API Specification Sync Pipeline
# 规范文档自动化同步检查
#
# 触发条件: OpenAPI 规范或 API 代码变更时
# 功能:
# 1. 验证 OpenAPI 规范格式
# 2. 检测破坏性变更 (oasdiff)
# 3. 合约测试 (Dredd)
# 4. 生成差异报告
name: API Specification Sync
on:
pull_request:
paths:
- 'backend/app/api/**'
- 'backend/app/models/**'
- 'specs/api/**'
- 'openapi.json'
push:
branches:
- main
- clean-release
paths:
- 'backend/app/api/**'
- 'backend/app/models/**'
env:
PYTHON_VERSION: '3.11'
NODE_VERSION: '20'
jobs:
# ═══════════════════════════════════════════════════════════════════════════════
# Phase 1: 导出当前 OpenAPI 规范
# ═══════════════════════════════════════════════════════════════════════════════
export-openapi:
name: Export OpenAPI Spec
runs-on: ubuntu-latest
outputs:
spec-changed: ${{ steps.check-diff.outputs.changed }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Install dependencies
run: |
cd backend
pip install -r requirements.txt
- name: Export OpenAPI spec from FastAPI
run: |
cd backend
python -c "
import json
from app.main import app
openapi_schema = app.openapi()
with open('../openapi-current.json', 'w', encoding='utf-8') as f:
json.dump(openapi_schema, f, indent=2, ensure_ascii=False)
print('OpenAPI spec exported successfully')
print(f'Paths: {len(openapi_schema.get(\"paths\", {}))}')
print(f'Schemas: {len(openapi_schema.get(\"components\", {}).get(\"schemas\", {}))}')
"
- name: Upload OpenAPI spec
uses: actions/upload-artifact@v4
with:
name: openapi-current
path: openapi-current.json
retention-days: 7
- name: Check if spec changed
id: check-diff
run: |
if [ -f "openapi.json" ]; then
if diff -q openapi.json openapi-current.json > /dev/null 2>&1; then
echo "changed=false" >> $GITHUB_OUTPUT
echo "OpenAPI spec unchanged"
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "OpenAPI spec has changes"
fi
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "No existing openapi.json found"
fi
# ═══════════════════════════════════════════════════════════════════════════════
# Phase 2: 验证 OpenAPI 规范格式
# ═══════════════════════════════════════════════════════════════════════════════
validate-spec:
name: Validate OpenAPI Spec
runs-on: ubuntu-latest
needs: export-openapi
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Download current spec
uses: actions/download-artifact@v4
with:
name: openapi-current
- name: Install Spectral
run: npm install -g @stoplight/spectral-cli
- name: Validate OpenAPI format
run: |
spectral lint openapi-current.json --format stylish || true
# 不强制失败,只报告问题
- name: Check for critical issues
run: |
# 检查必需字段
python3 -c "
import json
with open('openapi-current.json') as f:
spec = json.load(f)
issues = []
if 'openapi' not in spec:
issues.append('Missing openapi version')
if 'info' not in spec:
issues.append('Missing info section')
if 'paths' not in spec:
issues.append('Missing paths section')
if issues:
print('Critical issues found:')
for issue in issues:
print(f' - {issue}')
exit(1)
else:
print('OpenAPI spec validation passed')
print(f' - Version: {spec.get(\"openapi\")}')
print(f' - Title: {spec.get(\"info\", {}).get(\"title\")}')
print(f' - Paths: {len(spec.get(\"paths\", {}))}')
"
# ═══════════════════════════════════════════════════════════════════════════════
# Phase 3: 检测破坏性变更
# ═══════════════════════════════════════════════════════════════════════════════
detect-breaking-changes:
name: Detect Breaking Changes
runs-on: ubuntu-latest
needs: export-openapi
if: github.event_name == 'pull_request'
outputs:
has-breaking: ${{ steps.oasdiff.outputs.breaking }}
breaking-count: ${{ steps.oasdiff.outputs.breaking-count }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download current spec
uses: actions/download-artifact@v4
with:
name: openapi-current
- name: Get base branch spec
run: |
git checkout origin/${{ github.base_ref }} -- openapi.json 2>/dev/null || echo "{}" > openapi-base.json
if [ -f "openapi.json" ]; then
mv openapi.json openapi-base.json
fi
git checkout ${{ github.head_ref }} -- . 2>/dev/null || true
- name: Install oasdiff
run: |
curl -sSL https://github.com/Tufin/oasdiff/releases/latest/download/oasdiff_linux_amd64.tar.gz | tar xz
sudo mv oasdiff /usr/local/bin/
- name: Run oasdiff
id: oasdiff
run: |
# 检测破坏性变更
oasdiff breaking openapi-base.json openapi-current.json \
--format json > breaking-changes.json 2>&1 || true
# 解析结果
BREAKING_COUNT=$(python3 -c "
import json
try:
with open('breaking-changes.json') as f:
data = json.load(f)
count = len(data) if isinstance(data, list) else 0
print(count)
except:
print(0)
")
echo "breaking-count=$BREAKING_COUNT" >> $GITHUB_OUTPUT
if [ "$BREAKING_COUNT" -gt 0 ]; then
echo "breaking=true" >> $GITHUB_OUTPUT
echo "::warning::Found $BREAKING_COUNT breaking changes!"
else
echo "breaking=false" >> $GITHUB_OUTPUT
echo "No breaking changes detected"
fi
- name: Generate diff report
run: |
oasdiff diff openapi-base.json openapi-current.json \
--format markdown > api-diff-report.md 2>&1 || echo "No changes" > api-diff-report.md
- name: Upload diff report
uses: actions/upload-artifact@v4
with:
name: api-diff-report
path: |
api-diff-report.md
breaking-changes.json
- name: Comment PR with changes
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
let diffReport = '';
try {
diffReport = fs.readFileSync('api-diff-report.md', 'utf8');
} catch (e) {
diffReport = 'No API changes detected.';
}
let breakingCount = '${{ steps.oasdiff.outputs.breaking-count }}';
let breakingSection = '';
if (parseInt(breakingCount) > 0) {
breakingSection = `
## :warning: Breaking Changes Detected
**${breakingCount} breaking change(s) found!**
Please review carefully before merging. Breaking changes may affect:
- Frontend API calls
- External integrations
- Obsidian plugin compatibility
`;
}
const body = `## :mag: API Specification Change Report
${breakingSection}
<details>
<summary>View API Diff</summary>
${diffReport}
</details>
---
*Generated by API Spec Sync workflow*
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
# ═══════════════════════════════════════════════════════════════════════════════
# Phase 4: 合约测试 (Dredd)
# ═══════════════════════════════════════════════════════════════════════════════
contract-test:
name: Contract Test (Dredd)
runs-on: ubuntu-latest
needs: [export-openapi, validate-spec]
services:
neo4j:
image: neo4j:5.26-community
ports:
- 7474:7474
- 7687:7687
env:
NEO4J_AUTH: neo4j/testpassword
options: >-
--health-cmd "curl -f http://localhost:7474 || exit 1"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Download current spec
uses: actions/download-artifact@v4
with:
name: openapi-current
- name: Install dependencies
run: |
cd backend
pip install -r requirements.txt
npm install -g dredd
- name: Start FastAPI server
run: |
cd backend
export NEO4J_URI=bolt://localhost:7687
export NEO4J_USER=neo4j
export NEO4J_PASSWORD=testpassword
uvicorn app.main:app --host 0.0.0.0 --port 8000 &
sleep 10 # 等待服务器启动
- name: Wait for API to be ready
run: |
for i in {1..30}; do
if curl -s http://localhost:8000/api/v1/health > /dev/null; then
echo "API is ready"
break
fi
echo "Waiting for API... ($i/30)"
sleep 2
done
- name: Run Dredd contract tests
id: dredd
continue-on-error: true
run: |
dredd openapi-current.json http://localhost:8000 \
--reporter json:dredd-report.json \
--reporter markdown:dredd-report.md \
--hookfiles scripts/spec-tools/dredd-hooks.js \
--method GET \
--names || true
- name: Parse Dredd results
run: |
python3 -c "
import json
try:
with open('dredd-report.json') as f:
data = json.load(f)
stats = data.get('stats', {})
print('Contract Test Results:')
print(f' - Tests: {stats.get(\"tests\", 0)}')
print(f' - Passes: {stats.get(\"passes\", 0)}')
print(f' - Failures: {stats.get(\"failures\", 0)}')
print(f' - Pending: {stats.get(\"pending\", 0)}')
if stats.get('failures', 0) > 0:
print('::warning::Contract test failures detected')
except Exception as e:
print(f'Could not parse Dredd results: {e}')
"
- name: Upload Dredd report
uses: actions/upload-artifact@v4
with:
name: dredd-report
path: |
dredd-report.json
dredd-report.md
# ═══════════════════════════════════════════════════════════════════════════════
# Phase 5: 更新 OpenAPI 规范文件
# ═══════════════════════════════════════════════════════════════════════════════
update-spec:
name: Update OpenAPI Spec
runs-on: ubuntu-latest
needs: [export-openapi, validate-spec]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Download current spec
uses: actions/download-artifact@v4
with:
name: openapi-current
- name: Check if update needed
id: check
run: |
if [ -f "openapi.json" ]; then
if diff -q openapi.json openapi-current.json > /dev/null 2>&1; then
echo "update-needed=false" >> $GITHUB_OUTPUT
else
echo "update-needed=true" >> $GITHUB_OUTPUT
fi
else
echo "update-needed=true" >> $GITHUB_OUTPUT
fi
- name: Update OpenAPI spec
if: steps.check.outputs.update-needed == 'true'
run: |
mv openapi-current.json openapi.json
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add openapi.json
git commit -m "chore: update OpenAPI specification [skip ci]" || true
git push || true
# ═══════════════════════════════════════════════════════════════════════════════
# Summary Job
# ═══════════════════════════════════════════════════════════════════════════════
summary:
name: Summary
runs-on: ubuntu-latest
needs: [export-openapi, validate-spec, detect-breaking-changes, contract-test]
if: always()
steps:
- name: Generate summary
run: |
echo "## API Specification Sync Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Check | Status |" >> $GITHUB_STEP_SUMMARY
echo "|-------|--------|" >> $GITHUB_STEP_SUMMARY
if [ "${{ needs.export-openapi.result }}" == "success" ]; then
echo "| Export OpenAPI | :white_check_mark: |" >> $GITHUB_STEP_SUMMARY
else
echo "| Export OpenAPI | :x: |" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ needs.validate-spec.result }}" == "success" ]; then
echo "| Validate Spec | :white_check_mark: |" >> $GITHUB_STEP_SUMMARY
else
echo "| Validate Spec | :x: |" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ needs.detect-breaking-changes.outputs.has-breaking }}" == "true" ]; then
echo "| Breaking Changes | :warning: ${{ needs.detect-breaking-changes.outputs.breaking-count }} found |" >> $GITHUB_STEP_SUMMARY
else
echo "| Breaking Changes | :white_check_mark: None |" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ needs.contract-test.result }}" == "success" ]; then
echo "| Contract Tests | :white_check_mark: |" >> $GITHUB_STEP_SUMMARY
else
echo "| Contract Tests | :warning: |" >> $GITHUB_STEP_SUMMARY
fi