Skip to content
Open
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
7 changes: 5 additions & 2 deletions .github/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,11 @@ A package publishes only when all of these hold:

- The newest commit on main's first-parent history that changed the package
has a successful push run on main, with the package's own jobs run and
passed (`scripts/ci/newest_green.py`). A cancelled or failed run blocks the
release until a re-run passes.
passed (`scripts/ci/newest_green.py`). A push run on a later first-parent
commit counts too when the package's tree there is identical and its jobs
ran and passed, as when the merge queue pushes several merges at once
(proposal A in `scripts/ci/post_merge_batching.md`). Otherwise a cancelled
or failed run blocks the release until a re-run passes.
- Its version is not on PyPI, is higher than every version there, and has no
tag yet.
- Its CHANGELOG.md starts with an entry for exactly that version.
Expand Down
133 changes: 104 additions & 29 deletions scripts/ci/newest_green.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@

Every release follows one rule (desktop-release.yml set it first): take the
newest commit on main's own history (first parent) that changed the package,
and require that commit's latest successful push run on main, with the
package's own jobs run and passed. The release then builds main's tip. Nothing
changed the package after that commit, so the package's tree is the one that
run tested.
the subject S, and require a successful push run on main with the package's
own jobs run and passed. The release then builds main's tip. Nothing changed
the package after S, so the package's tree is the one that run tested.

The run may be S's own, or (proposal A in scripts/ci/post_merge_batching.md)
the push run of a later first-parent commit C on the way from S to the tip
whose package tree is identical to S's (checked with `git diff --quiet`, so a
wrong S fails closed). This covers the merge queue pushing several merges at
once: only the last commit of the push gets a run, and its scope diff (against
the previous tip) still selects the package. The newest qualifying C wins.
S's own runs are always looked up; later commits are found among the recent
successful push runs on main.

python scripts/ci/newest_green.py flow --tip "$GITHUB_SHA" \\
--github-output "$GITHUB_OUTPUT"

It writes `sha` (the commit) and `run_id` (the green run), or exits 1 and
says why: no commit changed the package, the commit's run is still going, or
it failed and needs a re-run. Needs git history (fetch-depth: 0) and the gh
CLI with GH_TOKEN (actions: read).
It writes `sha` (the commit whose run passed) and `run_id` (the green run), or
exits 1 and says why: no commit changed the package, no run on S or a later
commit with the same package tree passed with the package's jobs, or the tree
changed after S. Needs git history (fetch-depth: 0) and the gh CLI with
GH_TOKEN (actions: read).
"""

from __future__ import annotations
Expand Down Expand Up @@ -61,6 +70,10 @@
),
}
FAILED = {"failure", "cancelled", "timed_out", "startup_failure"}
# How many recent successful push runs on main to search for a later commit.
RECENT_RUNS = 100
# How many later commits' green runs to inspect for the package's jobs.
MAX_LATER_RUNS = 5


def subject(tip: str, paths: list[str]) -> str | None:
Expand All @@ -73,6 +86,34 @@ def subject(tip: str, paths: list[str]) -> str | None:
return out or None


def _git(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(["git", *args], capture_output=True, text=True)


def is_ancestor(ancestor: str, commit: str) -> bool:
return _git("merge-base", "--is-ancestor", ancestor, commit).returncode == 0


def tree_equal(subject: str, commit: str, paths: list[str]) -> bool:
"""True when `paths` are identical at both commits; False on any error."""
return _git("diff", "--quiet", subject, commit, "--", *paths).returncode == 0


def candidates(tip: str, subject: str) -> list[str]:
"""First-parent commits from tip back to subject, newest first.

