Skip to content

Commit c5f2e39

Browse files
committed
fix(ci): harden SDK drift issue lifecycle
Signed-off-by: Ignas Baranauskas <ibaranau@redhat.com>
1 parent 96cb59a commit c5f2e39

4 files changed

Lines changed: 416 additions & 227 deletions

File tree

‎.github/workflows/sdk-sync-dashboard.yml‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
run: |
6060
mkdir -p report
6161
mise run "$DRIFT_TASK" > report/drift.json 2> report/drift.stderr || true
62-
if ! jq -e 'has("synced") and (.synced | type == "boolean")' report/drift.json >/dev/null 2>&1; then
62+
if ! jq -e 'type == "object" and has("synced") and (.synced | type == "boolean") and (has("error") | not)' report/drift.json >/dev/null 2>&1; then
6363
jq -n --arg sdk "$SDK_NAME" '{sdk:$sdk, has_drift:"error", build_failed:"false"}' > report/status.json
6464
exit 0
6565
fi
@@ -100,6 +100,7 @@ jobs:
100100
matrix:
101101
sdk: ${{ fromJSON(needs.load_config.outputs.matrix) }}
102102
steps:
103+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
103104
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
104105
with:
105106
name: sdk-sync-${{ matrix.sdk.name }}
@@ -115,35 +116,44 @@ jobs:
115116
echo "::error::Drift detection failed for ${{ matrix.sdk.name }} SDK"
116117
cat report/drift.stderr
117118
exit 1
118-
- if: steps.status.outputs.has_drift == 'true' && steps.status.outputs.build_failed == 'true'
119-
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
120119
- name: Install tools
121-
if: steps.status.outputs.has_drift == 'true' && steps.status.outputs.build_failed == 'true'
120+
if: steps.status.outputs.has_drift == 'true'
122121
run: mise install --locked
123122
- name: Create or update drift issue
124-
if: steps.status.outputs.has_drift == 'true' && steps.status.outputs.build_failed == 'true'
123+
if: steps.status.outputs.has_drift == 'true'
125124
env:
126125
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
127126
run: |
127+
set +e
128128
RESULT=$(uv run python tasks/scripts/sdk_sync.py manage-issue \
129129
--drift-report "$(cat report/drift.json)" \
130130
--build-report "$(cat report/build.json)" \
131131
--sdk "${{ matrix.sdk.name }}" \
132132
--repo "$GITHUB_REPOSITORY" \
133133
--label "${{ matrix.sdk.label }}")
134-
echo "$RESULT" | jq .
135-
ACTION=$(echo "$RESULT" | jq -r '.action // "unknown"')
136-
if [ "$ACTION" = "error" ] || [ "$ACTION" = "unknown" ]; then
134+
STATUS=$?
135+
set -e
136+
if ! printf '%s\n' "$RESULT" | jq .; then
137+
printf '%s\n' "$RESULT"
138+
fi
139+
if [ "$STATUS" -ne 0 ]; then
140+
echo "::error::Issue management command failed with exit code $STATUS"
141+
exit "$STATUS"
142+
fi
143+
if ! ACTION=$(printf '%s\n' "$RESULT" | jq -er '.action // "unknown"'); then
144+
ACTION=unknown
145+
fi
146+
if [ "$ACTION" != "created" ] && [ "$ACTION" != "updated" ]; then
137147
echo "::error::Issue management for ${{ matrix.sdk.name }} failed: $ACTION"
138148
exit 1
139149
fi
140150
- name: Close resolved drift issue
141-
if: steps.status.outputs.has_drift == 'false' || (steps.status.outputs.has_drift == 'true' && steps.status.outputs.build_failed == 'false')
151+
if: steps.status.outputs.has_drift == 'false'
142152
env:
143153
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144154
run: |
145155
ISSUE=$(gh issue list --repo "$GITHUB_REPOSITORY" --label "${{ matrix.sdk.label }}" --state open --json number --jq '.[0].number')
146156
if [ -n "$ISSUE" ]; then
147-
gh issue close "$ISSUE" --repo "$GITHUB_REPOSITORY" --comment "SDK builds and tests pass after proto regeneration. Closing automatically."
157+
gh issue close "$ISSUE" --repo "$GITHUB_REPOSITORY" --comment "The SDK proto drift check passed. Closing automatically."
148158
echo "Closed issue #$ISSUE"
149159
fi

