diff --git a/.github/workflows/RELEASE-PROCESS.md b/.github/workflows/RELEASE-PROCESS.md index a21e316ea7..292345db34 100644 --- a/.github/workflows/RELEASE-PROCESS.md +++ b/.github/workflows/RELEASE-PROCESS.md @@ -134,7 +134,7 @@ GitHub Release creation may finish before or after the development bump and PR c **Steps**: 1. Checkout repository at tag -2. Extract version from tag name +2. Extract and validate the tag as `vX.Y.Z`; fail immediately on a malformed tag, before publishing the version output or running release commands 3. Check if release already exists 4. Generate release notes from commits 5. Create the GitHub Release without additional uploaded assets @@ -160,6 +160,7 @@ GitHub Release creation may finish before or after the development bump and PR c - Tags must follow format: `v{MAJOR}.{MINOR}.{PATCH}` - Example valid versions: `v0.1.11`, `v0.2.0`, `v1.0.0` +- The release workflow rejects malformed tags before checking or creating a release, and passes the validated version to later shell steps through environment variables rather than interpolating it into shell source - Auto-increment only bumps patch version - Cannot create duplicate tags (workflow will fail) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dde3c0e055..508d89d180 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,13 +21,16 @@ jobs: id: version run: | VERSION=${GITHUB_REF#refs/tags/} - echo "tag=$VERSION" >> $GITHUB_OUTPUT + if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "Error: '$VERSION' is not a valid release tag (expected vX.Y.Z)" + exit 1 + fi + echo "tag=$VERSION" >> "$GITHUB_OUTPUT" echo "Building release for $VERSION" - name: Check if release already exists id: check_release run: | - VERSION="${{ steps.version.outputs.tag }}" if gh release view "$VERSION" >/dev/null 2>&1; then echo "exists=true" >> $GITHUB_OUTPUT echo "Release $VERSION already exists, skipping..." @@ -36,12 +39,14 @@ jobs: echo "Release $VERSION does not exist, proceeding..." fi env: + VERSION: ${{ steps.version.outputs.tag }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Generate release notes if: steps.check_release.outputs.exists == 'false' + env: + VERSION: ${{ steps.version.outputs.tag }} run: | - VERSION="${{ steps.version.outputs.tag }}" VERSION_NO_V=${VERSION#v} # Find previous tag @@ -79,10 +84,10 @@ jobs: - name: Create GitHub Release if: steps.check_release.outputs.exists == 'false' run: | - VERSION="${{ steps.version.outputs.tag }}" VERSION_NO_V=${VERSION#v} gh release create "$VERSION" \ --title "Spec Kit - $VERSION_NO_V" \ --notes-file release_notes.md env: + VERSION: ${{ steps.version.outputs.tag }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/tests/test_github_workflows.py b/tests/test_github_workflows.py index a45d09f527..d51293c597 100644 --- a/tests/test_github_workflows.py +++ b/tests/test_github_workflows.py @@ -400,6 +400,210 @@ def test_publish_tag_validation_uses_environment_variable(): assert "${{ inputs.tag }}" not in step["run"] +def _release_workflow_steps(): + workflow = yaml.safe_load( + (WORKFLOWS_DIR / "release.yml").read_text(encoding="utf-8") + ) + return {step["name"]: step for step in workflow["jobs"]["release"]["steps"]} + + +RELEASE_VERSION_STEPS = ( + "Check if release already exists", + "Generate release notes", + "Create GitHub Release", +) + + +def test_release_version_uses_environment_variables(): + steps = _release_workflow_steps() + for name in RELEASE_VERSION_STEPS: + step = steps[name] + assert step["env"]["VERSION"] == "${{ steps.version.outputs.tag }}" + assert "${{ steps.version.outputs.tag }}" not in step["run"] + + +@requires_bash +@pytest.mark.parametrize("tag", ["v0.1.11", "v0.2.0", "v1.0.0"]) +def test_release_tag_validation_accepts_valid_tag(tmp_path, tag): + output = tmp_path / "github output" + result = subprocess.run( + ["bash", "-e", "-o", "pipefail", "-c", + _release_workflow_steps()["Extract version from tag"]["run"]], + cwd=tmp_path, + env={**os.environ, "GITHUB_REF": f"refs/tags/{tag}", "GITHUB_OUTPUT": str(output)}, + capture_output=True, text=True, check=False, + ) + assert result.returncode == 0, result.stderr + assert output.read_text() == f"tag={tag}\n" + assert f"Building release for {tag}" in result.stdout + + +@requires_bash +@pytest.mark.parametrize("tag", [ + "", "1.2.3", "v1.2", "v1.2.3.4", "v1.2.3-rc1", "v1.2.3+build", + "v1.a.3", "v1.2.3\nother=value", "v1.2.3$(touch${IFS}injected)", + "v1.2.3`touch${IFS}injected`", 'v1.2.3";touch${IFS}injected;#', +]) +def test_release_tag_validation_rejects_invalid_tag_before_output(tmp_path, tag): + output = tmp_path / "github-output" + result = subprocess.run( + ["bash", "-e", "-o", "pipefail", "-c", + _release_workflow_steps()["Extract version from tag"]["run"]], + cwd=tmp_path, + env={**os.environ, "GITHUB_REF": f"refs/tags/{tag}", "GITHUB_OUTPUT": str(output)}, + capture_output=True, text=True, check=False, + ) + assert result.returncode != 0 + assert "is not a valid release tag" in result.stdout + assert not output.exists() + assert not (tmp_path / "injected").exists() + + +@requires_bash +@pytest.mark.parametrize("step_name", RELEASE_VERSION_STEPS) +@pytest.mark.parametrize("tag", ["v1.2.3", "v1.2.3$(touch${IFS}injected)"]) +def test_release_steps_treat_version_as_data(tmp_path, step_name, tag): + step = _release_workflow_steps()[step_name] + script = step["run"].replace("${{ steps.version.outputs.tag }}", tag) + stubs = """ +gh() { printf '%s\\n' "$@" >> gh-args; } +git() { + case "$1" in + tag) echo v0.0.1 ;; + rev-list) echo 1 ;; + log) echo '- Example change' ;; + *) return 1 ;; + esac +} +""" + result = subprocess.run( + ["bash", "-e", "-o", "pipefail", "-c", stubs + script], + cwd=tmp_path, + env={**os.environ, "VERSION": tag, "GITHUB_TOKEN": "test-token", + "GITHUB_OUTPUT": str(tmp_path / "github-output")}, + capture_output=True, text=True, check=False, + ) + assert result.returncode == 0, result.stderr + assert not (tmp_path / "injected").exists() + if step_name == "Generate release notes": + assert f"spec-kit.git@{tag}" in (tmp_path / "release_notes.md").read_text() + else: + assert (tmp_path / "gh-args").read_text().splitlines()[:3] == [ + "release", "view" if step_name.startswith("Check") else "create", tag, + ] + + +@requires_bash +@pytest.mark.parametrize(("tag", "previous_tag", "commit_count", "release_exists"), [ + ("v0.1.11", True, 2, False), + ("v0.2.0", True, 2, False), + ("v1.0.0", True, 2, False), + ("v1.2.3", False, 2, False), + ("v1.2.3", False, 22, False), + ("v1.2.3", True, 2, True), + ("v1.2", True, 2, False), + ("v1.2.3$(touch${IFS}injected)", True, 2, False), + ("v1.2.3`touch${IFS}injected`", True, 2, False), + pytest.param( + 'v1.2.3";touch${IFS}injected;#', True, 2, False, + marks=pytest.mark.skipif( + sys.platform == "win32", + reason="Windows disallows double quotes in loose-ref filenames", + ), + ), +]) +def test_release_workflow_simulation( + tmp_path, tag, previous_tag, commit_count, release_exists +): + env = { + **os.environ, + "GIT_AUTHOR_NAME": "Release Test", "GIT_AUTHOR_EMAIL": "test@example.invalid", + "GIT_COMMITTER_NAME": "Release Test", "GIT_COMMITTER_EMAIL": "test@example.invalid", + "GITHUB_REF": f"refs/tags/{tag}", + "TEST_PYTHON": sys.executable, + "GH_CALLS": str(tmp_path / "gh-calls"), + "RELEASE_VIEW_STATUS": "0" if release_exists else "1", + } + + def git(*args): + return subprocess.run( + ["git", "-c", "commit.gpgsign=false", "-c", "core.hooksPath=/dev/null", *args], + cwd=tmp_path, env=env, capture_output=True, text=True, check=True, + ) + + git("init", "--quiet") + for index in range(commit_count): + git("commit", "--quiet", "--allow-empty", "-m", f"Release change {index}") + if previous_tag and index == 0: + git("tag", "v0.1.0") + git("tag", tag) + git("checkout", "--quiet", "--detach", f"refs/tags/{tag}") + + steps = _release_workflow_steps() + values = {"${{ secrets.GITHUB_TOKEN }}": "test-token"} + ran = [] + stub = """ +gh() { + "$TEST_PYTHON" -c 'import json, sys; print(json.dumps(sys.argv[1:]))' "$@" >> "$GH_CALLS" + if [ "$2" = view ]; then return "$RELEASE_VIEW_STATUS"; fi +} +""" + for index, (name, step) in enumerate(steps.items()): + if "run" not in step: + continue + if "if" in step: + assert step["if"] == "steps.check_release.outputs.exists == 'false'" + if values["${{ steps.check_release.outputs.exists }}"] != "false": + continue + output = tmp_path / f"output-{index}" + step_env = {key: values.get(value, value) for key, value in step.get("env", {}).items()} + assert "shell" not in step + # An unspecified shell on Linux uses bash -e, without pipefail. + result = subprocess.run( + ["bash", "-e", "-c", stub + step["run"]], + cwd=tmp_path, env={**env, **step_env, "GITHUB_OUTPUT": str(output)}, + capture_output=True, text=True, check=False, + ) + ran.append(name) + if result.returncode: + break + if output.exists(): + for line in output.read_text().splitlines(): + key, value = line.split("=", 1) + values[f"${{{{ steps.{step['id']}.outputs.{key} }}}}"] = value + + calls_path = tmp_path / "gh-calls" + calls = [json.loads(line) for line in calls_path.read_text().splitlines()] if calls_path.exists() else [] + assert not (tmp_path / "injected").exists() + notes_path = tmp_path / "release_notes.md" + if not re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", tag): + assert result.returncode != 0 + assert "is not a valid release tag" in result.stdout + assert ran == ["Extract version from tag"] + assert not calls + assert not notes_path.exists() + return + + assert result.returncode == 0, f"{ran[-1]}: {result.stdout}\n{result.stderr}" + assert calls[0] == ["release", "view", tag] + if release_exists: + assert ran == ["Extract version from tag", "Check if release already exists"] + assert len(calls) == 1 + assert not notes_path.exists() + else: + assert ran == ["Extract version from tag", *RELEASE_VERSION_STEPS] + assert calls[1:] == [[ + "release", "create", tag, "--title", f"Spec Kit - {tag[1:]}", + "--notes-file", "release_notes.md", + ]] + notes = notes_path.read_text() + assert f"spec-kit.git@{tag}" in notes + assert "## What's Changed" in notes + assert f"- Release change {commit_count - 1}" in notes + expected_count = min(commit_count, 20) if not previous_tag else commit_count - 1 + assert notes.count("- Release change ") == expected_count + + @requires_bash def test_publish_tag_validation_accepts_valid_tag(tmp_path): _write_project_version(tmp_path, "1.2.3")