From 0bde6b993e5695cbcc054291082669777ae1935b Mon Sep 17 00:00:00 2001 From: mignot Date: Wed, 25 Nov 2026 16:14:53 +0100 Subject: [PATCH 1/2] fix(release): decide direct merge of release PRs from the REST merge state Refs #80 Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 1 + README.md | 2 +- release/arm-release-pr-auto-merge.py | 26 ++++++++++--- release/test_arm_release_pr_auto_merge.py | 45 +++++++++++++++++++---- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a8dab4..970cd5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `release/arm-release-pr-auto-merge.py` decides whether to merge a release PR directly from its REST `mergeable_state` (`clean`, re-read while `unknown`) instead of matching the English text of GitHub's refusal; any other state, or a moved head, fails the run. - `coverage.yml` resolves report paths against the repository's tracked files (exact, `` roots, or a unique suffix) instead of matching only the changed files, so an ambiguous suffix outside the diff is no longer missed and one file reported under several prefixes counts once. A `.xml` file in a coverage artifact that is not valid Cobertura XML now fails the job. - The concurrency conformance check treats a workflow in which any job calls a release workflow as a release caller and expects the event form, so `examples/release-please-python.yml` (release jobs mixed with build and publish jobs) passes instead of being steered to a form that can lose a version bump. - `examples/pr-title.yml` declares the CI concurrency form and a `# Place at` header; a new CI step checks that every example passes the shipped conformance checks. diff --git a/README.md b/README.md index 68ee9a8..ee9895a 100644 --- a/README.md +++ b/README.md @@ -711,4 +711,4 @@ Release-please is the only way to prepare releases in an adopting repository: `. ### Auto-merging release PRs -With `auto-merge: true`, `release-please.yml` arms squash auto-merge on every open release PR (label `autorelease: pending`) through [`release/arm-release-pr-auto-merge.py`](release/arm-release-pr-auto-merge.py): the commit title is the PR title and the head commit is pinned, so a later push disarms it until the next run re-arms it. The release PR then merges as soon as the required checks pass, and the release is tagged and published **without human review**. Prerequisites in the adopting repository: `allow_auto_merge`, no required review, squash merges with `PR_TITLE` as the commit title, and the `pr-title` check required (all enforced by `github-config` for repositories with `release_pr: true`). Activate `auto-merge` only after those are in place. The PRs reported by `release-please-action` are armed as well, because the label listing can lag behind a PR opened seconds earlier. Only PRs from a `release-please--*` branch of the repository itself are armed, whatever their labels. A PR that GitHub refuses to arm because it is already clean is squash merged directly with the same title and the pinned head: the branch rules still apply, and no later run would arm it. \ No newline at end of file +With `auto-merge: true`, `release-please.yml` arms squash auto-merge on every open release PR (label `autorelease: pending`) through [`release/arm-release-pr-auto-merge.py`](release/arm-release-pr-auto-merge.py): the commit title is the PR title and the head commit is pinned, so a later push disarms it until the next run re-arms it. The release PR then merges as soon as the required checks pass, and the release is tagged and published **without human review**. Prerequisites in the adopting repository: `allow_auto_merge`, no required review, squash merges with `PR_TITLE` as the commit title, and the `pr-title` check required (all enforced by `github-config` for repositories with `release_pr: true`). Activate `auto-merge` only after those are in place. The PRs reported by `release-please-action` are armed as well, because the label listing can lag behind a PR opened seconds earlier. Only PRs from a `release-please--*` branch of the repository itself are armed, whatever their labels. A PR that GitHub refuses to arm and whose REST merge state (`mergeable_state`, re-read up to five times while it is `unknown`) is `clean` is squash merged directly with the same title and the pinned head: the branch rules still apply, and no later run would arm it. \ No newline at end of file diff --git a/release/arm-release-pr-auto-merge.py b/release/arm-release-pr-auto-merge.py index e85a8d7..434d86b 100644 --- a/release/arm-release-pr-auto-merge.py +++ b/release/arm-release-pr-auto-merge.py @@ -13,6 +13,7 @@ import runpy import subprocess import sys +import time from urllib.parse import quote DIRECTORY = Path(__file__).resolve().parent @@ -23,8 +24,10 @@ PENDING = 'autorelease: pending' PAGE_SIZE = 100 MAX_PAGES = 5 -# GitHub refuses to arm auto-merge when nothing is left to wait for; the PR is then merged directly. -CLEAN_STATUS = 'clean status' +# GitHub refuses to arm auto-merge when nothing is left to wait for; the PR is then merged directly, decided +# from the REST merge state (computed in the background, so `unknown` is read again a bounded number of times). +STATE_READS = 5 +STATE_WAIT = 2 MUTATION = ( 'mutation($id: ID!, $headline: String!, $oid: GitObjectID!) {' ' enablePullRequestAutoMerge(input: {pullRequestId: $id, mergeMethod: SQUASH,' @@ -72,7 +75,18 @@ def release_branch(pull, repository): return head_repo == repository and pull['head']['ref'].startswith(HEAD_PREFIX) -def arm(api, token, repository, run=graphql, reported=''): +def merge_state(api, number, head, sleep): + for read in range(STATE_READS): + pull = api.request('GET', f'/pulls/{number}') + if pull['head']['sha'] != head: + raise ValueError(f'PR #{number} head moved from {head} to {pull["head"]["sha"]} while arming') + state = pull.get('mergeable_state') + if state != 'unknown' or read == STATE_READS - 1: + return state + sleep(STATE_WAIT) + + +def arm(api, token, repository, run=graphql, reported='', sleep=time.sleep): # The issue listing can lag behind a PR release-please has just opened and labelled, so the PRs the # action reports are armed too; they are checked strictly because the listing's label filter is absent. provided = reported_numbers(reported) @@ -91,7 +105,8 @@ def arm(api, token, repository, run=graphql, reported=''): raise ValueError(f'PR #{number} has no usable title or head') result = run(MUTATION, {'id': pull['node_id'], 'headline': title, 'oid': head}, token) if result.returncode != 0: - if CLEAN_STATUS in (result.stderr + result.stdout).lower(): + state = merge_state(api, number, head, sleep) + if state == 'clean': # Nothing left to wait for; the sha pin and the branch rules still gate this merge, and # no later run would arm it. logging.info('PR #%s is already clean: merging it directly', number) @@ -99,7 +114,8 @@ def arm(api, token, repository, run=graphql, reported=''): {'merge_method': 'squash', 'commit_title': title, 'commit_message': '', 'sha': head}) armed.append(number) continue - raise ValueError(f'Cannot arm auto-merge on PR #{number}: {result.stderr.strip() or result.stdout.strip()}') + raise ValueError(f'Cannot arm auto-merge on PR #{number} (merge state {state}): ' + f'{result.stderr.strip() or result.stdout.strip()}') armed.append(number) return armed diff --git a/release/test_arm_release_pr_auto_merge.py b/release/test_arm_release_pr_auto_merge.py index 3d84a08..b2a90e6 100644 --- a/release/test_arm_release_pr_auto_merge.py +++ b/release/test_arm_release_pr_auto_merge.py @@ -33,7 +33,10 @@ def request(self, method, path, data=None): page = int(path.rsplit('page=', 1)[1]) start = (page - 1) * module.PAGE_SIZE return [{'number': n, 'pull_request': {}} for n in self.issues[start:start + module.PAGE_SIZE]] - return self.pulls[int(path.rsplit('/', 1)[1])] + found = self.pulls[int(path.rsplit('/', 1)[1])] + if isinstance(found, list): + return found.pop(0) if len(found) > 1 else found[0] + return found class Runner: @@ -66,13 +69,40 @@ def test_closed_and_other_branch_prs_are_ignored(self): self.assertEqual(module.arm(FakeAPI(pulls={7: pull(**changes)}), 'token', REPO, run), []) self.assertEqual(run.calls, []) - def test_clean_status_merges_directly_with_pinned_head(self): - api = FakeAPI() - run = Runner(1, 'Pull request is in clean status') - self.assertEqual(module.arm(api, 'token', REPO, run), [7]) + def test_clean_merge_state_merges_directly_whatever_the_error_text(self): + api = FakeAPI(pulls={7: [pull(), pull(mergeable_state='clean')]}) + self.assertEqual(module.arm(api, 'token', REPO, Runner(1, 'Pull request est propre')), [7]) self.assertEqual(api.writes, [('PUT', '/pulls/7/merge', { 'merge_method': 'squash', 'commit_title': 'chore: release main', 'commit_message': '', 'sha': HEAD})]) + def test_unknown_then_clean_merge_state_retries(self): + api = FakeAPI(pulls={7: [pull(), pull(mergeable_state='unknown'), pull(mergeable_state='clean')]}) + sleeps = [] + self.assertEqual(module.arm(api, 'token', REPO, Runner(1, 'refused'), sleep=sleeps.append), [7]) + self.assertEqual(sleeps, [module.STATE_WAIT]) + self.assertEqual(len(api.writes), 1) + + def test_blocked_merge_state_fails_closed(self): + api = FakeAPI(pulls={7: [pull(), pull(mergeable_state='blocked')]}) + with self.assertRaisesRegex(ValueError, 'merge state blocked'): + module.arm(api, 'token', REPO, Runner(1, 'Pull request is in clean status')) + self.assertEqual(api.writes, []) + + def test_unknown_merge_state_fails_after_bounded_reads(self): + api = FakeAPI(pulls={7: [pull(), pull(mergeable_state='unknown')]}) + sleeps = [] + with self.assertRaisesRegex(ValueError, 'merge state unknown'): + module.arm(api, 'token', REPO, Runner(1, 'refused'), sleep=sleeps.append) + self.assertEqual(len(sleeps), module.STATE_READS - 1) + self.assertEqual(api.writes, []) + + def test_head_moved_fails(self): + moved = pull(mergeable_state='clean', head={'sha': 'd' * 40, 'ref': 'release-please--branches--main', 'repo': {'full_name': REPO}}) + api = FakeAPI(pulls={7: [pull(), moved]}) + with self.assertRaisesRegex(ValueError, 'head moved'): + module.arm(api, 'token', REPO, Runner(1, 'refused')) + self.assertEqual(api.writes, []) + def test_labelled_prs_not_from_release_please_are_ignored(self): for head in ({'sha': HEAD, 'ref': 'feature/x', 'repo': {'full_name': REPO}}, {'sha': HEAD, 'ref': 'release-please--branches--main', 'repo': {'full_name': 'fork/repo'}}, @@ -83,8 +113,9 @@ def test_labelled_prs_not_from_release_please_are_ignored(self): self.assertEqual(run.calls, []) def test_other_failures_fail_closed(self): - with self.assertRaisesRegex(ValueError, 'Cannot arm auto-merge on PR #7: boom'): - module.arm(FakeAPI(), 'token', REPO, Runner(1, 'boom')) + api = FakeAPI(pulls={7: [pull(), pull(mergeable_state='blocked')]}) + with self.assertRaisesRegex(ValueError, r'Cannot arm auto-merge on PR #7 \(merge state blocked\): boom'): + module.arm(api, 'token', REPO, Runner(1, 'boom')) def test_unusable_title_or_head_fails(self): for changes in ({'title': ' '}, {'head': {'sha': 'abc', 'ref': 'release-please--branches--main', 'repo': {'full_name': REPO}}}): From ca92b8ac42c5576dbd638b8e34dc7d4bca3c10af Mon Sep 17 00:00:00 2001 From: mignot Date: Wed, 25 Nov 2026 16:15:41 +0100 Subject: [PATCH 2/2] fix(release): log the arming refusal when merging a clean PR directly Co-Authored-By: Claude Sonnet 5.5 --- release/arm-release-pr-auto-merge.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/release/arm-release-pr-auto-merge.py b/release/arm-release-pr-auto-merge.py index 434d86b..cf75d9d 100644 --- a/release/arm-release-pr-auto-merge.py +++ b/release/arm-release-pr-auto-merge.py @@ -109,7 +109,8 @@ def arm(api, token, repository, run=graphql, reported='', sleep=time.sleep): if state == 'clean': # Nothing left to wait for; the sha pin and the branch rules still gate this merge, and # no later run would arm it. - logging.info('PR #%s is already clean: merging it directly', number) + logging.info('PR #%s is clean (arming refused: %s): merging it directly', number, + result.stderr.strip() or result.stdout.strip()) api.request('PUT', f'/pulls/{number}/merge', {'merge_method': 'squash', 'commit_title': title, 'commit_message': '', 'sha': head}) armed.append(number)