From e283c08496c75b046399c7068f895e4a63ec168d Mon Sep 17 00:00:00 2001 From: Oliver Lin Date: Tue, 1 Dec 2026 22:04:59 +0800 Subject: [PATCH] fix: upload release assets while the release is still a draft MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The log repository has immutable releases enabled, so GitHub rejects asset uploads to a release that is already published: curl: (22) The requested URL returned error: 422 The release was created already published and the assets were uploaded afterwards, so the run failed at the upload step while leaving a published release with zero assets behind. Uploads now happen in the only order that works: create as a draft, attach every asset, then publish last. The draft reuse path also matters for retries — a run that failed mid-upload leaves its draft behind, and re-running publishes that same draft instead of failing on a duplicate tag. An already published release is detected and left untouched rather than patched, because an immutable release cannot be modified and attempting to PATCH it would fail the job for no useful reason. While repairing the release that the old code left behind, deleting it turned out not to free its tag: GitHub rejects reuse with "tag_name was used by an immutable release". That is a permanent record, so it also affects this repository's history rather than the upload order alone. The publish request now checks the response body and reports that specific cause together with the draft id and asset count, instead of surfacing a bare curl exit code. Tests: script-checks.yml asserts the create call precedes the first upload, that both assets are uploaded, and that the publish PATCH is the final call. It also asserts a published release is left alone with no further API calls. The curl stub honours -o and -w so it exercises the same code path as CI. Signed-off-by: Oliver Lin --- .github/workflows/script-checks.yml | 75 ++++++++++++++++++++ README-zh-cn.md | 3 +- README.md | 4 +- scripts/publish-log-release.sh | 102 ++++++++++++++++++++-------- 4 files changed, 152 insertions(+), 32 deletions(-) diff --git a/.github/workflows/script-checks.yml b/.github/workflows/script-checks.yml index 0d452e3..4b363f2 100644 --- a/.github/workflows/script-checks.yml +++ b/.github/workflows/script-checks.yml @@ -172,6 +172,81 @@ jobs: ! ./scripts/publish-log-release.sh owner/repo 2026-12-01-1 /tmp '' 2>/dev/null ! ./scripts/publish-log-release.sh owner/repo 2026-12-01-1 /nonexistent-checkout tok 2>/dev/null + - name: Test the release uploads assets before publishing + # The log repository has immutable releases: GitHub rejects asset uploads + # to a published release with 422 "Cannot upload assets to an immutable + # release". Assets must therefore be attached while the release is still a + # draft, and the release published last. + run: | + tmp="$(mktemp -d)" + mkdir -p "$tmp/repo/2026-12-01-1" "$tmp/bin" + printf 'a\n' > "$tmp/repo/2026-12-01-1/pkg.log" + printf 'b\n' > "$tmp/repo/2026-12-01-1/pkg2.log" + + # Stub curl and record the order of the API calls it receives. + cat > "$tmp/bin/curl" <<'STUB' + #!/usr/bin/env bash + printf '%s\n' "$*" >> "$CURL_CALLS" + # Honor -o and -w the way the script calls curl for the publish request. + out="" + wfmt="" + prev="" + for arg in "$@"; do + [[ "$prev" == "-o" ]] && out="$arg" + [[ "$prev" == "-w" ]] && wfmt="$arg" + prev="$arg" + done + body='{}' + status="200" + case "$*" in + *"/releases/tags/"*) exit 22 ;; + *"-X POST"*"/releases"*) body='{"id": 555, "draft": true}' ;; + *"/assets?per_page"*) body='[]' ;; + *"uploads.github.com"*) body='{"id": 7}' ;; + *"-X PATCH"*) body='{"id": 555, "draft": false}' ;; + esac + if [[ -n "$out" ]]; then + printf '%s' "$body" > "$out" + [[ "$wfmt" == '%{http_code}' ]] && printf '%s' "$status" + else + printf '%s\n' "$body" + fi + STUB + chmod +x "$tmp/bin/curl" + + export CURL_CALLS="$tmp/calls.log" + : > "$CURL_CALLS" + PATH="$tmp/bin:$PATH" ./scripts/publish-log-release.sh \ + owner/repo 2026-12-01-1 "$tmp/repo" tok + + # The draft release must be created before any asset upload. + create_line="$(grep -n 'api.github.com/repos/owner/repo/releases$' "$CURL_CALLS" \ + | head -1 | cut -d: -f1)" + first_upload="$(grep -n 'uploads.github.com' "$CURL_CALLS" | head -1 | cut -d: -f1)" + test -n "$create_line" + test -n "$first_upload" + test "$create_line" -lt "$first_upload" + + # Every asset is uploaded, and the publish request is the last call. + test "$(grep -c 'uploads.github.com' "$CURL_CALLS")" -eq 2 + test "$(wc -l < "$CURL_CALLS")" -eq "$(grep -n '"draft": false' "$CURL_CALLS" \ + | cut -d: -f1)" + + # An already published release must be left alone, not patched. + cat > "$tmp/bin/curl" <<'STUB' + #!/usr/bin/env bash + printf '%s\n' "$*" >> "$CURL_CALLS" + case "$*" in + *"/releases/tags/"*) echo '{"id": 555, "draft": false, "immutable": true}' ;; + *) echo '{}' ;; + esac + STUB + chmod +x "$tmp/bin/curl" + : > "$CURL_CALLS" + PATH="$tmp/bin:$PATH" ./scripts/publish-log-release.sh \ + owner/repo 2026-12-01-1 "$tmp/repo" tok + test "$(wc -l < "$CURL_CALLS")" -eq 1 + - name: Test the /log/ index does not clobber per-run indexes # stage-log-index.sh used to generate an "empty directory" index inside # every run directory and sync it over the real one, so published runs diff --git a/README-zh-cn.md b/README-zh-cn.md index 7f17f4e..4899ade 100644 --- a/README-zh-cn.md +++ b/README-zh-cn.md @@ -145,7 +145,8 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log - 该仓库的 README 在每次发布时重建,内含全部已归档运行的索引(按时间倒序), 并链接到每次运行的目录与发行版。 - 会创建以运行目录命名的发行版(如 `2026-12-01-46640252167`),把该次运行的 - 日志文件作为发行版附件上传,无需 clone 即可下载。发行版直接发布,不标记草稿。 + 日志文件作为发行版附件上传,无需 clone 即可下载。发行版先以草稿创建、传完附件 + 后再正式发布:该仓库启用了不可变发行版,GitHub 会拒绝向已发布的发行版追加附件。 - 提交者固定为 `zgs-software[bot]` (`363535165+zgs-software[bot]@users.noreply.github.com`),直接提交到 `main`。 - 认证使用 `scripts/github-app-token.sh` 由 `APP_KEY` secret 生成的 GitHub App diff --git a/README.md b/README.md index 0fd37c6..7fd479e 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,9 @@ same `-/-.log` layout: archived runs, newest first, linking each run's directory and release. - A release named after the run (`2026-12-01-46640252167`) is created with the run's log files attached as assets, so a run can be downloaded without - cloning. Releases are published directly, not as drafts. + cloning. Each release starts as a draft, receives its assets, and is published + last: the repository has immutable releases enabled, and GitHub rejects asset + uploads to an already published release. - Commits are made on `main` as `zgs-software[bot]` (`363535165+zgs-software[bot]@users.noreply.github.com`). - Authentication uses a GitHub App installation token minted by diff --git a/scripts/publish-log-release.sh b/scripts/publish-log-release.sh index cce4413..a621683 100755 --- a/scripts/publish-log-release.sh +++ b/scripts/publish-log-release.sh @@ -1,10 +1,17 @@ #!/usr/bin/env bash -# Create (or update) the GitHub release for one archived build-log run and attach -# that run's log files as release assets. +# Create the GitHub release for one archived build-log run with that run's log +# files attached. # -# Releases are idempotent: re-running a publish for the same run updates the -# existing release and replaces its assets instead of failing, so a retried -# workflow does not error out. +# The release is created as a DRAFT, the assets are uploaded, and only then is it +# published. The log repository has immutable releases enabled, and GitHub rejects +# asset uploads to a release that has already been published (HTTP 422, "Cannot +# upload assets to an immutable release"), so uploading before publishing is the +# only order that works. +# +# Because a published release cannot be modified, re-running is handled by +# deleting a draft left over from a previous attempt or by providing a separate +# repository-level guarantee: an already published release is detected and left +# alone rather than patched. set -euo pipefail if [[ $# -ne 4 ]]; then @@ -49,6 +56,9 @@ if [[ ! -d "$run_path" ]]; then exit 1 fi +tmp_dir="$(mktemp -d)" +trap 'rm -rf "$tmp_dir"' EXIT + api="https://api.github.com/repos/$repo" tag="$run_dir" title="Build logs $run_dir" @@ -59,40 +69,45 @@ body="Build logs of the ZGS publish run archived in \`$run_dir/\`. - Browsable copy: https://apt-general.zitzhen.cn/log/$run_dir/" # The boolean fields must be real JSON booleans; "False" as a string is rejected. +# Created as a draft so the assets can be attached before publication; see the +# note at the top of this script. create_payload="$(TAG="$tag" TITLE="$title" BODY="$body" python3 -c ' import json, os print(json.dumps({ "tag_name": os.environ["TAG"], "name": os.environ["TITLE"], "body": os.environ["BODY"], - "draft": False, + "draft": True, "prerelease": False, "generate_release_notes": False, })) ')" -update_payload="$(TITLE="$title" BODY="$body" python3 -c ' -import json, os -print(json.dumps({ - "name": os.environ["TITLE"], - "body": os.environ["BODY"], - "draft": False, - "prerelease": False, -})) -')" - auth=(-H "Authorization: Bearer $token" -H 'Accept: application/vnd.github+json') -existing_id="$(curl -fsS "${auth[@]}" "$api/releases/tags/$tag" 2>/dev/null \ - | python3 -c 'import json,sys; print(json.load(sys.stdin).get("id",""))' 2>/dev/null || true)" +# Look for an existing release for this run. A published immutable release can no +# longer be changed, so it is treated as already done; only a draft is reused. +existing="$(curl -fsS "${auth[@]}" "$api/releases/tags/$tag" 2>/dev/null || true)" -if [[ -n "$existing_id" ]]; then - echo "Updating the existing release $tag (id $existing_id)" >&2 - release_id="$existing_id" - curl -fsS -X PATCH "${auth[@]}" -H 'Content-Type: application/json' \ - --data "$update_payload" "$api/releases/$release_id" >/dev/null -else - echo "Creating release $tag" >&2 +release_id="" +if [[ -n "$existing" ]]; then + read -r existing_id existing_draft <<<"$(printf '%s' "$existing" | python3 -c ' +import json, sys +release = json.load(sys.stdin) +print(release.get("id", ""), "true" if release.get("draft") else "false") +')" + + if [[ -n "$existing_id" && "$existing_draft" == "true" ]]; then + echo "Reusing the draft release $tag (id $existing_id)" >&2 + release_id="$existing_id" + elif [[ -n "$existing_id" ]]; then + echo "Release $tag is already published; leaving it untouched" >&2 + exit 0 + fi +fi + +if [[ -z "$release_id" ]]; then + echo "Creating draft release $tag" >&2 release_id="$(curl -fsS -X POST "${auth[@]}" \ -H 'Content-Type: application/json' --data "$create_payload" "$api/releases" \ | python3 -c 'import json,sys; print(json.load(sys.stdin).get("id",""))')" @@ -108,11 +123,12 @@ assets=("$run_path"/*.log) shopt -u nullglob if (( ${#assets[@]} == 0 )); then - echo "Warning: no .log files in $run_path; the release has no assets" >&2 - exit 0 + echo "Warning: no .log files in $run_path; publishing an empty release" >&2 fi -# Snapshot the existing assets once, so a retry replaces instead of duplicating. +# Upload while still a draft: the repository has immutable releases enabled, so +# assets cannot be added once the release is published. Existing assets are +# replaced, which keeps a retried run from duplicating them. declare -A asset_ids=() while IFS=$'\t' read -r asset_name asset_id; do [[ -n "$asset_name" ]] && asset_ids["$asset_name"]="$asset_id" @@ -126,7 +142,8 @@ for asset in json.load(sys.stdin): ) uploaded=0 -for asset in "${assets[@]}"; do +for asset in "${assets[@]:-}"; do + [[ -z "$asset" ]] && continue asset_name="$(basename -- "$asset")" if [[ -n "${asset_ids[$asset_name]:-}" ]]; then @@ -144,4 +161,29 @@ for asset in "${assets[@]}"; do echo "Uploaded $asset_name" >&2 done -echo "Release $tag is live with $uploaded asset(s)" >&2 +# Every asset is in place, so publish. This is the step that would be impossible +# to follow up on afterwards, since the release becomes immutable. +echo "Publishing release $tag" >&2 +publish_body="$tmp_dir/publish.json" +publish_status="$(curl -sS -o "$publish_body" -w '%{http_code}' -X PATCH "${auth[@]}" \ + -H 'Content-Type: application/json' \ + --data '{"draft": false, "prerelease": false}' "$api/releases/$release_id")" + +if [[ "$publish_status" != "2"* ]]; then + # GitHub records the tag of an immutable release permanently: deleting that + # release does not free the tag, and reusing it fails with + # "tag_name was used by an immutable release". Report that instead of a bare + # curl exit code, because the assets are already uploaded and the release is + # otherwise complete. + if grep -q 'immutable release' "$publish_body" 2>/dev/null; then + echo "Error: the tag $tag was used by a previously deleted immutable release" >&2 + echo " and GitHub will not reuse it. The draft release $release_id has" >&2 + echo " all $uploaded asset(s) attached; pick a new tag for this run." >&2 + exit 1 + fi + echo "Error: publishing the release failed with HTTP $publish_status" >&2 + cat "$publish_body" >&2 || true + exit 1 +fi + +echo "Release $tag published with $uploaded asset(s)" >&2