-
Notifications
You must be signed in to change notification settings - Fork 0
feat(release): add tag/version validation + previous-tag scripts (epic 005 T005) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env bash | ||
| # Resolves the nearest reachable prior SemVer release tag from | ||
| # <current_tag> (FR-011). Prints the previous tag on stdout if one exists; | ||
| # prints nothing and exits 0 if this is the first release (FR-009's | ||
| # fallback path decides what to do with an empty result -- this script | ||
| # only reports the fact). | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "usage: $0 <current_tag>" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| [ $# -ge 1 ] || usage | ||
| current_tag="$1" | ||
|
|
||
| if ! git rev-parse -q --verify "refs/tags/${current_tag}" >/dev/null; then | ||
| echo "error: tag '$current_tag' does not exist in this repository" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # git's --match is a shell glob, not a regex: "v[0-9]*" also matches | ||
| # non-release tags like "v1.1.0-rc1" (any suffix after a leading digit). | ||
| # Walk back past any such match until we find one that's strict SemVer | ||
| # (vMAJOR.MINOR.PATCH, no suffix), or run out of tags entirely. | ||
| ref="${current_tag}^" | ||
| while candidate="$(git describe --tags --match "v[0-9]*" --abbrev=0 "$ref" 2>/dev/null)"; do | ||
| if [[ "$candidate" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | ||
| echo "$candidate" | ||
| exit 0 | ||
| fi | ||
| ref="${candidate}^" | ||
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env bash | ||
| # Fixture-based tests for previous-release-tag.sh (T005). Builds a throwaway | ||
| # git repo with a known tag history rather than touching the real repo's | ||
| # tags. Run manually: | ||
| # bash .github/scripts/tests/test-previous-release-tag.sh | ||
| set -euo pipefail | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| script="$script_dir/../previous-release-tag.sh" | ||
|
|
||
| tmp="$(mktemp -d)" | ||
| trap 'rm -rf "$tmp"' EXIT | ||
|
|
||
| pass=0 | ||
| fail=0 | ||
|
|
||
| check_eq() { | ||
| local desc="$1" expected="$2" actual="$3" | ||
| if [ "$expected" == "$actual" ]; then | ||
| pass=$((pass + 1)) | ||
| else | ||
| fail=$((fail + 1)) | ||
| echo "FAIL: $desc (expected '$expected', got '$actual')" | ||
| fi | ||
| } | ||
|
|
||
| assert_fail() { | ||
| local desc="$1"; shift | ||
| if "$@" >/dev/null 2>&1; then | ||
| fail=$((fail + 1)) | ||
| echo "FAIL (expected failure but succeeded): $desc" | ||
| else | ||
| pass=$((pass + 1)) | ||
| fi | ||
| } | ||
|
|
||
| ( | ||
| cd "$tmp" | ||
| git init -q | ||
| git config user.email "test@example.com" | ||
| git config user.name "Test" | ||
| # Lightweight, unsigned tags regardless of the caller's global git config | ||
| # (e.g. tag.gpgsign=true would otherwise force an annotated/signed tag | ||
| # here, which fails without an interactive editor or GPG key). | ||
| git config tag.gpgsign false | ||
| tag() { git -c tag.gpgsign=false tag "$1"; } | ||
|
|
||
| echo "one" >file.txt | ||
| git add file.txt | ||
| git commit -q -m "first" | ||
| tag v1.0.0 | ||
|
|
||
| echo "two" >file.txt | ||
| git commit -q -am "second" | ||
| tag v1.1.0 | ||
|
|
||
| echo "three" >file.txt | ||
| git commit -q -am "third" | ||
| tag v1.1.0-rc1 | ||
|
|
||
| echo "four" >file.txt | ||
| git commit -q -am "fourth" | ||
| tag v2.0.0 | ||
| ) | ||
|
|
||
| out="$(cd "$tmp" && bash "$script" "v1.1.0")" | ||
| check_eq "previous tag for v1.1.0 is v1.0.0" "v1.0.0" "$out" | ||
|
|
||
| out="$(cd "$tmp" && bash "$script" "v2.0.0")" | ||
| check_eq "previous tag for v2.0.0 is v1.1.0, not the intervening v1.1.0-rc1 prerelease" "v1.1.0" "$out" | ||
|
|
||
| out="$(cd "$tmp" && bash "$script" "v1.0.0")" | ||
| check_eq "first release has no previous tag (empty output, FR-009 fallback)" "" "$out" | ||
|
|
||
| assert_fail "nonexistent tag fails" bash -c "cd '$tmp' && bash '$script' v9.9.9" | ||
|
|
||
| echo "----" | ||
| echo "pass=$pass fail=$fail" | ||
| [ "$fail" -eq 0 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env bash | ||
| # Fixture-based tests for validate-release-tag.sh (T005, Constitution I: | ||
| # written before/alongside the script, not after). Run manually: | ||
| # bash .github/scripts/tests/test-validate-release-tag.sh | ||
| set -euo pipefail | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| script="$script_dir/../validate-release-tag.sh" | ||
|
|
||
| tmp="$(mktemp -d)" | ||
| trap 'rm -rf "$tmp"' EXIT | ||
|
|
||
| pass=0 | ||
| fail=0 | ||
|
|
||
| assert_ok() { | ||
| local desc="$1"; shift | ||
| local capture; capture="$(mktemp "$tmp/out.XXXXXX")" | ||
| if "$@" >"$capture" 2>&1; then | ||
| pass=$((pass + 1)) | ||
| else | ||
| fail=$((fail + 1)) | ||
| echo "FAIL (expected success): $desc" | ||
| cat "$capture" | ||
| fi | ||
| rm -f "$capture" | ||
| } | ||
|
|
||
| assert_fail() { | ||
| local desc="$1"; shift | ||
| local capture; capture="$(mktemp "$tmp/out.XXXXXX")" | ||
| if "$@" >"$capture" 2>&1; then | ||
| fail=$((fail + 1)) | ||
| echo "FAIL (expected failure but succeeded): $desc" | ||
| cat "$capture" | ||
| else | ||
| pass=$((pass + 1)) | ||
| fi | ||
| rm -f "$capture" | ||
| } | ||
|
|
||
| cargo_toml="$tmp/Cargo.toml" | ||
| cat >"$cargo_toml" <<'EOF' | ||
| [workspace] | ||
| members = ["crates/*"] | ||
|
|
||
| [workspace.package] | ||
| version = "1.2.3" | ||
| edition = "2021" | ||
| EOF | ||
|
|
||
| assert_ok "matching tag passes" bash "$script" "v1.2.3" "$cargo_toml" | ||
| assert_fail "mismatched patch fails" bash "$script" "v1.2.4" "$cargo_toml" | ||
| assert_fail "mismatched minor fails" bash "$script" "v1.3.3" "$cargo_toml" | ||
| assert_fail "missing v prefix fails" bash "$script" "1.2.3" "$cargo_toml" | ||
| assert_fail "non-semver tag fails" bash "$script" "v1.2" "$cargo_toml" | ||
| assert_fail "pre-release suffix fails (out of scope per spec.md Assumptions)" bash "$script" "v1.2.3-rc1" "$cargo_toml" | ||
| assert_fail "missing Cargo.toml fails" bash "$script" "v1.2.3" "$tmp/does-not-exist.toml" | ||
|
|
||
| # Mismatch error message must name both values (SC-006). | ||
| out="$(bash "$script" "v9.9.9" "$cargo_toml" 2>&1 || true)" | ||
| if [[ "$out" == *"v9.9.9"* && "$out" == *"1.2.3"* ]]; then | ||
| pass=$((pass + 1)) | ||
| else | ||
| fail=$((fail + 1)) | ||
| echo "FAIL: mismatch error must name both tag and workspace version, got: $out" | ||
| fi | ||
|
|
||
| echo "----" | ||
| echo "pass=$pass fail=$fail" | ||
| [ "$fail" -eq 0 ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #!/usr/bin/env bash | ||
| # Validates a release tag against Cargo.toml's canonical workspace version | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| # (FR-010). Previous-release-tag resolution (FR-011) is a separate concern, | ||
| # implemented in previous-release-tag.sh. See | ||
| # specs/005-ci-release-versioning/spec.md. | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "usage: $0 <tag> [cargo_toml_path]" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| [ $# -ge 1 ] || usage | ||
| tag="$1" | ||
| cargo_toml="${2:-Cargo.toml}" | ||
|
|
||
| if [ ! -f "$cargo_toml" ]; then | ||
| echo "error: $cargo_toml not found" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if ! [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | ||
| echo "error: tag '$tag' is not a valid SemVer release tag (expected vMAJOR.MINOR.PATCH)" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| workspace_version="$(awk ' | ||
| /^[[:space:]]*\[workspace\.package\]/ { in_section = 1; next } | ||
| /^[[:space:]]*\[/ { in_section = 0 } | ||
| in_section && /^[[:space:]]*version[[:space:]]*=/ { | ||
| match($0, /"[^"]+"/) | ||
| print substr($0, RSTART + 1, RLENGTH - 2) | ||
| exit | ||
| } | ||
| ' "$cargo_toml")" | ||
|
|
||
| if [ -z "$workspace_version" ]; then | ||
| echo "error: could not find [workspace.package].version in $cargo_toml" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| expected_tag="v${workspace_version}" | ||
|
|
||
| if [ "$tag" != "$expected_tag" ]; then | ||
| echo "error: tag/version mismatch: tag is '$tag', but [workspace.package].version is '$workspace_version' (expected tag '$expected_tag')" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "ok: tag '$tag' matches workspace version '$workspace_version'" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.