#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Download the measured quality record (badges/quality.json on ci-badges). fetch-quality.py OUT.json Writes nothing (and exits 0) when main CI has not published one yet, so the Pages build shows a "not measured yet" dashboard instead of failing. """ import base64 import json import os from pathlib import Path import sys from urllib.error import HTTPError from urllib.request import Request, urlopen def main() -> None: out = Path(sys.argv[1]) api = os.environ["GITHUB_API_URL"].rstrip("/") repo = os.environ["GITHUB_REPOSITORY"] url = f"{api}/repos/{repo}/contents/badges/quality.json?ref=ci-badges" request = Request(url, headers={"Authorization": f"token {os.environ['GITEA_TOKEN']}"}) try: with urlopen(request, timeout=20) as response: content = base64.b64decode(json.load(response)["content"]) except HTTPError as error: if error.code == 404: print("No quality record published yet") return raise if not isinstance(json.loads(content), dict): sys.exit("invalid quality record") out.write_bytes(content) print(f"Fetched quality record ({len(content)} bytes)") if __name__ == "__main__": main()