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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/env python3
"""Live-updating CI review comment.



Polls the GitHub Actions API for job statuses in the current run, assembles

the review comment from whatever results are available, and upserts it as a

PR comment. Repeats every ``--interval`` seconds until all jobs are

completed (or ``--timeout`` is reached), so the comment updates in real time

as each job finishes.



The comment is identified by the ``<!-- hermes-ci-review-bot -->`` marker

β€” the same one ``assemble_review_comment.py`` uses β€” so it replaces any

previous comment from an earlier run.



Architecture:



  - :func:`classify_jobs` (pure, testable) β€” takes a list of raw API job

    dicts and returns ``(completed, pending, job_urls)`` where ``completed``

    is a ``{name: result}`` dict (for :func:`assemble_review_comment.assemble`)

    and ``pending`` is a list of job names still running.



  - :func:`find_comment_id` / :func:`upsert_comment` β€” thin API wrappers.



  - :func:`_fetch_timings_statuses` β€” downloads the ci-timings artifact

    (if available) and parses the ``review_status=`` line from it, merging

    the status objects into the review statuses array.



  - :func:`run` β€” the polling loop. Calls the API, classifies, assembles,

    upserts, sleeps, repeats. Before its final exit, it gives downstream jobs

    a short grace period to appear.



The orchestrator job names (detect, all-checks-pass, comment-live, etc.)

are excluded from the comment β€” they're infrastructure, not review signal.

"""

from __future__ import annotations

import argparse
import json
import os
import subprocess
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path

API_BASE = "https://api.github.com"

# Job names that are infrastructure (this script, the gate, the detector)
# and should never appear in the review comment.
_INFRA_JOBS = frozenset({
    "detect",
    "all-checks-pass",
    "comment-pending",
    "comment-results",
    "comment-live",
    "CI review comment (pending)",
    "CI review comment (results)",
    "CI review comment (live)",
    "All required checks pass",
    "Detect affected areas",
})

# Map GitHub API conclusion values to our result strings.
_CONCLUSION_MAP = {
    "success": "success",
    "failure": "failure",
    "skipped": "skipped",
    "cancelled": "skipped",
    "neutral": "skipped",
    "timed_out": "failure",
    "action_required": "skipped",
}

def classify_jobs(api_jobs: list[dict]) -> tuple[dict[str, str], list[str], dict[str, str]]:
    """Classify raw API job dicts into completed + pending + job_urls.



    Returns ``(completed, pending, job_urls)``:



    - ``completed``: ``{job_name: result}`` where result is

      ``"success"`` / ``"failure"`` / ``"skipped"``. Only non-infra jobs

      that have finished.

    - ``pending``: list of job names still running (in_progress / queued

      / waiting). Excludes infra jobs.

    - ``job_urls``: ``{job_name: html_url}`` β€” direct links to each

      job's logs page, for the assembler to use in ❌ Error links.



    The API returns orchestrator-level jobs and sub-workflow jobs

    (workflow_call) in separate runs β€” :func:`collect_run_jobs` merges

    them. Each sub-workflow job has a ``_workflow_name`` prefix so the

    display name is ``"Workflow / job"``.

    """
    completed: dict[str, str] = {}
    pending: list[str] = []
    job_urls: dict[str, str] = {}

    for job in api_jobs:
        name = job.get("name", "unknown")
        if job.get("_workflow_name"):
            name = f"{job['_workflow_name']} / {name}"
        if name in _INFRA_JOBS:
            continue
        status = job.get("status", "")
        conclusion = job.get("conclusion", "")
        html_url = job.get("html_url", "")

        if html_url:
            job_urls[name] = html_url

        if status in ("in_progress", "queued", "waiting"):
            pending.append(name)
        elif status == "completed":
            result = _CONCLUSION_MAP.get(conclusion, "skipped")
            completed[name] = result
        # else: unknown status β†’ skip

    return completed, pending, job_urls


# ---------------------------------------------------------------------------
# API helpers
# ---------------------------------------------------------------------------


def _api_request(url: str, token: str) -> dict:
    """Authenticated GitHub API GET (single page)."""
    req = urllib.request.Request(url, headers={
        "Authorization": f"Bearer {token}",
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28",
        "User-Agent": "ci-live-comment",
    })
    with urllib.request.urlopen(req) as resp:
        data: dict = json.loads(resp.read())
        return data


