Merge pull request 'fix: override=true must not needlessly delete a tag' (#39) from limiting-factor/forgejo-release:wip-override into main

Reviewed-on: https://code.forgejo.org/actions/forgejo-release/pulls/39
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
This commit is contained in:
earl-warren 2025-02-23 20:43:42 +00:00
commit e2af55222e
No known key found for this signature in database
GPG key ID: F128CBE6AB3A7201
2 changed files with 87 additions and 60 deletions

View file

@ -19,6 +19,8 @@ if ${VERBOSE:-false}; then set -x; fi
: ${RETRY:=1} : ${RETRY:=1}
: ${DELAY:=10} : ${DELAY:=10}
TAG_FILE="$TMP_DIR/tag$$.json"
export GNUPGHOME export GNUPGHOME
setup_tea() { setup_tea() {
@ -29,16 +31,35 @@ setup_tea() {
fi fi
} }
get_tag() {
if ! test -f "$TAG_FILE"; then
if api GET repos/$REPO/tags/"$TAG" >"$TAG_FILE"; then
echo "tag $TAG exists"
else
echo "tag $TAG does not exists"
fi
fi
test -s "$TAG_FILE"
}
matched_tag() {
if get_tag; then
local sha=$(jq --raw-output .commit.sha <"$TAG_FILE")
test "$sha" = "$SHA"
else
return 1
fi
}
ensure_tag() { ensure_tag() {
if api GET repos/$REPO/tags/"$TAG" > "$TMP_DIR"/tag.json ; then if get_tag; then
local sha=$(jq --raw-output .commit.sha < "$TMP_DIR"/tag.json) if ! matched_tag; then
if test "$sha" != "$SHA" ; then cat "$TAG_FILE"
cat "$TMP_DIR"/tag.json
echo "the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA" echo "the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA"
false return 1
fi fi
else else
api POST repos/$REPO/tags --data-raw '{"tag_name": "'"$TAG"'", "target": "'"$SHA"'"}' api POST repos/$REPO/tags --data-raw '{"tag_name": "'"$TAG"'", "target": "'"$SHA"'"}' >"$TAG_FILE"
fi fi
} }
@ -112,7 +133,9 @@ maybe_override() {
return return
fi fi
api DELETE repos/$REPO/releases/tags/"$TAG" >&/dev/null || true api DELETE repos/$REPO/releases/tags/"$TAG" >&/dev/null || true
api DELETE repos/$REPO/tags/"$TAG" >& /dev/null || true if get_tag && ! matched_tag; then
api DELETE repos/$REPO/tags/"$TAG"
fi
} }
upload() { upload() {

View file

@ -2,6 +2,7 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
set -ex set -ex
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
test_teardown() { test_teardown() {
setup_api setup_api
@ -44,15 +45,18 @@ test_ensure_tag() {
# idempotent # idempotent
# #
ensure_tag ensure_tag
api GET repos/$REPO/tags/$TAG > $TMP_DIR/tag1.json mv $TAG_FILE $TMP_DIR/tag1.json
ensure_tag ensure_tag
api GET repos/$REPO/tags/$TAG > $TMP_DIR/tag2.json mv $TAG_FILE $TMP_DIR/tag2.json
diff -u $TMP_DIR/tag[12].json diff -u $TMP_DIR/tag[12].json
# #
# sanity check on the SHA of an existing tag # sanity check on the SHA of an existing tag
# #
( (
SHA=12345 SHA=12345
! matched_tag
! ensure_tag ! ensure_tag
) )
api DELETE repos/$REPO/tags/$TAG api DELETE repos/$REPO/tags/$TAG