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
121 changes: 121 additions & 0 deletions .github/scripts/test_auto_merge_freeze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env python3
"""Tests for the auto-merge workflow's freeze gate.

Run with: python3 -m unittest discover -s .github/scripts
"""

import os
from pathlib import Path
import subprocess
import tempfile
import textwrap
import unittest


WORKFLOW = Path(__file__).resolve().parents[1] / 'workflows/auto-merge-openapi-updates.yml'
FREEZE_STEP = textwrap.dedent(
WORKFLOW.read_text()
.split(' id: freeze\n', 1)[1]
.split(' run: |\n', 1)[1]
.split('\n - name:', 1)[0]
)

# Model a candidate with a new GHES directory so the old inference would freeze.
GH_STUB = r'''
gh() {
printf '%s\n' "$*" >>"$GH_CALLS"
case "$1 $2" in
'label list')
printf '%s\n' "$FREEZE_LABELS"
if [ "$FAIL_READ" = labels ]; then
echo 'Could not read labels (HTTP 503)' >&2
return 1
fi
;;
'issue list')
printf '%s\n' "$FREEZE_ISSUES"
if [ "$FAIL_READ" = issues ]; then
echo 'Could not read issues (HTTP 503)' >&2
return 1
fi
;;
api\ *)
printf '%s\n' ghes-3.98
if [[ "$2" != *'?ref=main' ]]; then
printf '%s\n' ghes-3.99
fi
;;
*)
echo "Unexpected gh command: $*" >&2
return 99
;;
esac
}
'''


class MergeFreezeTest(unittest.TestCase):
def run_freeze(self, labels='merge-freeze', issues='', fail_read=''):
with tempfile.TemporaryDirectory() as directory:
output = Path(directory) / 'output'
calls = Path(directory) / 'calls'
output.touch()
calls.touch()
result = subprocess.run(
['bash', '-c', GH_STUB + FREEZE_STEP],
cwd=directory,
env={
**os.environ,
'GH_REPO': 'test/repo',
'GH_CALLS': str(calls),
'GITHUB_OUTPUT': str(output),
'FREEZE_LABELS': labels,
'FREEZE_ISSUES': issues,
'FAIL_READ': fail_read,
'DEFAULT_BRANCH': 'main',
'SHA_30': 'a' * 40,
'SHA_31': 'b' * 40,
},
capture_output=True,
text=True,
)
outputs = dict(line.split('=', 1) for line in output.read_text().splitlines())
return result, outputs, calls.read_text()

def test_open_freeze_issue_holds_merges(self):
result, outputs, calls = self.run_freeze(issues='#123 Release freeze')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(outputs['status'], 'frozen')
self.assertIn('#123 Release freeze', outputs['detail'])
self.assertIn('issue list --label merge-freeze --state open', calls)

def test_no_open_freeze_issue_allows_new_ghes_directory(self):
result, outputs, calls = self.run_freeze()
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(outputs['status'], 'clear')
self.assertNotIn('/contents/', calls)

def test_missing_freeze_label_allows_progress(self):
result, outputs, calls = self.run_freeze(labels='')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(outputs['status'], 'clear')
self.assertNotIn('issue list', calls)

def test_label_read_failure_stops_the_step(self):
result, outputs, calls = self.run_freeze(fail_read='labels')
self.assertNotEqual(result.returncode, 0)
self.assertNotIn('status', outputs)
self.assertIn('Could not read labels', result.stderr)
self.assertNotIn('issue list', calls)

def test_issue_read_failure_stops_the_step_even_with_partial_output(self):
result, outputs, _ = self.run_freeze(
issues='#123 Release freeze', fail_read='issues',
)
self.assertNotEqual(result.returncode, 0)
self.assertNotIn('status', outputs)
self.assertIn('Could not read issues', result.stderr)