def _api_get_paginated(url: str, token: str, list_key: str | None = None) -> list:
    """Authenticated GitHub API GET with pagination."""
    results: list = []
    while url:
        req = urllib.request.Request(url, headers={
            "Authorization": f"Bearer {token}",
            "Accept": "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
            "User-Agent": "ci-live-comment",
        })
        with urllib.request.urlopen(req) as resp:
            data = json.loads(resp.read())
            link_header = resp.headers.get("Link", "")

        if list_key:
            results.extend(data.get(list_key, []))
        elif isinstance(data, list):
            results.extend(data)
        else:
            return data

        next_url = None
        for part in link_header.split(","):
            part = part.strip()
            if 'rel="next"' in part:
                next_url = part[part.find("<") + 1:part.find(">")]
                break
        url = next_url

    return results


def collect_run_jobs(token: str, repo: str, run_id: str) -> list[dict]:
    """Collect all jobs in the orchestrator run + sub-workflow runs.



    Returns a flat list of job dicts (same shape as the API returns, plus

    ``_workflow_name`` on sub-workflow jobs).

    """
    owner, repo_name = repo.split("/")
    run_info = _api_request(f"{API_BASE}/repos/{owner}/{repo_name}/actions/runs/{run_id}", token)
    created_at = run_info.get("created_at", "")
    head_sha = run_info.get("head_sha", "")

    # Orchestrator jobs
    orch_jobs = _api_get_paginated(
        f"{API_BASE}/repos/{owner}/{repo_name}/actions/runs/{run_id}/jobs",
        token, list_key="jobs",
    )

    # Sub-workflow runs (workflow_call)
    sub_runs = _api_get_paginated(
        f"{API_BASE}/repos/{owner}/{repo_name}/actions/runs?head_sha={head_sha}&event=workflow_call&per_page=100",
        token, list_key="workflow_runs",
    )
    sub_runs = [r for r in sub_runs if r.get("created_at", "") >= created_at]

    all_jobs: list[dict] = []
    # Orchestrator jobs: skip workflow-call placeholder steps (they're
    # sub-workflow triggers, not review signal), but KEEP in_progress /
    # queued jobs so the poller knows they're still running.
    for job in orch_jobs:
        steps = job.get("steps") or []
        if any(s.get("name", "").startswith("Run ./.github/workflows/") for s in steps):
            continue
        all_jobs.append(job)

    # Sub-workflow jobs (workflow_call).
    # These runs may not exist yet on the first few polls β€” that's fine,
    # classify_jobs() will just show 0 pending for them.
    for sr in sub_runs:
        sr_id = sr["id"]
        sr_name = sr.get("name", "")
        sr_jobs = _api_get_paginated(
            f"{API_BASE}/repos/{owner}/{repo_name}/actions/runs/{sr_id}/jobs",
            token, list_key="jobs",
        )
        for j in sr_jobs:
            j["_workflow_name"] = sr_name
            all_jobs.append(j)

    return all_jobs


def find_comment_id(token: str, repo: str, pr_number: str) -> int | None:
    """Find our existing review comment by marker prefix."""
    owner, repo_name = repo.split("/")
    comments = _api_get_paginated(
        f"{API_BASE}/repos/{owner}/{repo_name}/issues/{pr_number}/comments",
        token,
    )
    for c in comments:
        body = c.get("body", "") if isinstance(c, dict) else ""
        if body.startswith("<!-- hermes-ci-review-bot -->"):
            return c.get("id") if isinstance(c, dict) else None
    return None


def upsert_comment(

    token: str, repo: str, pr_number: str, body: str, comment_id: int | None = None

) -> int | None:
    """Create or update the review comment. Returns the comment ID."""
    owner, repo_name = repo.split("/")
    if comment_id is None:
        comment_id = find_comment_id(token, repo, pr_number)

    if comment_id:
        url = f"{API_BASE}/repos/{owner}/{repo_name}/issues/comments/{comment_id}"
        method = "PATCH"
    else:
        url = f"{API_BASE}/repos/{owner}/{repo_name}/issues/{pr_number}/comments"
        method = "POST"

    data = json.dumps({"body": body}).encode("utf-8")
    req = urllib.request.Request(url, data=data, method=method, headers={
        "Authorization": f"Bearer {token}",
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28",
        "Content-Type": "application/json",
        "User-Agent": "ci-live-comment",
    })
    try:
        with urllib.request.urlopen(req) as resp:
            result = json.loads(resp.read())
            return result.get("id")
    except urllib.error.HTTPError as e:
        print(f"  API error {e.code}: {e.reason}", file=sys.stderr)
        return None


