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
37 changes: 30 additions & 7 deletions .github/workflows/open-maintenance-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+maintenance-push-retry.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retry transient GitHub server errors when publishing a validated maintenance branch.
26 changes: 22 additions & 4 deletions tests/open_maintenance_pr/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down