#!/usr/bin/env python3 """Upload an approved Discovery report to a KAB project, as a file artifact. A workflow step's output is always registered as output.json, so the customer's page reaches KAB this way, after the Engagement Lead has approved the run. The `file` type is used because `report` artifacts need Azure Blob, S3 or MongoDB storage through a route that has no blob store on crucible-kab-dev; `file` artifacts stay on the shared volume. Render the clean copy first with the Feature's scripts/render_discovery_report.py. Authentication: your KAB session's access token, from --token-file (default ~/.kab_token). It is sent only as the Authorization header and never printed. Uploading needs the Editor role in the project. Standard library only. Usage (from the repository root): python3 scripts/kab_publish_report.py --project --name # shows what it would do python3 scripts/kab_publish_report.py --project --name --apply """ from __future__ import annotations import argparse import json import sys import urllib.error import urllib.request import uuid from pathlib import Path from kab_run_workflow import CP1_TENANT, DEFAULT_API TYPES = {".html": "text/html", ".md": "text/markdown"} def multipart(fields: dict, path: Path, boundary: str) -> bytes: parts = [f'--{boundary}\r\nContent-Disposition: form-data; name="{k}"\r\n\r\n{v}\r\n'.encode() for k, v in fields.items()] head = (f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="{path.name}"\r\n' f"Content-Type: {TYPES[path.suffix]}\r\n\r\n").encode() return b"".join(parts) + head + path.read_bytes() + f"\r\n--{boundary}--\r\n".encode() def main() -> int: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("report", type=Path) parser.add_argument("--project", required=True) parser.add_argument("--name", required=True) parser.add_argument("--description", default="Discovery report") parser.add_argument("--api", default=DEFAULT_API) parser.add_argument("--tenant", default=CP1_TENANT) parser.add_argument("--token-file", type=Path, default=Path.home() / ".kab_token") parser.add_argument("--apply", action="store_true", help="upload; without it nothing is sent") args = parser.parse_args() if args.report.suffix not in TYPES: raise SystemExit("the report must be a .html or .md file") text = args.report.read_text() if "Draft. Not for release" in text: raise SystemExit("this copy is marked as a draft: render a clean one from the approved run") print(f"{args.report.name}: {len(text) // 1024} KB, to project {args.project} as {args.name!r}") if not args.apply: print("Nothing sent. Add --apply to upload.") return 0 boundary = uuid.uuid4().hex body = multipart({"name": args.name, "type_id": "file", "description": args.description}, args.report, boundary) request = urllib.request.Request(f"{args.api}/api/v1/projects/{args.project}/artifacts/upload", data=body, method="POST", headers={ "Authorization": f"Bearer {args.token_file.read_text().strip()}", "X-Tenant-ID": args.tenant, "X-Project-ID": args.project, "Content-Type": f"multipart/form-data; boundary={boundary}"}) try: with urllib.request.urlopen(request, timeout=60) as response: print("Uploaded:", json.load(response).get("id")) except urllib.error.HTTPError as error: print(f"HTTP {error.code}: {error.read().decode(errors='replace')[:300]}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())