# ---------------------------------------------------------------------------
# Artifact fetching (ci-timings review_status)
# ---------------------------------------------------------------------------


def _fetch_artifact_statuses(

    token: str, repo: str, run_id: str, artifact_name: str,

) -> list[dict]:
    """Download a workflow artifact and extract review_status entries.



    The ci-timings job writes a ``review-status.json`` file containing

    ``review_status=<json>`` (GITHUB_OUTPUT format) into its artifact.

    This function downloads the artifact, parses the line, and returns

    the parsed status array. Returns ``[]`` if the artifact doesn't exist

    yet or can't be parsed.

    """
    try:
        result = subprocess.run(
            ["gh", "run", "download", run_id, "--repo", repo,
             "--name", artifact_name, "--dir", "/tmp/artifact-dl"],
            capture_output=True, timeout=30,
        )
        if result.returncode != 0:
            return []
    except Exception:
        return []

    status_file = Path("/tmp/artifact-dl/review-status.json")
    if not status_file.exists():
        return []

    try:
        content = status_file.read_text(encoding="utf-8").strip()
        # GITHUB_OUTPUT format: review_status=<json>
        if content.startswith("review_status="):
            content = content[len("review_status="):]
        statuses = json.loads(content)
        if isinstance(statuses, list):
            return statuses
    except (json.JSONDecodeError, OSError):
        pass

    return []


# ---------------------------------------------------------------------------
# Comment assembly
# ---------------------------------------------------------------------------


def _import_assembler():
    """Import assemble_review_comment.py from the same directory."""
    here = Path(__file__).resolve().parent
    sys.path.insert(0, str(here))
    import assemble_review_comment as asm
    return asm


def build_comment_body(

    asm_mod,

    completed: dict[str, str],

    pending: list[str],

    run_url: str,

    job_urls: dict[str, str],

    review_statuses_json: str,

    commit_info: str = "",

) -> str:
    """Assemble the comment body from current job states + static inputs."""
    needs_json = json.dumps(completed) if completed else ""

    return asm_mod.assemble(
        needs_json=needs_json,
        run_url=run_url,
        job_urls=job_urls,
        review_statuses_json=review_statuses_json,
        pending_jobs=pending if pending else None,
        commit_info=commit_info,
    )


def _merge_statuses(

    base_statuses: list[dict], extra_statuses: list[dict]

) -> str:
    """Merge two status arrays into one JSON string."""
    merged = list(base_statuses) + list(extra_statuses)
    return json.dumps(merged) if merged else ""


def _commit_info_for_state(commit_info: str, pending: list[str]) -> str:
    """Use past tense in the final comment after every CI job completes."""
    if pending:
        return commit_info
    return commit_info.replace("<sub>running on ", "<sub>ran on ", 1)


# ---------------------------------------------------------------------------
# Polling loop
# ---------------------------------------------------------------------------


