#!/usr/bin/env python3 """Coordinate candidate preparation and record independently authored QA evidence.""" import argparse import json from pathlib import Path import re import sys from urllib.error import HTTPError import candidate_release as candidate import candidate_runtime as runtime import hifox_release_trigger as hfx import production_cd as cd STATE = Path.home() / '.local/state/kg-candidate-release' CLI = str(Path.home() / '.local/bin/hifox') WORKFLOW = candidate.API + '/actions/workflows/prepare-candidate.yml/dispatches?return_run_details=true' QA_AUTHORS = {cd.RELEASE_OWNER, '62a66d1e-4311-4b54-8004-bbb03c69c08c', 'bdf4f412-8c3f-45ab-84f4-638c60a43806'} WAIT_TEST = 'b59c34ee-94f4-41a8-8577-dea73b2c6414' TESTING = 'f8906482-bad7-4af8-9a3b-d52630eff3f7' QA_FENCE = re.compile(r'^```kg-qa-v1\s*\n(\{.*?\})\s*\n```\s*$', re.MULTILINE | re.DOTALL) def task(task_id): cd.require(cd.matches(cd.UUID, task_id), 'provide the task UUID') obj = hfx.hifox(CLI, 'task', 'view', task_id) if not isinstance(obj, dict) or obj.get('id') != task_id or obj.get('spaceId') != cd.SPACE: raise cd.CDError('task is outside the release space') return obj def timeline(task_id): rows = hfx.hifox(CLI, 'api', 'get', f'/rest/v1/tasks/{task_id}/timeline') cd.require(isinstance(rows, list), 'task timeline unavailable') return [row for row in rows if row.get('event') == 'commented'] def owner_plan(task_id): task(task_id) matches = [candidate.FENCE.search(row.get('content') or '') for row in reversed(sorted( timeline(task_id), key=lambda row: row.get('createdAt', ''))) if (row.get('actor') or {}).get('id') == cd.RELEASE_OWNER] match = next((row for row in matches if row), None) if match is None: raise cd.CDError('release owner must post a kg-release-v1 plan on the release task') plan = json.loads(match[1]) candidate.check_plan(plan) cd.require(plan['task'] == task_id, 'release plan belongs to another task') for item in plan['items']: requirement = task(item['task']) identifier = requirement.get('identifier') pr = candidate.read_forge(candidate.API + f'/pulls/{item["pr"]}') fields = ' '.join(str(v or '') for v in (pr.get('title'), pr.get('body'), (pr.get('head') or {}).get('ref'))) cd.require(isinstance(identifier, str) and re.search(rf'(?