Files
DroidDeck/tools/release/validate_apk.py
T
Kurt Himebauch 34fff40b56 ci: run Kotlin unit tests separately from APK builds (#3566)
Run Kotlin tests beside APK compilation while keeping merge and release checks intact. Pin the APK and test workflows to the same pull request base, retry Robolectric rate limits on both Maven Central hosts, and verify source parity when main advances.
2026-12-02 16:47:42 -05:00

187 lines
7.7 KiB
Python

#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
import time
from urllib.parse import urlencode
from check_authorship import check_revision, event_violations
RATE_LIMIT_WAIT = 1800
def rate_limit_wait():
try:
reset = json.loads(subprocess.check_output(["gh", "api", "rate_limit"], text=True))["resources"]["core"]["reset"]
except (subprocess.CalledProcessError, KeyError, ValueError):
return 60
return max(5, min(RATE_LIMIT_WAIT, reset - time.time() + 5))
def api(path, *args, attempts=3):
for attempt in range(attempts):
done = subprocess.run(["gh", "api", path, *args], text=True, capture_output=True)
if done.returncode == 0:
return json.loads(done.stdout)
if "--method" in args or "rate limit" not in done.stderr.lower() or attempt == attempts - 1:
sys.stderr.write(done.stderr)
raise subprocess.CalledProcessError(done.returncode, done.args, done.stdout, done.stderr)
time.sleep(rate_limit_wait())
def compilation_state(run, jobs):
for job in jobs:
if job["name"] in ("Compile and test APK", "Build APK"):
if job["status"] != "completed":
return "pending"
return "success" if job["conclusion"] == "success" else "failure"
return "failure" if run["status"] == "completed" else "pending"
def run_state(run):
if run["status"] != "completed":
return "pending"
return "success" if run["conclusion"] == "success" else "failure"
def latest_run(repo, workflow, sha, source_event):
query = urlencode({"head_sha": sha, "event": source_event, "per_page": 20})
runs = api(f"repos/{repo}/actions/workflows/{workflow}/runs?{query}")["workflow_runs"]
runs = [item for item in runs if item["head_sha"] == sha]
return max(runs, key=lambda item: item["id"]) if runs else None
def pull_request_run_base(run, sha):
# A PR can reopen at the same head SHA after main advances. Do not mix results
# from different event snapshots, even if both workflows individually passed.
title = run.get("display_title") or ""
match = re.search(rf" on {re.escape(sha)} against ([0-9a-f]{{40}})$", title)
return match[1] if match else None
def combined_state(build, tests):
if build == "failure":
return "failure", "APK compilation failed"
if tests == "failure":
return "failure", "Unit tests failed"
if build == "pending":
return "pending", "Source validated; awaiting APK compilation"
if tests == "pending":
return "pending", "APK compiled; awaiting unit tests"
return "success", "Source validation, APK compilation and unit tests passed"
def fetch_source(ref, sha):
subprocess.run(["git", "fetch", "--no-tags", "origin", ref], check=True)
fetched = subprocess.check_output(["git", "rev-parse", "FETCH_HEAD"], text=True).strip()
if fetched != sha:
raise RuntimeError("Pull request changed while validating")
def documentation_only(sha):
base = subprocess.check_output(["git", "merge-base", "origin/main", sha], text=True).strip()
paths = subprocess.check_output(["git", "diff", "--no-renames", "--name-only", "-z", base, sha]).split(b"\0")
return all(path.endswith(b".md") or path in (b"LICENSE", b".gitignore") for path in paths if path)
def trusted_list_violation(repo, pr, sha):
changed = subprocess.run(
["git", "diff", "--no-renames", "--quiet", f"origin/main...{sha}", "--", ".github/trusted-test-builds.txt"],
check=False,
)
if changed.returncode not in (0, 1):
raise RuntimeError("Could not check trusted contributor changes")
if changed.returncode == 0:
return []
opener = pr["user"]["login"]
permission = api(f"repos/{repo}/collaborators/{opener}/permission")
if permission["permission"] != "admin" and permission.get("role_name") != "maintain":
return [f"Only a maintainer may open a PR changing .github/trusted-test-builds.txt (opened by @{opener})"]
return []
def main():
with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as source:
event = json.load(source)
repo = os.environ["GITHUB_REPOSITORY"]
event_name = os.environ["GITHUB_EVENT_NAME"]
pr_number = event.get("number") if event_name == "pull_request_target" else None
if event_name == "workflow_dispatch":
pr_number = int(event.get("inputs", {}).get("pr_number") or 0)
run = event.get("workflow_run", {})
expected = event.get("pull_request", {}).get("head", {}).get("sha")
if event_name == "workflow_run":
expected = run["head_sha"]
if run["event"] == "pull_request":
prs = api(f"repos/{repo}/commits/{expected}/pulls")
owner = (run.get("head_repository") or {}).get("owner", {}).get("login")
if not prs and owner and run.get("head_branch"):
query = urlencode({"state": "open", "head": f"{owner}:{run['head_branch']}"})
prs = api(f"repos/{repo}/pulls?{query}")
pr_number = next((pr["number"] for pr in prs
if pr["state"] == "open" and pr["head"]["sha"] == expected), None)
if pr_number is None:
print("Ignoring a superseded or closed pull request build")
return
elif run["head_branch"] != event["repository"]["default_branch"]:
return
if pr_number:
pr = api(f"repos/{repo}/pulls/{pr_number}")
sha = pr["head"]["sha"]
if pr["state"] != "open" or (expected and sha != expected):
print("Ignoring a superseded or closed pull request event")
return
ref = f"refs/pull/{pr_number}/head"
source_event = "pull_request"
else:
pr = None
sha = expected or event.get("after") or os.environ["GITHUB_SHA"]
ref = sha
source_event = "push"
state = "failure"
description = "APK source validation failed"
target = f"https://github.com/{repo}/actions/runs/{os.environ['GITHUB_RUN_ID']}"
try:
fetch_source(ref, sha)
commits, errors = check_revision(sha)
if pr:
errors.extend(event_violations({"pull_request": pr}))
errors.extend(trusted_list_violation(repo, pr, sha))
if errors:
raise RuntimeError("\n".join(errors))
print(f"Source and metadata validated for {len(commits)} commits")
if pr and documentation_only(sha):
state = "success"
description = "Source validated; no APK source changes"
return
build_state = tests_state = "pending"
latest = latest_run(repo, "build.yml", sha, source_event)
if latest:
jobs = api(f"repos/{repo}/actions/runs/{latest['id']}/jobs?per_page=100")["jobs"]
build_state = compilation_state(latest, jobs)
target = latest["html_url"]
tests = latest_run(repo, "unit-tests.yml", sha, source_event)
if tests:
if pr and latest and build_state == "success":
build_base = pull_request_run_base(latest, sha)
tests_base = pull_request_run_base(tests, sha)
if not build_base or build_base != tests_base:
state = "pending"
description = "APK and unit tests must run against the same main commit"
return
tests_state = run_state(tests)
if tests_state == "failure" and build_state != "failure":
target = tests["html_url"]
state, description = combined_state(build_state, tests_state)
finally:
api(f"repos/{repo}/statuses/{sha}", "--method", "POST",
"-f", "context=Build APK", "-f", f"state={state}",
"-f", f"description={description}", "-f", f"target_url={target}")
if __name__ == "__main__":
main()