def run(

    token: str,

    repo: str,

    run_id: str,

    pr_number: str,

    run_url: str,

    review_statuses_json: str = "",

    commit_info: str = "",

    interval: int = 15,

    timeout: int = 1800,

    dry_run: bool = False,

) -> int:
    """Poll for job statuses and update the PR comment until all done.



    Returns 0 always β€” comment posting is best-effort.

    """
    asm = _import_assembler()
    start = time.time()
    last_body = ""
    quiet_grace_used = False

    # Parse the base statuses once (from review-labels, lockfile-diff, etc.)
    try:
        base_statuses = json.loads(review_statuses_json) if review_statuses_json else []
    except (json.JSONDecodeError, TypeError):
        base_statuses = []
    print(f"  Loaded {len(base_statuses)} base review status entries")

    while True:
        elapsed = time.time() - start
        if elapsed > timeout:
            print(f"Timeout ({timeout}s) reached β€” stopping poll.", file=sys.stderr)
            break

        try:
            jobs = collect_run_jobs(token, repo, run_id)
        except Exception as e:
            print(f"  API error collecting jobs: {e}", file=sys.stderr)
            time.sleep(interval)
            continue

        completed, pending, job_urls = classify_jobs(jobs)
        total = len(completed) + len(pending)
        print(f"  [{elapsed:.0f}s] {len(completed)} completed, {len(pending)} pending "
              f"({total} total jobs)")

        # Try to fetch ci-timings artifact statuses (may not exist yet).
        artifact_statuses = _fetch_artifact_statuses(
            token, repo, run_id, "ci-timings-review-status",
        )
        if artifact_statuses:
            print(f"  Found ci-timings artifact with {len(artifact_statuses)} status entries")

        merged_json = _merge_statuses(base_statuses, artifact_statuses)
        current_commit_info = _commit_info_for_state(commit_info, pending)

        body = build_comment_body(
            asm, completed, pending, run_url, job_urls,
            merged_json,
            current_commit_info,
        )

        if body != last_body:
            if dry_run:
                print("--- DRY RUN β€” comment body ---")
                print(body)
                print("--- END ---")
            else:
                cid = upsert_comment(token, repo, pr_number, body)
                if cid:
                    print(f"  Updated comment {cid}")
                else:
                    print("  Failed to update comment (will retry)", file=sys.stderr)
            last_body = body
        else:
            print("  No change since last poll.")

        if not pending and not quiet_grace_used:
            quiet_grace_used = True
            print("  No visible jobs pending β€” waiting 10s for downstream jobs to appear.")
            time.sleep(10)
            continue

        if not pending:
            # Check if any dependency failed. If so, exit non-zero so the
            # run shows as failed β€” this lets ``gh run rerun --failed``
            # (e.g. from label-rerun.yml) pick up and rerun the failed jobs.
            failed_deps = [name for name, result in completed.items() if result == "failure"]
            if failed_deps:
                print(f"  All jobs done, but {len(failed_deps)} failed: {', '.join(failed_deps)}")
                print("  Exiting with error so the run can be rerun via --failed.")
                return 1
            print("  All jobs completed β€” done.")
            break

        quiet_grace_used = False
        time.sleep(interval)

    return 0


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--interval", type=int, default=15,
                        help="Seconds between polls (default: 15).")
    parser.add_argument("--timeout", type=int, default=1800,
                        help="Max seconds to poll before giving up (default: 1800).")
    parser.add_argument("--review-statuses-file", type=Path, default=None,
                        help="Path to a JSON file with merged review statuses from workflow_call jobs.")
    parser.add_argument("--dry-run", action="store_true",
                        help="Print comment body instead of posting to PR.")
    args = parser.parse_args()

    token = os.environ.get("GITHUB_TOKEN", "")
    repo = os.environ.get("GITHUB_REPOSITORY", "")
    run_id = os.environ.get("GITHUB_RUN_ID", "")
    pr_number = os.environ.get("PR_NUMBER", "")
    run_url = os.environ.get("RUN_URL", "")

    if not args.dry_run:
        if not token:
            print("GITHUB_TOKEN is required", file=sys.stderr)
            return 1
        if not repo:
            print("GITHUB_REPOSITORY is required", file=sys.stderr)
            return 1
        if not run_id:
            print("GITHUB_RUN_ID is required", file=sys.stderr)
            return 1
        if not pr_number:
            print("PR_NUMBER is required", file=sys.stderr)
            return 1

    # Read merged review statuses from file (prepared by the ci.yml step).
    review_statuses_json = ""
    if args.review_statuses_file:
        try:
            review_statuses_json = args.review_statuses_file.read_text(encoding="utf-8")
        except OSError as e:
            print(f"Warning: could not read review statuses file: {e}", file=sys.stderr)

    # Build commit info line from env vars (set by ci.yml).
    commit_sha = os.environ.get("COMMIT_SHA", "")
    commit_msg = os.environ.get("COMMIT_MESSAGE", "")
    commit_url = os.environ.get("COMMIT_URL", "")
    commit_info = ""
    if commit_sha:
        short_sha = commit_sha[:7]
        if commit_msg:
            # Truncate commit message to first line, max 60 chars.
            first_line = commit_msg.split("\n")[0][:60]
            if commit_url:
                commit_info = f"<sub>running on [{short_sha}]({commit_url}) β€” {first_line}</sub>"
            else:
                commit_info = f"<sub>running on {short_sha} β€” {first_line}</sub>"
        elif commit_url:
            commit_info = f"<sub>running on [{short_sha}]({commit_url})</sub>"
        else:
            commit_info = f"<sub>running on {short_sha}</sub>"

    return run(
        token=token,
        repo=repo,
        run_id=run_id,
        pr_number=pr_number,
        run_url=run_url,
        review_statuses_json=review_statuses_json,
        commit_info=commit_info,
        interval=args.interval,
        timeout=args.timeout,
        dry_run=args.dry_run,
    )


if __name__ == "__main__":
    sys.exit(main())