mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
A PR run stops at its first failure: on pull request events the deploy matrix runs with fail-fast, so one failed row cancels the rest of its batch, and the batch 1 gate stops every later batch, so PR #2006 showed one failure per run. With the 🏃 Run all label the PR workflow calls the orchestrator with chunk_gate false, and call-test-deploy ties the matrix fail-fast to chunk_gate, so every row and batch deploys to its end; push and dispatch runs are unchanged. Every labeled event used to cancel the running PR pipeline and start a new one (42 of the last 200 PR runs): a run whose jobs all skip still joins the concurrency group, and the superseded-run canceller keeps it as the newest. Only 🧩 Subset, 🛡️ Trusted and 🏃 Run all now restart PR CI; any other label gets a group of its own and skips, in the PR workflow and in the canceller. The labels CI sets carry an emoji: ⬆️ update, 🔴 major, 🟡 minor, 🟢 patch, 💥 ci-failure, 🔓 vulnerability, 🔥 vulnerability-runtime, 💤 stale. pr.sh labels updater PRs through the label CLI with a new --level option, which also drops a stale level label when an existing PR changes level. test_pr_ci_labels.py keeps the three label lists equal and fails against the main workflows. Tests: update, security, meta/ci, workflow and repository lint suites green after the fixture updates (65 + 3 passed, 826 in the first full run); MEDUSA 100 %. 🎯 Fix: 88 % 💥 Break: 7 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 82 🚦 Gate: ⛔ fix 88 % < 90, break 7 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
236 lines
8.5 KiB
Python
236 lines
8.5 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import cli.contributing.update.merge.__main__ as merge_main
|
|
from cli.contributing.update.label import MINOR, UPDATE
|
|
from cli.contributing.update.merge import github
|
|
from utils.roles.mapping import ROLE_FILE_META_SERVICES
|
|
|
|
REPO = "infinito-nexus/core"
|
|
SHA = "a" * 40
|
|
RUN_ID = 111
|
|
OWN_RUN_ID = 222
|
|
CSV_HEADER = "round,rounds_total,pass,pass_mode,role,seconds,hosts\n"
|
|
|
|
|
|
def pull_request(**overrides):
|
|
pr = {
|
|
"number": 7,
|
|
"state": "open",
|
|
"draft": False,
|
|
"user": {"login": "infinito-nexus-updater[bot]"},
|
|
"head": {
|
|
"sha": SHA,
|
|
"ref": "update/docker-image-versions-20261121-abc1234",
|
|
"repo": {"full_name": REPO},
|
|
},
|
|
"base": {"ref": "main"},
|
|
"labels": [{"name": UPDATE.name}, {"name": MINOR.name}],
|
|
}
|
|
pr.update(overrides)
|
|
return pr
|
|
|
|
|
|
def check(name="🔀 Pull Request / 🏁 Finish pipeline", **overrides):
|
|
run = {
|
|
"name": name,
|
|
"status": "completed",
|
|
"conclusion": "success",
|
|
"details_url": f"https://github.com/{REPO}/actions/runs/{RUN_ID}/job/1",
|
|
}
|
|
run.update(overrides)
|
|
return run
|
|
|
|
|
|
def role_runtimes(*roles):
|
|
rows = "".join(f",,,,{role},1.00,localhost=executed\n" for role in roles)
|
|
buffer = io.BytesIO()
|
|
with zipfile.ZipFile(buffer, "w") as archive:
|
|
archive.writestr("role-runtimes-web-app-nextcloud.csv", CSV_HEADER + rows)
|
|
return buffer.getvalue()
|
|
|
|
|
|
class FakeGh:
|
|
"""Answers the REST paths the merge gate reads, recording every call.
|
|
|
|
Args:
|
|
prs: pull requests associated with ``SHA``.
|
|
checks: check runs on ``SHA``.
|
|
statuses: commit statuses on ``SHA``.
|
|
files: ``filename`` paths the pull request touches.
|
|
artifacts: ``{name: zip bytes}`` of the CI run's artifacts.
|
|
"""
|
|
|
|
def __init__(self, prs, checks, statuses, files, artifacts):
|
|
self.routes = {
|
|
f"repos/{REPO}/commits/{SHA}/pulls": prs,
|
|
f"repos/{REPO}/commits/{SHA}/check-runs": checks,
|
|
f"repos/{REPO}/commits/{SHA}/status": statuses,
|
|
f"repos/{REPO}/pulls/7/files": [
|
|
{"filename": path, "previous_filename": None} for path in files
|
|
],
|
|
f"repos/{REPO}/actions/runs/{RUN_ID}/artifacts": [
|
|
{"id": index, "name": name, "expired": False, "size_in_bytes": 512}
|
|
for index, name in enumerate(artifacts)
|
|
],
|
|
}
|
|
self.blobs = dict(enumerate(artifacts.values()))
|
|
self.calls = []
|
|
|
|
def __call__(self, args):
|
|
self.calls.append(list(args))
|
|
if args[1] == "--paginate":
|
|
route = args[2].split("?")[0]
|
|
return "\n".join(json.dumps(item) for item in self.routes[route]).encode()
|
|
artifact_id = int(args[1].split("/")[-2])
|
|
return self.blobs[artifact_id]
|
|
|
|
|
|
def green_gh(**overrides):
|
|
fields = {
|
|
"prs": [pull_request()],
|
|
"checks": [check()],
|
|
"statuses": [{"context": "codecov", "state": "success"}],
|
|
"files": [f"roles/web-app-nextcloud/{ROLE_FILE_META_SERVICES}"],
|
|
"artifacts": {
|
|
"role-runtimes-web-app-nextcloud": role_runtimes(
|
|
"web-app-nextcloud", "svc-db-postgres"
|
|
)
|
|
},
|
|
}
|
|
fields.update(overrides)
|
|
return FakeGh(**fields)
|
|
|
|
|
|
def evaluate(gh):
|
|
return merge_main.evaluate(gh, REPO, SHA, RUN_ID, OWN_RUN_ID, "main")
|
|
|
|
|
|
class TestEvaluate(unittest.TestCase):
|
|
def test_all_green_and_every_changed_role_deployed_merges(self):
|
|
pr, refusals = evaluate(green_gh())
|
|
self.assertEqual((pr["number"], refusals), (7, []))
|
|
|
|
def test_a_dependabot_pull_request_merges_like_the_updater(self):
|
|
dependabot = pull_request(
|
|
user={"login": "dependabot[bot]"},
|
|
head={
|
|
"sha": SHA,
|
|
"ref": "dependabot/pip/roles/web-app-nextcloud/files/requests-2.32.0",
|
|
"repo": {"full_name": REPO},
|
|
},
|
|
)
|
|
self.assertEqual(evaluate(green_gh(prs=[dependabot]))[1], [])
|
|
|
|
def test_a_non_bot_author_is_refused(self):
|
|
human = pull_request(user={"login": "octocat"})
|
|
refusals = evaluate(green_gh(prs=[human]))[1]
|
|
self.assertEqual(refusals, ["author octocat is no update bot"])
|
|
|
|
def test_a_missing_update_label_is_refused(self):
|
|
unlabeled = pull_request(labels=[{"name": "minor"}])
|
|
refusals = evaluate(green_gh(prs=[unlabeled]))[1]
|
|
self.assertEqual(refusals, [f"label {UPDATE.name} is missing"])
|
|
|
|
def test_a_pending_check_refuses(self):
|
|
pending = check("🌐 Translate catalogs", status="in_progress", conclusion=None)
|
|
refusals = evaluate(green_gh(checks=[check(), pending]))[1]
|
|
self.assertEqual(refusals, ["check 🌐 Translate catalogs is in_progress"])
|
|
|
|
def test_a_failed_check_refuses(self):
|
|
failed = check("🔐 CodeQL", conclusion="failure")
|
|
refusals = evaluate(green_gh(checks=[check(), failed]))[1]
|
|
self.assertEqual(refusals, ["check 🔐 CodeQL concluded failure"])
|
|
|
|
def test_a_failed_commit_status_refuses(self):
|
|
statuses = [{"context": "codecov", "state": "failure"}]
|
|
refusals = evaluate(green_gh(statuses=statuses))[1]
|
|
self.assertEqual(refusals, ["status codecov is failure"])
|
|
|
|
def test_the_deciding_run_own_check_does_not_count(self):
|
|
own = check(
|
|
"🔀 Merge a proven update PR",
|
|
status="in_progress",
|
|
conclusion=None,
|
|
details_url=f"https://github.com/{REPO}/actions/runs/{OWN_RUN_ID}/job/9",
|
|
)
|
|
self.assertEqual(evaluate(green_gh(checks=[check(), own]))[1], [])
|
|
|
|
def test_a_changed_role_absent_from_role_runtimes_refuses(self):
|
|
artifacts = {"role-runtimes-web-app-matomo": role_runtimes("web-app-matomo")}
|
|
refusals = evaluate(green_gh(artifacts=artifacts))[1]
|
|
self.assertEqual(
|
|
refusals, ["role web-app-nextcloud changed, but no deploy row ran it"]
|
|
)
|
|
|
|
def test_no_role_runtimes_artifact_refuses(self):
|
|
artifacts = {"inventory-web-app-nextcloud": role_runtimes("web-app-nextcloud")}
|
|
refusals = evaluate(green_gh(artifacts=artifacts))[1]
|
|
self.assertEqual(
|
|
refusals, ["no role-runtimes artifact proves web-app-nextcloud"]
|
|
)
|
|
|
|
def test_a_pull_request_touching_no_role_needs_no_artifact(self):
|
|
gh = green_gh(files=[".github/workflows/call-lint.yml"], artifacts={})
|
|
self.assertEqual(evaluate(gh)[1], [])
|
|
self.assertFalse(any("artifacts" in " ".join(call) for call in gh.calls))
|
|
|
|
def test_no_open_pull_request_for_the_head_refuses(self):
|
|
closed = pull_request(state="closed")
|
|
pr, refusals = evaluate(green_gh(prs=[closed]))
|
|
self.assertIsNone(pr)
|
|
self.assertEqual(refusals, [f"0 open pull requests have head {SHA}, not one"])
|
|
|
|
|
|
class TestMain(unittest.TestCase):
|
|
def run_main(self, gh, sha=SHA):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
output = Path(tmp) / "github_output"
|
|
argv = [
|
|
"--repo",
|
|
REPO,
|
|
"--sha",
|
|
sha,
|
|
"--run-id",
|
|
str(RUN_ID),
|
|
"--own-run-id",
|
|
str(OWN_RUN_ID),
|
|
"--base",
|
|
"main",
|
|
"--output",
|
|
str(output),
|
|
]
|
|
stdout = io.StringIO()
|
|
with patch.object(github, "run_gh", gh), redirect_stdout(stdout):
|
|
code = merge_main.main(argv)
|
|
with output.open(encoding="utf-8") as handle:
|
|
return code, handle.read(), stdout.getvalue()
|
|
|
|
def test_a_merge_verdict_writes_pr_and_sha(self):
|
|
code, outputs, _ = self.run_main(green_gh())
|
|
self.assertEqual((code, outputs), (0, f"merge=true\npr=7\nsha={SHA}\n"))
|
|
|
|
def test_a_refusal_writes_merge_false_and_exits_zero(self):
|
|
gh = green_gh(checks=[check(conclusion="cancelled")])
|
|
code, outputs, printed = self.run_main(gh)
|
|
self.assertEqual((code, outputs), (0, "merge=false\n"))
|
|
self.assertIn("stays open for a manual merge", printed)
|
|
|
|
def test_a_short_sha_is_rejected_before_any_call(self):
|
|
gh = green_gh()
|
|
with self.assertRaises(SystemExit), redirect_stderr(io.StringIO()):
|
|
self.run_main(gh, sha="abc1234")
|
|
self.assertEqual(gh.calls, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|