-
Notifications
You must be signed in to change notification settings - Fork 315
268 lines (243 loc) · 12.4 KB
/
Copy pathapi-breakage.yml
File metadata and controls
268 lines (243 loc) · 12.4 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
---
name: Python API breakage checks
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
sdk-api:
name: Python API
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7
with:
enable-cache: true
- name: Install workspace deps (dev)
run: uv sync --frozen --group dev
- name: Run Python API breakage check
id: api_breakage
# Let this step fail so CI is visibly red on breakage.
# Later reporting steps still run because they use if: always().
env:
ACP_VERSION_CHECK_BASE_REF: ${{ github.event_name == 'pull_request' && github.base_ref || github.event.before }}
ACP_VERSION_CHECK_SKIP: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.body || '', 'skip-acp-check')
}}
SDK_API_BREAKAGE_REPORT_PATH: sdk-api-breakage-report.json
run: |
uv run python .github/scripts/check_sdk_api_breakage.py 2>&1 | tee api-breakage.log
exit_code=${PIPESTATUS[0]}
echo "exit_code=${exit_code}" >> "$GITHUB_OUTPUT"
exit "${exit_code}"
- name: Write API breakage summary
if: ${{ always() }}
env:
EXIT_CODE: ${{ steps.api_breakage.outputs.exit_code }}
IS_FORK: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
LOG_PATH: api-breakage.log
REPORT_PATH: sdk-api-breakage-report.json
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
python3 <<'PY' >> "$GITHUB_STEP_SUMMARY"
import json
import os
from pathlib import Path
exit_code = int(os.environ.get('EXIT_CODE', '0') or '0')
is_fork = os.environ.get('IS_FORK', 'false') == 'true'
run_url = os.environ['RUN_URL']
status = '✅ **PASSED**' if exit_code == 0 else '❌ **FAILED**'
try:
report = json.loads(Path(os.environ['REPORT_PATH']).read_text())
except Exception:
report = {}
default_changes = report.get('field_default_changes', [])
default_changes_since_base = report.get(
'field_default_changes_since_base',
default_changes,
)
print(f'## Python API breakage checks — {status}')
print()
print(f"**Result:** {status}")
if exit_code != 0:
print()
print('> ⚠️ Breaking API changes or policy violations detected.')
print()
if default_changes_since_base:
print('### Behavioral default changes detected')
print()
print(
'These public `Field(default=...)` changes were introduced '
'by this changeset and were auto-marked with the '
'`release-note-required` label:'
)
print()
for change in default_changes_since_base:
print(
'- `{object_path}`: `{old_default}` → `{new_default}`'.format(
**change,
)
)
print()
elif default_changes:
print('### Behavioral default changes detected')
print()
print(
'These public `Field(default=...)` changes differ from the '
'latest released baseline, but they were already present '
'before this changeset, so the workflow did not add the '
'`release-note-required` label on this run:'
)
print()
for change in default_changes:
print(
'- `{object_path}`: `{old_default}` → `{new_default}`'.format(
**change,
)
)
print()
if is_fork:
print(
'_Fork PR detected: sticky PR comment was skipped because '
'the GitHub token is read-only for `pull_request` workflows '
'from forks._'
)
print()
if exit_code != 0:
try:
log = Path(os.environ['LOG_PATH']).read_text()
except Exception as exc:
log = f'Unable to read log file: {exc}'
excerpt = log[:1000].replace('```', '``\\`')
print('<details><summary>Log excerpt (first 1000 characters)</summary>')
print()
print('```text')
print(excerpt)
print('```')
print()
print('</details>')
print()
print(f'[Action log]({run_url})')
PY
- name: Sync release-note-required label
if: ${{ always() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }}
uses: actions/github-script@v9
env:
REPORT_PATH: sdk-api-breakage-report.json
with:
script: |
const fs = require('fs');
let defaultChangesSinceBase = [];
try {
const report = JSON.parse(fs.readFileSync(process.env.REPORT_PATH, 'utf8'));
defaultChangesSinceBase = report.field_default_changes_since_base || report.field_default_changes || [];
} catch (_error) {
defaultChangesSinceBase = [];
}
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const label = 'release-note-required';
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number,
per_page: 100,
});
const hasLabel = currentLabels.some((item) => item.name === label);
if (defaultChangesSinceBase.length > 0 && !hasLabel) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [label],
});
}
if (defaultChangesSinceBase.length === 0 && hasLabel) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: label,
});
}
- name: Post API breakage report to PR
if: ${{ always() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }}
uses: actions/github-script@v9
env:
EXIT_CODE: ${{ steps.api_breakage.outputs.exit_code }}
LOG_PATH: api-breakage.log
REPORT_PATH: sdk-api-breakage-report.json
with:
script: |
const fs = require('fs');
const marker = '<!-- api-breakage-report -->';
const exitCode = Number(process.env.EXIT_CODE || '0');
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const status = exitCode === 0 ? '✅ **PASSED**' : '❌ **FAILED**';
let defaultChanges = [];
let defaultChangesSinceBase = [];
try {
const report = JSON.parse(fs.readFileSync(process.env.REPORT_PATH, 'utf8'));
defaultChanges = report.field_default_changes || [];
defaultChangesSinceBase = report.field_default_changes_since_base || defaultChanges;
} catch (_error) {
defaultChanges = [];
defaultChangesSinceBase = [];
}
let body = `${marker}\n## Python API breakage checks — ${status}\n\n**Result:** ${status}\n`;
if (exitCode !== 0) {
body += `\n> ⚠️ Breaking API changes or policy violations detected.\n`;
let log = '';
try {
log = fs.readFileSync(process.env.LOG_PATH, 'utf8');
} catch (e) {
log = `Unable to read log file: ${e}`;
}
const excerpt = log.slice(0, 1000).replace(/```/g, '``\\`');
body += `\n<details><summary>Log excerpt (first 1000 characters)</summary>\n\n\`\`\`text\n${excerpt}\n\`\`\`\n\n</details>\n`;
}
if (defaultChangesSinceBase.length > 0) {
body += '\n### Behavioral default changes detected\n\n';
body += 'These public `Field(default=...)` changes were introduced by this PR and were auto-marked with the `release-note-required` label:\n\n';
for (const change of defaultChangesSinceBase) {
body += `- \`${change.object_path}\`: \`${change.old_default}\` → \`${change.new_default}\`\n`;
}
} else if (defaultChanges.length > 0) {
body += '\n### Behavioral default changes detected\n\n';
body += 'These public `Field(default=...)` changes differ from the latest released baseline, but they were already present on the base branch, so this PR was not auto-marked with the `release-note-required` label:\n\n';
for (const change of defaultChanges) {
body += `- \`${change.object_path}\`: \`${change.old_default}\` → \`${change.new_default}\`\n`;
}
}
body += `\n[Action log](${runUrl})\n`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find((c) => c.body && c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}