‎tasks/scripts/sdk_sync.py‎

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ def _load_sdk_configs() -> dict[str, dict]:
7373
7474
## Context
7575
76-
The root `proto/` directory has changed and the {display_name} SDK's generated bindings are out of sync. The drifted files are: {drifted_names}.
76+
The {display_name} SDK's proto drift check detected drift. The drifted files are: {drifted_names}.
7777
{build_context}
7878
7979
## Steps
8080
8181
1. **Regenerate bindings**: Run `mise run {proto_task}` to regenerate language-specific bindings from the updated protos.
82-
2. **Fix compilation errors**: Read the build log above. Update the SDK source code to handle new/changed/removed proto fields:
82+
2. **Fix compilation errors, if any**: Consult the build log if available. Update the SDK source code as needed to handle new/changed/removed proto fields:
8383
{source_dirs}
84-
3. **Fix test failures**: Update tests that assert on proto types that changed shape.
84+
3. **Fix test failures, if any**: Update tests that assert on proto types that changed shape.
8585
4. **Verify**: Run `mise run {build_task}` and `mise run {test_task}` until both pass.
8686
5. **Create a PR**: Commit all changes and create a PR referencing this issue.
8787
@@ -165,6 +165,19 @@ def _render_agent_section(
165165
"The build log above shows the exact error. Your job is to fix the "
166166
f"{display_name} SDK code so it compiles and passes tests with the updated protos."
167167
)
168+
elif build_report and build_report.get("success") is True:
169+
if sdk == "go":
170+
build_context = (
171+
"\nRegeneration, build, and tests passed in CI, but the "
172+
"committed bindings still need to be regenerated and committed."
173+
)
174+
else:
175+
build_context = (
176+
"\nThe subsequent regeneration, typecheck, and tests passed in CI. "
177+
"Generated TypeScript bindings are gitignored; rerun "
178+
f"`mise run {paths['drift_task']}` to confirm compatibility "
179+
"before changing SDK source."
180+
)
168181
else:
169182
build_context = "\nThe SDK build status is unknown. Check if it compiles after regeneration."
170183

