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
58 changes: 58 additions & 0 deletions .github/scripts/test_release_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
from pathlib import Path
import subprocess
import tempfile
import textwrap
import unittest

from release_version import ROOT, choose_version, parse_version, stamp, version_code
Expand Down Expand Up @@ -43,6 +46,61 @@ def test_stamps_both_apps_and_the_lockfile(self):
self.assertIn('versionName = "0.1.6"', android)
self.assertIn('versionCode = 1006', android)

def test_workflow_rejects_stale_automatic_releases_before_version_selection(self):
# Execute the workflow's actual shell against a local origin, without
# adding a YAML dependency to the release tests.
workflow = (ROOT / ".github/workflows/release.yml").read_text()
step = workflow.split(" - name: Choose the release version\n", 1)[1]
script = textwrap.dedent(step.split(" run: |\n", 1)[1].split("\n\n", 1)[0])
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
for name in [".github/scripts/release_version.py", "linux/Cargo.toml"]:
target = root / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text((ROOT / name).read_text())

def git(*args):
return subprocess.check_output(
["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
"-c", "commit.gpgsign=false", *args],
cwd=root, text=True, stderr=subprocess.DEVNULL,
).strip()

git("init", "--initial-branch=main")
git("add", ".")
git("commit", "-m", "Old main commit")
stale = git("rev-parse", "HEAD")
git("commit", "--allow-empty", "-m", "Current main commit")
current = git("rev-parse", "HEAD")
git("remote", "add", "origin", str(root))
output = root / "output"

for event, sha, succeeds in [
("workflow_run", current, True),
("workflow_run", stale, False),
("workflow_dispatch", stale, True),
("push", stale, True),
]:
with self.subTest(event=event, sha=sha):
git("checkout", "--detach", sha)
output.write_text("")
result = subprocess.run(
["bash", "-e", "-o", "pipefail", "-c", script], cwd=root,
env={**os.environ, "GITHUB_EVENT_NAME": event,
"GITHUB_OUTPUT": str(output),
"REQUESTED_VERSION": "" if event == "workflow_run" else "v0.1.6"},
capture_output=True, text=True,
)
if succeeds:
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn(f"sha={sha}\n", output.read_text())
if event != "workflow_run":
self.assertIn("tag=v0.1.6\n", output.read_text())
else:
self.assertNotEqual(result.returncode, 0)
self.assertIn("Refusing to release stale CI commit", result.stdout)
self.assertEqual(output.read_text(), "")


if __name__ == "__main__":
unittest.main()
11 changes: 10 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ jobs:
REQUESTED_VERSION: ${{ inputs.version || (github.event_name == 'push' && github.ref_name) || '' }}
run: |
args=()
if [ "$GITHUB_EVENT_NAME" = workflow_run ]; then args+=(--automatic); fi
if [ "$GITHUB_EVENT_NAME" = workflow_run ]; then
# Check the live tip after waiting for the release concurrency group.
# Keep this guard here: an older checkout may predate script fixes.
main_sha=$(git ls-remote --exit-code origin refs/heads/main | cut -f1)
if [ "$(git rev-parse HEAD)" != "$main_sha" ]; then
echo "::error::Refusing to release stale CI commit; run CI on the current main tip."
exit 1
fi
args+=(--automatic)
fi
python3 .github/scripts/release_version.py select "$REQUESTED_VERSION" "${args[@]}" >> "$GITHUB_OUTPUT"

linux:
Expand Down