#!/usr/bin/env bash set -euo pipefail usage() { printf '%s\n' \ 'Usage: verify-gitea-release-tag.sh --tag TAG --event-sha SHA' \ ' --allowed-signers FILE --notes-output FILE' } tag= event_sha= allowed_signers= notes_output= while (($# > 0)); do case "$1" in --tag) tag=${2-} shift 2 ;; --event-sha) event_sha=${2-} shift 2 ;; --allowed-signers) allowed_signers=${2-} shift 2 ;; --notes-output) notes_output=${2-} shift 2 ;; --help|-h) usage exit 0 ;; *) printf 'verify-gitea-release-tag: unknown argument: %s\n' "$1" >&2 usage >&2 exit 2 ;; esac done if [[ -z "$tag" || -z "$event_sha" || -z "$allowed_signers" || -z "$notes_output" ]]; then usage >&2 exit 2 fi if [[ ! "$event_sha" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then printf '%s\n' \ 'verify-gitea-release-tag: event SHA is not a full object id' >&2 exit 1 fi if ! git check-ref-format "refs/tags/$tag"; then printf '%s\n' 'verify-gitea-release-tag: invalid tag name' >&2 exit 1 fi if [[ ! -f "$allowed_signers" || -L "$allowed_signers" || ! -s "$allowed_signers" ]]; then printf '%s\n' \ 'verify-gitea-release-tag: allowed signers must be a non-empty regular file' \ >&2 exit 1 fi tag_ref="refs/tags/$tag" if ! tag_object=$(git rev-parse --verify "$tag_ref"); then printf '%s\n' \ 'verify-gitea-release-tag: tag ref does not exist' >&2 exit 1 fi if [[ "$(git cat-file -t "$tag_object")" != tag ]]; then printf '%s\n' \ 'verify-gitea-release-tag: lightweight tags are not allowed; create a signed annotated tag' \ >&2 exit 1 fi tag_target=$( git cat-file tag "$tag_object" | sed -n '1s/^object //p' ) target_type=$( git cat-file tag "$tag_object" | sed -n '2s/^type //p' ) if [[ "$target_type" != commit || ! "$tag_target" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then printf '%s\n' \ 'verify-gitea-release-tag: annotated tag must directly target a commit' >&2 exit 1 fi git cat-file -e "$tag_target^{commit}" if ! git cat-file tag "$tag_object" | grep -q '^-----BEGIN SSH SIGNATURE-----$'; then printf '%s\n' \ 'verify-gitea-release-tag: annotated tag must use an SSH signature' >&2 exit 1 fi git -c gpg.ssh.allowedSignersFile="$allowed_signers" \ verify-tag "$tag_ref" >&2 checked_out_commit=$(git rev-parse --verify 'HEAD^{commit}') if [[ "$checked_out_commit" != "$tag_target" ]]; then printf '%s\n' \ 'verify-gitea-release-tag: checkout HEAD differs from the tag target' >&2 exit 1 fi if [[ "$event_sha" != "$tag_object" && "$event_sha" != "$tag_target" ]]; then printf '%s\n' \ 'verify-gitea-release-tag: event SHA differs from the tag and target objects' \ >&2 exit 1 fi python3 - "$tag_object" "$notes_output" <<'PY' from pathlib import Path import subprocess import sys def fail(message: str) -> None: print(f"verify-gitea-release-tag: {message}", file=sys.stderr) raise SystemExit(1) tag = subprocess.run( ["git", "cat-file", "tag", sys.argv[1]], check=True, stdout=subprocess.PIPE, ).stdout try: _, message = tag.split(b"\n\n", 1) except ValueError: fail("annotated tag has no release-note subject") marker = b"\n-----BEGIN SSH SIGNATURE-----\n" signature_offset = message.rfind(marker) if signature_offset < 0: fail("annotated tag must use an SSH signature") notes = message[:signature_offset] try: text = notes.decode("utf-8").rstrip("\n") except UnicodeDecodeError: fail("release notes are not valid UTF-8") if not text or not text.splitlines()[0]: fail("annotated tag has no release-note subject") Path(sys.argv[2]).write_text(text + "\n", encoding="utf-8") PY printf '%s\n' "$tag_target"