|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | +import os |
| 7 | +import urllib.parse |
| 8 | + |
| 9 | +import click |
| 10 | + |
| 11 | +from kcidev.libs.common import kci_err, kci_msg, kci_warning, kcidev_session |
| 12 | + |
| 13 | +KCIDB_SCHEMA_MAJOR = 5 |
| 14 | +KCIDB_SCHEMA_MINOR = 3 |
| 15 | + |
| 16 | + |
| 17 | +# --------------------- |
| 18 | +# ID generation |
| 19 | +# --------------------- |
| 20 | + |
| 21 | + |
| 22 | +def _sha256_hex(data): |
| 23 | + return hashlib.sha256(data.encode("utf-8")).hexdigest() |
| 24 | + |
| 25 | + |
| 26 | +def generate_checkout_id(origin, repo_url, branch, commit, patchset_hash=""): |
| 27 | + """ |
| 28 | + Deterministic checkout ID from source identity. |
| 29 | + Format: origin:ck-<sha256(repo_url|branch|commit|patchset_hash)> |
| 30 | + """ |
| 31 | + seed = f"{repo_url}|{branch}|{commit}|{patchset_hash}" |
| 32 | + return f"{origin}:ck-{_sha256_hex(seed)}" |
| 33 | + |
| 34 | + |
| 35 | +def generate_build_id(origin, checkout_id, arch, config_name, compiler, start_time): |
| 36 | + """ |
| 37 | + Deterministic build ID from build parameters. |
| 38 | + Format: origin:b-<sha256(checkout_id|arch|config_name|compiler|start_time)> |
| 39 | + """ |
| 40 | + seed = f"{checkout_id}|{arch}|{config_name}|{compiler}|{start_time}" |
| 41 | + return f"{origin}:b-{_sha256_hex(seed)}" |
| 42 | + |
| 43 | + |
| 44 | +# --------------------- |
| 45 | +# Payload building |
| 46 | +# --------------------- |
| 47 | + |
| 48 | + |
| 49 | +def build_checkout_payload(origin, checkout_id, **kwargs): |
| 50 | + """Build a checkout object for the KCIDB JSON payload.""" |
| 51 | + checkout = { |
| 52 | + "id": checkout_id, |
| 53 | + "origin": origin, |
| 54 | + } |
| 55 | + for key in [ |
| 56 | + "tree_name", |
| 57 | + "git_repository_url", |
| 58 | + "git_repository_branch", |
| 59 | + "git_commit_hash", |
| 60 | + "patchset_hash", |
| 61 | + "start_time", |
| 62 | + "valid", |
| 63 | + ]: |
| 64 | + if kwargs.get(key) is not None: |
| 65 | + checkout[key] = kwargs[key] |
| 66 | + return checkout |
| 67 | + |
| 68 | + |
| 69 | +def build_build_payload(origin, build_id, checkout_id, **kwargs): |
| 70 | + """Build a build object for the KCIDB JSON payload.""" |
| 71 | + build = { |
| 72 | + "id": build_id, |
| 73 | + "origin": origin, |
| 74 | + "checkout_id": checkout_id, |
| 75 | + } |
| 76 | + for key in [ |
| 77 | + "start_time", |
| 78 | + "duration", |
| 79 | + "architecture", |
| 80 | + "compiler", |
| 81 | + "config_name", |
| 82 | + "config_url", |
| 83 | + "log_url", |
| 84 | + "comment", |
| 85 | + "command", |
| 86 | + "status", |
| 87 | + ]: |
| 88 | + if kwargs.get(key) is not None: |
| 89 | + build[key] = kwargs[key] |
| 90 | + return build |
| 91 | + |
| 92 | + |
| 93 | +def build_submission_payload(checkouts, builds): |
| 94 | + """Build the complete KCIDB v5.3 submission JSON.""" |
| 95 | + payload = { |
| 96 | + "version": {"major": KCIDB_SCHEMA_MAJOR, "minor": KCIDB_SCHEMA_MINOR}, |
| 97 | + } |
| 98 | + if checkouts: |
| 99 | + payload["checkouts"] = checkouts |
| 100 | + if builds: |
| 101 | + payload["builds"] = builds |
| 102 | + return payload |
| 103 | + |
| 104 | + |
| 105 | +# --------------------- |
| 106 | +# Config resolution |
| 107 | +# --------------------- |
| 108 | + |
| 109 | + |
| 110 | +def _parse_kcidb_rest_env(env_value): |
| 111 | + """ |
| 112 | + Parse KCIDB_REST env var. |
| 113 | + Format: https://token@host[:port][/path] |
| 114 | + Returns (url, token) or (None, None) on failure. |
| 115 | + """ |
| 116 | + parsed = urllib.parse.urlparse(env_value) |
| 117 | + token = parsed.username |
| 118 | + if not token: |
| 119 | + return None, None |
| 120 | + |
| 121 | + # Rebuild URL without credentials |
| 122 | + host = parsed.hostname |
| 123 | + if parsed.port: |
| 124 | + host = f"{host}:{parsed.port}" |
| 125 | + path = parsed.path if parsed.path else "/" |
| 126 | + if not path.endswith("/submit"): |
| 127 | + path = path.rstrip("/") + "/submit" |
| 128 | + |
| 129 | + clean_url = urllib.parse.urlunparse( |
| 130 | + (parsed.scheme, host, path, parsed.params, parsed.query, parsed.fragment) |
| 131 | + ) |
| 132 | + return clean_url, token |
| 133 | + |
| 134 | + |
| 135 | +def resolve_kcidb_config(cfg, instance, cli_rest_url, cli_token): |
| 136 | + """ |
| 137 | + Resolve KCIDB REST URL and token. Priority: |
| 138 | + 1. CLI flags (--kcidb-rest-url, --kcidb-token) |
| 139 | + 2. KCIDB_REST environment variable |
| 140 | + 3. Instance config (kcidb_rest_url, kcidb_token in TOML) |
| 141 | +
|
| 142 | + Returns (rest_url, token) tuple. |
| 143 | + Raises click.Abort if no valid credentials found. |
| 144 | + """ |
| 145 | + # 1. CLI flags |
| 146 | + if cli_rest_url or cli_token: |
| 147 | + if not cli_rest_url or not cli_token: |
| 148 | + kci_err("Both --kcidb-rest-url and --kcidb-token must be provided together") |
| 149 | + raise click.Abort() |
| 150 | + return cli_rest_url, cli_token |
| 151 | + |
| 152 | + # 2. Environment variable |
| 153 | + kcidb_rest_env = os.environ.get("KCIDB_REST") |
| 154 | + if kcidb_rest_env: |
| 155 | + url, token = _parse_kcidb_rest_env(kcidb_rest_env) |
| 156 | + if url and token: |
| 157 | + return url, token |
| 158 | + kci_err("KCIDB_REST env var set but could not parse token from it") |
| 159 | + |
| 160 | + # 3. Instance config |
| 161 | + if cfg and instance and instance in cfg: |
| 162 | + inst_cfg = cfg[instance] |
| 163 | + rest_url = inst_cfg.get("kcidb_rest_url") |
| 164 | + token = inst_cfg.get("kcidb_token") |
| 165 | + if rest_url and token: |
| 166 | + return rest_url, token |
| 167 | + |
| 168 | + kci_err( |
| 169 | + "No KCIDB credentials found. Provide --kcidb-rest-url and --kcidb-token, " |
| 170 | + "set KCIDB_REST env var, or configure kcidb_rest_url/kcidb_token in config file" |
| 171 | + ) |
| 172 | + raise click.Abort() |
| 173 | + |
| 174 | + |
| 175 | +# --------------------- |
| 176 | +# REST submission |
| 177 | +# --------------------- |
| 178 | + |
| 179 | + |
| 180 | +def submit_to_kcidb(rest_url, token, payload, timeout=60): |
| 181 | + """ |
| 182 | + POST the JSON payload to the KCIDB REST API. |
| 183 | +
|
| 184 | + Returns the response text on success. |
| 185 | + Raises click.Abort on failure. |
| 186 | + """ |
| 187 | + headers = { |
| 188 | + "Content-Type": "application/json", |
| 189 | + "Authorization": f"Bearer {token}", |
| 190 | + } |
| 191 | + try: |
| 192 | + response = kcidev_session.post( |
| 193 | + rest_url, json=payload, headers=headers, timeout=timeout |
| 194 | + ) |
| 195 | + except Exception as e: |
| 196 | + kci_err(f"KCIDB API connection error: {e}") |
| 197 | + raise click.Abort() |
| 198 | + |
| 199 | + if response.status_code < 300: |
| 200 | + try: |
| 201 | + return response.json() |
| 202 | + except Exception: |
| 203 | + return response.text |
| 204 | + else: |
| 205 | + kci_err(f"KCIDB submission failed: HTTP {response.status_code}") |
| 206 | + try: |
| 207 | + kci_err(response.json()) |
| 208 | + except Exception: |
| 209 | + kci_err(response.text) |
| 210 | + raise click.Abort() |
0 commit comments