diff --git a/.github/scripts/release_installers.py b/.github/scripts/release_installers.py new file mode 100644 index 00000000..212185da --- /dev/null +++ b/.github/scripts/release_installers.py @@ -0,0 +1,162 @@ +"""Daily check that the desktop installers exist (release-installers.yml). + +Looks at the newest published release of the repository and at the nightly +desktop build: + +* the release must carry an installer for every platform the desktop build + makes (Windows `*Setup*.exe`, macOS `.dmg`, Linux `.AppImage`). When one is + missing and no desktop build is running, the release is built again + (build-desktop-app.yaml dispatched with `release_tag`), once per release. +* the newest nightly run of build-desktop-app.yaml must not have failed, or + failed to start. + +What it finds goes to the job summary and to one issue, "Desktop installers +missing", opened or updated while something is wrong and closed once all is +well. The check fails when something is wrong that no rebuild is fixing yet. + +Usage: python release_installers.py +(GH_TOKEN needs contents: read, actions: write, issues: write) +""" + +import json +import os +import subprocess +import sys + +WORKFLOW = "build-desktop-app.yaml" +ISSUE_TITLE = "Desktop installers missing" +PLATFORMS = { + "Windows": lambda n: n.endswith(".exe") and "Setup" in n, + "macOS": lambda n: n.endswith(".dmg"), + "Linux": lambda n: n.endswith(".AppImage"), +} +FAILED = {"failure", "startup_failure", "timed_out"} + + +def marker(tag): + """Hidden note in the issue that a rebuild of `tag` was started.""" + return f"" + + +def missing_platforms(asset_names): + """Platforms without an installer among a release's asset names.""" + return [p for p, match in PLATFORMS.items() if not any(match(n) for n in asset_names)] + + +def newest_release(releases): + """The newest published release (prereleases count, drafts do not).""" + published = [r for r in releases if not r.get("draft") and r.get("published_at")] + return max(published, key=lambda r: r["published_at"], default=None) + + +def desktop_build_busy(runs): + """Whether a release or manual desktop build is queued or running.""" + return any( + r["status"] != "completed" and r["event"] in ("release", "workflow_dispatch") + for r in runs + ) + + +def nightly_failed(runs): + """The newest nightly run, when it failed (or failed to start).""" + nightly = [r for r in runs if r["event"] == "schedule"] + if nightly and nightly[0]["status"] == "completed" and nightly[0]["conclusion"] in FAILED: + return nightly[0] + return None + + +def rebuild_title(tag): + """The run name of a desktop build that rebuilds the release `tag`.""" + return f"Rebuild installers of {tag}" + + +def rebuilt(tag, runs, issue_body): + """Whether the release `tag` was already rebuilt once (issue note or run).""" + return marker(tag) in (issue_body or "") or any( + r.get("event") == "workflow_dispatch" and r.get("display_title") == rebuild_title(tag) + for r in runs) + + +def plan(release, runs, issue_body): + """What to report and whether to start a rebuild. + + Returns (problems, rebuild, failing): lines for the report, the tag to + rebuild (or None), and whether the check should fail. + """ + problems, rebuild, failing = [], None, False + if release: + tag = release["tag_name"] + missing = missing_platforms([a["name"] for a in release.get("assets", [])]) + if missing: + where = f"[{tag}]({release['html_url']})" + if desktop_build_busy(runs): + problems.append(f"{where} has no installer for {', '.join(missing)} yet; a desktop build is running.") + elif rebuilt(tag, runs, issue_body): + problems.append(f"{where} has no installer for {', '.join(missing)}, and its rebuild did not attach them. See the desktop build runs.") + failing = True + else: + problems.append(f"{where} has no installer for {', '.join(missing)}; its installers are being built again.") + rebuild = tag + failed = nightly_failed(runs) + if failed: + problems.append(f"The nightly desktop build [{failed['conclusion'].replace('_', ' ')}]({failed['html_url']}).") + failing = True + return problems, rebuild, failing + + +def gh(*args, data=None): + out = subprocess.run( + ["gh", "api", *args], check=True, capture_output=True, text=True, + input=json.dumps(data) if data is not None else None, + ).stdout + return json.loads(out) if out.strip() else None + + +def main(repo, branch): + releases = gh(f"repos/{repo}/releases?per_page=30") + # Releases, rebuilds and manual runs; pull request runs could push them + # off the page, so they are asked for by event. + runs = [] + for event in ("schedule", "release", "workflow_dispatch"): + runs += gh(f"repos/{repo}/actions/workflows/{WORKFLOW}/runs?event={event}&per_page=30")["workflow_runs"] + runs.sort(key=lambda r: r["created_at"], reverse=True) + issues = gh(f"repos/{repo}/issues?state=open&per_page=100") + issue = next((i for i in issues if i["title"] == ISSUE_TITLE and "pull_request" not in i), None) + + release = newest_release(releases) + problems, rebuild, failing = plan(release, runs, (issue["body"] or "") if issue else "") + + if rebuild: + gh("-X", "POST", f"repos/{repo}/actions/workflows/{WORKFLOW}/dispatches", "--input", "-", + data={"ref": branch, "inputs": {"release_tag": rebuild}}) + + summary = os.environ.get("GITHUB_STEP_SUMMARY") + text = "\n".join(f"- {p}" for p in problems) or ( + f"Every desktop installer of {release['tag_name']} is there." if release else "No release yet.") + if summary: + with open(summary, "a", encoding="utf-8") as f: + f.write(text + "\n") + print(text) + + if problems: + notes = ((issue["body"] or "") if issue else "").split("\n") + kept = [n for n in notes if n.startswith("