Stop reading JSON with a greedy regex, and stop set -e hiding the empty case
My first fix was wrong about why the release could not be created. Gitea materialises a release row for every pushed tag, so the lookup should have found one; the create that followed hit a release that already existed. The lookup failed because of how it read the id. `sed 's/.*"id":\([0-9]*\).*/\1/'` is greedy, so `.*"id":` matches the last occurrence in the document, not the first — "id" appears again inside the nested author object — and it does not match `"id": 13` with a space after the colon at all. Checked against this instance: for v0.5.0, whose id is 12, the old pattern returns 1. It now greps the first "id", tolerates the whitespace, and re-reads the release after creating rather than parsing the create response, so a 409 resolves to the release that was already there instead of to an error. `set -e` was hiding the empty case: a command substitution in an assignment fails the script, and grep exits non-zero when it matches nothing, so the "no release yet" branch could never be reached — the job would simply stop. Both extractions are guarded, and both paths are exercised. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4815a50c94
commit
7de8bacfc0
1 file changed
+24
-11
@@ -110,24 +110,37 @@ jobs:
|
||||
tag="${GITHUB_REF_NAME}"
|
||||
api="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}"
|
||||
|
||||
# The release usually exists already, created by hand with its notes.
|
||||
id=$(curl -sS -H "Authorization: token $TOKEN" "$api/releases/tags/$tag" \
|
||||
| sed -n 's/.*"id":\([0-9]*\).*/\1/p' | head -1)
|
||||
# Gitea materialises a release row for every pushed tag, so this
|
||||
# nearly always finds one and the create below is for a tag pushed
|
||||
# some other way.
|
||||
#
|
||||
# The id is read with grep rather than sed because sed's .* is greedy:
|
||||
# "id" appears again inside the nested author object, so the old
|
||||
# pattern took the author's id when it matched at all — and it did not
|
||||
# match "id": 13 with a space, which is what actually happened. The
|
||||
# first "id" in a release object is the release's own.
|
||||
release_id() {
|
||||
curl -sS -H "Authorization: token $TOKEN" "$api/releases/tags/$tag" \
|
||||
| grep -o '"id"[[:space:]]*:[[:space:]]*[0-9]*' \
|
||||
| head -1 | grep -o '[0-9]*$'
|
||||
}
|
||||
|
||||
id=$(release_id || true)
|
||||
if [ -z "$id" ]; then
|
||||
echo "creating release $tag"
|
||||
body=$(curl -sS -w '\n%{http_code}' -X POST \
|
||||
echo "no release for $tag yet; creating one"
|
||||
out=$(curl -sS -w '\n%{http_code}' -X POST \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\":\"$tag\",\"name\":\"$tag\"}" \
|
||||
"$api/releases")
|
||||
code=$(printf '%s' "$body" | tail -1)
|
||||
echo "create returned $code"
|
||||
id=$(printf '%s' "$body" | sed -n 's/.*"id":\([0-9]*\).*/\1/p' | head -1)
|
||||
# Whatever went wrong, the server said so, and the previous version
|
||||
# of this step threw that away and reported only that it had failed.
|
||||
[ -n "$id" ] || printf '%s\n' "$body" | head -5 >&2
|
||||
echo "create returned $(printf '%s' "$out" | tail -1)"
|
||||
# Re-read rather than parsing the create response: a 409 means it
|
||||
# was there all along, which is a success for our purposes.
|
||||
id=$(release_id || true)
|
||||
[ -n "$id" ] || printf '%s\n' "$out" | head -5 >&2
|
||||
fi
|
||||
[ -n "$id" ] || { echo "could not find or create the release" >&2; exit 1; }
|
||||
echo "release id $id"
|
||||
|
||||
for f in dist/*; do
|
||||
echo "uploading $(basename "$f")"
|
||||
|
||||
Reference in new issue
Block a user