81 lines
2.6 KiB
Bash
81 lines
2.6 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
set -Eeuo pipefail
|
||
|
|
|
||
|
|
: "${GITEA_API_URL:?GITEA_API_URL is required}"
|
||
|
|
: "${GITEA_REPOSITORY:?GITEA_REPOSITORY is required}"
|
||
|
|
: "${GITEA_TOKEN:?GITEA_TOKEN is required}"
|
||
|
|
: "${RELEASE_TAG:?RELEASE_TAG is required}"
|
||
|
|
: "${RELEASE_NOTES:?RELEASE_NOTES is required}"
|
||
|
|
|
||
|
|
if [[ ! "$RELEASE_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||
|
|
printf 'captioneer: release tag must match vMAJOR.MINOR.BUG, got %q\n' "$RELEASE_TAG" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
if (( $# == 0 )); then
|
||
|
|
printf 'captioneer: at least one release asset is required\n' >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
for asset in "$@"; do
|
||
|
|
[[ -f "$asset" ]] || { printf 'captioneer: release asset not found: %s\n' "$asset" >&2; exit 1; }
|
||
|
|
done
|
||
|
|
|
||
|
|
api_base="${GITEA_API_URL%/}/repos/$GITEA_REPOSITORY"
|
||
|
|
authorization="Authorization: token $GITEA_TOKEN"
|
||
|
|
release_response="$(mktemp)"
|
||
|
|
trap 'rm -f -- "$release_response"' EXIT
|
||
|
|
|
||
|
|
status="$(curl --silent --show-error --output "$release_response" --write-out '%{http_code}' \
|
||
|
|
--header "$authorization" \
|
||
|
|
"$api_base/releases/tags/$RELEASE_TAG")"
|
||
|
|
|
||
|
|
release_payload="$(jq -n \
|
||
|
|
--arg tag "$RELEASE_TAG" \
|
||
|
|
--arg name "Captioneer $RELEASE_TAG" \
|
||
|
|
--rawfile body "$RELEASE_NOTES" \
|
||
|
|
'{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')"
|
||
|
|
|
||
|
|
case "$status" in
|
||
|
|
200)
|
||
|
|
release_id="$(jq -er '.id' "$release_response")"
|
||
|
|
curl --fail-with-body --silent --show-error \
|
||
|
|
--request PATCH \
|
||
|
|
--header "$authorization" \
|
||
|
|
--header 'Content-Type: application/json' \
|
||
|
|
--data "$release_payload" \
|
||
|
|
"$api_base/releases/$release_id" > "$release_response"
|
||
|
|
;;
|
||
|
|
404)
|
||
|
|
curl --fail-with-body --silent --show-error \
|
||
|
|
--request POST \
|
||
|
|
--header "$authorization" \
|
||
|
|
--header 'Content-Type: application/json' \
|
||
|
|
--data "$release_payload" \
|
||
|
|
"$api_base/releases" > "$release_response"
|
||
|
|
release_id="$(jq -er '.id' "$release_response")"
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
printf 'captioneer: Gitea release lookup failed with HTTP %s\n' "$status" >&2
|
||
|
|
cat "$release_response" >&2
|
||
|
|
exit 1
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
|
||
|
|
for asset in "$@"; do
|
||
|
|
asset_name="$(basename -- "$asset")"
|
||
|
|
while IFS= read -r attachment_id; do
|
||
|
|
[[ -n "$attachment_id" ]] || continue
|
||
|
|
curl --fail-with-body --silent --show-error \
|
||
|
|
--request DELETE \
|
||
|
|
--header "$authorization" \
|
||
|
|
"$api_base/releases/$release_id/assets/$attachment_id" >/dev/null
|
||
|
|
done < <(jq -r --arg name "$asset_name" '.assets[]? | select(.name == $name) | .id' "$release_response")
|
||
|
|
|
||
|
|
curl --fail-with-body --silent --show-error \
|
||
|
|
--request POST \
|
||
|
|
--header "$authorization" \
|
||
|
|
--form "attachment=@$asset" \
|
||
|
|
"$api_base/releases/$release_id/assets?name=$asset_name" >/dev/null
|
||
|
|
printf 'Uploaded %s\n' "$asset_name"
|
||
|
|
done
|