CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 24m48s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 3s
CI / Deploy (pull_request) Skipped
268 lines
11 KiB
Python
268 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate an existing draft release before resuming its image build."""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import importlib.util
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from urllib.error import HTTPError, URLError
|
|
from urllib.parse import quote, urlsplit
|
|
from urllib.request import Request, build_opener
|
|
|
|
_HERE = Path(__file__).resolve().parent
|
|
_ASSETS_SCRIPT = _HERE / "ci-release-assets.py"
|
|
_spec = importlib.util.spec_from_file_location("ci_release_assets", _ASSETS_SCRIPT)
|
|
if _spec is None or _spec.loader is None:
|
|
raise RuntimeError("Could not load release asset helpers")
|
|
_assets_module = importlib.util.module_from_spec(_spec)
|
|
sys.modules[_spec.name] = _assets_module
|
|
_spec.loader.exec_module(_assets_module)
|
|
|
|
PublishError = _assets_module.PublishError
|
|
_NoRedirect = _assets_module._NoRedirect
|
|
_assets = _assets_module._assets
|
|
_inputs = _assets_module._inputs
|
|
_json_request = _assets_module._json_request
|
|
|
|
_COMMIT = re.compile(r"[0-9a-fA-F]{40}\Z")
|
|
_ARCHIVES = re.compile(r"crank-community-(?:admin-api|mcp-server|ui)-.+\.tar\.gz\Z")
|
|
_MAX_ASSET_SIZE = 512 * 1024 * 1024
|
|
_MAX_TOTAL_SIZE = 1024 * 1024 * 1024
|
|
_REQUIRED_SOURCE_STATUSES = (
|
|
"CI / Required Checks (push)",
|
|
"CI / Rust Checks (push)",
|
|
"CI / UI Checks (push)",
|
|
)
|
|
|
|
|
|
def _fail(message: str) -> None:
|
|
raise PublishError(message)
|
|
|
|
|
|
def _git(*args: str) -> str:
|
|
try:
|
|
result = subprocess.run(
|
|
["git", *args], check=False, stdout=subprocess.PIPE,
|
|
stderr=subprocess.DEVNULL, text=True,
|
|
)
|
|
except OSError:
|
|
_fail("Could not inspect checked out Git history")
|
|
if result.returncode:
|
|
_fail("Resume source tag is unavailable or is not based on checked out main")
|
|
return result.stdout.strip()
|
|
|
|
|
|
def _check_source(tag: str, source_sha: str, env: dict[str, str]) -> None:
|
|
if env.get("GITHUB_EVENT_NAME") != "workflow_dispatch" or env.get("GITHUB_REF") != "refs/heads/main":
|
|
_fail("Resume is allowed only for manual dispatches from main")
|
|
if not _COMMIT.fullmatch(source_sha):
|
|
_fail("RESUME_SOURCE_SHA must be an exact 40-character commit SHA")
|
|
tag_sha = _git("rev-parse", "--verify", f"refs/tags/{tag}^{{commit}}")
|
|
if tag_sha.lower() != source_sha.lower():
|
|
_fail("Release tag does not point to the requested source commit")
|
|
try:
|
|
result = subprocess.run(
|
|
["git", "merge-base", "--is-ancestor", tag_sha, "HEAD"],
|
|
check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
)
|
|
except OSError:
|
|
_fail("Could not verify release source ancestry")
|
|
if result.returncode:
|
|
_fail("Release source is not an ancestor of checked out main")
|
|
|
|
|
|
def _api_tag_commit(api: str, tag: str, token: str, opener) -> str:
|
|
result, status = _json_request(
|
|
opener, "GET", f"{api}/tags/{quote(tag, safe='')}", token
|
|
)
|
|
if status != 200 or not isinstance(result, dict) or result.get("name") != tag:
|
|
_fail("Gitea did not return the requested tag")
|
|
commit = result.get("commit")
|
|
if not isinstance(commit, dict) or not isinstance(commit.get("sha"), str):
|
|
_fail("Gitea returned a malformed tag commit")
|
|
return commit["sha"]
|
|
|
|
|
|
def _download(origin: str, item: dict, token: str, opener) -> bytes:
|
|
url = urlsplit(item["browser_download_url"])
|
|
trusted = urlsplit(origin)
|
|
if (
|
|
url.scheme != "https" or url.netloc != trusted.netloc
|
|
or url.username or url.password or url.fragment or not url.path.startswith("/")
|
|
):
|
|
_fail(f"Release asset {item['name']} has an untrusted download URL")
|
|
size = item["size"]
|
|
if size < 0 or size > _MAX_ASSET_SIZE:
|
|
_fail(f"Release asset {item['name']} exceeds the download size limit")
|
|
request = Request(item["browser_download_url"], headers={"Authorization": f"token {token}"})
|
|
chunks: list[bytes] = []
|
|
total = 0
|
|
try:
|
|
with opener.open(request, timeout=30) as response:
|
|
while True:
|
|
chunk = response.read(min(1024 * 1024, size + 1 - total))
|
|
if not chunk:
|
|
break
|
|
total += len(chunk)
|
|
if total > size:
|
|
_fail(f"Release asset {item['name']} size does not match Gitea metadata")
|
|
chunks.append(chunk)
|
|
except PublishError:
|
|
raise
|
|
except HTTPError as exc:
|
|
status = exc.code
|
|
exc.close()
|
|
_fail(f"Could not download release asset (HTTP {status})")
|
|
except (URLError, TimeoutError, OSError):
|
|
_fail("Could not download release asset or request timed out")
|
|
if total != size:
|
|
_fail(f"Release asset {item['name']} size does not match Gitea metadata")
|
|
return b"".join(chunks)
|
|
|
|
|
|
def _sha_manifest(data: bytes, expected: set[str]) -> dict[str, str]:
|
|
try:
|
|
lines = data.decode("utf-8").splitlines()
|
|
except UnicodeDecodeError:
|
|
_fail("SHA256SUMS is not valid UTF-8")
|
|
parsed: dict[str, str] = {}
|
|
for line in lines:
|
|
match = re.fullmatch(r"([0-9a-fA-F]{64}) [ *](?:dist/)?([^\s/\\]+)", line)
|
|
if not match or match.group(2) in parsed:
|
|
_fail("SHA256SUMS has an invalid or duplicate entry")
|
|
parsed[match.group(2)] = match.group(1).lower()
|
|
if set(parsed) != expected:
|
|
_fail("SHA256SUMS does not cover exactly all release payload assets")
|
|
return parsed
|
|
|
|
|
|
def _verify_payloads(payload: dict[str, bytes], manifest_data: bytes, tag: str) -> dict[str, str]:
|
|
archives = sorted(name for name in payload if _ARCHIVES.fullmatch(name))
|
|
checksums = [name for name in payload if name.endswith("-checksums.txt")]
|
|
sboms = [name for name in payload if name.endswith("-sbom.spdx.json")]
|
|
expected_archives = {
|
|
f"crank-community-admin-api-{tag}.tar.gz",
|
|
f"crank-community-mcp-server-{tag}.tar.gz",
|
|
f"crank-community-ui-{tag}.tar.gz",
|
|
}
|
|
if (
|
|
set(archives) != expected_archives
|
|
or checksums != [f"crank-community-{tag}-checksums.txt"]
|
|
or sboms != [f"crank-community-{tag}-sbom.spdx.json"]
|
|
):
|
|
_fail("Draft release is missing an expected archive, checksums file, or SBOM")
|
|
if len(payload) != 5:
|
|
_fail("Draft release contains an unexpected payload asset set")
|
|
manifest = _sha_manifest(manifest_data, set(payload))
|
|
hashes = {name: hashlib.sha256(data).hexdigest() for name, data in payload.items()}
|
|
if any(manifest[name] != digest for name, digest in hashes.items()):
|
|
_fail("SHA256SUMS does not match a release payload asset")
|
|
try:
|
|
checksum_lines = payload[checksums[0]].decode("utf-8").splitlines()
|
|
except UnicodeDecodeError:
|
|
_fail("Release checksums file is not valid UTF-8")
|
|
checksum_entries: dict[str, str] = {}
|
|
for line in checksum_lines:
|
|
match = re.fullmatch(r"([0-9a-fA-F]{64}) [ *](?:dist/)?([^\s/\\]+)", line)
|
|
if not match or match.group(2) in checksum_entries:
|
|
_fail("Release checksums file has an invalid or duplicate entry")
|
|
checksum_entries[match.group(2)] = match.group(1).lower()
|
|
if set(checksum_entries) != set(archives) or any(
|
|
checksum_entries[name] != hashlib.sha256(payload[name]).hexdigest() for name in archives
|
|
):
|
|
_fail("Release checksums file does not match all three release archives")
|
|
return hashes
|
|
|
|
|
|
def _require_source_ci(status_data: object, status_code: int) -> None:
|
|
if status_code != 200 or not isinstance(status_data, dict):
|
|
_fail("Could not read required source CI statuses")
|
|
statuses = status_data.get("statuses")
|
|
if not isinstance(statuses, list):
|
|
_fail("Gitea did not return source CI status records")
|
|
for context in _REQUIRED_SOURCE_STATUSES:
|
|
matches = [
|
|
item for item in statuses
|
|
if isinstance(item, dict) and item.get("context") == context
|
|
]
|
|
if not matches:
|
|
_fail(f"Required source CI status is missing: {context}")
|
|
if any(not isinstance(item.get("updated_at"), str) for item in matches):
|
|
_fail(f"Required source CI status has invalid timestamp: {context}")
|
|
latest = max(matches, key=lambda item: item["updated_at"])
|
|
if latest.get("status") != "success":
|
|
_fail(f"Required source CI status is not successful: {context}")
|
|
|
|
|
|
def verify(env=None, opener=None) -> dict[str, object]:
|
|
env = os.environ if env is None else env
|
|
origin, api, tag, token = _inputs(env)
|
|
source_sha = env.get("RESUME_SOURCE_SHA", "")
|
|
_check_source(tag, source_sha, env)
|
|
opener = opener or build_opener(_NoRedirect())
|
|
|
|
release, status = _json_request(
|
|
opener, "GET", f"{api}/releases/tags/{quote(tag, safe='')}", token, allow_404=True
|
|
)
|
|
if status == 404 or not isinstance(release, dict):
|
|
_fail("The requested release does not already exist")
|
|
release_id = release.get("id")
|
|
if (
|
|
release.get("tag_name") != tag or release.get("draft") is not True
|
|
or not isinstance(release_id, int) or release_id <= 0
|
|
):
|
|
_fail("The requested release is not the expected existing draft")
|
|
if _api_tag_commit(api, tag, token, opener).lower() != source_sha.lower():
|
|
_fail("Gitea tag does not point to the requested source commit")
|
|
status_data, status_code = _json_request(
|
|
opener, "GET", f"{api}/commits/{source_sha}/status", token
|
|
)
|
|
_require_source_ci(status_data, status_code)
|
|
existing = _assets(api, release_id, token, opener)
|
|
if len(existing) != 6:
|
|
_fail("Draft release must contain exactly the five expected payloads and SHA256SUMS")
|
|
sums_names = [name for name in existing if name == "SHA256SUMS"]
|
|
if len(sums_names) != 1:
|
|
_fail("Draft release must contain one canonical SHA256SUMS asset")
|
|
if sum(item["size"] for item in existing.values()) > _MAX_TOTAL_SIZE:
|
|
_fail("Draft release payload exceeds the total download size limit")
|
|
payload: dict[str, bytes] = {}
|
|
for name, item in existing.items():
|
|
payload[name] = _download(origin, item, token, opener)
|
|
if "SHA256SUMS" not in payload:
|
|
_fail("Draft release must contain one canonical SHA256SUMS asset")
|
|
sums = payload.pop("SHA256SUMS")
|
|
hashes = _verify_payloads(payload, sums, tag)
|
|
return {
|
|
"tag": tag,
|
|
"release_id": release_id,
|
|
"source_sha": source_sha.lower(),
|
|
"assets": hashes,
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
try:
|
|
result = verify()
|
|
except PublishError as exc:
|
|
print(f"Gitea release resume: {exc}", file=sys.stderr)
|
|
return 1
|
|
output = os.environ.get("GITHUB_OUTPUT")
|
|
if not output:
|
|
print("GITHUB_OUTPUT is unavailable", file=sys.stderr)
|
|
return 1
|
|
with open(output, "a", encoding="utf-8") as target:
|
|
target.write(f"release_id={result['release_id']}\n")
|
|
target.write(f"source_sha={result['source_sha']}\n")
|
|
target.write(f"release_tag={result['tag']}\n")
|
|
print(f"Validated existing draft {result['tag']} (id {result['release_id']}) and source CI")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|