@@ -205,26 +218,59 @@ def _run_cmd(
205218

206219

207220
def _ensure_label(repo: str, label: str, description: str) -> None:
208-
check = _run_cmd(["gh", "label", "view", label, "--repo", repo], capture=True)
221+
check = _run_cmd(
222+
[
223+
"gh",
224+
"label",
225+
"list",
226+
"--repo",
227+
repo,
228+
"--search",
229+
label,
230+
"--limit",
231+
"20",
232+
"--json",
233+
"name",
234+
],
235+
capture=True,
236+
)
209237
if check.returncode != 0:
210-
result = _run_cmd(
211-
[
212-
"gh",
213-
"label",
214-
"create",
215-
label,
216-
"--repo",
217-
repo,
218-
"--description",
219-
description,
220-
"--color",
221-
"D93F0B",
222-
],
223-
capture=True,
238+
details = check.stderr.strip() or "unknown error"
239+
raise RuntimeError(f"Failed to look up label '{label}' in {repo}: {details}")
240+
try:
241+
labels = json.loads(check.stdout)
242+
except json.JSONDecodeError as error:
243+
raise RuntimeError(f"Invalid label list for {repo}: {error.msg}") from error
244+
if not isinstance(labels, list) or any(
245+
not isinstance(item, dict) or not isinstance(item.get("name"), str)
246+
for item in labels
247+
):
248+
raise RuntimeError(f"Invalid label list for {repo}: expected label names")
249+
# GitHub's search is fuzzy; similar SDK labels do not establish existence.
250+
if any(item["name"].casefold() == label.casefold() for item in labels):
251+
return
252+
if len(labels) >= 20:
253+
raise RuntimeError(
254+
f"Label search for '{label}' in {repo} was truncated; cannot confirm absence"
224255
)
225-
if result.returncode != 0:
226-
details = result.stderr.strip() or "unknown error"
227-
raise RuntimeError(f"Failed to create label '{label}': {details}")
256+
result = _run_cmd(
257+
[
258+
"gh",
259+
"label",
260+
"create",
261+
label,
262+
"--repo",
263+
repo,
264+
"--description",
265+
description,
266+
"--color",
267+
"D93F0B",
268+
],
269+
capture=True,
270+
)
271+
if result.returncode != 0:
272+
details = result.stderr.strip() or "unknown error"
273+
raise RuntimeError(f"Failed to create label '{label}': {details}")
228274

229275

230276
def _find_open_issue(repo: str, label: str) -> dict | None:
@@ -239,20 +285,44 @@ def _find_open_issue(repo: str, label: str) -> dict | None:
239285
label,
240286
"--state",
241287
"open",
288+
"--limit",
289+
"1",
242290
"--json",
243291
"url,number",
244-
"--jq",
245-
".[0]",
246292
],
247293
capture=True,
248294
)
249-
if result.returncode == 0 and result.stdout.strip():
250-
try:
251-
data = json.loads(result.stdout.strip())
252-
return {"url": data["url"], "number": str(data["number"])}
253-
except (json.JSONDecodeError, KeyError, TypeError):
254-
pass
255-
return None
295+
context = f"for {repo} with label '{label}'"
296+
if result.returncode != 0:
297+
details = result.stderr.strip() or "no stderr"
298+
raise RuntimeError(
299+
f"Failed to list open issues {context} "
300+
f"(exit code {result.returncode}): {details}"
301+
)
302+
try:
303+
issues = json.loads(result.stdout)
304+
except json.JSONDecodeError as error:
305+
raise RuntimeError(
306+
f"Invalid issue list {context}: expected JSON array ({error.msg})"
307+
) from error
308+
if not isinstance(issues, list):
309+
raise RuntimeError(f"Invalid issue list {context}: expected JSON array")
310+
if not issues:
311+
return None
312+
313+
issue = issues[0]
314+
if not isinstance(issue, dict):
315+
raise RuntimeError(f"Invalid issue list {context}: expected issue object")
316+
url = issue.get("url")
317+
number = issue.get("number")
318+
if (
319+
not isinstance(url, str)
320+
or not url.strip()
321+
or type(number) is not int
322+
or number <= 0
323+
):
324+
raise RuntimeError(f"Invalid issue list {context}: invalid issue URL or number")
325+
return {"url": url, "number": str(number)}
256326

257327

258328
# --- public functions ---
@@ -348,18 +418,23 @@ def _manage_issue(
348418
# --- CLI ---
349419

350420

351-
def _load_json_arg(value: str) -> dict | list:
352-
if value == "-":
353-
return json.load(sys.stdin)
354-
return json.loads(value)
421+
def _load_json_arg(value: str) -> dict:
422+
report = json.load(sys.stdin) if value == "-" else json.loads(value)
423+
if not isinstance(report, dict):
424+
raise ValueError("Expected report to be a JSON object")
425+
return report
355426

356427

357428
def cmd_manage_issue(args: argparse.Namespace) -> int:
358-
drift_report = _load_json_arg(args.drift_report)
359-
build_report = None
360-
if args.build_report:
361-
build_report = _load_json_arg(args.build_report)
362-
result = manage_issue(drift_report, build_report, args.sdk, args.repo, args.label)
429+
try:
430+
drift_report = _load_json_arg(args.drift_report)
431+
build_report = _load_json_arg(args.build_report) if args.build_report else None
432+
except ValueError as error:
433+
result = {"issue_url": "", "action": "error", "reason": str(error)}
434+
else:
435+
result = manage_issue(
436+
drift_report, build_report, args.sdk, args.repo, args.label
437+
)
363438
print(json.dumps(result))
364439
return 1 if result.get("action") == "error" else 0
365440

0 commit comments

Comments
 (0)