From 954a691b1533e02cfad01959ac768dd276d8598b Mon Sep 17 00:00:00 2001 From: Rohan Devasthale Date: Fri, 20 Nov 2026 11:58:08 +0530 Subject: [PATCH] ci(mergify): merge approved PRs through a merge queue Branch protection requires PRs to be up to date with `main`, but the `merge` action never updates a branch, so approved PRs sit unmerged. Use a merge queue that checks one PR at a time on the PR itself: Mergify rebases it on `main`, waits for CI, then merges. Checks live only in `queue_conditions`, and `max_parallel_checks` is 1. Otherwise Mergify tests copies of PRs on branches in this repository. Fork PRs that change workflows stay out of the queue. Those, and forks without maintainer edits, are merged by hand. Docs-only PRs never get unit/e2e checks because `test.yaml` uses `paths-ignore`. Let them merge with pre-commit, pkglint and readthedocs. Run the publish workflow only for release tags (`*.*.*`), so code from other branches never runs in a job that can get a PyPI token. `e2e/mergify_lint.py` now finds the full CI and docs-only groups by their shape, reads e2e checks only from the full CI group, and fails if any appear elsewhere. It also accepts spaces around `=`. Co-Authored-By: Claude Closes: #1474 Signed-off-by: Rohan Devasthale --- .github/workflows/python-publish.yaml | 3 +- .mergify.yml | 117 ++++++++++++++++---------- e2e/mergify_lint.py | 101 +++++++++++++++++++--- 3 files changed, 162 insertions(+), 59 deletions(-) diff --git a/.github/workflows/python-publish.yaml b/.github/workflows/python-publish.yaml index 23866e8f..8b7806c9 100644 --- a/.github/workflows/python-publish.yaml +++ b/.github/workflows/python-publish.yaml @@ -4,7 +4,8 @@ name: Upload Python Package on: - - push + push: + tags: ["*.*.*"] permissions: contents: read diff --git a/.mergify.yml b/.mergify.yml index da9af3c4..960cf6cc 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -1,3 +1,72 @@ +# Check one PR at a time, on the PR itself. Mergify then updates the PR +# with main and merges it once CI passes, which the "branches must be up +# to date" rule requires. Do not raise this: with more than one parallel +# check, Mergify tests copies of PRs on branches in this repository instead. +merge_queue: + max_parallel_checks: 1 + +queue_rules: + - name: default + update_method: rebase + merge_method: merge + # No merge_conditions: Mergify then reuses queue_conditions, which keeps + # the checks on the PR itself. + queue_conditions: + - "-draft" + # At least 1 reviewer from maintainers + - "#approved-reviews-by>=1" + # Fork PRs that change workflows are merged by hand. So are fork PRs + # without "Allow edits by maintainers", since Mergify can't update + # them. Merge those only after approval and the checks below pass. + - or: + - head-repo-full-name=python-wheel-build/fromager + - "-files~=^\\.github/workflows/" + - or: + - and: + # Unit tests + - check-success=unit (3.12, 1.75, ubuntu-latest) + - check-success=unit (3.13, 1.75, ubuntu-latest) + - check-success=unit (3.14, 1.75, ubuntu-latest) + - check-success=unit (3.14, 1.75, macos-latest) + + # checks (see check.yaml) + - check-success=pre-commit + - check-success=pkglint + - check-success=readthedocs + + # E2E test suites (consolidated from individual tests) + # Python 3.12 - Ubuntu only (macOS excluded) + - check-success=e2e (3.12, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) + - check-success=e2e (3.12, 1.75, ci_bootstrap_suite, ubuntu-latest) + - check-success=e2e (3.12, 1.75, ci_build_suite, ubuntu-latest) + - check-success=e2e (3.12, 1.75, ci_config_suite, ubuntu-latest) + - check-success=e2e (3.12, 1.75, ci_specialized_suite, ubuntu-latest) + - check-success=e2e (3.12, 1.75, ci_workflow_suite, ubuntu-latest) + # Python 3.13 - Ubuntu only (macOS excluded) + - check-success=e2e (3.13, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) + - check-success=e2e (3.13, 1.75, ci_bootstrap_suite, ubuntu-latest) + - check-success=e2e (3.13, 1.75, ci_build_suite, ubuntu-latest) + - check-success=e2e (3.13, 1.75, ci_config_suite, ubuntu-latest) + - check-success=e2e (3.13, 1.75, ci_specialized_suite, ubuntu-latest) + - check-success=e2e (3.13, 1.75, ci_workflow_suite, ubuntu-latest) + # Python 3.14 - Ubuntu only (macOS excluded) + - check-success=e2e (3.14, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) + - check-success=e2e (3.14, 1.75, ci_bootstrap_suite, ubuntu-latest) + - check-success=e2e (3.14, 1.75, ci_build_suite, ubuntu-latest) + - check-success=e2e (3.14, 1.75, ci_config_suite, ubuntu-latest) + - check-success=e2e (3.14, 1.75, ci_specialized_suite, ubuntu-latest) + - check-success=e2e (3.14, 1.75, ci_workflow_suite, ubuntu-latest) + + # Docs-only PRs: test.yaml skips these paths (paths-ignore), so unit + # and e2e checks never run. Keep this in sync with paths-ignore. + # True when every changed file is under docs/, a top-level *.md or + # *.rst file, or LICENSE. + - and: + - "-files~=^(?!docs/)(?![^/]+\\.(md|rst)$)(?!LICENSE$)" + - check-success=pre-commit + - check-success=pkglint + - check-success=readthedocs + pull_request_rules: - name: Add CI label conditions: @@ -24,51 +93,11 @@ pull_request_rules: add: - mergify - name: Automatic merge on approval - conditions: - - and: - - "-draft" - - # Unit tests - - check-success=unit (3.12, 1.75, ubuntu-latest) - - check-success=unit (3.13, 1.75, ubuntu-latest) - - check-success=unit (3.14, 1.75, ubuntu-latest) - - check-success=unit (3.14, 1.75, macos-latest) - - # checks (see check.yaml) - - check-success=pre-commit - - check-success=pkglint - - check-success=readthedocs - - # E2E test suites (consolidated from individual tests) - # Python 3.12 - Ubuntu only (macOS excluded) - - check-success=e2e (3.12, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) - - check-success=e2e (3.12, 1.75, ci_bootstrap_suite, ubuntu-latest) - - check-success=e2e (3.12, 1.75, ci_build_suite, ubuntu-latest) - - check-success=e2e (3.12, 1.75, ci_config_suite, ubuntu-latest) - - check-success=e2e (3.12, 1.75, ci_specialized_suite, ubuntu-latest) - - check-success=e2e (3.12, 1.75, ci_workflow_suite, ubuntu-latest) - # Python 3.13 - Ubuntu only (macOS excluded) - - check-success=e2e (3.13, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) - - check-success=e2e (3.13, 1.75, ci_bootstrap_suite, ubuntu-latest) - - check-success=e2e (3.13, 1.75, ci_build_suite, ubuntu-latest) - - check-success=e2e (3.13, 1.75, ci_config_suite, ubuntu-latest) - - check-success=e2e (3.13, 1.75, ci_specialized_suite, ubuntu-latest) - - check-success=e2e (3.13, 1.75, ci_workflow_suite, ubuntu-latest) - # Python 3.14 - Ubuntu only (macOS excluded) - - check-success=e2e (3.14, 1.75, ci_bootstrap_parallel_suite, ubuntu-latest) - - check-success=e2e (3.14, 1.75, ci_bootstrap_suite, ubuntu-latest) - - check-success=e2e (3.14, 1.75, ci_build_suite, ubuntu-latest) - - check-success=e2e (3.14, 1.75, ci_config_suite, ubuntu-latest) - - check-success=e2e (3.14, 1.75, ci_specialized_suite, ubuntu-latest) - - check-success=e2e (3.14, 1.75, ci_workflow_suite, ubuntu-latest) - - # At least 1 reviewer from maintainers - - and: - - "#approved-reviews-by>=1" - + # Approval and CI requirements are in queue_rules above. + conditions: [] actions: - merge: - method: merge + queue: + name: default - name: Ping author on conflicts conditions: diff --git a/e2e/mergify_lint.py b/e2e/mergify_lint.py index dcd50414..d4f70731 100644 --- a/e2e/mergify_lint.py +++ b/e2e/mergify_lint.py @@ -4,6 +4,7 @@ import pathlib import re import sys +import typing import yaml from packaging.version import Version @@ -11,20 +12,92 @@ # Parse the mergify settings to find the rules that are in place. mergify_settings_file = pathlib.Path(".mergify.yml") mergify_settings = yaml.safe_load(mergify_settings_file.read_text(encoding="utf8")) +# Matches 'check-success=e2e (3.12, 1.75, ci_build_suite, ubuntu-latest)', +# with or without spaces around '=', and captures the part in parentheses. +E2E_CHECK = re.compile(r"^check-success\s*=\s*e2e\s*(\(.*\))$") +# The docs-only group is the one that limits which files changed. +DOCS_ONLY_MARKER = re.compile(r"^-files\s*~=") + + +def e2e_job(condition: str) -> str | None: + """Return the e2e job parameters for an e2e check condition, else None.""" + match = E2E_CHECK.match(condition.strip()) + return match.group(1) if match else None + + +def iter_conditions(conditions: list[typing.Any]) -> typing.Iterator[str]: + """Yield every string condition, descending into and/or/not groups.""" + for rule in conditions: + if isinstance(rule, str): + yield rule + elif isinstance(rule, dict): + for nested in rule.values(): + yield from iter_conditions(nested) + + +def find_ci_checks(conditions: list[typing.Any]) -> list[str]: + """Return the checks of the full CI group in the queue conditions. + + The queue conditions must have one ``or`` with two ``and`` groups: a + docs-only group (it has a ``-files~=`` condition) and the full CI group. + E2e checks must only appear in the full CI group, as plain strings. + """ + candidates: list[tuple[list[str], list[typing.Any]]] = [] + for rule in conditions: + if not (isinstance(rule, dict) and list(rule) == ["or"]): + continue + groups = rule["or"] + if len(groups) != 2 or not all( + isinstance(g, dict) and list(g) == ["and"] for g in groups + ): + continue + docs = [ + g["and"] + for g in groups + if any( + isinstance(c, str) and DOCS_ONLY_MARKER.match(c.strip()) + for c in g["and"] + ) + ] + if len(docs) == 1: + ci = [g["and"] for g in groups if g["and"] is not docs[0]] + candidates.append((ci[0], docs[0])) + if len(candidates) != 1: + raise ValueError( + f"expected one 'or' with a full CI group and a docs-only group in " + f"{mergify_settings_file}, found {len(candidates)}" + ) + ci_checks, docs_checks = candidates[0] + if not all(isinstance(c, str) for c in ci_checks): + raise ValueError( + f"the CI group in {mergify_settings_file} must only contain plain checks" + ) + if any(e2e_job(c) for c in iter_conditions(docs_checks)): + raise ValueError( + f"the docs-only group in {mergify_settings_file} must not require e2e checks" + ) + total = sum(1 for c in iter_conditions(conditions) if e2e_job(c)) + found = sum(1 for c in ci_checks if e2e_job(c)) + if total != found: + raise ValueError( + f"e2e checks found outside the CI group in {mergify_settings_file}" + ) + return ci_checks + + +queue_rule = next( + item for item in mergify_settings["queue_rules"] if item["name"] == "default" +) +if "merge_conditions" in queue_rule: + raise ValueError( + f"{mergify_settings_file}: keep checks in queue_conditions only, " + "so Mergify checks PRs in place" + ) existing_jobs = set() -for item in mergify_settings["pull_request_rules"]: - if item["name"] == "Automatic merge on approval": - conditions = item["conditions"][0]["and"] - # Look for 'check-success=e2e (something, something, something, something)' - for rule in conditions: - if not isinstance(rule, str): - continue - if not rule.startswith("check-success=e2e"): - continue - parameters = rule.partition(" ")[-1] - existing_jobs.add(parameters) - if not existing_jobs: - raise ValueError(f"Could not find e2e jobs in {mergify_settings_file}") +for rule in find_ci_checks(queue_rule["queue_conditions"]): + job = e2e_job(rule) + if job: + existing_jobs.add(job) print("existing jobs:\n ", "\n ".join(str(j) for j in sorted(existing_jobs)), sep="") # Parse the github actions file to find the test jobs that are defined. @@ -116,7 +189,7 @@ if RC: print(f"\njobs list to paste into {mergify_settings_file}:\n") for job_name in sorted(expected_jobs): - print(f" - check-success=e2e {job_name}") + print(f" - check-success=e2e {job_name}") print() sys.exit(RC)