if __name__ == '__main__':
unittest.main()
66 changes: 9 additions & 57 deletions .github/workflows/auto-merge-openapi-updates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ name: Auto-merge OpenAPI description updates
# gate that has already run against the authoritative source.
# * Every decision is made by deterministic code, so the same inputs always
# produce the same merge/no-merge answer and it can be tested.
# * Merges are held during a GHES release-candidate window. See the "Check
# for an active merge freeze" step.
# * Merges are held while an open issue carries the `merge-freeze` label.
# See the "Check for an active merge freeze" step.
#
# Scripts are run from a checkout of this repository's default branch, never
# from the candidate pull request.
Expand Down Expand Up @@ -162,25 +162,21 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
SHA_30: ${{ steps.select.outputs.sha_30 }}
SHA_31: ${{ steps.select.outputs.sha_31 }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
set -euo pipefail

# Around a GHES release candidate, hold description merges until the
# coordinated release content is ready. A new GHES version appearing
# in the candidate before it exists on the default branch is the
# signal that this window may be active.
# An open merge-freeze-labeled issue is the source of truth for a
# hold. Closing it lets candidates introduce new GHES directories;
# inferring a freeze from those directories would block the very
# merge needed to publish them.

reason=''

# --- Signal 1: an explicit, human-controlled hold ----------------
# A label is the escape hatch that does not depend on inference, and
# lets a human stop merges for any reason at all.
# Failed label or issue reads stop the step under `set -euo pipefail`
# rather than treating an unknown freeze state as clear.
# Output is captured before matching: under `pipefail`, an early
# exiting `grep -q` could otherwise make the producer look failed
# and silently drop this signal.
# and misread the freeze state.
labels=$(gh label list --search 'merge-freeze' --json name -q '.[].name')
if grep -qxF 'merge-freeze' <<<"$labels"; then
frozen=$(gh issue list --label 'merge-freeze' --state open --json number,title \
Expand All @@ -190,50 +186,6 @@ jobs:
fi
fi

# --- Signal 2: a GHES version not yet on the default branch ------
# Contents are read at the pinned commit, not at a branch name.
# These reads fail closed: a transient API error that looked like
# "no GHES directories" would silently drop the signal, so only a
# genuine 404 (the directory does not exist at that ref) becomes an
# empty listing. Anything else holds the merge.
errors=$(mktemp)

ghes_dirs() {
local ref="$1" dir="$2" listing
if listing=$(gh api "repos/$GH_REPO/contents/$dir?ref=$ref" -q '.[].name' 2>"$errors"); then
grep '^ghes-' <<<"$listing" | sort -u || true
return 0
fi
grep -q 'HTTP 404' "$errors" && return 0
cat "$errors" >&2
return 1
}

hold_unverifiable() {
echo "::error::Could not read $1 at $2 to check for a GHES release window; holding."
echo "status=frozen" >>"$GITHUB_OUTPUT"
echo "detail=Could not verify the GHES release state for $1 at $2" >>"$GITHUB_OUTPUT"
exit 0
}

if ! ghes_on_default=$(ghes_dirs "$DEFAULT_BRANCH" descriptions); then
hold_unverifiable descriptions "$DEFAULT_BRANCH"
fi

for sha in "$SHA_30" "$SHA_31"; do
[ -n "$sha" ] || continue
for dir in descriptions descriptions-next; do
if ! on_pr=$(ghes_dirs "$sha" "$dir"); then
hold_unverifiable "$dir" "${sha:0:7}"
fi
[ -n "$on_pr" ] || continue
new=$(comm -13 <(echo "$ghes_on_default") <(echo "$on_pr") | tr '\n' ' ')
if [ -n "${new// /}" ]; then
reason+="${sha:0:7}/$dir introduces new GHES version(s): $new "
fi
done
done

if [ -n "$reason" ]; then
echo "status=frozen" >>"$GITHUB_OUTPUT"
echo "detail=$reason" >>"$GITHUB_OUTPUT"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/scripts-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ on:
push:
paths:
- '.github/scripts/**'
- '.github/workflows/auto-merge-openapi-updates.yml'
- '.github/workflows/scripts-tests.yml'
pull_request:
paths:
- '.github/scripts/**'
- '.github/workflows/auto-merge-openapi-updates.yml'
- '.github/workflows/scripts-tests.yml'
workflow_dispatch:

Expand Down
Loading