|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Tag and push a GitHub release from the version in package.json. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python docs/release.py # dry-run (shows what would happen) |
| 7 | + python docs/release.py --push # create tag and push to trigger release workflow |
| 8 | +
|
| 9 | +What it does: |
| 10 | + 1. Reads the version from package.json |
| 11 | + 2. Checks that the npm package version has been bumped (if published) |
| 12 | + 3. Checks that the git tag doesn't already exist on GitHub |
| 13 | + 4. Ensures dist/ is built and committed |
| 14 | + 5. Creates a git tag v{version} and pushes it to origin |
| 15 | + 6. The release workflow creates a GitHub Release and updates the floating v1 tag |
| 16 | +
|
| 17 | +Requires: |
| 18 | + - python3 (no external dependencies) |
| 19 | + - git on PATH |
| 20 | + - network access to npmjs.org |
| 21 | +""" |
| 22 | + |
| 23 | +import json |
| 24 | +import subprocess |
| 25 | +import sys |
| 26 | +import urllib.request |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 30 | +PACKAGE_JSON = REPO_ROOT / "package.json" |
| 31 | +NPM_PACKAGE = "@auths/verify-action" |
| 32 | +GITHUB_REPO = "auths-dev/auths-verify-github-action" |
| 33 | + |
| 34 | + |
| 35 | +def get_package_version() -> str: |
| 36 | + data = json.loads(PACKAGE_JSON.read_text()) |
| 37 | + version = data.get("version") |
| 38 | + if not version: |
| 39 | + print("ERROR: No version field in package.json", file=sys.stderr) |
| 40 | + sys.exit(1) |
| 41 | + return version |
| 42 | + |
| 43 | + |
| 44 | +def get_npm_version() -> str | None: |
| 45 | + url = f"https://registry.npmjs.org/{NPM_PACKAGE}/latest" |
| 46 | + req = urllib.request.Request(url, headers={"User-Agent": "auths-release-script/1.0"}) |
| 47 | + try: |
| 48 | + with urllib.request.urlopen(req, timeout=10) as resp: |
| 49 | + data = json.loads(resp.read()) |
| 50 | + return data.get("version") |
| 51 | + except Exception: |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +def git(*args: str) -> str: |
| 56 | + result = subprocess.run( |
| 57 | + ["git", *args], |
| 58 | + capture_output=True, |
| 59 | + text=True, |
| 60 | + cwd=REPO_ROOT, |
| 61 | + ) |
| 62 | + if result.returncode != 0: |
| 63 | + print(f"ERROR: git {' '.join(args)} failed:\n{result.stderr.strip()}", file=sys.stderr) |
| 64 | + sys.exit(1) |
| 65 | + return result.stdout.strip() |
| 66 | + |
| 67 | + |
| 68 | +def local_tag_exists(tag: str) -> bool: |
| 69 | + result = subprocess.run( |
| 70 | + ["git", "tag", "-l", tag], |
| 71 | + capture_output=True, |
| 72 | + text=True, |
| 73 | + cwd=REPO_ROOT, |
| 74 | + ) |
| 75 | + return bool(result.stdout.strip()) |
| 76 | + |
| 77 | + |
| 78 | +def remote_tag_exists(tag: str) -> bool: |
| 79 | + result = subprocess.run( |
| 80 | + ["git", "ls-remote", "--tags", "origin", f"refs/tags/{tag}"], |
| 81 | + capture_output=True, |
| 82 | + text=True, |
| 83 | + cwd=REPO_ROOT, |
| 84 | + ) |
| 85 | + return bool(result.stdout.strip()) |
| 86 | + |
| 87 | + |
| 88 | +def delete_local_tag(tag: str) -> None: |
| 89 | + subprocess.run( |
| 90 | + ["git", "tag", "-d", tag], |
| 91 | + capture_output=True, |
| 92 | + cwd=REPO_ROOT, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + push = "--push" in sys.argv |
| 98 | + |
| 99 | + version = get_package_version() |
| 100 | + tag = f"v{version}" |
| 101 | + print(f"package.json version: {version}") |
| 102 | + print(f"Git tag: {tag}") |
| 103 | + |
| 104 | + # Check npm for version collision (this action isn't published to npm, |
| 105 | + # but check anyway in case it ever is) |
| 106 | + published = get_npm_version() |
| 107 | + if published: |
| 108 | + print(f"npm version: {published}") |
| 109 | + if published == version: |
| 110 | + print(f"\nWARNING: Version {version} matches npm. Consider bumping.", file=sys.stderr) |
| 111 | + else: |
| 112 | + print("npm version: (not published)") |
| 113 | + |
| 114 | + # GitHub is the source of truth for tags. |
| 115 | + if remote_tag_exists(tag): |
| 116 | + print(f"\nERROR: Git tag {tag} already exists on origin.", file=sys.stderr) |
| 117 | + print("Bump the version in package.json or delete the remote tag/release first.", file=sys.stderr) |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + if local_tag_exists(tag): |
| 121 | + print(f"Local tag {tag} exists but not on origin — deleting stale local tag.") |
| 122 | + delete_local_tag(tag) |
| 123 | + |
| 124 | + # Check we're on a clean working tree |
| 125 | + status = git("status", "--porcelain") |
| 126 | + if status: |
| 127 | + print(f"\nERROR: Working tree is not clean:\n{status}", file=sys.stderr) |
| 128 | + print("Commit or stash changes before releasing.", file=sys.stderr) |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | + # Check dist/ is committed (the release workflow validates this too) |
| 132 | + diff = subprocess.run( |
| 133 | + ["git", "diff", "--name-only", "--", "dist/"], |
| 134 | + capture_output=True, |
| 135 | + text=True, |
| 136 | + cwd=REPO_ROOT, |
| 137 | + ) |
| 138 | + if diff.stdout.strip(): |
| 139 | + print(f"\nERROR: dist/ has uncommitted changes:\n{diff.stdout.strip()}", file=sys.stderr) |
| 140 | + print("Run `npm run build` and commit dist/ before releasing.", file=sys.stderr) |
| 141 | + sys.exit(1) |
| 142 | + |
| 143 | + if not push: |
| 144 | + print(f"\nDry run: would create and push tag {tag}") |
| 145 | + print("Run with --push to execute.") |
| 146 | + return |
| 147 | + |
| 148 | + print(f"\nCreating tag {tag}...", flush=True) |
| 149 | + result = subprocess.run( |
| 150 | + ["git", "tag", "-a", tag, "-m", f"release: {version}"], |
| 151 | + cwd=REPO_ROOT, |
| 152 | + ) |
| 153 | + if result.returncode != 0: |
| 154 | + print(f"\nERROR: git tag failed (exit {result.returncode})", file=sys.stderr) |
| 155 | + sys.exit(1) |
| 156 | + |
| 157 | + print(f"Pushing tag {tag} to origin...", flush=True) |
| 158 | + result = subprocess.run( |
| 159 | + ["git", "push", "origin", tag], |
| 160 | + cwd=REPO_ROOT, |
| 161 | + ) |
| 162 | + if result.returncode != 0: |
| 163 | + print(f"\nERROR: git push failed (exit {result.returncode})", file=sys.stderr) |
| 164 | + sys.exit(1) |
| 165 | + |
| 166 | + print(f"\nDone. Release workflow will run at:") |
| 167 | + print(f" https://github.com/{GITHUB_REPO}/actions") |
| 168 | + print(f"\nThe workflow will also update the floating v1 tag.") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments