From 36b226416ec5bceb8387740f2a521816e29c9ab6 Mon Sep 17 00:00:00 2001 From: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:25:32 +0800 Subject: [PATCH 1/4] feat(ci): detect cross-PR Flyway version collisions Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com> --- scripts/migration-preflight.py | 139 ++++++++++++++++++++++ scripts/tests/test_migration_preflight.py | 69 +++++++++++ 2 files changed, 208 insertions(+) create mode 100644 scripts/migration-preflight.py create mode 100644 scripts/tests/test_migration_preflight.py diff --git a/scripts/migration-preflight.py b/scripts/migration-preflight.py new file mode 100644 index 000000000..ab02cf4a6 --- /dev/null +++ b/scripts/migration-preflight.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +"""Read GitHub PR file metadata and summarize possible Flyway version collisions.""" + +import html +import json +import os +from pathlib import Path +import re +import urllib.error +import urllib.request + + +MIGRATION = re.compile( + r"server/skillhub-app/src/main/resources/db/migration/V([0-9]+)__[^/]+\.sql\Z" +) + + +class ScanError(Exception): + pass + + +def version(path): + match = MIGRATION.fullmatch(path) + return int(match[1]) if match else None + + +def introduced_version(file): + number = version(file["filename"]) + if file["status"] == "added": + return number + if file["status"] == "renamed" and version(file.get("previous_filename", "")) != number: + return number + return None + + +class GitHub: + def __init__(self, repository, token): + if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repository): + raise ScanError("Invalid repository name") + self.base = f"https://api.github.com/repos/{repository}" + self.token = token + + def get(self, path): + request = urllib.request.Request(self.base + path, headers={ + "Authorization": f"Bearer {self.token}", + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + }) + try: + with urllib.request.urlopen(request, timeout=30) as response: + return json.load(response) + except urllib.error.HTTPError as error: + raise ScanError(f"GitHub API HTTP {error.code}; scan incomplete") from error + except (urllib.error.URLError, TimeoutError, ValueError, OSError) as error: + # Do not echo server payloads, tokens, request headers or arbitrary URLs. + raise ScanError(f"GitHub API {type(error).__name__}; scan incomplete") from error + + def pages(self, path, limit=None): + result = [] + for page in range(1, 1001): + separator = "&" if "?" in path else "?" + batch = self.get(f"{path}{separator}per_page=100&page={page}") + if not isinstance(batch, list): + raise ScanError("Unexpected GitHub pagination response") + result.extend(batch) + if len(batch) < 100: + return result + if limit is not None and len(result) >= limit: + raise ScanError("PR files reached GitHub's 3000-file limit") + raise ScanError("Pagination safety limit reached") + + +def scan(api): + versions = {} + errors = [] + prs = api.pages("/pulls?state=open&sort=created&direction=asc") + for pr in prs: + number = pr["number"] + try: + files = api.pages(f"/pulls/{number}/files", limit=3000) + current = api.get(f"/pulls/{number}") + if current["head"]["sha"] != pr["head"]["sha"] or current["state"] != "open": + raise ScanError("PR changed during scan; rerun required") + if len(files) != current["changed_files"]: + raise ScanError("PR file list incomplete; rerun required") + for file in files: + value = introduced_version(file) + if value is not None: + versions.setdefault(value, {}).setdefault(number, []).append(file["filename"]) + except ScanError as error: + errors.append(f"PR #{number}: {error}") + return {v: entries for v, entries in sorted(versions.items()) if len(entries) > 1}, errors, len(prs) + + +def safe(value): + # HTML text in a pre block avoids Markdown/link/table injection from filenames. + return html.escape(str(value)).replace("\r", " ").replace("\n", " ") + + +def summary(collisions, errors, count): + lines = ["## Flyway migration preflight", "", + "Advisory only: no SQL is downloaded or executed; no PR is changed.", "", + f"Open PRs inspected: {count}.", ""] + if errors: + lines += ["**Warning: scan incomplete. Absence of a reported collision is not clearance.**", "", "
",
+                  *(safe(error) for error in errors), "
", ""] + if collisions: + lines += ["**Warning: migration versions introduced by multiple open PRs:**", "", "
"]
+        for value, entries in collisions.items():
+            lines.append(f"V{value}")
+            for number, paths in sorted(entries.items()):
+                lines.extend(f"  PR #{number}: {safe(path)}" for path in sorted(set(paths)))
+        lines += ["
", "", "Authors should coordinate numbering and rerun migration tests before merge."] + elif not errors: + lines.append("No cross-PR version collisions found in this snapshot.") + lines += ["", "Scope: added migrations and renames introducing a different version in the application migration directory. " + "Same-version renames, modified/deleted files and conflicts with already merged migrations are not checked. " + "Concurrent PR changes can make this advisory snapshot stale.", ""] + return "\n".join(lines) + + +def main(): + try: + api = GitHub(os.environ.get("GITHUB_REPOSITORY", ""), os.environ.get("GITHUB_TOKEN", "")) + collisions, errors, count = scan(api) + except (ScanError, KeyError, TypeError) as error: + collisions, errors, count = {}, [f"Scan unavailable ({type(error).__name__}); no clearance issued."], 0 + report = summary(collisions, errors, count) + destination = os.environ.get("GITHUB_STEP_SUMMARY") + if destination: + with Path(destination).open("a", encoding="utf-8") as output: + output.write(report) + else: + print(report) + return 0 # Deliberately advisory; incomplete scans remain visible in the summary. + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/tests/test_migration_preflight.py b/scripts/tests/test_migration_preflight.py new file mode 100644 index 000000000..b40f2f4e0 --- /dev/null +++ b/scripts/tests/test_migration_preflight.py @@ -0,0 +1,69 @@ +"""Offline behavior tests for the read-only migration preflight.""" +import importlib.util +from pathlib import Path +import unittest + +ROOT = Path(__file__).resolve().parents[2] +spec = importlib.util.spec_from_file_location("preflight", ROOT / "scripts/migration-preflight.py") +m = importlib.util.module_from_spec(spec) +spec.loader.exec_module(m) +PREFIX = "server/skillhub-app/src/main/resources/db/migration/" + + +class PreflightTest(unittest.TestCase): + def test_status_and_rename_boundaries(self): + path = PREFIX + "V001__new.sql" + self.assertEqual(m.introduced_version(dict(filename=path, status="added")), 1) + for status in ("modified", "removed", "copied"): + self.assertIsNone(m.introduced_version(dict(filename=path, status=status))) + self.assertIsNone(m.introduced_version(dict(filename=path, status="renamed", + previous_filename=PREFIX + "V1__old.sql"))) + self.assertEqual(m.introduced_version(dict(filename=path, status="renamed", + previous_filename=PREFIX + "V2__old.sql")), 1) + for path in ("tests/V1__fake.sql", PREFIX + "V1_2__other.sql", PREFIX + "V1__new.sql/no"): + self.assertIsNone(m.introduced_version(dict(filename=path, status="added"))) + + def test_multiple_collisions_sorted_and_single_pr_not_collision(self): + class API: + def pages(self, path, limit=None): + if path.startswith("/pulls?"): + return [dict(number=n, head=dict(sha=str(n))) for n in (9, 2)] + return [dict(filename=PREFIX + f"V{v}__a.sql", status="added") for v in (20, 3, 3)] + def get(self, path): + return dict(head=dict(sha=path.split("/")[-1]), state="open", changed_files=3) + collisions, errors, count = m.scan(API()) + self.assertEqual(list(collisions), [3, 20]) + self.assertEqual(errors, []) + self.assertEqual(count, 2) + report = m.summary(collisions, errors, count) + self.assertLess(report.index("PR #2"), report.index("PR #9")) + self.assertEqual(report.count("PR #2:"), 2) + + def test_incomplete_never_reports_clearance(self): + report = m.summary({}, ["API HTTP 403"], 3) + self.assertIn("scan incomplete", report) + self.assertNotIn("No cross-PR", report) + + def test_untrusted_filename_is_text(self): + attack = PREFIX + "V1__\n::error::x.sql" + report = m.summary({1: {2: [attack], 3: [PREFIX + "V1__a.sql"]}}, [], 2) + self.assertNotIn("