#!/usr/bin/env python3 """Create or update a Gitea Release and stream binary attachments via Gitea API.""" from __future__ import annotations import argparse import hashlib import json import os import re import subprocess import sys import urllib.error import urllib.parse import urllib.request from pathlib import Path ASSET_SUFFIXES = (".zip", ".appimage", ".deb", ".exe", ".dmg") def run_git_origin_url() -> str: try: output = subprocess.check_output( ["git", "remote", "get-url", "origin"], text=True, stderr=subprocess.DEVNULL, ) return output.strip() except (subprocess.CalledProcessError, FileNotFoundError): return "" def parse_server_and_repo(remote_url: str) -> tuple[str, str]: """Derive (server_url, owner/repo) from an HTTPS or SSH git remote URL.""" url = remote_url.strip() if not url: return "", "" https_match = re.match(r"^(https?://[^/]+)/(.+?)(?:\.git)?/?$", url) if https_match: return https_match.group(1).rstrip("/"), https_match.group(2).strip("/") ssh_match = re.match(r"^(?:ssh://)?[^@]+@([^:/]+)[:/](.+?)(?:\.git)?/?$", url) if ssh_match: host = ssh_match.group(1) repo_path = ssh_match.group(2).strip("/") return f"https://{host}", repo_path return "", "" def extract_release_notes(changelog_path: Path, tag: str) -> str: """Extract the matching version section from docs/CHANGELOG.md.""" if not changelog_path.is_file(): return f"Release {tag}" version = tag.lstrip("vV") text = changelog_path.read_text(encoding="utf-8") lines = text.splitlines() heading_pattern = re.compile( rf"^##\s+(?:\[{re.escape(version)}\]|{re.escape(version)}|{re.escape(tag)})(?:\s|$)" ) next_heading_pattern = re.compile(r"^##\s+") start_idx: int | None = None for idx, line in enumerate(lines): if heading_pattern.match(line.strip()): start_idx = idx + 1 break if start_idx is None: return f"Release {tag}" collected: list[str] = [] for line in lines[start_idx:]: if next_heading_pattern.match(line.strip()): break collected.append(line) while collected and (not collected[-1].strip() or collected[-1].strip() == "---"): collected.pop() while collected and not collected[0].strip(): collected.pop(0) notes = "\n".join(collected).strip() return notes or f"Release {tag}" def compute_sha256(file_path: Path) -> str: digest = hashlib.sha256() with file_path.open("rb") as handle: while chunk := handle.read(1024 * 1024): digest.update(chunk) return digest.hexdigest() def collect_release_assets(assets_dir: Path) -> list[tuple[Path, str]]: """Collect distributable assets directly under assets_dir and write SHA256SUMS.txt.""" if not assets_dir.is_dir(): return [] distributables = sorted( ( entry for entry in assets_dir.iterdir() if entry.is_file() and entry.name.lower().endswith(ASSET_SUFFIXES) ), key=lambda p: p.name, ) if not distributables: return [] records: list[tuple[Path, str]] = [] checksum_lines: list[str] = [] for artifact in distributables: sha = compute_sha256(artifact) records.append((artifact, sha)) checksum_lines.append(f"{sha} {artifact.name}") sums_path = assets_dir / "SHA256SUMS.txt" sums_path.write_text("\n".join(checksum_lines) + "\n", encoding="utf-8") sums_sha = compute_sha256(sums_path) records.append((sums_path, sums_sha)) return records def api_request( method: str, url: str, token: str, payload: dict | None = None, ) -> tuple[int, dict | list | str | None]: headers = { "Authorization": f"token {token}", "Accept": "application/json", } data = None if payload is not None: headers["Content-Type"] = "application/json" data = json.dumps(payload).encode("utf-8") request = urllib.request.Request(url, data=data, headers=headers, method=method) try: with urllib.request.urlopen(request, timeout=30) as response: raw = response.read().decode("utf-8", errors="replace") return response.status, json.loads(raw) if raw.strip() else None except urllib.error.HTTPError as exc: raw = exc.read().decode("utf-8", errors="replace") try: body: dict | list | str | None = json.loads(raw) if raw.strip() else None except json.JSONDecodeError: body = raw return exc.code, body def upload_asset_with_curl( upload_url: str, token: str, file_path: Path, ) -> None: """Stream multipart file upload using curl to avoid buffering ~200MB assets in memory.""" proc = subprocess.run( [ "curl", "-sS", "-w", "\n%{http_code}", "-X", "POST", "-H", f"Authorization: token {token}", "-F", f"attachment=@{file_path}", upload_url, ], text=True, capture_output=True, ) if proc.returncode != 0: raise RuntimeError( f"curl upload failed for {file_path.name} (exit={proc.returncode}): {proc.stderr.strip()}" ) lines = proc.stdout.splitlines() status_str = lines[-1].strip() if lines else "" body = "\n".join(lines[:-1]).strip() try: status_code = int(status_str) except ValueError as exc: raise RuntimeError(f"Unexpected curl output for {file_path.name}: {proc.stdout}") from exc if status_code == 413: raise RuntimeError( f"Upload rejected with HTTP 413 Payload Too Large for {file_path.name}. " "Check Gitea app.ini [attachment] MAX_SIZE and Nginx/Ingress client_max_body_size (>= 300M)." ) if status_code >= 400: raise RuntimeError( f"Upload failed for {file_path.name} (HTTP {status_code}): {body}" ) def parse_args(argv: list[str] | None = None) -> argparse.Namespace: parser = argparse.ArgumentParser(description="Publish Gitea Release and client binaries.") parser.add_argument("--tag", required=True, help="Git release tag, e.g. v0.77.0") parser.add_argument( "--assets-dir", default="client/release", help="Directory containing packaged client binaries (default: client/release)", ) parser.add_argument( "--changelog", default="docs/CHANGELOG.md", help="Path to changelog markdown file (default: docs/CHANGELOG.md)", ) parser.add_argument( "--server", default="", help="Gitea base URL (defaults to GITHUB_SERVER_URL or origin remote host)", ) parser.add_argument( "--repo", default="", help="Repository slug owner/name (defaults to GITHUB_REPOSITORY or origin remote)", ) parser.add_argument( "--dry-run", action="store_true", help="Verify release notes and asset checksums without calling remote Gitea APIs", ) return parser.parse_args(argv) def main(argv: list[str] | None = None) -> int: args = parse_args(argv) derived_server, derived_repo = parse_server_and_repo(run_git_origin_url()) server = (args.server or os.environ.get("GITHUB_SERVER_URL") or derived_server).rstrip("/") repo = (args.repo or os.environ.get("GITHUB_REPOSITORY") or derived_repo).strip("/") if not server or not repo: print("error: unable to resolve Gitea server URL or owner/repo slug", file=sys.stderr) return 1 notes = extract_release_notes(Path(args.changelog), args.tag) assets_dir = Path(args.assets_dir) assets = collect_release_assets(assets_dir) if args.dry_run: print(f"server: {server}") print(f"repo: {repo}") print(f"tag: {args.tag}") print(f"notes_length: {len(notes)}") print(f"assets_count: {len(assets)}") for path, sha in assets: size_mb = path.stat().st_size / (1024 * 1024) print(f" - {path.name} ({size_mb:.2f} MiB) sha256={sha}") return 0 token = os.environ.get("GITEA_TOKEN") or os.environ.get("GITHUB_TOKEN") or "" if not token: print( "error: GITEA_TOKEN or GITHUB_TOKEN must be set to publish a Gitea Release", file=sys.stderr, ) return 1 api_base = f"{server}/api/v1/repos/{repo}" encoded_tag = urllib.parse.quote(args.tag, safe="") status, existing = api_request("GET", f"{api_base}/releases/tags/{encoded_tag}", token) if status == 404: create_status, release_obj = api_request( "POST", f"{api_base}/releases", token, payload={ "tag_name": args.tag, "name": args.tag, "body": notes, "draft": False, "prerelease": False, }, ) if create_status >= 400 or not isinstance(release_obj, dict): print( f"error: failed to create Gitea Release {args.tag} (HTTP {create_status}): {release_obj}", file=sys.stderr, ) return 1 print(f"created release {args.tag} (id={release_obj.get('id')})") elif status == 200 and isinstance(existing, dict): release_id = existing["id"] patch_status, release_obj = api_request( "PATCH", f"{api_base}/releases/{release_id}", token, payload={"name": args.tag, "body": notes}, ) if patch_status >= 400 or not isinstance(release_obj, dict): print( f"error: failed to update Gitea Release {args.tag} (HTTP {patch_status}): {release_obj}", file=sys.stderr, ) return 1 print(f"updated release {args.tag} (id={release_obj.get('id')})") else: print( f"error: failed querying release {args.tag} (HTTP {status}): {existing}", file=sys.stderr, ) return 1 release_id = release_obj["id"] list_status, asset_list = api_request("GET", f"{api_base}/releases/{release_id}/assets", token) existing_assets: dict[str, int] = {} if list_status == 200 and isinstance(asset_list, list): for item in asset_list: if isinstance(item, dict) and "name" in item and "id" in item: existing_assets[str(item["name"])] = int(item["id"]) for asset_path, _sha in assets: existing_id = existing_assets.get(asset_path.name) if existing_id is not None: del_status, del_body = api_request( "DELETE", f"{api_base}/releases/{release_id}/assets/{existing_id}", token, ) if del_status >= 400: print( f"error: failed removing old asset {asset_path.name} (HTTP {del_status}): {del_body}", file=sys.stderr, ) return 1 encoded_name = urllib.parse.quote(asset_path.name, safe="") upload_url = f"{api_base}/releases/{release_id}/assets?name={encoded_name}" print(f"uploading {asset_path.name}...") upload_asset_with_curl(upload_url, token, asset_path) print(f"published release {args.tag} with {len(assets)} asset(s)") return 0 if __name__ == "__main__": raise SystemExit(main())