Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .github/ci-policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
merge_queue: false
required_checks: []
status_contexts: []
exceptions: []
436 changes: 436 additions & 0 deletions .github/ci-policy/check.py

Large diffs are not rendered by default.

153 changes: 153 additions & 0 deletions .github/ci-policy/gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env python3
"""ci-policy caller logic (D-6, eng review D10 and D14). Run from the BASE revision's bytes: the head is only read as
data (`git archive`), nothing from it is executed. Needs `git` and, for the API reads, `gh` with GH_TOKEN.

pr --repo OWNER/NAME --pr N --base SHA --head SHA [--private] [--default-branch main] [--root DIR]
merge-group --repo OWNER/NAME --group-ref REF

`pr` runs check.py on the head's workflow files and policy file. A PR that edits `.github/ci-policy*` or the caller
workflow also fails `policy-changed` until a GitHub review APPROVED by a human who is not the author or a bot, with
write access, on the current head SHA. A new push moves the head, so it resets the approval. `merge-group` lists the PRs queued
in the group (the merge commits on the group head's first-parent chain), finds each by its recorded head and copies its latest
`ci-policy` check-run result; a commit whose PR the queue data does not identify fails closed."""
import argparse
import json
import re
import subprocess
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import check as ci_check # noqa: E402

CHECK_NAME = "ci-policy"
WRITERS = {"OWNER", "MEMBER", "COLLABORATOR"}
VERDICTS = {"APPROVED", "CHANGES_REQUESTED", "DISMISSED"} # a COMMENTED review changes nothing
COMPARE_CAP = 250 # the compare API lists at most this many commits
GROUP_REF = re.compile(r"^refs/heads/(gh-readonly-queue/(.+)/pr-(\d+)-[0-9a-f]{40})$")


def is_protected(path: str) -> bool:
return path.startswith(".github/ci-policy") or path == ".github/workflows/ci-policy.yml"


def gh_api(path: str):
"""JSON for one object, or a list for a paginated array (`check_runs` unwrapped)."""
jq = (".check_runs[]" if "/check-runs" in path else ".commits[]" if "/compare/" in path
else ".[]" if path.endswith(("/reviews", "/pulls")) else ".")
out = subprocess.run(["gh", "api", "--paginate", path, "--jq", jq], check=True, capture_output=True, text=True).stdout
items = [json.loads(line) for line in out.splitlines() if line.strip()]
return items if jq != "." else items[0]


def approver(reviews: list, head: str, author: str) -> str | None:
latest = {}
for r in reviews: # chronological: the last verdict of each reviewer stands
if r["state"] in VERDICTS:
latest[r["user"]["login"]] = r
return next((login for login, r in latest.items()
if r["state"] == "APPROVED" and r["commit_id"] == head and login != author
and r["user"]["type"] == "User" and r.get("author_association") in WRITERS), None)


def changed_paths(root: Path, base: str, head: str) -> list[str]:
out = subprocess.run(["git", "-C", str(root), "-c", "core.quotepath=off", "diff", "--name-only", "--no-renames", f"{base}...{head}"],
check=True, capture_output=True, text=True).stdout
return out.splitlines()


def extract_head(root: Path, head: str, dest: Path) -> None:
"""The head's `.github` tree as plain files; a repo without one yields an empty dir (the checker then reports D-2)."""
archive = subprocess.run(["git", "-C", str(root), "archive", head, ".github"], capture_output=True)
if archive.returncode == 0:
subprocess.run(["tar", "-x", "-C", str(dest)], input=archive.stdout, check=True)


def check_pr(root: Path, repo: str, pr: int, base: str, head: str, private: bool, default_branch: str, api=gh_api) -> list:
live = api(f"repos/{repo}/pulls/{pr}")
if live["head"]["sha"] != head:
return [ci_check.Violation("-", "head-moved", f"PR head is {live['head']['sha'][:12]}, not {head[:12]}: a newer run reports")]
found = []
touched = [p for p in changed_paths(root, base, head) if is_protected(p)]
if touched and not approver(api(f"repos/{repo}/pulls/{pr}/reviews"), head, live["user"]["login"]):
found.append(ci_check.Violation(touched[0], "policy-changed",
f"PR edits {', '.join(touched)}: needs a non-author human APPROVED review on head {head[:12]}"))
with tempfile.TemporaryDirectory() as tmp:
extract_head(root, head, Path(tmp))
violations, _ = ci_check.check(Path(tmp), repo, private, default_branch)
return found + violations


