mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
Combine authorship validation with the required APK result (#1329)
Combine trusted source and squash-metadata validation with the exact-head APK compilation result. Preserve automatic trailer cleanup in the installed hooks. Validated with 10 authorship/hook tests and 9 combined-gate tests.
This commit is contained in:
1 parent
701e70bd3e
commit
792a69ebfe
4 files changed
+230
-35
No files matched your search
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Publish the APK result only after trusted source and metadata validation."""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from check_authorship import check_revision, event_violations
|
||||
|
||||
|
||||
def api(path, *args):
|
||||
return json.loads(subprocess.check_output(["gh", "api", path, *args], text=True))
|
||||
|
||||
|
||||
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 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 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")
|
||||
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}))
|
||||
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
|
||||
query = urlencode({"head_sha": sha, "event": source_event, "per_page": 20})
|
||||
runs = api(f"repos/{repo}/actions/workflows/build.yml/runs?{query}")["workflow_runs"]
|
||||
runs = [item for item in runs if item["head_sha"] == sha]
|
||||
if runs:
|
||||
latest = max(runs, key=lambda item: item["id"])
|
||||
jobs = api(f"repos/{repo}/actions/runs/{latest['id']}/jobs?per_page=100")["jobs"]
|
||||
state = compilation_state(latest, jobs)
|
||||
target = latest["html_url"]
|
||||
else:
|
||||
state = "pending"
|
||||
description = {"pending": "Source validated; awaiting APK compilation",
|
||||
"success": "Source validation and APK compilation passed",
|
||||
"failure": "APK compilation failed"}[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()
|
||||
@@ -0,0 +1,107 @@
|
||||
import contextlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
RELEASE = Path(__file__).resolve().parents[1] / "release"
|
||||
sys.path.insert(0, str(RELEASE))
|
||||
import validate_apk
|
||||
|
||||
|
||||
class ValidateApkTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
subprocess.run(["git", "init", "-q", self.root], check=True)
|
||||
self.git("config", "user.name", "Human Developer")
|
||||
self.git("config", "user.email", "human@example.org")
|
||||
self.git("commit", "--allow-empty", "-qm", "Baseline")
|
||||
self.git("update-ref", "refs/remotes/origin/main", "HEAD")
|
||||
self.statuses = []
|
||||
|
||||
def git(self, *args):
|
||||
return subprocess.check_output(["git", "-C", self.root, *args], text=True).strip()
|
||||
|
||||
def validate(self, message="Source change", body="", result="success", stale=False, completed=False, filename="source.kt"):
|
||||
(self.root / filename).write_text(message)
|
||||
self.git("add", filename)
|
||||
self.git("commit", "--allow-empty", "-qm", message)
|
||||
sha = self.git("rev-parse", "HEAD")
|
||||
pr = {"number": 1, "state": "open", "head": {"sha": sha}, "user": {"login": "human", "type": "User"},
|
||||
"title": "Source change", "body": body}
|
||||
event = {"number": 1, "pull_request": {"head": {"sha": "stale" if stale else sha}}}
|
||||
if completed:
|
||||
event = {"workflow_run": {"event": "pull_request", "head_sha": "stale" if stale else sha}}
|
||||
path = self.root / "event.json"
|
||||
path.write_text(json.dumps(event))
|
||||
|
||||
def api(path, *args):
|
||||
if "/statuses/" in path:
|
||||
self.statuses.append(dict(value.split("=", 1) for value in args if "=" in value))
|
||||
return {}
|
||||
if path.endswith("/pulls/1"):
|
||||
return pr
|
||||
if "/commits/" in path and path.endswith("/pulls"):
|
||||
return [pr]
|
||||
if "workflows/build.yml/runs?" in path:
|
||||
return {"workflow_runs": [{"id": 7, "head_sha": sha, "status": "completed", "html_url": "https://example.org/build"}]}
|
||||
if path.endswith("/jobs?per_page=100"):
|
||||
return {"jobs": [{"name": "Compile and test APK", "status": "in_progress" if result == "pending" else "completed", "conclusion": result}]}
|
||||
raise AssertionError(path)
|
||||
|
||||
env = {"GITHUB_EVENT_PATH": str(path), "GITHUB_REPOSITORY": "owner/repo",
|
||||
"GITHUB_EVENT_NAME": "workflow_run" if completed else "pull_request_target", "GITHUB_RUN_ID": "8"}
|
||||
with contextlib.chdir(self.root), patch.dict(os.environ, env), patch.object(validate_apk, "api", side_effect=api), patch.object(validate_apk, "fetch_source"):
|
||||
validate_apk.main()
|
||||
|
||||
def test_success_requires_compilation_and_clean_authorship(self):
|
||||
self.validate()
|
||||
self.assertEqual(self.statuses[-1]["state"], "success")
|
||||
|
||||
def test_successful_build_cannot_hide_bot_commit(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.validate(message="Change\n\nCo-authored-by: OpenAI Codex <codex@openai.com>")
|
||||
self.assertEqual(self.statuses[-1]["state"], "failure")
|
||||
|
||||
def test_successful_build_cannot_hide_squash_metadata(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.validate(body="Co-authored-by: Claude <claude@example.org>")
|
||||
self.assertEqual(self.statuses[-1]["state"], "failure")
|
||||
|
||||
def test_pending_or_failed_compilation_never_passes(self):
|
||||
for result in ("pending", "failure"):
|
||||
self.validate(result=result)
|
||||
self.assertEqual(self.statuses[-1]["state"], result)
|
||||
|
||||
def test_superseded_event_cannot_replace_current_status(self):
|
||||
self.validate(stale=True)
|
||||
self.assertEqual(self.statuses, [])
|
||||
|
||||
def test_completed_build_revalidates_current_squash_metadata(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.validate(body="Co-authored-by: Claude <claude@example.org>", completed=True)
|
||||
self.assertEqual(self.statuses[-1]["state"], "failure")
|
||||
|
||||
def test_superseded_build_cannot_replace_current_status(self):
|
||||
self.validate(stale=True, completed=True)
|
||||
self.assertEqual(self.statuses, [])
|
||||
|
||||
def test_documentation_passes_without_waiting_for_a_skipped_build(self):
|
||||
self.validate(filename="README.md", result="pending")
|
||||
self.assertEqual(self.statuses[-1]["state"], "success")
|
||||
|
||||
def test_renaming_code_to_documentation_still_requires_compilation(self):
|
||||
(self.root / "source.kt").write_text("source")
|
||||
self.git("add", "source.kt")
|
||||
self.git("commit", "-qm", "Add source")
|
||||
self.git("update-ref", "refs/remotes/origin/main", "HEAD")
|
||||
self.git("mv", "source.kt", "README.md")
|
||||
self.git("commit", "-qm", "Rename source")
|
||||
with contextlib.chdir(self.root):
|
||||
self.assertFalse(validate_apk.documentation_only(self.git("rev-parse", "HEAD")))
|
||||
Reference in new issue
Block a user