|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check that the release metadata scattered across the repo agrees with itself. |
| 3 | +
|
| 4 | +A release touches four places, and nothing but care keeps them in sync: |
| 5 | +
|
| 6 | + app/build.gradle.kts versionCode / versionName |
| 7 | + fastlane/metadata/android/{locale}/changelogs/N.txt store changelog, named by versionCode |
| 8 | + version.json what the in-app update check reads |
| 9 | + CHANGELOG.md the human history |
| 10 | +
|
| 11 | +Getting one wrong is quiet: a missing changelogs/N.txt just means the store shows |
| 12 | +nothing, and a stale version.json prompts every user to "update" to what they have. |
| 13 | +
|
| 14 | +Run with no arguments from the repo root. Exits non-zero on the first failure. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import json |
| 20 | +import re |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +# Both F-Droid and Play cap a changelog at 500 characters and truncate past it. |
| 25 | +CHANGELOG_LIMIT = 500 |
| 26 | + |
| 27 | +LOCALES = ("en-US", "zh-CN") |
| 28 | + |
| 29 | +REPO = Path(__file__).resolve().parent.parent |
| 30 | + |
| 31 | + |
| 32 | +class Failures(list): |
| 33 | + def check(self, ok: bool, message: str) -> bool: |
| 34 | + if not ok: |
| 35 | + self.append(message) |
| 36 | + return ok |
| 37 | + |
| 38 | + |
| 39 | +def read_version() -> tuple[int, str]: |
| 40 | + text = (REPO / "app/build.gradle.kts").read_text() |
| 41 | + code = re.search(r"versionCode\s*=\s*(\d+)", text) |
| 42 | + name = re.search(r'versionName\s*=\s*"([^"]+)"', text) |
| 43 | + if not code or not name: |
| 44 | + sys.exit("could not parse versionCode/versionName out of app/build.gradle.kts") |
| 45 | + return int(code.group(1)), name.group(1) |
| 46 | + |
| 47 | + |
| 48 | +def main() -> int: |
| 49 | + version_code, version_name = read_version() |
| 50 | + print(f"versionCode={version_code} versionName={version_name}") |
| 51 | + f = Failures() |
| 52 | + |
| 53 | + # Store changelogs are named by versionCode, so a bump needs a new file per locale. |
| 54 | + for locale in LOCALES: |
| 55 | + path = REPO / f"fastlane/metadata/android/{locale}/changelogs/{version_code}.txt" |
| 56 | + if not f.check(path.exists(), f"missing {path.relative_to(REPO)} for versionCode {version_code}"): |
| 57 | + continue |
| 58 | + body = path.read_text() |
| 59 | + f.check(body.strip() != "", f"{path.relative_to(REPO)} is empty") |
| 60 | + f.check( |
| 61 | + len(body) <= CHANGELOG_LIMIT, |
| 62 | + f"{path.relative_to(REPO)} is {len(body)} chars, over the {CHANGELOG_LIMIT} store limit", |
| 63 | + ) |
| 64 | + |
| 65 | + # version.json is fetched from main by the in-app update check — if it disagrees |
| 66 | + # with versionName, users are told to update to a version that does not exist. |
| 67 | + version_json = REPO / "version.json" |
| 68 | + try: |
| 69 | + data = json.loads(version_json.read_text()) |
| 70 | + except (OSError, json.JSONDecodeError) as e: |
| 71 | + f.append(f"version.json is unreadable: {e}") |
| 72 | + else: |
| 73 | + f.check( |
| 74 | + data.get("version") == version_name, |
| 75 | + f"version.json says {data.get('version')!r}, app/build.gradle.kts says {version_name!r}", |
| 76 | + ) |
| 77 | + for field in ("release_notes", "release_notes_zh"): |
| 78 | + f.check(bool(data.get(field, "").strip()), f"version.json has no {field}") |
| 79 | + |
| 80 | + changelog = (REPO / "CHANGELOG.md").read_text() |
| 81 | + f.check( |
| 82 | + f"## [{version_name}]" in changelog, |
| 83 | + f"CHANGELOG.md has no '## [{version_name}]' section", |
| 84 | + ) |
| 85 | + f.check( |
| 86 | + re.search(rf"^\[{re.escape(version_name)}\]:\s*http", changelog, re.M) is not None, |
| 87 | + f"CHANGELOG.md has no '[{version_name}]: <url>' link reference at the bottom", |
| 88 | + ) |
| 89 | + |
| 90 | + if f: |
| 91 | + print("\nrelease metadata is out of sync:", file=sys.stderr) |
| 92 | + for problem in f: |
| 93 | + print(f" - {problem}", file=sys.stderr) |
| 94 | + return 1 |
| 95 | + |
| 96 | + print("release metadata is consistent") |
| 97 | + return 0 |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + sys.exit(main()) |
0 commit comments