From 1573c71120617df36a0356aaa95987f6b71b5e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Di=20Remigio=20Eik=C3=A5s?= Date: Thu, 19 Nov 2026 10:41:01 +0100 Subject: [PATCH] =?UTF-8?q?fix(publish-release):=20=F0=9F=90=9B=20wait=20f?= =?UTF-8?q?or=20GitHub=20to=20list=20a=20draft=20created=20a=20moment=20ag?= =?UTF-8?q?o=20(#126)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit :robot: _AI text below_ :robot: ## Summary Closes #125 The Publish run of v1.6.2 failed: `publish-release` listed the releases about 0.6 s after `draft-release` created the draft, found none for the tag, and stopped. The same listing showed the draft minutes later, and it was published by hand. GitHub's list of releases lags behind a new draft for a moment; it may also still show a draft that `draft-release` just deleted (a re-run), whose id then fails to publish. - `publish-release` now tries up to five times, waiting 2, 4, 8 and 16 s in between (about 30 s), while the listing doesn't show exactly one draft of the tag or publishing the draft it found fails. It logs each retry and fails with the same error after the last attempt (`Found N draft releases of vX.Y.Z, expected exactly one. Run draft-release (again) first.`, or `Could not publish the draft release ...`). A tag that is already published still needs nothing. - Tests: the fake `gh` can answer each listing differently, fail the publishing of given ids, and `sleep` only records its argument. New cases: a listing that shows the draft at the third attempt (the v1.6.2 case), a stale id that fails to publish followed by the right one, and the existing failure cases now check the five listings and the waits. 5 of the publish tests fail against the old action. - Docs: the action's description and the re-run caveat in `docs/caveats.md`. Not verified against GitHub: how long the listing lags. 0.6 s was too short in the v1.6.2 run; 30 s is a guess with a wide margin. Adopters whose publish job runs minutes after `draft-release` (a package upload in between) weren't affected. ## Checklist - [ ] Bug fix for the current release: add the `backport` label (or ask a maintainer to) - [x] `just lint` and `just test` pass (ran the equivalents: ruff, ty, prek, full pytest 367 passed, 7 skipped; `just` isn't installed here) - [ ] docs/adopting.md, docs/organisation-setup.md, and docs/repository-setup.md updated if the stubs, inputs or setup changed - [ ] Breaking change or deprecation: the Migration section says what consumers must change (cherry picked from commit ccba48f0221418909a7b91ea8a982297acd2ae3f) --- actions/publish-release/action.yml | 57 +++++++++++++++--------- docs/caveats.md | 6 ++- tests/test_actions.py | 69 +++++++++++++++++++++++++----- 3 files changed, 99 insertions(+), 33 deletions(-) diff --git a/actions/publish-release/action.yml b/actions/publish-release/action.yml index fd9212f..bfba2a1 100644 --- a/actions/publish-release/action.yml +++ b/actions/publish-release/action.yml @@ -3,7 +3,8 @@ description: >- Publish the draft GitHub release of a v* tag. Run it last, after every upload (PyPI) has succeeded: with immutable releases, publishing locks the tag and the files and creates GitHub's release attestation. It publishes only if the tag has exactly one - draft, and does nothing if the tag is already published. Needs contents write permission. + draft, waiting about 30 s for GitHub to list a draft created a moment ago, and does + nothing if the tag is already published. Needs contents write permission. inputs: tag: @@ -32,24 +33,38 @@ runs: # Publish the draft by its id: `gh release edit "$tag"` picks one of several # drafts of a tag in an undocumented way, and could publish an incomplete one. - releases=$(gh api --paginate "repos/$GH_REPO/releases" \ - --jq '.[] | [.id, .tag_name, .draft] | @tsv') - drafts=() - published=false - while read -r id name draft; do - if [[ "$name" == "$tag" && "$draft" == true ]]; then - drafts+=("$id") - elif [[ "$name" == "$tag" ]]; then - published=true + # The listing lags behind draft-release for a moment: right after it, it may not + # show the new draft yet, or still show the draft it replaced. Try again with + # growing waits, about 30 s in all, before giving up. + for wait in 2 4 8 16 0; do + releases=$(gh api --paginate "repos/$GH_REPO/releases" \ + --jq '.[] | [.id, .tag_name, .draft] | @tsv') + drafts=() + published=false + while read -r id name draft; do + if [[ "$name" == "$tag" && "$draft" == true ]]; then + drafts+=("$id") + elif [[ "$name" == "$tag" ]]; then + published=true + fi + done <<< "$releases" + # A re-run after the release was published has nothing left to do. + if [[ ${#drafts[@]} -eq 0 && "$published" == true ]]; then + echo "The release $tag is already published." + exit 0 fi - done <<< "$releases" - # A re-run after the release was published has nothing left to do. - if [[ ${#drafts[@]} -eq 0 && "$published" == true ]]; then - echo "The release $tag is already published." - exit 0 - fi - if [[ ${#drafts[@]} -ne 1 ]]; then - echo "::error::Found ${#drafts[@]} draft releases of $tag, expected exactly one: run draft-release (again) first." - exit 1 - fi - gh api -X PATCH "repos/$GH_REPO/releases/${drafts[0]}" -F draft=false + if [[ ${#drafts[@]} -eq 1 ]]; then + if gh api -X PATCH "repos/$GH_REPO/releases/${drafts[0]}" -F draft=false; then + exit 0 + fi + problem="Could not publish the draft release ${drafts[0]} of $tag." + else + problem="Found ${#drafts[@]} draft releases of $tag, expected exactly one." + fi + if [[ $wait -gt 0 ]]; then + echo "$problem Trying again in ${wait}s." + sleep "$wait" + fi + done + echo "::error::$problem Run draft-release (again) first." + exit 1 diff --git a/docs/caveats.md b/docs/caveats.md index 9eca58b..5db47fa 100644 --- a/docs/caveats.md +++ b/docs/caveats.md @@ -92,8 +92,10 @@ its draft, and `publish-release` publishes only if the tag has exactly one draft, so it never publishes an incomplete one. A tag whose release is already published stops `draft-release` with an error, as published releases are immutable, and leaves - `publish-release` nothing to do. Whether GitHub accepts - a second draft for one tag is not verified; the actions don't depend on it. + `publish-release` nothing to do. Whether GitHub accepts a second draft for one tag is + not verified; the actions don't depend on it. GitHub's list of releases can miss a + draft created a moment ago, or still show one just deleted: `publish-release` tries + again for about 30 s before it fails, so it can run right after `draft-release`. - **`job.workflow_*` is GitHub.com-only** (not on GitHub Enterprise Server), and actionlint 1.7.12 doesn't know it yet: [`.github/actionlint.yaml`](https://github.com/Algorithmiq/release-tools/blob/main/.github/actionlint.yaml) ignores the two properties in `release.yml` and `backport.yml`. Consumers' stubs don't diff --git a/tests/test_actions.py b/tests/test_actions.py index b6f20a8..9278501 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -70,23 +70,33 @@ def bin_dir(tmp_path: Path) -> Path: "git-cliff is missing: run the tests with `just test`" ) assert shutil.which("jq"), "jq is missing: the fake gh runs the --jq of its callers" - # `gh api` of the releases answers the list in releases.json (empty by default), - # filtered by the --jq program like gh does; `gh api -X ...` only records its call. + # `gh api` of the releases answers the list in releases.json (empty by default), or + # in releases-N.json for the Nth listing if there is one, filtered by the --jq + # program like gh does. `gh api -X ...` only records its call, and fails for a + # release whose URL is a line of failing-urls. sleep only records its argument. stand_ins = { "gh": ( 'if [[ "$1 $2" == "api graphql" ]]; then exec "$FAKE_GH" "$@"; fi\n' 'if [[ "$1" == api ]]; then\n' f' echo "$*" >> "{tmp_path}/gh-api-calls"\n' - ' if [[ " $* " == *" -X "* ]]; then exit 0; fi\n' + ' if [[ " $* " == *" -X "* ]]; then\n' + f' ! grep -qxF "$4" "{tmp_path}/failing-urls" 2> /dev/null\n' + " exit\n" + " fi\n" + f' echo >> "{tmp_path}/listings"\n' + f' count=$(wc -l < "{tmp_path}/listings" | tr -d " ")\n' + f' listing="{tmp_path}/releases-$count.json"\n' + f' [[ -f "$listing" ]] || listing="{tmp_path}/releases.json"\n' " program=.\n" " while [[ $# -gt 0 ]]; do\n" ' if [[ "$1" == --jq ]]; then program="$2"; fi\n' " shift\n" " done\n" - f' exec jq -r "$program" "{tmp_path}/releases.json"\n' + ' exec jq -r "$program" "$listing"\n' "fi\n" f'printf "%s\\n" "$@" > "{tmp_path}/gh-args"' - ) + ), + "sleep": f'echo "$1" >> "{tmp_path}/sleeps"', } (tmp_path / "releases.json").write_text("[]", encoding="utf-8") for name, body in stand_ins.items(): @@ -389,9 +399,15 @@ def test_release_notes_refuse_other_refs(bin_dir: Path, tmp_path: Path) -> None: ) -def set_releases(tmp_path: Path, *releases: tuple[int, str, bool]) -> None: - """Make the fake gh list RELEASES, each (id, tag name, is a draft).""" - (tmp_path / "releases.json").write_text( +def set_releases( + tmp_path: Path, *releases: tuple[int, str, bool], listing: int = 0 +) -> None: + """Make the fake gh list RELEASES, each (id, tag name, is a draft). + + With LISTING, only its LISTINGth listing; the others list releases.json. + """ + name = f"releases-{listing}.json" if listing else "releases.json" + (tmp_path / name).write_text( json.dumps( [ {"id": id_, "tag_name": tag, "draft": draft, "name": tag} @@ -619,10 +635,43 @@ def test_publish_release_needs_exactly_one_draft( result = run_action("publish-release", publish_env(), bin_dir, tmp_path) assert result.returncode == 1 assert ( - f"::error::Found {count} draft releases of v2.0.0, expected exactly one: run" + f"::error::Found {count} draft releases of v2.0.0, expected exactly one. Run" " draft-release (again) first." in result.stdout ) - assert len(gh_api_calls(tmp_path)) == 1 + # The listing can lag behind draft-release: it is read five times, about 30 s. + assert len(gh_api_calls(tmp_path)) == 5 + assert (tmp_path / "sleeps").read_text().split() == ["2", "4", "8", "16"] + + +def test_publish_release_waits_for_the_listing_to_show_the_draft( + tmp_path: Path, bin_dir: Path +) -> None: + # The listing doesn't show a draft created a moment ago (the v1.6.2 release). + set_releases(tmp_path, (1, "v1.9.0", False)) + set_releases(tmp_path, (1, "v1.9.0", False), (2, "v2.0.0", True), listing=3) + result = run_action("publish-release", publish_env(), bin_dir, tmp_path) + assert result.returncode == 0, result.stdout + result.stderr + assert "Found 0 draft releases of v2.0.0, expected exactly one." in result.stdout + assert gh_api_calls(tmp_path)[3:] == [ + "api -X PATCH repos/x/y/releases/2 -F draft=false" + ] + assert (tmp_path / "sleeps").read_text().split() == ["2", "4"] + + +def test_publish_release_tries_again_when_the_listed_draft_is_gone( + tmp_path: Path, bin_dir: Path +) -> None: + # After draft-release replaced draft 1 with draft 2, the listing still shows 1. + set_releases(tmp_path, (2, "v2.0.0", True)) + set_releases(tmp_path, (1, "v2.0.0", True), listing=1) + (tmp_path / "failing-urls").write_text("repos/x/y/releases/1\n") + result = run_action("publish-release", publish_env(), bin_dir, tmp_path) + assert result.returncode == 0, result.stdout + result.stderr + assert "Could not publish the draft release 1 of v2.0.0." in result.stdout + assert [call for call in gh_api_calls(tmp_path) if "PATCH" in call] == [ + "api -X PATCH repos/x/y/releases/1 -F draft=false", + "api -X PATCH repos/x/y/releases/2 -F draft=false", + ] def test_publish_release_rerun_after_publishing_does_nothing(