diff --git a/.github/workflows/open-maintenance-pr.yaml b/.github/workflows/open-maintenance-pr.yaml index 329a32e..43df743 100644 --- a/.github/workflows/open-maintenance-pr.yaml +++ b/.github/workflows/open-maintenance-pr.yaml @@ -208,13 +208,36 @@ jobs: run: | set -euo pipefail gh auth setup-git --hostname github.com --force - base_ref=$(git ls-remote --exit-code origin "refs/heads/$BASE") - if [[ "${base_ref%%$'\t'*}" != "$(git rev-parse HEAD^)" ]]; then - echo "::error::The target branch advanced after patch validation" >&2 - exit 1 - fi - echo "Pushing $BRANCH, replacing any earlier maintenance commit (force with lease)" - git push --force-with-lease origin "HEAD:refs/heads/$BRANCH" + parent=$(git rev-parse HEAD^) + commit=$(git rev-parse HEAD) + branch_ref="refs/heads/$BRANCH" + lease=$(git rev-parse --verify --quiet "refs/remotes/origin/$BRANCH" || true) + for delay in 1 2 4 0; do + base_ref=$(git ls-remote --exit-code origin "refs/heads/$BASE") + if [[ "${base_ref%%$'\t'*}" != "$parent" ]]; then + echo "::error::The target branch advanced after patch validation" >&2 + exit 1 + fi + echo "Pushing $BRANCH, replacing any earlier maintenance commit (force with lease)" + if output=$(git push --force-with-lease="$branch_ref:$lease" origin "HEAD:$branch_ref" 2>&1); then + printf '%s\n' "$output" + exit 0 + else + status=$? + fi + printf '%s\n' "$output" >&2 + if ! grep -Eq '^remote: Internal Server Error[[:space:]]*$|HTTP (500|502|503|504)([[:space:];]|$)' <<<"$output"; then + exit "$status" + fi + if remote_branch=$(git ls-remote origin "$branch_ref") && [[ "${remote_branch%%$'\t'*}" == "$commit" ]]; then + exit 0 + fi + if ((delay == 0)); then + exit "$status" + fi + echo "GitHub returned a server error. Retrying in ${delay}s." + sleep "$delay" + done - name: 📝 Write maintenance pull request body env: BODY: ${{ inputs.body }} diff --git a/changelog.d/+maintenance-push-retry.bugfix.md b/changelog.d/+maintenance-push-retry.bugfix.md new file mode 100644 index 0000000..54e5abe --- /dev/null +++ b/changelog.d/+maintenance-push-retry.bugfix.md @@ -0,0 +1 @@ +Retry transient GitHub server errors when publishing a validated maintenance branch. diff --git a/tests/open_maintenance_pr/test_open.py b/tests/open_maintenance_pr/test_open.py index ead1f4b..83261ac 100644 --- a/tests/open_maintenance_pr/test_open.py +++ b/tests/open_maintenance_pr/test_open.py @@ -206,16 +206,30 @@ def test_validate_selects_the_current_target( @pytest.mark.parametrize( - ("target", "returncode", "diagnostic", "published"), + ("target", "returncode", "diagnostic", "published", "rejection"), [ pytest.param( - "HEAD^", 0, "Pushing automation/maintenance", "{commit}\trefs/heads/automation/maintenance\n", id="current" + "HEAD^", + 0, + "Pushing automation/maintenance", + "{commit}\trefs/heads/automation/maintenance\n", + "", + id="current", ), - pytest.param("HEAD", 1, "target branch advanced", "", id="advanced"), + pytest.param("HEAD", 1, "target branch advanced", "", "", id="advanced"), + pytest.param( + "HEAD^", + 0, + "Retrying in 1s", + "{commit}\trefs/heads/automation/maintenance\n", + "Internal Server Error", + id="server-error", + ), + pytest.param("HEAD^", 1, "Permission denied", "", "Permission denied", id="permission-denied"), ], ) def test_push_requires_the_validated_target( - repository: Path, tmp_path: Path, target: str, returncode: int, diagnostic: str, published: str + repository: Path, tmp_path: Path, target: str, returncode: int, diagnostic: str, published: str, rejection: str ) -> None: remote = tmp_path / "remote.git" run(repository, "git", "clone", "--bare", ".", str(remote)) @@ -224,6 +238,10 @@ def test_push_requires_the_validated_target( (repository / "pylock.toml").write_text("lock-version = '2.0'\n") run(repository, "git", "commit", "-am", "Update lock") run(repository, "git", "push", "origin", f"{target}:refs/heads/main") + if rejection: + hook = remote / "hooks" / "pre-receive" + hook.write_text(f"#!/bin/sh\nif [ ! -e tried ]; then\n touch tried\n echo '{rejection}' >&2\n exit 1\nfi\n") + hook.chmod(stat.S_IRWXU) gh = tmp_path / "gh" gh.write_text("#!/bin/sh\nexit 0\n") gh.chmod(stat.S_IRWXU)