| 1 | """Tags main as a nightly pre-release and starts build.yml for it (.github/workflows/nightly.yml). |
| 2 | |
| 3 | Tag: v<next minor>.0-nightly.<UTC yyyymmddhhmm>. It sorts above the last release and below the next |
| 4 | one (release_feed.rs), so a nightly install moves on to the next release on its own. Only a build |
| 5 | that runs a pre-release is offered pre-releases, so stable installs never see nightlies. |
| 6 | """ |
| 7 | |
| 8 | import datetime |
| 9 | import json |
| 10 | import re |
| 11 | import subprocess |
| 12 | import sys |
| 13 | import urllib.parse |
| 14 | |
| 15 | STABLE_TAG = re.compile(r"^v(\d+)\.(\d+)\.(\d+)$") |
| 16 | NIGHTLY_TAG = re.compile(r"^v\d+\.\d+\.\d+-nightly\.\d{12}$") |
| 17 | KEEP_NIGHTLIES = 7 |
| 18 | BUILD_WORKFLOW = "build.yml" |
| 19 | |
| 20 | |
| 21 | def next_version(tags: list[str]) -> str: |
| 22 | stable = [tuple(map(int, match.groups())) for match in map(STABLE_TAG.match, tags) if match] |
| 23 | major, minor, _ = max(stable, default=(0, 0, 0)) |
| 24 | return f"{major}.{minor + 1}.0" |
| 25 | |
| 26 | |
| 27 | def nightly_tag(tags: list[str], now: datetime.datetime) -> str: |
| 28 | return f"v{next_version(tags)}-nightly.{now.strftime('%Y%m%d%H%M')}" |
| 29 | |
| 30 | |
| 31 | def stale_nightlies(tags: list[str], keep: int = KEEP_NIGHTLIES) -> list[str]: |
| 32 | nightlies = sorted((tag for tag in tags if NIGHTLY_TAG.match(tag)), key=lambda tag: tag.rsplit(".", 1)[1]) |
| 33 | return nightlies[:-keep] if keep else nightlies |
| 34 | |
| 35 | |
| 36 | def run(*command: str) -> str: |
| 37 | return subprocess.run(command, check=True, capture_output=True, text=True).stdout.strip() |
| 38 | |
| 39 | |
| 40 | def gh_api(method: str, path: str, *fields: str) -> str: |
| 41 | return run("gh", "api", "-X", method, path, *fields) |
| 42 | |
| 43 | |
| 44 | def delete_nightly(repository: str, tag: str) -> None: |
| 45 | try: |
| 46 | release = json.loads(gh_api("GET", f"repos/{repository}/releases/tags/{urllib.parse.quote(tag)}")) |
| 47 | gh_api("DELETE", f"repos/{repository}/releases/{release['id']}") |
| 48 | except subprocess.CalledProcessError: |
| 49 | pass |
| 50 | run("git", "push", "--delete", "origin", tag) |
| 51 | |
| 52 | |
| 53 | def main() -> int: |
| 54 | repository = run("gh", "repo", "view", "--json", "nameWithOwner", "--jq", ".nameWithOwner") |
| 55 | if any(NIGHTLY_TAG.match(tag) for tag in run("git", "tag", "--points-at", "HEAD").split()): |
| 56 | print("main has not moved since the last nightly") |
| 57 | return 0 |
| 58 | tags = run("git", "tag", "--list", "v*").split() |
| 59 | tag = nightly_tag(tags, datetime.datetime.now(datetime.timezone.utc)) |
| 60 | run("git", "tag", tag) |
| 61 | run("git", "push", "origin", tag) |
| 62 | # A tag pushed with the workflow token starts no workflow; a dispatch does. |
| 63 | run("gh", "workflow", "run", BUILD_WORKFLOW, "--ref", tag) |
| 64 | print(f"started {BUILD_WORKFLOW} for {tag}") |
| 65 | for stale in stale_nightlies(tags + [tag]): |
| 66 | delete_nightly(repository, stale) |
| 67 | print(f"deleted {stale}") |
| 68 | return 0 |
| 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | sys.exit(main()) |