-
Notifications
You must be signed in to change notification settings - Fork 11
188 lines (171 loc) · 6.99 KB
/
Copy pathopenapi-breaking-changes.yml
File metadata and controls
188 lines (171 loc) · 6.99 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
name: OpenAPI Breaking Changes
on:
pull_request:
branches: [ main ]
paths:
- 'openapi.yaml'
- 'openapi/**'
workflow_dispatch:
permissions:
contents: read
pull-requests: write
issues: write
jobs:
detect:
name: Detect breaking changes
runs-on: ubuntu-latest
steps:
- name: Checkout base spec
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Checkout head spec
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
path: head
- name: Install oasdiff
env:
OASDIFF_VERSION: 1.16.0
OASDIFF_SHA256: 2f424431c441a85e2d73ff884609f55c98c283ca2ce3d88537a7a029379dd521
run: |
curl -fsSL \
"https://github.com/oasdiff/oasdiff/releases/download/v${OASDIFF_VERSION}/oasdiff_${OASDIFF_VERSION}_linux_amd64.tar.gz" \
-o /tmp/oasdiff.tgz
echo "${OASDIFF_SHA256} /tmp/oasdiff.tgz" | sha256sum -c -
tar -xzf /tmp/oasdiff.tgz -C /tmp oasdiff
sudo mv /tmp/oasdiff /usr/local/bin/oasdiff
oasdiff --version
- name: Run oasdiff breaking
id: oasdiff
run: |
set +e
oasdiff breaking \
base/openapi.yaml head/openapi.yaml \
--format markdown \
--fail-on ERR > breaking.md
status=$?
# The markdown format prints every finding with a :warning: marker, so it
# cannot be filtered by severity; singleline prefixes each with its level.
oasdiff breaking \
base/openapi.yaml head/openapi.yaml \
--format singleline \
--fail-on ERR > breaking.txt
singleline_status=$?
set -e
# Both runs diff the same specs with the same --fail-on, so their exit codes
# must agree; anything above 1 is a crash rather than a finding.
if [ "$status" -gt 1 ] || [ "$singleline_status" -ne "$status" ]; then
echo "::error::oasdiff exited with $status (markdown) / $singleline_status (singleline)"
exit 1
fi
echo "status=$status" >> "$GITHUB_OUTPUT"
if [ "$status" -ne 0 ] && [ -s breaking.md ]; then
echo "has_breaking=true" >> "$GITHUB_OUTPUT"
else
echo "has_breaking=false" >> "$GITHUB_OUTPUT"
fi
- name: Build comment body
if: steps.oasdiff.outputs.has_breaking == 'true'
env:
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
# GitHub rejects a comment body over 65536 characters, and one change to a
# shared schema yields a finding per inheriting subschema per endpoint.
MAX_ERRORS=100
MAX_WARNINGS=25
MAX_BODY=64000
errors=$(grep -c '^error ' breaking.txt || true)
warnings=$(grep -c '^warning ' breaking.txt || true)
# `error at head/openapi.yaml, in API GET /quotes added …` -> `- `GET /quotes` — added …`
fmt() { sed 's/^\(error\|warning\) at [^,]*, in API \([A-Z]* [^ ]*\) /- `\2` — /'; }
{
echo '## :warning: Breaking OpenAPI changes detected'
echo ''
echo "\`oasdiff\` reports **$errors error / $warnings warning** changes to \`openapi.yaml\`."
echo 'This PR will need approval from an API reviewer before merge.'
echo ''
echo "### Errors ($errors)"
echo ''
grep -m "$MAX_ERRORS" '^error ' breaking.txt | fmt
if [ "$errors" -gt "$MAX_ERRORS" ]; then
echo "- _…and $((errors - MAX_ERRORS)) more errors._"
fi
if [ "$warnings" -gt 0 ]; then
echo ''
echo "### Warnings ($warnings)"
echo ''
echo '<details><summary>Show sample</summary>'
echo ''
grep -m "$MAX_WARNINGS" '^warning ' breaking.txt | fmt
if [ "$warnings" -gt "$MAX_WARNINGS" ]; then
echo "- _…and $((warnings - MAX_WARNINGS)) more warnings._"
fi
echo ''
echo '</details>'
fi
echo ''
echo '---'
echo "_Detected by [oasdiff](https://github.com/oasdiff/oasdiff). Full report: [job summary]($RUN_URL) or the \`oasdiff-report\` artifact._"
} > comment.md
if [ "$(wc -c < comment.md)" -gt "$MAX_BODY" ]; then
# Trim on line boundaries so a multi-byte character is never split.
LC_ALL=C awk -v max="$MAX_BODY" '{ n += length($0) + 1; if (n > max) exit } 1' \
comment.md > comment.trimmed
printf '\n_Comment truncated. Full report: [job summary](%s)._\n' "$RUN_URL" >> comment.trimmed
mv comment.trimmed comment.md
fi
- name: Publish full report to job summary
if: steps.oasdiff.outputs.has_breaking == 'true'
run: |
# The step summary is capped at 1 MiB.
{
echo '## oasdiff breaking changes'
echo ''
head -c 900000 breaking.md
} >> "$GITHUB_STEP_SUMMARY"
- name: Upload full report
if: steps.oasdiff.outputs.has_breaking == 'true'
uses: actions/upload-artifact@v4
with:
name: oasdiff-report
path: |
breaking.md
breaking.txt
- name: Ensure breaking-change label exists
if: steps.oasdiff.outputs.has_breaking == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
gh label create "breaking-change" \
--color B60205 \
--description "Introduces a breaking change to the OpenAPI spec" \
2>/dev/null || true
- name: Add breaking-change label
if: steps.oasdiff.outputs.has_breaking == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: gh pr edit "$PR_NUMBER" --add-label "breaking-change"
- name: Remove breaking-change label if previously set
if: steps.oasdiff.outputs.has_breaking == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: gh pr edit "$PR_NUMBER" --remove-label "breaking-change" || true
- name: Upsert PR comment (breaking)
if: steps.oasdiff.outputs.has_breaking == 'true'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: oasdiff-breaking-changes
path: comment.md
- name: Clear PR comment (no breaking)
if: steps.oasdiff.outputs.has_breaking == 'false'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: oasdiff-breaking-changes
delete: true