ci: validate accepted stories with targeted ATDD and gate feature checks
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Failing after 25m35s
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) Failing after 1s
CI / Deploy (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Failing after 25m35s
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) Failing after 1s
CI / Deploy (pull_request) Skipped
This commit is contained in:
1 parent
9567bbf3aa
commit
1baeefe889
6 files changed
+808
-145
No files matched your search
Executable
+285
@@ -0,0 +1,285 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Publish immutable Crank release assets to Gitea, safely supporting retries."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import quote, urlencode, urlsplit
|
||||
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
||||
|
||||
_REPO = re.compile(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+\Z")
|
||||
_TAG = re.compile(r"v[A-Za-z0-9._+-]+\Z")
|
||||
|
||||
|
||||
|
||||
class PublishError(Exception):
|
||||
"""Sanitized release publication failure safe to show in CI logs."""
|
||||
|
||||
|
||||
class _NoRedirect(HTTPRedirectHandler):
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
raise PublishError(
|
||||
f"Gitea API redirected (HTTP {code}); refusing to forward credentials"
|
||||
)
|
||||
|
||||
|
||||
def _inputs(env):
|
||||
raw_base = env.get("GITEA_SERVER_URL", "")
|
||||
parts = urlsplit(raw_base)
|
||||
if (
|
||||
parts.scheme != "https"
|
||||
or not parts.hostname
|
||||
or parts.username
|
||||
or parts.password
|
||||
or parts.query
|
||||
or parts.fragment
|
||||
or parts.path not in ("", "/")
|
||||
):
|
||||
raise PublishError("GITEA_SERVER_URL must be the trusted HTTPS Gitea origin")
|
||||
repo = env.get("GITEA_REPOSITORY", "")
|
||||
if not _REPO.fullmatch(repo):
|
||||
raise PublishError("GITEA_REPOSITORY must be owner/repository")
|
||||
tag = env.get("RELEASE_TAG", "")
|
||||
if not _TAG.fullmatch(tag):
|
||||
raise PublishError("RELEASE_TAG is not a supported version tag")
|
||||
token = env.get("GITEA_TOKEN", "")
|
||||
if (
|
||||
not token
|
||||
or len(token) > 4096
|
||||
or any(ord(char) < 32 or ord(char) == 127 for char in token)
|
||||
):
|
||||
raise PublishError("GITEA_TOKEN is unavailable or has invalid format")
|
||||
origin = f"{parts.scheme}://{parts.netloc}"
|
||||
owner, name = repo.split("/", 1)
|
||||
api = f"{origin}/api/v1/repos/{quote(owner, safe='')}/{quote(name, safe='')}"
|
||||
return origin, api, tag, token
|
||||
|
||||
|
||||
def _json_request(opener, method, url, token, payload=None, allow_404=False):
|
||||
body = None if payload is None else json.dumps(payload).encode("utf-8")
|
||||
headers = {"Authorization": f"token {token}", "Accept": "application/json"}
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
request = Request(url, data=body, headers=headers, method=method)
|
||||
try:
|
||||
with opener.open(request, timeout=20) as response:
|
||||
return json.loads(response.read()), response.status
|
||||
except PublishError:
|
||||
raise
|
||||
except HTTPError as exc:
|
||||
status = exc.code
|
||||
exc.close()
|
||||
if allow_404 and status == 404:
|
||||
return None, 404
|
||||
raise PublishError(f"Gitea release API request failed (HTTP {status})") from None
|
||||
except (URLError, TimeoutError, OSError):
|
||||
raise PublishError("Gitea release API request failed or timed out") from None
|
||||
except (json.JSONDecodeError, UnicodeDecodeError):
|
||||
raise PublishError("Gitea returned invalid release API data") from None
|
||||
|
||||
|
||||
def _release_id(api, tag, token, opener):
|
||||
tag_path = f"{api}/releases/tags/{quote(tag, safe='')}"
|
||||
release, status = _json_request(opener, "GET", tag_path, token, allow_404=True)
|
||||
if status == 404:
|
||||
release, status = _json_request(
|
||||
opener,
|
||||
"POST",
|
||||
f"{api}/releases",
|
||||
token,
|
||||
payload={
|
||||
"tag_name": tag,
|
||||
"name": f"Crank Community {tag}",
|
||||
"body": "Checksums, evidence, and SBOM are attached to this release.",
|
||||
},
|
||||
)
|
||||
if status not in (200, 201) or not isinstance(release, dict):
|
||||
raise PublishError("Gitea did not return a release record")
|
||||
if release.get("tag_name") != tag:
|
||||
raise PublishError("Gitea returned a release for a different tag")
|
||||
release_id = release.get("id")
|
||||
if not isinstance(release_id, int) or release_id <= 0:
|
||||
raise PublishError("Gitea returned an invalid release identifier")
|
||||
return release_id
|
||||
|
||||
|
||||
def _assets(api, release_id, token, opener):
|
||||
collected = {}
|
||||
page_size = 50
|
||||
max_pages = 20
|
||||
for page_num in range(1, max_pages + 1):
|
||||
url = f"{api}/releases/{release_id}/assets?{urlencode({'page': page_num, 'limit': page_size})}"
|
||||
result, status = _json_request(opener, "GET", url, token)
|
||||
if status != 200 or not isinstance(result, list):
|
||||
raise PublishError("Gitea returned invalid release asset data")
|
||||
for item in result:
|
||||
if (
|
||||
not isinstance(item, dict)
|
||||
or not isinstance(item.get("name"), str)
|
||||
or not isinstance(item.get("id"), int)
|
||||
or not isinstance(item.get("size"), int)
|
||||
or not isinstance(item.get("browser_download_url"), str)
|
||||
):
|
||||
raise PublishError("Gitea returned a malformed release asset")
|
||||
if item["name"] in collected:
|
||||
raise PublishError(
|
||||
f"Gitea returned duplicate release asset name {item['name']}"
|
||||
)
|
||||
collected[item["name"]] = item
|
||||
if len(result) < page_size:
|
||||
return collected
|
||||
raise PublishError("Gitea release asset history exceeded the pagination limit")
|
||||
|
||||
|
||||
def _same_bytes(origin, attachment, local_path, token, opener):
|
||||
download = urlsplit(attachment["browser_download_url"])
|
||||
trusted = urlsplit(origin)
|
||||
if (
|
||||
download.scheme != "https"
|
||||
or download.netloc != trusted.netloc
|
||||
or download.username
|
||||
or download.password
|
||||
or download.fragment
|
||||
):
|
||||
raise PublishError(
|
||||
f"Existing asset {attachment['name']} has an untrusted download URL; refusing to replace it"
|
||||
)
|
||||
expected_size = local_path.stat().st_size
|
||||
if attachment["size"] != expected_size:
|
||||
return False
|
||||
request = Request(
|
||||
attachment["browser_download_url"],
|
||||
headers={"Authorization": f"token {token}"},
|
||||
)
|
||||
try:
|
||||
with opener.open(request, timeout=30) as response:
|
||||
digest = hashlib.sha256()
|
||||
total = 0
|
||||
while True:
|
||||
chunk = response.read(1024 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
total += len(chunk)
|
||||
if total > expected_size:
|
||||
return False
|
||||
digest.update(chunk)
|
||||
except PublishError:
|
||||
raise
|
||||
except HTTPError as exc:
|
||||
status = exc.code
|
||||
exc.close()
|
||||
if 300 <= status < 400:
|
||||
raise PublishError(
|
||||
f"Existing asset {attachment['name']} redirects; refusing to forward credentials"
|
||||
) from None
|
||||
raise PublishError(
|
||||
f"Could not compare existing release asset (HTTP {status})"
|
||||
) from None
|
||||
except (URLError, TimeoutError, OSError):
|
||||
raise PublishError("Could not download existing release asset for comparison") from None
|
||||
return total == expected_size and digest.digest() == _hash_file(local_path)
|
||||
|
||||
|
||||
def _hash_file(path):
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(1024 * 1024), b""):
|
||||
digest.update(chunk)
|
||||
return digest.digest()
|
||||
|
||||
|
||||
def _curl_config(token):
|
||||
escaped = token.replace("\\", "\\\\").replace('"', '\\"')
|
||||
config = tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False)
|
||||
try:
|
||||
config.write(f'header = "Authorization: token {escaped}"\n')
|
||||
config.flush()
|
||||
os.chmod(config.name, 0o600)
|
||||
return config.name
|
||||
finally:
|
||||
config.close()
|
||||
|
||||
|
||||
def _upload(api, release_id, path, token):
|
||||
name = path.name
|
||||
url = f"{api}/releases/{release_id}/assets?{urlencode({'name': name})}"
|
||||
auth_config = _curl_config(token)
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"curl",
|
||||
"--fail",
|
||||
"--silent",
|
||||
"--show-error",
|
||||
"--max-time",
|
||||
"180",
|
||||
"--config",
|
||||
auth_config,
|
||||
"-F",
|
||||
f"attachment=@{path}",
|
||||
url,
|
||||
],
|
||||
check=False,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
except OSError:
|
||||
raise PublishError("curl is unavailable for release asset upload") from None
|
||||
finally:
|
||||
Path(auth_config).unlink(missing_ok=True)
|
||||
if result.returncode != 0:
|
||||
raise PublishError(f"Gitea rejected release asset {name} (curl exit {result.returncode})")
|
||||
|
||||
|
||||
def publish(env=None, assets=None, opener=None, upload=_upload):
|
||||
env = os.environ if env is None else env
|
||||
origin, api, tag, token = _inputs(env)
|
||||
if not assets:
|
||||
raise PublishError("No release assets were provided")
|
||||
paths = [Path(item) for item in assets]
|
||||
if any(not path.is_file() for path in paths):
|
||||
raise PublishError("A requested release asset file is missing")
|
||||
names = [path.name for path in paths]
|
||||
if len(names) != len(set(names)):
|
||||
raise PublishError("Release asset names must be unique")
|
||||
opener = opener or build_opener(_NoRedirect())
|
||||
release_id = _release_id(api, tag, token, opener)
|
||||
existing = _assets(api, release_id, token, opener)
|
||||
published = []
|
||||
for path in paths:
|
||||
attachment = existing.get(path.name)
|
||||
if attachment is not None:
|
||||
if not _same_bytes(origin, attachment, path, token, opener):
|
||||
raise PublishError(
|
||||
f"Release asset {path.name} already exists with different bytes; immutable asset left unchanged"
|
||||
)
|
||||
published.append((path.name, "already present with matching bytes"))
|
||||
continue
|
||||
upload(api, release_id, path, token)
|
||||
published.append((path.name, "uploaded"))
|
||||
return {"tag": tag, "release_id": release_id, "assets": published}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
argv = sys.argv[1:] if argv is None else argv
|
||||
try:
|
||||
result = publish(assets=argv)
|
||||
except PublishError as exc:
|
||||
print(f"Gitea release assets: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"Gitea release {result['tag']} (id {result['release_id']}):")
|
||||
for name, state in result["assets"]:
|
||||
print(f"{state}: {name}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
active_log=""
|
||||
cleanup() {
|
||||
if [[ -n "$active_log" ]]; then
|
||||
rm -f -- "$active_log"
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
run_atdd() {
|
||||
local description="$1"
|
||||
shift
|
||||
active_log="$(mktemp)"
|
||||
if cargo test "$@" >"$active_log" 2>&1; then
|
||||
if ! grep -Eq '^test result: ok\. [1-9][0-9]* passed' "$active_log"; then
|
||||
cat "$active_log" >&2
|
||||
printf 'ATDD command selected zero passing tests: %s\n' "$description" >&2
|
||||
return 1
|
||||
fi
|
||||
grep '^test result:' "$active_log"
|
||||
rm -f -- "$active_log"
|
||||
active_log=""
|
||||
return 0
|
||||
fi
|
||||
cat "$active_log" >&2
|
||||
printf 'ATDD command failed: %s\n' "$description" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
# Initial accepted recovery scope for Crank Story 3.17. Update with the story
|
||||
# being published and add only AC-required affected security/contract cases.
|
||||
run_atdd "MCP completion" -p mcp-server --test integration mcp_completion -- --test-threads=1
|
||||
run_atdd "static resource list" -p mcp-server --test integration static_resources::list -- --test-threads=1
|
||||
Reference in new issue
Block a user