#!/usr/bin/env bash set -euo pipefail root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd) verifier="$root/scripts/verify-gitea-release-tag.sh" restorer="$root/scripts/restore-gitea-release-tag.sh" temporary=$(mktemp -d) trap 'rm -rf -- "$temporary"' EXIT repository="$temporary/repository" mkdir -p "$repository" git -C "$repository" init -q -b main git -C "$repository" config user.name 'Echoion Release Test' git -C "$repository" config user.email 'release-test@echoion.invalid' ssh-keygen -q -t ed25519 -N '' -f "$temporary/release-key" git -C "$repository" config gpg.format ssh git -C "$repository" config user.signingkey "$temporary/release-key" printf '%s\n' test >"$repository/source.txt" git -C "$repository" add source.txt git -C "$repository" commit -q -m 'Test release source' git -C "$repository" tag -s -a v1.2.3 \ -m 'Echoion 1.2.3' \ -m $'First release-note paragraph.\n\nSecond paragraph.' git -C "$repository" switch -q --detach v1.2.3 principal='release-test@echoion.invalid' printf '%s %s\n' "$principal" "$(<"$temporary/release-key.pub")" \ >"$temporary/allowed-signers" tag_object=$(git -C "$repository" rev-parse 'refs/tags/v1.2.3^{tag}') target_commit=$(git -C "$repository" rev-parse 'refs/tags/v1.2.3^{}') remote="$temporary/remote.git" git clone -q --bare "$repository" "$remote" # Reproduce actions/checkout replacing the annotated tag object with the # workflow event's target commit, then restore the authoritative remote ref. git -C "$repository" update-ref refs/tags/v1.2.3 "$target_commit" test "$(git -C "$repository" cat-file -t refs/tags/v1.2.3)" = commit ( cd "$repository" "$restorer" --repository "$remote" --tag v1.2.3 ) test "$(git -C "$repository" rev-parse refs/tags/v1.2.3)" = "$tag_object" actual_target=$( cd "$repository" "$verifier" \ --tag v1.2.3 \ --event-sha "$tag_object" \ --allowed-signers "$temporary/allowed-signers" \ --notes-output "$temporary/notes.md" ) test "$actual_target" = "$target_commit" grep -Fqx 'Echoion 1.2.3' "$temporary/notes.md" grep -Fqx 'First release-note paragraph.' "$temporary/notes.md" grep -Fqx 'Second paragraph.' "$temporary/notes.md" if grep -Fq -- 'SSH SIGNATURE' "$temporary/notes.md"; then printf '%s\n' 'release notes contain the SSH signature' >&2 exit 1 fi # Git 2.34 can underflow while formatting %(contents:body) for an SSH-signed # tag with only a subject. Subject-only release notes must remain supported. git -C "$repository" switch -q main git -C "$repository" tag -s -a v1.2.4 -m 'Echoion 1.2.4' subject_only_tag=$(git -C "$repository" rev-parse 'refs/tags/v1.2.4^{tag}') ( cd "$repository" "$verifier" \ --tag v1.2.4 \ --event-sha "$subject_only_tag" \ --allowed-signers "$temporary/allowed-signers" \ --notes-output "$temporary/subject-only-notes.md" \ >/dev/null ) test "$(<"$temporary/subject-only-notes.md")" = 'Echoion 1.2.4' ( cd "$repository" "$verifier" \ --tag v1.2.3 \ --event-sha "$target_commit" \ --allowed-signers "$temporary/allowed-signers" \ --notes-output "$temporary/notes-from-commit.md" \ >/dev/null ) if ( cd "$repository" "$verifier" \ --tag v1.2.3 \ --event-sha 0000000000000000000000000000000000000000 \ --allowed-signers "$temporary/allowed-signers" \ --notes-output "$temporary/rejected-notes.md" \ >/dev/null 2>&1 ); then printf '%s\n' 'mismatched event SHA was accepted' >&2 exit 1 fi printf '%s\n' 'other@example.invalid ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA' \ >"$temporary/untrusted-signers" if ( cd "$repository" "$verifier" \ --tag v1.2.3 \ --event-sha "$target_commit" \ --allowed-signers "$temporary/untrusted-signers" \ --notes-output "$temporary/untrusted-notes.md" \ >/dev/null 2>&1 ); then printf '%s\n' 'untrusted SSH signer was accepted' >&2 exit 1 fi git -C "$repository" tag lightweight "$target_commit" lightweight_error="$temporary/lightweight-error.txt" if ( cd "$repository" "$verifier" \ --tag lightweight \ --event-sha "$target_commit" \ --allowed-signers "$temporary/allowed-signers" \ --notes-output "$temporary/lightweight-notes.md" \ >/dev/null 2>"$lightweight_error" ); then printf '%s\n' 'lightweight tag was accepted' >&2 exit 1 fi grep -Fqx \ 'verify-gitea-release-tag: lightweight tags are not allowed; create a signed annotated tag' \ "$lightweight_error" printf '%s\n' 'Gitea signed-tag verification tests passed'