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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, `<source>` 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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
29 changes: 23 additions & 6 deletions release/arm-release-pr-auto-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import runpy
import subprocess
import sys
import time
from urllib.parse import quote

DIRECTORY = Path(__file__).resolve().parent
Expand All @@ -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,'
Expand Down Expand Up @@ -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)
Expand All @@ -91,15 +105,18 @@ 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)
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)
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

Expand Down
45 changes: 38 additions & 7 deletions release/test_arm_release_pr_auto_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'}},
Expand All @@ -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}}}):
Expand Down