Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions .github/scripts/customer_guard_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""CI gate for CoderHelm's "Customer details guard" check run.

CoderHelm sets that check run on every pull request of a public repository
it is installed on, from its own records (services/common/src/customer_guard).
This gate makes the repository's CI wait for it: it fails when the guard's
newest run on the PR's head failed, or when there is none, finished, after
waiting up to five minutes. A skipped run (a pull request from a fork) passes.

Only the guard's own app counts (GUARD_APP, `coderhelm`): a check of the
same name from anything else, a workflow job included, is ignored.

An edit to the PR's text after CI finished sets the guard again; the
"Customer details guard rerun" workflow then runs this job again, so the
required CI follows the text.

Until the guard is deployed (the last successful deploy of the platform
does not yet contain it), the gate passes: nothing could set the check.

It prints only "pass" or "fail".

python3 .github/scripts/customer_guard_gate.py # in Actions, on pull_request
"""

import json
import os
import sys
import time
import urllib.error
import urllib.parse
import urllib.request

CHECK = "Customer details guard"
APP = os.environ.get("GUARD_APP", "coderhelm")
PLATFORM = "CoderHelm/coderhelm-platform"
GUARD_SOURCE = "services/worker/src/passes/customer_guard.rs"
WAIT_SECS = int(os.environ.get("GUARD_WAIT_SECS", "300"))
POLL_SECS = 15


class NotFound(Exception):
pass


def api(path):
"""GET one GitHub API path; NotFound on a 404, any other failure raises."""
base = os.environ.get("GITHUB_API_URL", "https://api.github.com")
req = urllib.request.Request(base + path)
req.add_header("Accept", "application/vnd.github+json")
token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN")
if token:
req.add_header("Authorization", f"Bearer {token}")
try:
with urllib.request.urlopen(req, timeout=30) as r:
return json.load(r)
except urllib.error.HTTPError as e:
if e.code == 404:
raise NotFound() from None
raise


def latest(runs, app=APP):
"""The guard's newest check run from its own app, or None."""
own = [r for r in runs if r.get("name") == CHECK and (r.get("app") or {}).get("slug") == app]
if not own:
return None
return max(own, key=lambda r: (r.get("started_at") or "", r.get("id") or 0))


def verdict(run):
"""'pass', 'fail', or None while there is no finished run."""
if run is None or run.get("status") != "completed":
return None
return "pass" if run.get("conclusion") in ("success", "neutral", "skipped") else "fail"


def guard_live(get=api):
"""Is the guard deployed: the last successful deploy's commit has it. A
read that fails counts as deployed, so a failure never waves a PR on."""
try:
runs = get(f"/repos/{PLATFORM}/actions/workflows/deploy.yml/runs?branch=main&status=success&per_page=1")
sha = (runs.get("workflow_runs") or [{}])[0].get("head_sha")
if not sha:
return True
get(f"/repos/{PLATFORM}/contents/{GUARD_SOURCE}?ref={sha}")
return True
except NotFound:
return False
except Exception:
return True


def head_sha():
sha = os.environ.get("HEAD_SHA", "")
if sha:
return sha
with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as f:
return json.load(f)["pull_request"]["head"]["sha"]


def run(repo, sha, get=api, sleep=time.sleep, now=time.monotonic, wait=WAIT_SECS):
"""'pass' or 'fail' for the guard on `sha`."""
if not guard_live(get):
return "pass"
name = urllib.parse.quote(CHECK)
deadline = now() + wait
while True:
try:
runs = get(f"/repos/{repo}/commits/{sha}/check-runs?check_name={name}&filter=all&per_page=100")
v = verdict(latest(runs.get("check_runs") or []))
if v:
return v
except Exception:
pass
if now() >= deadline:
return "fail"
sleep(POLL_SECS)


def main():
try:
result = run(os.environ["GITHUB_REPOSITORY"], head_sha())
except Exception:
result = "fail"
print(result)
return 0 if result == "pass" else 1


if __name__ == "__main__":
sys.exit(main())
106 changes: 106 additions & 0 deletions .github/scripts/test_customer_guard_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Tests for customer_guard_gate.py, against a fake GitHub API."""

import contextlib
import io
import os
import pathlib
import sys
import unittest
from unittest import mock

sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))

import customer_guard_gate as g # noqa: E402


def check(conclusion, status="completed", app="coderhelm", started="2026-01-01T00:00:00Z", id=1):
return {"name": g.CHECK, "app": {"slug": app}, "status": status,
"conclusion": conclusion, "started_at": started, "id": id}


class Fake:
"""GitHub, answering from a list of check-run listings (one per poll)."""

def __init__(self, polls, live=True):
self.polls, self.live, self.calls = list(polls), live, 0

def __call__(self, path):
if "/actions/workflows/deploy.yml/runs" in path:
return {"workflow_runs": [{"head_sha": "d3p10y"}]}
if "/contents/" in path:
if self.live:
return {}
raise g.NotFound()
self.calls += 1
runs = self.polls.pop(0) if len(self.polls) > 1 else self.polls[0]
return {"check_runs": runs}


class Clock:
def __init__(self):
self.t = 0.0

def now(self):
return self.t

def sleep(self, s):
self.t += s


def gate(polls, live=True, wait=300):
c = Clock()
return g.run("o/r", "abc", get=Fake(polls, live), sleep=c.sleep, now=c.now, wait=wait)


class Gate(unittest.TestCase):
def test_passing_and_skipped_pass(self):
self.assertEqual(gate([[check("success")]]), "pass")
self.assertEqual(gate([[check("neutral")]]), "pass")

def test_failing_fails(self):
self.assertEqual(gate([[check("failure")]]), "fail")

def test_missing_fails_after_the_wait(self):
self.assertEqual(gate([[]]), "fail")

def test_it_waits_for_a_running_guard(self):
polls = [[], [check(None, status="in_progress")], [check("success")]]
self.assertEqual(gate(polls), "pass")

def test_the_newest_run_counts(self):
runs = [check("failure", id=1), check("success", started="2026-01-01T00:05:00Z", id=2)]
self.assertEqual(gate([runs]), "pass")
runs = [check("success", id=1), check("failure", started="2026-01-01T00:05:00Z", id=2)]
self.assertEqual(gate([runs]), "fail")

def test_only_the_guards_own_app_counts(self):
self.assertEqual(gate([[check("success", app="github-actions")]]), "fail")
both = [check("success", app="github-actions", started="2026-01-02T00:00:00Z"), check("failure")]
self.assertEqual(gate([both]), "fail")

def test_before_the_guard_is_deployed_it_passes(self):
self.assertEqual(gate([[]], live=False), "pass")

def test_a_failed_read_never_passes(self):
def broken(path):
raise OSError("down")
c = Clock()
self.assertTrue(g.guard_live(broken))
self.assertEqual(g.run("o/r", "abc", get=broken, sleep=c.sleep, now=c.now, wait=60), "fail")

def test_it_prints_only_pass_or_fail(self):
out = io.StringIO()
env = {"GITHUB_REPOSITORY": "o/r", "HEAD_SHA": "abc"}
with mock.patch.dict(os.environ, env), mock.patch.object(g, "run", return_value="fail"), \
contextlib.redirect_stdout(out):
code = g.main()
self.assertEqual((code, out.getvalue()), (1, "fail\n"))
out = io.StringIO()
with mock.patch.dict(os.environ, env), mock.patch.object(g, "run", side_effect=RuntimeError("x")), \
contextlib.redirect_stdout(out):
code = g.main()
self.assertEqual((code, out.getvalue()), (1, "fail\n"))


if __name__ == "__main__":
unittest.main()
Loading
Loading