Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ jobs:
set -eu
podman run --rm -v "$PWD:/mnt:ro,z" -w /mnt \
docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh \
tests/test_host_contract.sh
tests/test_host_contract.sh scripts/release-version.sh
tests/test_host_contract.sh
tests/test_release_version.py

- name: Build
if: env.IS_TAG != 'true'
Expand Down
13 changes: 2 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@ jobs:
GH_TOKEN: ${{ github.token }}
run: |
set -eu
v=$(sed -n 's/^version = "\(.*\)"$/\1/p' pyproject.toml | head -1)
[ -n "$v" ] || { echo "pyproject.toml names no version" >&2; exit 1; }
if gh release view "v$v" >/dev/null 2>&1; then
echo "v$v is already released"
echo "tag=" >> "$GITHUB_OUTPUT"
elif grep -q "^## \[$v\]" CHANGELOG.md; then
echo "tag=v$v" >> "$GITHUB_OUTPUT"
else
echo "v$v has no CHANGELOG heading; not releasing" >&2
exit 1
fi
tag=$(scripts/release-version.sh)
echo "tag=$tag" >> "$GITHUB_OUTPUT"

# The App's token, because a tag created with GITHUB_TOKEN does not start
# container.yml, and the release would never be promoted.
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ repos:
- repo: local
hooks:
- id: shellcheck
name: shellcheck (bin/sdlc and the tests)
entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh tests/test_host_contract.sh
name: shellcheck (bin/sdlc, the tests and scripts)
entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh tests/test_host_contract.sh scripts/release-version.sh
language: system
pass_filenames: false
always_run: true
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project are documented here, following

## [Unreleased]

### Fixed

- `release.yml` no longer mistakes a failed `gh release view` (auth, network,
rate limit) for an unreleased version: only `release not found` counts, and
any other failure stops the job. The CHANGELOG heading check is a literal
match, so a version's dots no longer match any character. The decision now
lives in `scripts/release-version.sh`, tested by
`tests/test_release_version.py`.

## [0.7.3] - 2026-11-17

### Changed
Expand Down
33 changes: 33 additions & 0 deletions scripts/release-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# Print the tag release.yml should cut, or nothing when there is none.
#
# Reads the version from pyproject.toml in the current directory. Prints
# vX.Y.Z when that version has no release yet and CHANGELOG.md has a
# "## [X.Y.Z]" heading, prints nothing when it is already released, and exits
# non-zero for everything else: no version, no heading, or a gh failure other
# than "release not found". An auth or network error must stop the release,
# not read as "unreleased".
set -euo pipefail

v=$(sed -n 's/^version = "\(.*\)"$/\1/p' pyproject.toml | head -1)
[ -n "$v" ] || { echo "pyproject.toml names no version" >&2; exit 1; }

if out=$(gh release view "v$v" 2>&1); then
echo "v$v is already released" >&2
exit 0
fi
if ! printf '%s\n' "$out" | grep -q 'release not found'; then
printf 'gh release view v%s failed:\n%s\n' "$v" "$out" >&2
exit 1
fi

# A literal match, not a regex: the heading is exactly "## [X.Y.Z]", alone or
# followed by a space, so 0.7.3 matches neither "## [0x7x3]" nor "## [0.7.30]".
if awk -v h="## [$v]" 'index($0, h) == 1 &&
(length($0) == length(h) || substr($0, length(h) + 1, 1) == " ") {f = 1}
END {exit !f}' CHANGELOG.md; then
echo "v$v"
else
echo "v$v has no CHANGELOG heading; not releasing" >&2
exit 1
fi
68 changes: 68 additions & 0 deletions tests/test_release_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""`scripts/release-version.sh` must release only what it is sure of.

release.yml cuts whatever tag this prints. Two ways it used to get that wrong:
the CHANGELOG heading was matched as a regex, so the dots in a version matched
any character, and every `gh release view` failure counted as "not released",
so an auth or network error looked like a version waiting to ship. This runs
the script against a fake `gh` and asserts each case.

Run: tests/test_release_version.py (needs python3 and bash)
"""

from __future__ import annotations

import os
import subprocess
import tempfile
from pathlib import Path

SCRIPT = Path(__file__).resolve().parent.parent / "scripts" / "release-version.sh"

GH_RELEASED = "#!/bin/sh\necho 'title: v0.7.3'\n"
GH_MISSING = "#!/bin/sh\necho 'release not found' >&2\nexit 1\n"
GH_AUTH = "#!/bin/sh\necho 'HTTP 401: Bad credentials' >&2\nexit 1\n"


def run(version: str, changelog: str, gh: str) -> tuple[int, str]:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "pyproject.toml").write_text(f'[project]\nversion = "{version}"\n')
(root / "CHANGELOG.md").write_text(changelog)
bindir = root / "bin"
bindir.mkdir()
(bindir / "gh").write_text(gh)
(bindir / "gh").chmod(0o755)
env = {**os.environ, "PATH": f"{bindir}{os.pathsep}{os.environ['PATH']}"}
proc = subprocess.run(
[str(SCRIPT)], cwd=root, env=env, capture_output=True, text=True, check=False
)
return proc.returncode, proc.stdout.strip()


CASES = [
# (name, version, changelog, gh, expected exit, expected stdout)
("unreleased with heading", "0.7.3", "## [0.7.3] - 2026-11-17\n", GH_MISSING, 0, "v0.7.3"),
("heading with no date", "0.7.3", "## [0.7.3]\n", GH_MISSING, 0, "v0.7.3"),
("already released", "0.7.3", "## [0.7.3] - 2026-11-17\n", GH_RELEASED, 0, ""),
("no heading", "0.7.3", "## [0.7.2] - 2026-11-17\n", GH_MISSING, 1, ""),
("dots are not wildcards", "0.7.3", "## [0x7x3] - 2026-11-17\n", GH_MISSING, 1, ""),
("longer version is not a match", "0.7.3", "## [0.7.30] - 2026-11-17\n", GH_MISSING, 1, ""),
("gh auth failure is not unreleased", "0.7.3", "## [0.7.3] - 2026-11-17\n", GH_AUTH, 1, ""),
("no version", "", "## [0.7.3]\n", GH_MISSING, 1, ""),
]


def main() -> int:
failed = 0
for name, version, changelog, gh, want_code, want_out in CASES:
code, out = run(version, changelog, gh)
ok = code == want_code and out == want_out
failed += not ok
detail = "" if ok else f" (exit {code}, stdout {out!r})"
print(f"{'ok' if ok else 'FAIL'}: {name}{detail}")
return 1 if failed else 0


if __name__ == "__main__":
raise SystemExit(main())
Loading