The subject is last. main() also checks that a commit descends from the
subject before it accepts that commit's run.
"""
out = subprocess.run(
["git", "rev-list", "--first-parent", tip, f"^{subject}"],
check=True,
capture_output=True,
text=True,
).stdout.split()
return [*out, subject]


def select_run(runs: list[dict], sha: str, repo: str) -> dict | None:
"""The newest successful push run on this repository's main for `sha`."""
green = [
Expand Down Expand Up @@ -106,9 +147,9 @@ def job_problems(jobs: list[dict], prefixes: list[str]) -> list[str]:
return problems


def _gh(path: str, key: str) -> list[dict]:
def _gh(path: str, key: str, paginate: bool = True) -> list[dict]:
out = subprocess.run(
["gh", "api", "--paginate", path, "--jq", f".{key}[]"],
["gh", "api", *(["--paginate"] if paginate else []), path, "--jq", f".{key}[]"],
check=True,
capture_output=True,
text=True,
Expand All @@ -131,31 +172,65 @@ def main(argv: list[str] | None = None) -> int:
if not sha:
print(f"No commit on main changes {args.package}.", file=sys.stderr)
return 1
runs = _gh(
f"repos/{repo}/actions/workflows/{workflow}/runs"
f"?head_sha={sha}&event=push&branch=main&per_page=50",
"workflow_runs",
shas = candidates(args.tip, sha)
base = f"repos/{repo}/actions/workflows/{workflow}/runs"
# S's own runs (any state, for the message), plus the recent green push
# runs on main, where a later commit's run would be.
own = _gh(
f"{base}?head_sha={sha}&event=push&branch=main&per_page=50", "workflow_runs"
)
recent = (
_gh(
f"{base}?event=push&branch=main&status=success&per_page={RECENT_RUNS}",
"workflow_runs",
paginate=False,
)
if len(shas) > 1
else []
)
run = select_run(runs, sha, repo)
runs = own + recent
run, problems_seen, looked_up = None, [], 0
for later in shas: # newest first; the subject is last
green = select_run(runs, later, repo)
if green is None:
continue
if later != sha and not is_ancestor(sha, later):
continue
if not tree_equal(sha, later, paths):
print(
f"Refusing to release {args.package}: its tree at {later[:12]} "
f"differs from {sha[:12]}, the newest commit that changed it.",
file=sys.stderr,
)
return 1
if later != sha:
# Bound the API calls: a package unchanged for weeks has many
# later green runs that skipped its jobs. The subject is always
# checked, as before.
if looked_up >= MAX_LATER_RUNS:
continue
looked_up += 1
problems = job_problems(
_gh(f"repos/{repo}/actions/runs/{green['id']}/jobs?per_page=100", "jobs"),
prefixes,
)
if not problems:
run, sha = green, later
break
problems_seen.append(
f"run {green['id']} for {later[:12]} passed but {'; '.join(problems)}"
)
if run is None:
states = sorted({f"{r.get('status')}/{r.get('conclusion')}" for r in runs}) or [
states = sorted({f"{r.get('status')}/{r.get('conclusion')}" for r in own}) or [
"none"
]
detail = f" ({'; '.join(problems_seen)})" if problems_seen else ""
print(
f"Refusing to release {args.package}: {sha[:12]}, the newest commit that "
f"changed it, has no successful {workflow} push run on main (runs: "
f"{', '.join(states)}). Wait for it, or re-run it until it passes.",
file=sys.stderr,
)
return 1
problems = job_problems(
_gh(f"repos/{repo}/actions/runs/{run['id']}/jobs?per_page=100", "jobs"),
prefixes,
)
if problems:
print(
f"Refusing to release {args.package}: run {run['id']} for {sha[:12]} "
f"passed but {'; '.join(problems)}.",
f"changed it, has no successful {workflow} push run on main with its "
f"jobs passed (its runs: {', '.join(states)}), and neither does any "
f"later commit with the same package tree{detail}. Wait for one, or "
"re-run it until it passes.",
file=sys.stderr,
)
return 1
Expand Down
19 changes: 14 additions & 5 deletions scripts/ci/post_merge_batching.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Post-merge batching: accept a later commit's green run (design only)

Status: proposal for the coordinator. Nothing here is implemented. The release
rule described under "Today" stays in force until a separate pull request
changes it.
# Post-merge batching: accept a later commit's green run

Status: A is implemented in `scripts/ci/newest_green.py`, so it applies to
release-package.yml, release-program-graph.yml and release-and-publish.yml.
`desktop-release.yml` still uses the rule under "Today". B and C are not
implemented.

A shipped without B on purpose. A merge-queue push that lands several merges
at once starts one push run, for its last commit, and that run's `scope`
diffs against the previous tip, so it already runs the jobs of every package
the batch changed. That is the case that stranded openadapt-flow 4.1.0 on
2026-12-01. A later run that skipped a package's jobs is still refused.
The script inspects at most five later commits' green runs, then the
subject's own runs, as before.

## Problem

Expand Down
3 changes: 2 additions & 1 deletion scripts/ci/release_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
every package and publishes a package when all of these hold:

- the newest commit on main's first-parent history that changed the package
has a successful push run with the package's own jobs run and passed
has a successful push run with the package's own jobs run and passed, or
a later first-parent commit with the identical package tree does
(scripts/ci/newest_green.py, the rule desktop-release.yml set);
- the version in its pyproject.toml is not on PyPI, is higher than every
version PyPI has, and has no tag yet;
Expand Down
Loading
Loading