def check_merge_group(repo: str, group_ref: str, api=gh_api) -> list:
"""Every PR queued in the group (the ref names only the last one) must have a successful `ci-policy` result.

Only queue data GitHub itself records identifies a PR. Walk the first-parent chain from the group head down to the
base branch (the compare response also lists each PR's own commits, which are not queue commits). Each chain commit
must be a merge commit: its second parent is the PR head the queue merged, and `commits/{head}/pulls` names the one open PR that has that head. Titles and tree
contents prove nothing about identity and are not read. A squashed commit records no head, so it fails closed."""
def bad(reason):
return [ci_check.Violation("-", "merge-group", reason)]

if not (m := GROUP_REF.match(group_ref)):
return bad(f"cannot read the PR number from {group_ref!r}")
branch, base_branch, last = m[1], m[2], int(m[3])
group_head = api(f"repos/{repo}/git/ref/heads/{branch}")["object"]["sha"]
commits = api(f"repos/{repo}/compare/{base_branch}...{group_head}?per_page=100")
if len(commits) >= COMPARE_CAP:
return bad(f"binding unavailable: compare lists {len(commits)} commits, so it may be truncated")
by_sha = {c["sha"]: c for c in commits} # compare also lists each PR's own commits; only the first-parent chain is the queue's
queued = {} # PR number -> the head the queue merged
sha = group_head
while sha in by_sha:
c = by_sha[sha]
parents = c["parents"]
if len(parents) != 2:
return bad(f"binding unavailable: group commit {c['sha'][:12]} is not a merge commit, so it records no PR head")
head = parents[1]["sha"]
owners = [p["number"] for p in api(f"repos/{repo}/commits/{head}/pulls")
if p["state"] == "open" and p["head"]["sha"] == head and p["base"]["ref"] == base_branch]
if len(owners) != 1:
return bad(f"binding unavailable: queued head {head[:12]} belongs to {len(owners)} open PRs, not one")
if owners[0] in queued:
return bad(f"PR #{owners[0]} is queued by two group commits")
queued[owners[0]] = head
sha = parents[0]["sha"]
if last not in queued:
return bad(f"binding unavailable: PR #{last} from the ref is not among the group's merged heads")
found = []
for pr, head in sorted(queued.items()):
runs = api(f"repos/{repo}/commits/{head}/check-runs?check_name={CHECK_NAME}&per_page=100")
latest = max(runs, key=lambda r: r["id"], default=None)
if latest is None or latest["status"] != "completed" or latest["conclusion"] != "success":
state = latest and (latest["conclusion"] or latest["status"])
found += bad(f"PR #{pr} head {head[:12]}: latest {CHECK_NAME} result is {state or 'missing'}, not success")
return found


def main(argv=None) -> int:
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest="cmd", required=True)
pr = sub.add_parser("pr")
pr.add_argument("--root", type=Path, default=Path("."))
pr.add_argument("--repo", required=True)
pr.add_argument("--pr", type=int, required=True)
pr.add_argument("--base", required=True)
pr.add_argument("--head", required=True)
pr.add_argument("--private", action="store_true")
pr.add_argument("--default-branch", default="main")
mg = sub.add_parser("merge-group")
mg.add_argument("--repo", required=True)
mg.add_argument("--group-ref", required=True)
args = ap.parse_args(argv)
if args.cmd == "pr":
found = check_pr(args.root, args.repo, args.pr, args.base, args.head, args.private, args.default_branch)
else:
found = check_merge_group(args.repo, args.group_ref)
for v in found:
print(f"{args.repo} {v.workflow} {v.rule} {v.reason}")
return 1 if found else 0


if __name__ == "__main__":
sys.exit(main())
77 changes: 77 additions & 0 deletions .github/workflows/ci-policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ci-policy: class=gate
# ci-policy caller (D-6, eng review D10 = A and D14 = A). Same content in every enrolled repo; scripts/ci-policy/sync.py
# keeps the copies current. See standards/general/ci.md.
#
# SECURITY — same boundary as mechanical-tier.yml: `pull_request_target` and `pull_request_review` run PR checks with
# the BASE revision's checker. The job checks out the base SHA and only FETCHES head objects; gate.py reads the head's
# `.github` tree as data. Do not add a head checkout, a head `run:` or a secret to this workflow.
# `merge_group` checks out the default branch and copies every queued PR's `ci-policy` result (gate.py merge-group); gate.py fetches the group and head commits it must compare.
# Not a required check until D-9 promotion; a push trigger is never needed (D-4).

name: ci-policy

on:
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]
pull_request_review:
types: [submitted, dismissed]
merge_group:

permissions:
contents: read
pull-requests: read
checks: read

# One group per event type: a review run must not cancel the run for a newer push.
concurrency:
group: ci-policy-${{ github.event_name }}-${{ github.event.pull_request.number || github.event.merge_group.head_sha }}
cancel-in-progress: true

jobs:
ci-policy:
name: ci-policy
runs-on: ${{ github.repository_owner == 'cheese-work' && fromJSON('["self-hosted","Linux","X64","cheese-x99-gate"]') || fromJSON('["ubuntu-latest"]') }}
timeout-minutes: 10
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GROUP_REF: ${{ github.event.merge_group.head_ref }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
PRIVATE: ${{ github.event.repository.private && '--private' || '' }}
steps:
- name: Discard a reused workspace
run: |
find "$GITHUB_WORKSPACE" -mindepth 1 -delete
# A plain git checkout of the BASE sha (or the default branch for a queue run): no `actions/checkout` and no head
# ref, so the pull_request_target checkout rule has nothing to match and the base-only boundary stays visible.
- name: Check out the trusted base
run: |
auth="$(printf 'x-access-token:%s' "$GH_TOKEN" | base64 -w0)"
git init --quiet .
git remote add origin "https://github.com/${GITHUB_REPOSITORY}.git"
git -c "http.https://github.com/.extraheader=AUTHORIZATION: basic $auth" \
fetch --quiet --no-tags origin "+refs/heads/*:refs/remotes/origin/*"
git checkout --quiet --detach "${BASE_SHA:-refs/remotes/origin/${DEFAULT_BRANCH}}"
- name: Fetch the head objects without checking them out
if: github.event_name != 'merge_group'
run: |
auth="$(printf 'x-access-token:%s' "$GH_TOKEN" | base64 -w0)"
git -c "http.https://github.com/.extraheader=AUTHORIZATION: basic $auth" \
fetch --no-tags origin "+refs/pull/${PR_NUMBER}/head:refs/remotes/pull/head"
test "$(git rev-parse refs/remotes/pull/head)" = "$HEAD_SHA"
# Some X99 runners ship no PyYAML (CHE-2397); --target avoids PEP 668.
- name: Install PyYAML
run: |
python3 -m pip install --quiet --target "$RUNNER_TEMP/pydeps" pyyaml
echo "PYTHONPATH=$RUNNER_TEMP/pydeps" >> "$GITHUB_ENV"
- name: Check the PR against the base policy
if: github.event_name != 'merge_group'
run: |
python3 .github/ci-policy/gate.py pr --repo "$GITHUB_REPOSITORY" --pr "$PR_NUMBER" \
--base "$BASE_SHA" --head "$HEAD_SHA" --default-branch "$DEFAULT_BRANCH" $PRIVATE
- name: Copy the queued PR's result
if: github.event_name == 'merge_group'
run: |
python3 .github/ci-policy/gate.py merge-group --repo "$GITHUB_REPOSITORY" --group-ref "$GROUP_REF"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ci-policy: class=pr-validation
name: CI

on:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/host-agent.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# ci-policy: class=pr-validation
name: Host agent core

on:
workflow_dispatch:
push:
branches: [main]
paths:
- 'host-agent/**'
- 'artemis/runtime/adb_gateway.py'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/preview-controller.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ci-policy: class=manual
name: Preview controller (inactive)

on:
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/whats-new.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ci-policy: class=pr-validation
name: What's New

# Separate from CI so adding or removing the no-whats-new label re-runs only
Expand All @@ -9,6 +10,10 @@ on:
permissions:
contents: read

concurrency:
group: whats-new-${{ github.ref }}
cancel-in-progress: true

jobs:
entry:
name: What's New entry
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ exclude = [
# CHE-564: unmodified upstream directory fails the fork's UP ruleset;
# excluded to keep the fork's diff against upstream minimal.
"playground",
# CHE-3275: vendored ci-policy checker, kept byte-identical to
# cheese-work/multica-dotfiles (S3a, de75e73); not formatted here.
".github/ci-policy",
]

# Same as Black.
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_workflow_runner_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def _x99_labels(runs_on):
def test_every_active_job_runs_on_the_x99_pool():
for file, name, _workflow, job in _jobs():
labels = _x99_labels(job["runs-on"])
assert labels[0] == "self-hosted" and "cheese-x99" in labels, (file, name, labels)
on_pool = "cheese-x99" in labels or "cheese-x99-gate" in labels
assert labels[0] == "self-hosted" and on_pool, (file, name, labels)


def test_only_the_frontend_job_requires_the_browser_runner():
Expand Down
Loading