~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Regression tests for ``scripts/generate_ci_health.py``.
These tests lock in the correctness fixes from the analyzer review:
* structural YAML parsing (not regex), counting EVERY ``uses:`` occurrence —
both the named form (``- name: X\\n uses: Y``) and the unnamed form
(``- uses: Y``) — with NO deduplication;
* per-occurrence pinning classification (local / pinned-SHA / unpinned-tag /
expression / docker-digest);
* checkout settings paired to their step's ``with.persist-credentials``;
* per-job harden-runner assessment with reusable-workflow jobs exempt;
* effective permissions (top-level or job-level);
* pre-commit ``local``/``meta`` exempt from floating-rev detection;
* malformed pre-commit YAML rendered as a finding, never a traceback;
* byte-stable report output (no wall-clock timestamp);
* ``--check`` gate fails on raw critical/high findings, not a rounded score;
* ``--out`` accepts absolute paths; ``--github-output`` emits all fields;
* ``--live`` degrades to "unavailable" instead of silently reporting health
when ``gh`` is missing, erroring, or returning junk, raises a ``high``
finding for every conclusion in ``_FAILED_CONCLUSIONS``, never raises a
``high`` finding for deliberate ``cancelled``/``skipped`` outcomes, and
queries only ``main``;
* a window whose runs include NO success is stale unconditionally, like the
sibling's "no success ⇒ stale" rule — never "fresh" just because
its most recent (failing, cancelled, ...) run happens to be young — and
raises a ``medium`` finding that names the newest run's OWN conclusion,
never relabelling it a success;
* no rendered line ends in whitespace (the repo runs the bare
``trailing-whitespace`` hook, so anything else could never be committed);
* live status is default-branch status: ``pull_request`` runs are excluded
even when their head branch is called ``main`` (fork default branches are);
* a scanner with no default-branch history of its own is read through the
gate that calls it, and every conclusion shown under its name is resolved
to that scanner's OWN jobs inside the gate run — a red gate summary next to
green scanner jobs must not paint the scanners red, an unresolved job list
renders ``UNRESOLVED`` (never ``FAIL``), and a failed scanner job is a
``high`` finding because it is now a measurement;
* the staleness threshold uses the sibling's formula
(``max(60.0, STALE_MULTIPLIER * min cadence)`` measured on the last
SUCCESSFUL run) — similar, not identical: no-cron workflows get the floor,
gated scanners use the gate's cadence, there is no new-file exemption and
ages are fractional days — and a run older than it renders ``STALE`` with
its date instead of ``PASS``;
* an empty history renders ``NO RUNS`` with a ``medium`` finding and does
not count as coverage for its category, and an in-flight run renders
``RUNNING`` with the previous conclusion — neither masquerades as healthy;
* the workflow's comments do not claim wiring (a gating step) that no step
performs — asserted structurally, over the parsed ``run:`` commands, so an
explanatory comment cannot satisfy the assertion.
Run: ``pytest tests/ci/test_ci_health.py -v``
"""
from __future__ import annotations
import datetime
import importlib.util
import pathlib
import sys
import pytest
# The analyzer lives in scripts/ (not a package), so load it by path.
_SCRIPT = (
pathlib.Path(__file__).resolve().parents[2]
/ "scripts"
/ "generate_ci_health.py"
)
_spec = importlib.util.spec_from_file_location("generate_ci_health", _SCRIPT)
assert _spec is not None and _spec.loader is not None
cih = importlib.util.module_from_spec(_spec)
sys.modules["generate_ci_health"] = cih
_spec.loader.exec_module(cih)
SHA = "1af3b93b6815bc44a9784bd300feb67ff0d1eeb3" # 40-hex, real actions/checkout sha
SHA2 = "0a1b2c3d4e5f60718293a4b5c6d7e8f901234567"
def _run_cli(argv: list[str]) -> int:
"""Invoke the analyzer's main() with a synthetic argv (it uses argparse)."""
old = sys.argv
sys.argv = ["generate_ci_health", *argv]
try:
return cih.main()
finally:
sys.argv = old
# --------------------------------------------------------------------------- #
# Pure helpers
# --------------------------------------------------------------------------- #
class TestPinningKind:
@pytest.mark.parametrize(
"ref",
[
"./.github/actions/build",
"$/.github/actions/build",
"$/.github/workflows/release.yml",
],
)
def test_local_action_excluded(self, ref):
assert cih._pinning_kind(ref) == "local"
def test_docker_digest_pinned(self):
assert (
cih._pinning_kind("docker://alpine@sha256:" + "a" * 64) == "pinned"
)
def test_docker_tag_unpinned(self):
assert cih._pinning_kind("docker://alpine:3.19") == "unpinned"
def test_expression_unverifiable(self):
assert (
cih._pinning_kind("actions/checkout@${{ inputs.version }}")
== "expression"
)
def test_sha_pinned(self):
assert cih._pinning_kind(f"actions/checkout@{SHA}") == "pinned"
def test_sha_pinned_case_insensitive(self):
assert cih._pinning_kind(f"actions/checkout@{SHA.upper()}") == "pinned"
def test_floating_tag_unpinned(self):
assert cih._pinning_kind("actions/checkout@v4") == "unpinned"
def test_branch_unpinned(self):
assert cih._pinning_kind("owner/repo@main") == "unpinned"
def test_marketplace_subpath_pinned(self):
# owner/repo/sub/path@sha must classify on the @sha, not the slash.
assert (
cih._pinning_kind(f"owner/repo/.github/actions/foo@{SHA2}")
== "pinned"
)
def test_self_repository_actions_and_workflows_do_not_raise_pinning_findings():
workflow = {
"jobs": {
"build": {"steps": [{"uses": "$/.github/actions/build"}]},
"release": {"uses": "$/.github/workflows/release.yml"},
}
}
findings = []
analysis = _assert_via_tempfile(
workflow, findings, tmp_checkouts=0, tmp_safe=0
)
assert analysis.total_uses == 0
assert not [finding for finding in findings if finding.area == "pinning"]
class TestActionHead:
def test_simple(self):
assert cih._action_head("actions/checkout@v4") == "actions/checkout"
def test_subpath(self):
assert (
cih._action_head("owner/repo/.github/actions/foo@sha1")
== "owner/repo/.github/actions/foo"
)
def test_no_at(self):
assert cih._action_head("./local-action") == "./local-action"
class TestIsFalse:
@pytest.mark.parametrize("value", [False, "false", "False", "FALSE"])
def test_falsey(self, value):
assert cih._is_false(value) is True
@pytest.mark.parametrize("value", [None, True, "true", 0, ""])
def test_not_falsey(self, value):
assert cih._is_false(value) is False
# --------------------------------------------------------------------------- #
# Structural parsing — occurrence counting, no dedup, unnamed steps
# --------------------------------------------------------------------------- #
def _uses_in(parsed: object) -> list[str]:
found: list[str] = []
for mapping, _is_step in cih._iter_action_mappings(parsed):
if isinstance(mapping, dict):
v = mapping.get("uses")
if isinstance(v, str):
found.append(v.strip())
return found
class TestStructuralParsing:
def test_unnamed_uses_step_is_counted(self):
"""The bug: `- uses:` (no `- name:`) was missed by the old regex."""
parsed = {
"jobs": {
"a": {
"steps": [
{"uses": f"actions/checkout@{SHA}"}, # unnamed
{"name": "X", "uses": f"actions/setup-python@{SHA}"},
]
}
}
}
assert len(_uses_in(parsed)) == 2
def test_duplicate_occurrences_not_collapsed(self):
"""The bug: repeated refs were deduped, hiding unpinned copies."""
parsed = {
"jobs": {
"a": {
"steps": [
{"uses": f"actions/checkout@{SHA}"},
{
"uses": f"actions/checkout@{SHA}"
}, # same ref, second occurrence
{
"uses": "actions/checkout@v4"
}, # unpinned third occurrence
]
}
}
}
assert len(_uses_in(parsed)) == 3
def test_nested_composite_and_matrix(self):
parsed = {
"jobs": {
"build": {"steps": [{"uses": f"actions/checkout@{SHA}"}]},
"matrix": {
"strategy": {"matrix": {"x": [{"uses": "deep@v1"}]}}
},
}
}
uses = _uses_in(parsed)
assert uses == [f"actions/checkout@{SHA}"]
class TestCheckoutPairing:
def test_safe_checkout_counted(self):
findings = []
wf = {
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
]
}
}
}
# Drive analyze_workflows via a temp workflow file + monkeypatch.
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)
def test_unsafe_checkout_flagged(self):
findings = []
wf = {
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}"
}, # no persist-credentials
]
}
}
}
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=0)
assert any(
f.area == "checkout" and f.severity == "high" for f in findings
)
def test_string_false_accepted(self):
findings = []
wf = {
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": "false"},
},
]
}
}
}
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)
def test_exact_action_identity_checkout_other_not_checkout(self):
findings = []
wf = {
"permissions": {},
"jobs": {
"a": {
"steps": [
{"uses": f"step-security/harden-runner@{SHA}"},
{"uses": f"actions/checkout-other@{SHA}"},
]
}
},
}
_assert_via_tempfile(wf, findings, tmp_checkouts=0, tmp_safe=0)
class TestReusableJobExempt:
def test_job_level_uses_not_flagged_for_harden(self):
"""The bug: reusable-workflow wrappers were flagged missing harden-runner."""
findings = []
wf = {
"jobs": {
"call": {
"uses": "./.github/workflows/reusable.yml"
}, # no steps → exempt
"ordinary": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
]
},
}
}
analysis = _assert_via_tempfile(
wf, findings, tmp_checkouts=1, tmp_safe=1
)
assert analysis.non_exempt_jobs == 1 # only "ordinary" counts
assert analysis.missing_harden == ()
assert not any(
"call" in f.file for f in findings if f.area == "harden-runner"
)
def test_steps_absent_job_is_malformed(self):
"""A job without steps or a reusable call is invalid and must fail --check."""
findings = []
wf = {
"permissions": {},
"jobs": {"norunner": {"runs-on": "ubuntu-latest"}},
}
analysis = _assert_via_tempfile(
wf, findings, tmp_checkouts=0, tmp_safe=0
)
assert analysis.non_exempt_jobs == 0
assert any(
f.severity == "high" and "neither steps nor uses" in f.message
for f in findings
)
def test_reusable_job_exempt_from_harden_but_permissions_assessed(self):
findings = []
wf = {"jobs": {"call": {"uses": "./.github/workflows/x.yml"}}}
analysis = _assert_via_tempfile(
wf, findings, tmp_checkouts=0, tmp_safe=0
)
assert analysis.non_exempt_jobs == 0
assert analysis.permissions_jobs == 1
assert any(
f.area == "permissions" and "call" in f.file for f in findings
)
assert not any(f.area == "harden-runner" for f in findings)
def test_exact_action_identity_harden_lookalike_not_harden(self):
findings = []
wf = {
"permissions": {},
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner-lookalike@{SHA}"
},
]
}
},
}
analysis = _assert_via_tempfile(
wf, findings, tmp_checkouts=0, tmp_safe=0
)
assert analysis.harden_ok_jobs == 0
assert any(f.area == "harden-runner" for f in findings)
class TestPermissions:
def test_top_level_permissions_cover_all_jobs(self):
findings = []
wf = {
"permissions": {"contents": "read"},
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
]
}
},
}
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)
assert not any(f.area == "permissions" for f in findings)
def test_missing_permissions_flagged(self):
findings = []
wf = {
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
]
}
}
}
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)
assert any(f.area == "permissions" for f in findings)
def test_empty_permissions_object_counts(self):
findings = []
wf = {
"permissions": {}, # the minimal `{}` form is an explicit declaration
"jobs": {
"a": {
"steps": [
{
"uses": f"step-security/harden-runner@{SHA}",
"with": {"egress-policy": "audit"},
},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
]
}
},
}
_assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)
assert not any(f.area == "permissions" for f in findings)
def test_read_all_permissions_string_counts(self):
findings = []
wf = {
"permissions": "read-all",
"jobs": {"call": {"uses": "./.github/workflows/x.yml"}},
}
analysis = _assert_via_tempfile(
wf, findings, tmp_checkouts=0, tmp_safe=0
)
assert analysis.permissions_jobs == 1
assert analysis.permissions_ok_jobs == 1
assert not any(f.area == "permissions" for f in findings)
# --------------------------------------------------------------------------- #
# Pre-commit analysis
# --------------------------------------------------------------------------- #
class TestPrecommit:
def test_local_not_floating(self, monkeypatch, tmp_path):
cfg = {"repos": [{"repo": "local", "hooks": [{"id": "my-hook"}]}]}
pc = _run_precommit(monkeypatch, tmp_path, cfg)
assert pc["floating_revs"] == []
def test_meta_not_floating(self, monkeypatch, tmp_path):
cfg = {
"repos": [
{"repo": "meta", "rev": "", "hooks": [{"id": "check-yaml"}]}
]
}
pc = _run_precommit(monkeypatch, tmp_path, cfg)
assert pc["floating_revs"] == []
def test_branch_rev_is_floating(self, monkeypatch, tmp_path):
cfg = {
"repos": [
{
"repo": "https://github.com/owner/repo",
"rev": "main",
"hooks": [{"id": "x"}],
}
]
}
pc = _run_precommit(monkeypatch, tmp_path, cfg)
assert len(pc["floating_revs"]) == 1
def test_sha_rev_not_floating(self, monkeypatch, tmp_path):
cfg = {
"repos": [
{
"repo": "https://github.com/owner/repo",
"rev": SHA,
"hooks": [{"id": "x"}],
}
]
}
pc = _run_precommit(monkeypatch, tmp_path, cfg)
assert pc["floating_revs"] == []
def test_release_tag_not_floating(self, monkeypatch, tmp_path):
cfg = {
"repos": [
{
"repo": "https://github.com/owner/repo",
"rev": "v1.2.3",
"hooks": [{"id": "x"}],
}
]
}
pc = _run_precommit(monkeypatch, tmp_path, cfg)
assert pc["floating_revs"] == []
assert pc["hooks"][0].revision_kind == "tag_or_unknown"
class TestMalformedWorkflows:
def test_multi_document_workflow_rejected(self, monkeypatch, tmp_path):
path = tmp_path / "multi.yml"
path.write_text(
f"jobs:\n a:\n uses: owner/repo@{SHA}\n---\njobs:\n b:\n uses: owner/other@{SHA2}\n",
encoding="utf-8",
)
monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))
findings = []
analysis = cih.analyze_workflows(findings)
assert analysis.total_uses == 0
assert any(
f.severity == "high" and "multi-document" in f.message
for f in findings
)
class TestScanners:
def test_required_category_satisfied_and_missing(
self, monkeypatch, tmp_path
):
codeql = tmp_path / "codeql.yml"
codeql.write_text("name: CodeQL\n", encoding="utf-8")
monkeypatch.setattr(cih, "_workflow_files", lambda: (codeql,))
monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "dependabot.yml")
findings = []
categories = {row.name: row for row in cih.analyze_scanners(findings)}
assert categories["SAST"].satisfied is True
assert categories["Secrets"].satisfied is False
assert any(
f.severity == "medium" and f.file == "Secrets" for f in findings
)
def test_missing_optional_category_emits_no_finding(
self, monkeypatch, tmp_path
):
"""Only REQUIRED categories emit findings when unsatisfied. An unsatisfied
OPTIONAL category (DAST, Containers, IaC, …) must be reported as
not-satisfied but NEVER produce a scanner finding."""
codeql = tmp_path / "codeql.yml"
codeql.write_text("name: CodeQL\n", encoding="utf-8")
monkeypatch.setattr(cih, "_workflow_files", lambda: (codeql,))
monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")
findings = []
rows = cih.analyze_scanners(findings)
optional_names = {r.name for r in rows if not r.required}
assert optional_names, (
"no optional scanner categories — analyzer contract changed"
)
unsatisfied_optional = {
r.name for r in rows if not r.required and not r.satisfied
}
assert unsatisfied_optional, (
"expected ≥1 unsatisfied optional category in this fixture"
)
scanner_finding_files = {
f.file for f in findings if f.area == "scanners"
}
assert not (unsatisfied_optional & scanner_finding_files), (
f"optional categories must not produce findings: "
f"{unsatisfied_optional & scanner_finding_files}"
)
def test_test_workflows_are_not_scanners(self, monkeypatch, tmp_path):
"""``gitleaks-rule-tests.yml`` tests the gitleaks config; it is not
Secrets-scanner evidence and gets no live tracking."""
paths = []
for name in (
"gitleaks.yml",
"gitleaks-rule-tests.yml",
"codeql-test.yml",
):
path = tmp_path / name
path.write_text("name: X\n", encoding="utf-8")
paths.append(path)
monkeypatch.setattr(cih, "_workflow_files", lambda: tuple(paths))
monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")
findings = []
categories = {row.name: row for row in cih.analyze_scanners(findings)}
assert categories["Secrets"].workflows == ("gitleaks.yml",)
assert categories["SAST"].satisfied is False
assert cih._scanner_workflow_files() == ("gitleaks.yml",)
def test_idle_scanner_does_not_count_for_its_category(
self, monkeypatch, tmp_path
):
"""A scanner with NO RUNS in the live window is not coverage: it is
dropped from its category's evidence, and a required category left
empty is unsatisfied (scored and reported) like a missing one."""
paths = []
for name in (
"codeql.yml",
"gitleaks.yml",
"nuclei.yml",
"owasp-zap-scan.yml",
):
path = tmp_path / name
path.write_text("name: X\n", encoding="utf-8")
paths.append(path)
monkeypatch.setattr(cih, "_workflow_files", lambda: tuple(paths))
monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")
findings = []
scanners = cih.analyze_scanners(findings)
run = cih.WorkflowRun(
1, "success", "push", "main", "2026-01-01T00:00:00Z"
)
live = (
cih.LiveStatus("codeql.yml", (run,), "success", None),
cih.LiveStatus("gitleaks.yml", (), None, None),
cih.LiveStatus("nuclei.yml", (), None, None),
cih.LiveStatus("owasp-zap-scan.yml", (), None, "gh timed out"),
)
before = len(findings)
categories = {
row.name: row
for row in cih.discount_idle_scanners(scanners, live, findings)
}
assert categories["SAST"].satisfied is True
assert categories["Secrets"].satisfied is False
assert categories["Secrets"].workflows == ()
# An unavailable query is not evidence of an idle scanner.
assert categories["DAST"].workflows == ("owasp-zap-scan.yml",)
assert categories["DAST"].satisfied is True
assert [(f.severity, f.file) for f in findings[before:]] == [
("medium", "Secrets")
]
required = cih.score_dimensions(
cih.analyze_workflows([]),
cih.analyze_precommit([]),
tuple(categories.values()),
)[-1]
assert required.name == "Required scanners"
assert required.ratio is not None and required.ratio < 1.0
def test_malformed_yaml_returns_parse_error_not_traceback(
self, monkeypatch, tmp_path
):
pc_file = tmp_path / ".pre-commit-config.yaml"
pc_file.write_text(
"repos:\n - repo: x\nbad: [unclosed", encoding="utf-8"
)
findings = []
pc = cih.analyze_precommit(findings, path=pc_file)
assert pc["present"] is True
assert "parse_error" in pc
assert pc["hook_count"] == 0
assert any(
f.severity in ("high", "medium")
and "parse error" in f.message.casefold()
for f in findings
)
# --------------------------------------------------------------------------- #
# Real-repo invariants + CLI contract
# --------------------------------------------------------------------------- #
class TestRealRepoInvariants:
"""Guardian tests against the actual repository tree (like
test_release_gate_integrity.py). These assert the POLICY holds on the
real workflows, not a frozen count."""
def test_all_actions_pinned(self):
findings = []
wf = cih.analyze_workflows(findings)
assert wf.total_uses > 0, "no actions found — analyzer is broken"
assert wf.pinned_uses == wf.total_uses, (
f"{wf.total_uses - wf.pinned_uses} unpinned action(s); "
f"findings: {[f for f in findings if f.area == 'pinning']}"
)
def test_all_checkouts_safe(self):
findings = []
wf = cih.analyze_workflows(findings)
assert wf.total_checkouts > 0
assert wf.safe_checkouts == wf.total_checkouts
def test_no_missing_harden_on_non_exempt_jobs(self):
findings = []
cih.analyze_workflows(findings)
# The previously-false-positive reusable wrappers must NOT appear.
# (osv-scanner.yml, osv-scanner-scheduled.yml, ui-full-shards.yml)
harden_files = [f.file for f in findings if f.area == "harden-runner"]
for wrapper in (
"osv-scanner.yml",
"osv-scanner-scheduled.yml",
"ui-full-shards.yml",
):
assert not any(wrapper in filename for filename in harden_files), (
f"{wrapper} incorrectly flagged (reusable wrapper should be exempt): {harden_files}"
)
def test_report_is_byte_stable(self):
a, _ = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[],
)
b, _ = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[],
)
assert a == b, (
"report output is not deterministic (volatile timestamp?)"
)
def test_render_never_reads_the_clock(self):
"""Freezing ``_utcnow`` proves nothing about ``render()`` unless
``render()`` is the only thing that could read a clock: it never
calls ``_utcnow``, so assert structurally that it reads no clock at
all."""
import inspect
source = inspect.getsource(cih.render)
for forbidden in ("datetime.now", "utcnow", "time.time"):
assert forbidden not in source, (
f"render() reads the wall clock via {forbidden}; the report "
"would change byte-for-byte on every run"
)
def test_live_dates_come_from_created_at_not_the_clock(self, monkeypatch):
"""The live table is the one part of the report that carries dates.
With the clock frozen at a sentinel instant and the run entries
fixed, every date rendered must be a ``createdAt``."""
frozen = datetime.datetime(
2019, 3, 4, 13, 37, 5, tzinfo=datetime.timezone.utc
)
monkeypatch.setattr(cih, "_utcnow", lambda: frozen)
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"success",
databaseId=11,
createdAt="2026-01-02T03:04:05Z",
),
_run_entry(
"success",
databaseId=10,
createdAt="2026-01-01T03:04:05Z",
),
]
),
)
live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)
markdown = _render_report(live)
row = _row_for(markdown, "codeql.yml")
assert "| 2026-01-02 |" in row, row
assert "2019-03-04" not in markdown, (
"the sentinel generation date reached the report"
)
assert "13:37" not in markdown
class TestCLIGate:
def test_check_passes_when_no_findings(self, tmp_path):
"""On the healthy real repo, --check exits 0."""
rc = _run_cli(["--check", "--out", str(tmp_path / "check.md")])
assert rc == 0
def test_check_fails_on_unpinned_action(self, monkeypatch, tmp_path):
bad_wf = {
"permissions": {},
"jobs": {
"a": {
"steps": [
{"uses": f"step-security/harden-runner@{SHA}"},
{
"uses": f"actions/checkout@{SHA}",
"with": {"persist-credentials": False},
},
{"uses": "owner/action@v4"},
]
}
},
}
monkeypatch.setattr(
cih,
"_workflow_files",
lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),
)
rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])
assert rc == 1
def test_check_fails_on_unsafe_checkout(self, monkeypatch, tmp_path):
bad_wf = {
"permissions": {},
"jobs": {
"a": {
"steps": [
{"uses": f"step-security/harden-runner@{SHA}"},
{"uses": f"actions/checkout@{SHA}"},
]
}
},
}
monkeypatch.setattr(
cih,
"_workflow_files",
lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),
)
rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])
assert rc == 1
def test_check_fails_on_expression_ref(self, monkeypatch, tmp_path):
bad_wf = {
"permissions": {},
"jobs": {
"a": {
"steps": [
{"uses": f"step-security/harden-runner@{SHA}"},
{"uses": "owner/action@${{ inputs.ref }}"},
]
}
},
}
monkeypatch.setattr(
cih,
"_workflow_files",
lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),
)
rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])
assert rc == 1
class TestCLIPaths:
def test_absolute_out_does_not_crash(self, tmp_path):
out = tmp_path / "abs.md"
rc = _run_cli(["--out", str(out)])
assert rc == 0
assert out.exists()
def test_github_output_emits_all_fields(self, tmp_path, monkeypatch):
gh_out = tmp_path / "gh_output"
monkeypatch.setenv("GITHUB_OUTPUT", str(gh_out))
rc = _run_cli(["--github-output", "--out", str(tmp_path / "o.md")])
assert rc == 0
text = gh_out.read_text()
for key in (
"score=",
"grade=",
"findings=",
"findings_critical=",
"findings_high=",
):
assert key in text, f"missing {key!r} in GITHUB_OUTPUT:\n{text}"
# --------------------------------------------------------------------------- #
# Helpers — drive analyze_workflows against synthetic temp workflows
# --------------------------------------------------------------------------- #
def _write_wf(tmp_path: pathlib.Path, name: str, obj: object) -> pathlib.Path:
import yaml as _yaml
p = tmp_path / name
p.write_text(_yaml.safe_dump(obj), encoding="utf-8")
return p
def _assert_via_tempfile(wf_obj, findings, tmp_checkouts: int, tmp_safe: int):
"""Point the analyzer at a single temp workflow and assert checkout counts."""
import tempfile
with tempfile.TemporaryDirectory() as d:
dpath = pathlib.Path(d)
import yaml as _yaml
wf_path = dpath / "wf.yml"
wf_path.write_text(_yaml.safe_dump(wf_obj), encoding="utf-8")
# Monkeypatch the module-level file list to our single temp file.
orig = cih._workflow_files
setattr(cih, "_workflow_files", lambda: (wf_path,))
try:
analysis = cih.analyze_workflows(findings)
finally:
setattr(cih, "_workflow_files", orig)
assert analysis.total_checkouts == tmp_checkouts, (
f"expected {tmp_checkouts} checkouts, got {analysis.total_checkouts}"
)
assert analysis.safe_checkouts == tmp_safe, (
f"expected {tmp_safe} safe checkouts, got {analysis.safe_checkouts}"
)
return analysis
def _run_precommit(monkeypatch, tmp_path, cfg_obj):
import yaml as _yaml
pc_file = tmp_path / ".pre-commit-config.yaml"
pc_file.write_text(_yaml.safe_dump(cfg_obj), encoding="utf-8")
# Pass path= explicitly: analyze_precommit binds PRECOMMIT_FILE as a
# default arg at def-time, so monkeypatching the module global has no
# effect. The path= param is the intended test seam.
return cih.analyze_precommit([], path=pc_file)
# --------------------------------------------------------------------------- #
# Live scanner status (--live)
# --------------------------------------------------------------------------- #
def _fake_run(monkeypatch, handler):
"""Replace subprocess.run inside the analyzer with *handler*.
*handler* receives the argv list and returns a
``(returncode, stdout, stderr)`` triple, or raises to simulate OSError /
timeout. Calls are recorded on the returned list.
"""
calls: list[list[str]] = []
real_run = cih.subprocess.run
class _Result:
def __init__(self, returncode: int, stdout: str, stderr: str):
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def _run(command, **kwargs):
# subprocess.run is shared with recent_cicd_changes()'s `git log`;
# only gh invocations are faked, everything else runs for real.
if not (isinstance(command, list) and command and command[0] == "gh"):
return real_run(command, **kwargs)
calls.append(list(command))
return _Result(*handler(list(command)))
monkeypatch.setattr(cih.subprocess, "run", _run)
return calls
def _gh_ok(runs: list[dict]):
"""Handler where `gh --version` succeeds and `gh run list` returns *runs*."""
import json as _json
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
return (0, _json.dumps(runs), "")
return handler
def _gh_with_jobs(runs: list[dict], jobs: list[dict]):
"""Handler where `gh run list` returns *runs* and `gh run view` *jobs*."""
import json as _json
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
return (0, _json.dumps({"jobs": jobs}), "")
return (0, _json.dumps(runs), "")
return handler
def _job(name: str, conclusion: str | None) -> dict:
return {"name": name, "conclusion": conclusion}
def _iso_days_ago(days: float) -> str:
"""An ISO-8601 run timestamp *days* in the past, as ``gh`` renders one."""
moment = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
days=days
)
return moment.strftime("%Y-%m-%dT%H:%M:%SZ")
def _run_entry(conclusion: str, **kw) -> dict:
# The default run is RECENT: a fixed calendar date would age into the
# staleness threshold and silently start raising findings in tests that
# are about something else entirely.
return {
"databaseId": kw.get("databaseId", 123),
"conclusion": conclusion,
"event": kw.get("event", "schedule"),
"headBranch": kw.get("headBranch", "main"),
"createdAt": kw.get("createdAt", _iso_days_ago(1)),
}
class TestLiveStatusDisabled:
def test_disabled_returns_empty_and_never_shells_out(self, monkeypatch):
calls = _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))
findings: list = []
assert (
cih.analyze_live_status(findings, ("codeql.yml",), enabled=False)
== ()
)
assert findings == []
assert calls == [], "disabled --live must not invoke gh at all"
def test_no_scanner_workflows_returns_empty(self, monkeypatch):
calls = _fake_run(monkeypatch, _gh_ok([]))
assert cih.analyze_live_status([], (), enabled=True) == ()
assert calls == []
class TestLiveStatusUnavailable:
"""The dangerous failure mode: looking clean while checking nothing."""
def test_gh_missing_marks_unavailable_without_findings(self, monkeypatch):
def handler(command):
raise OSError("no gh")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(
findings, ("codeql.yml", "gitleaks.yml"), enabled=True
)
assert [entry.workflow for entry in live] == [
"codeql.yml",
"gitleaks.yml",
]
assert all(entry.unavailable for entry in live)
assert all(entry.runs == () for entry in live)
assert findings == [], "unavailable gh must not manufacture findings"
def test_gh_version_nonzero_marks_unavailable(self, monkeypatch):
_fake_run(monkeypatch, lambda command: (127, "", ""))
live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)
assert live[0].unavailable == "gh exit 127"
def test_run_list_nonzero_uses_first_stderr_line(self, monkeypatch):
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
return (1, "", "gh: not authenticated\nrun gh auth login\n")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].unavailable == "gh: not authenticated"
assert findings == []
def test_run_list_nonzero_without_stderr_reports_exit_code(
self, monkeypatch
):
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
return (3, "", " ")
_fake_run(monkeypatch, handler)
live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)
assert live[0].unavailable == "exit 3"
def test_invalid_json_is_reported_not_raised(self, monkeypatch):
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
return (0, "not json at all", "")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].unavailable == "gh returned invalid JSON"
assert findings == []
def test_non_list_payload_is_reported(self, monkeypatch):
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
return (0, '{"conclusion": "failure"}', "")
_fake_run(monkeypatch, handler)
live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)
assert live[0].unavailable == "gh returned non-list payload"
def test_timeout_on_run_list_is_contained_per_workflow(self, monkeypatch):
import subprocess as _sp
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
if "--workflow=codeql.yml" in command:
raise _sp.TimeoutExpired(cmd=command, timeout=15)
return (0, "[]", "")
_fake_run(monkeypatch, handler)
live = cih.analyze_live_status(
[], ("codeql.yml", "gitleaks.yml"), enabled=True
)
assert live[0].unavailable == "gh timed out"
assert live[1].unavailable is None, (
"one slow workflow must not abort the remaining queries"
)
class TestLiveStatusFindings:
@pytest.mark.parametrize(
"conclusion", ["failure", "timed_out", "startup_failure"]
)
def test_failing_conclusions_raise_high_finding(
self, monkeypatch, conclusion
):
_fake_run(
monkeypatch,
_gh_ok([_run_entry(conclusion, databaseId=99, event="push")]),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == conclusion
# The lone run also has no success in the window (BL3-1/R1), so a
# companion "no successful run" medium finding is expected too --
# only the high one is asserted on below.
highs = [f for f in findings if f.severity == "high"]
assert len(highs) == 1
finding = highs[0]
assert finding.area == "scanner-runtime"
assert finding.file == "codeql.yml"
# Message must carry enough context to act on without re-querying.
assert conclusion in finding.message
assert "99" in finding.message
assert "push" in finding.message
def test_success_raises_nothing(self, monkeypatch):
_fake_run(monkeypatch, _gh_ok([_run_entry("success")]))
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == "success"
assert findings == [], "a passing run is not noise"
@pytest.mark.parametrize("conclusion", ["cancelled", "skipped", "neutral"])
def test_benign_conclusions_raise_no_high_but_flag_no_success(
self, monkeypatch, conclusion
):
"""Not a --check-gating failure (`_FAILED_CONCLUSIONS` excludes
these), but also not evidence the scanner works: BL3-1/R1 marks a
window with no successful run stale unconditionally, so a lone
cancelled/skipped/neutral run still raises the "no successful run"
medium finding -- it just never raises a high one."""
_fake_run(monkeypatch, _gh_ok([_run_entry(conclusion)]))
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == conclusion
assert live[0].stale
assert [f.severity for f in findings] == ["medium"], (
f"{conclusion!r} must never raise a high (--check-gating) finding"
)
assert "no successful run in the query window" in findings[0].message
def test_only_the_latest_run_gates(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry("success", databaseId=3),
_run_entry("failure", databaseId=2),
_run_entry("failure", databaseId=1),
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == "success"
assert len(live[0].runs) == 3
assert findings == [], "an already-fixed scanner must not stay red"
def test_empty_history_is_a_medium_finding_not_a_failure(self, monkeypatch):
"""A scanner that never ran on main in the window (e.g. its only
caller is commented out) is absent evidence: a ``medium`` finding,
never a ``high`` (--check-gating) one."""
_fake_run(monkeypatch, _gh_ok([]))
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].runs == ()
assert live[0].latest_conclusion is None
assert live[0].unavailable is None
assert [(f.severity, f.file) for f in findings] == [
("medium", "codeql.yml")
]
assert "no qualifying run on main" in findings[0].message
def test_null_conclusion_becomes_unknown_not_a_crash(self, monkeypatch):
_fake_run(monkeypatch, _gh_ok([_run_entry(None)]))
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == "unknown"
assert findings == []
class TestLiveQueryShape:
def test_query_is_scoped_to_main_and_limited(self, monkeypatch):
calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))
_ = cih.analyze_live_status([], ("codeql.yml",), enabled=True, limit=5)
run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]
assert len(run_list) == 1
command = run_list[0]
assert "--workflow=codeql.yml" in command
assert f"--branch={cih._LIVE_BRANCH}" in command, (
"--branch narrows the query to runs whose HEAD REF is named "
"main; it is a help, not the guard -- a fork PR from a branch "
"called main passes it, so the event filter does the real work "
"(see TestLiveEventFilter)"
)
assert "--limit=5" in command
def test_one_query_per_scanner_workflow(self, monkeypatch):
calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))
workflows = ("codeql.yml", "gitleaks.yml", "semgrep.yml")
live = cih.analyze_live_status([], workflows, enabled=True)
run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]
assert len(run_list) == len(workflows)
assert tuple(entry.workflow for entry in live) == workflows
class TestScannerWorkflowFiles:
def test_resolves_real_scanner_filenames(self):
files = cih._scanner_workflow_files()
assert files, "no scanner workflows resolved from the real repo tree"
assert files == tuple(sorted(files))
assert len(set(files)) == len(files)
for name in ("codeql.yml", "gitleaks.yml", "semgrep.yml"):
assert name in files, f"{name} missing from scanner workflow list"
real = {path.name for path in cih._workflow_files()}
assert set(files) <= real, "scanner list drifted from the repo tree"
class TestLiveRendering:
def _render(self, live):
markdown, _ = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[],
live,
)
return markdown
def test_no_live_section_when_live_is_empty(self):
assert "## Live scanner status" not in self._render(())
@pytest.mark.parametrize(
("conclusion", "label"),
[
("success", "PASS"),
("failure", "FAIL"),
("timed_out", "TIMED_OUT"),
("startup_failure", "STARTUP_FAILURE"),
("cancelled", "CANCELLED"),
("skipped", "SKIPPED"),
("action_required", "ACTION_REQUIRED"),
("neutral", "NEUTRAL"),
# `gh`'s own "stale" conclusion (a run that outlived its log
# retention) is NOT this report's staleness verdict.
("stale", "STALE (gh)"),
("unresolved", "UNRESOLVED"),
],
)
def test_conclusions_render_distinctly(self, conclusion, label):
run = cih.WorkflowRun(1, conclusion, "schedule", "main", "2025-01-01")
markdown = self._render(
(cih.LiveStatus("codeql.yml", (run,), conclusion, None),)
)
assert "## Live scanner status" in markdown
assert f"| {label} " in markdown
@pytest.mark.parametrize(
"conclusion", ["failure", "timed_out", "startup_failure"]
)
def test_every_failing_conclusion_gets_the_note(self, conclusion):
run = cih.WorkflowRun(1, conclusion, "schedule", "main", "2025-01-01")
markdown = self._render(
(cih.LiveStatus("codeql.yml", (run,), conclusion, None),)
)
assert "latest run did not pass" in markdown, (
f"{conclusion} raises a high finding but the table stayed silent"
)
def test_gh_stale_conclusion_does_not_collide_with_the_staleness_label(
self,
):
"""Both `gh`'s `stale` conclusion and this report's staleness verdict
want the word STALE. They mean different things, so they must not
render the same cell."""
run = cih.WorkflowRun(1, "stale", "schedule", "main", "2025-01-01")
row = _row_for(
self._render(
(cih.LiveStatus("codeql.yml", (run,), "stale", None),)
),
"codeql.yml",
)
assert "| STALE (gh) |" in row, row
assert "| STALE |" not in row, (
"gh's stale conclusion is indistinguishable from the report's "
"own staleness verdict"
)
assert "| unresolved |" not in row
def test_every_known_gh_conclusion_has_a_label(self):
unlabelled = sorted(
conclusion
for conclusion in cih._KNOWN_CONCLUSIONS
if conclusion not in cih._STATUS_LABELS
)
assert unlabelled == [], (
f"{unlabelled} would render as a raw API string in the table"
)
def test_passing_run_gets_no_note(self):
run = cih.WorkflowRun(1, "success", "schedule", "main", "2025-01-01")
markdown = self._render(
(cih.LiveStatus("codeql.yml", (run,), "success", None),)
)
assert "latest run did not pass" not in markdown
def test_unavailable_entry_is_called_out(self):
markdown = self._render(
(cih.LiveStatus("codeql.yml", (), None, "gh exit 127"),)
)
assert "_unavailable: gh exit 127_" in markdown
assert "`gh` may not be installed or authenticated" in markdown
def test_unavailable_reason_cannot_break_the_table(self):
markdown = self._render(
(cih.LiveStatus("codeql.yml", (), None, "a|b\nc"),)
)
rows = [
line
for line in markdown.splitlines()
if line.startswith("| `codeql.yml`")
]
assert len(rows) == 1
# The newline is flattened and the pipe is backslash-escaped, so the
# cell cannot open a sixth column.
assert "\\|" in rows[0]
assert rows[0].replace("\\|", "").count("|") == 6, (
f"row split by injection: {rows[0]}"
)
def test_live_findings_do_not_move_the_score(self):
baseline_md, baseline_score = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[],
)
finding = cih.Finding(
"high", "scanner-runtime", "codeql.yml", "latest run failure"
)
run = cih.WorkflowRun(1, "failure", "schedule", "main", "2025-01-01")
_, live_score = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[finding],
(cih.LiveStatus("codeql.yml", (run,), "failure", None),),
)
assert live_score == baseline_score, (
"runtime status must not be folded into the configuration score"
)
assert baseline_md != ""
class TestLiveCLIGate:
def test_live_failure_fails_the_check_gate(self, monkeypatch, tmp_path):
_fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))
rc = _run_cli(["--check", "--live", "--out", str(tmp_path / "o.md")])
assert rc == 1
def test_live_success_passes_the_check_gate(self, monkeypatch, tmp_path):
_fake_run(monkeypatch, _gh_ok([_run_entry("success")]))
rc = _run_cli(["--check", "--live", "--out", str(tmp_path / "o.md")])
assert rc == 0
def test_unavailable_gh_still_passes_but_says_so(
self, monkeypatch, tmp_path
):
_fake_run(monkeypatch, lambda command: (127, "", ""))
out = tmp_path / "o.md"
rc = _run_cli(["--check", "--live", "--out", str(out)])
assert rc == 0
assert "unavailable" in out.read_text()
def test_live_off_by_default(self, monkeypatch, tmp_path):
calls = _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))
out = tmp_path / "o.md"
rc = _run_cli(["--out", str(out)])
assert rc == 0
assert not [c for c in calls if c[:3] == ["gh", "run", "list"]]
assert "## Live scanner status" not in out.read_text()
class TestLiveWorkflowWiring:
"""--live once shipped wired to nothing; lock the wiring down."""
def test_workflow_passes_live_with_token_and_actions_read(self):
import yaml as _yaml
path = (
pathlib.Path(cih.ROOT)
/ ".github"
/ "workflows"
/ "ci-health-report.yml"
)
workflow = _yaml.safe_load(path.read_text(encoding="utf-8"))
job = workflow["jobs"]["report"]
assert job["permissions"].get("actions") == "read", (
"without actions: read, gh run list 403s and the live section "
"silently reports nothing"
)
steps = [
step
for step in job["steps"]
if "--live" in str(step.get("run", ""))
]
assert len(steps) == 1, "no step actually runs the analyzer with --live"
assert "GH_TOKEN" in steps[0].get("env", {}), (
"without GH_TOKEN the live query degrades to 'unavailable'"
)
@pytest.mark.parametrize(
"workflow",
[
{"name": "Missing jobs", "on": "push"},
{"jobs": None},
{"jobs": []},
{"jobs": {}},
{"jobs": {"scan": None}},
{"jobs": {"scan": {"steps": []}}},
{"jobs": {"scan": {"steps": "invalid"}}},
{"jobs": {"scan": {"steps": [None]}}},
],
)
def test_malformed_workflow_structure_fails_check(
monkeypatch, tmp_path, workflow
):
"""Invalid jobs must not disappear from the report's security checks."""
path = _write_wf(tmp_path, "invalid.yml", workflow)
monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))
findings = []
cih.analyze_workflows(findings)
assert any(f.severity == "high" and f.area == "workflow" for f in findings)
assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1
@pytest.mark.parametrize("run_id", [None, "invalid", [], {}, True, 1.5, 0, -1])
def test_malformed_run_id_is_unavailable_and_does_not_abort_others(
monkeypatch, run_id
):
"""Invalid row data must degrade per workflow instead of raising during conversion."""
import json
def handler(command):
if command == ["gh", "--version"]:
return 0, "synthetic gh", ""
row = _run_entry("success")
if "--workflow=codeql.yml" in command:
row["databaseId"] = run_id
return 0, json.dumps([row]), ""
_fake_run(monkeypatch, handler)
findings = []
live = cih.analyze_live_status(findings, ("codeql.yml", "gitleaks.yml"))
assert live[0].unavailable == "gh returned malformed run entries"
assert live[0].runs == () and live[0].latest_conclusion is None
assert (
live[1].latest_conclusion == "success" and live[1].unavailable is None
)
assert findings == []
@pytest.mark.parametrize(
"newest",
[False, {}, _run_entry("success", event=[]), _run_entry("unrecognized")],
)
def test_malformed_newest_run_cannot_promote_older_success(monkeypatch, newest):
"""Dropping a malformed newest entry would falsely label the previous run latest."""
_fake_run(monkeypatch, _gh_ok([newest, _run_entry("success")]))
live = cih.analyze_live_status([], ("codeql.yml",))
assert live[0].unavailable == "gh returned malformed run entries"
assert live[0].latest_conclusion is None and live[0].runs == ()
def test_live_queries_share_a_bounded_time_budget(monkeypatch):
"""Serial per-call timeouts must leave room to publish unavailable results."""
import subprocess
from types import SimpleNamespace
clock = [0.0]
timeouts = []
monkeypatch.setattr(
cih, "time", SimpleNamespace(monotonic=lambda: clock[0])
)
monkeypatch.setattr(cih, "_gh_available", lambda: None)
def run(command, **kwargs):
timeout = kwargs["timeout"]
timeouts.append(timeout)
clock[0] += timeout
raise subprocess.TimeoutExpired(command, timeout)
monkeypatch.setattr(cih.subprocess, "run", run)
live = cih.analyze_live_status(
[], tuple(f"scanner{i}.yml" for i in range(20))
)
assert sum(timeouts) == 120.0
assert timeouts == [15.0] * 8
assert len(live) == 20
assert all(entry.unavailable == "gh timed out" for entry in live[:8])
assert all(
entry.unavailable == "live query time budget exhausted"
for entry in live[8:]
)
def test_last_live_query_uses_only_remaining_time(monkeypatch):
"""The last request must not overrun the shared budget by a full timeout."""
import subprocess
from types import SimpleNamespace
clock = [0.0]
timeouts = []
monkeypatch.setattr(
cih, "time", SimpleNamespace(monotonic=lambda: clock[0])
)
monkeypatch.setattr(cih, "_gh_available", lambda: None)
monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 20.0)
def run(command, **kwargs):
timeout = kwargs["timeout"]
timeouts.append(timeout)
clock[0] += timeout
raise subprocess.TimeoutExpired(command, timeout)
monkeypatch.setattr(cih.subprocess, "run", run)
live = cih.analyze_live_status(
[], ("codeql.yml", "gitleaks.yml", "semgrep.yml")
)
assert timeouts == [15.0, 5.0]
assert live[2].unavailable == "live query time budget exhausted"
@pytest.mark.parametrize(
"step",
[
{},
{"name": "No command"},
{"uses": None},
{"uses": 23},
{"uses": ""},
{"uses": " "},
{"run": None},
{"run": False},
{"uses": "./local", "run": "echo conflicting"},
],
)
def test_invalid_step_commands_fail_check(monkeypatch, tmp_path, step):
"""A valid first step must not hide a later malformed action or shell step."""
workflow = {
"permissions": {},
"jobs": {
"scan": {
"steps": [
{"uses": f"step-security/harden-runner@{SHA}"},
step,
]
}
},
}
path = _write_wf(tmp_path, "invalid-command.yml", workflow)
monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))
findings = []
cih.analyze_workflows(findings)
assert any(
f.severity == "high" and "exactly one valid uses or run" in f.message
for f in findings
)
assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1
def test_reusable_job_cannot_also_define_steps(monkeypatch, tmp_path):
"""A reusable call and a runner job are mutually exclusive workflow forms."""
workflow = {
"permissions": {},
"jobs": {
"scan": {
"uses": "./.github/workflows/reuse.yml",
"steps": [{"uses": f"step-security/harden-runner@{SHA}"}],
}
},
}
path = _write_wf(tmp_path, "invalid-job.yml", workflow)
monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))
findings = []
cih.analyze_workflows(findings)
assert any(
f.severity == "high"
and "cannot combine reusable uses with steps" in f.message
for f in findings
)
assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1
# --------------------------------------------------------------------------- #
# Round-1 review fixes
# --------------------------------------------------------------------------- #
def _render_report(live=()):
markdown, _ = cih.render(
cih.analyze_workflows([]),
cih.analyze_precommit(),
cih.analyze_scanners([]),
cih.recent_cicd_changes(),
[],
live,
)
return markdown
def _row_for(markdown: str, workflow: str) -> str:
rows = [
line
for line in markdown.splitlines()
if line.startswith(f"| `{workflow}`")
]
assert len(rows) == 1, f"expected one row for {workflow}, got {rows}"
return rows[0]
class TestNoTrailingWhitespace:
"""``.pre-commit-config.yaml:9`` runs the bare hook::
- id: trailing-whitespace
with no ``--markdown-linebreak-ext`` and no ``exclude``. A markdown hard
break (two trailing spaces) in the generator's output is therefore
stripped from the committed file by the hook, so the artefact can never
match its own generator and the report PR never stops re-opening.
"""
def test_static_render_has_no_trailing_whitespace(self):
for line in _render_report().splitlines():
assert line == line.rstrip(), f"trailing whitespace: {line!r}"
def test_live_render_has_no_trailing_whitespace(self):
run = cih.WorkflowRun(
1, "success", "schedule", "main", "2026-01-02T03:04:05Z"
)
live = (
cih.LiveStatus("codeql.yml", (run,), "success", None),
cih.LiveStatus(
"semgrep.yml", (), None, None, "release-gate.yml", False
),
cih.LiveStatus("grype.yml", (run,), "success", None, None, True),
cih.LiveStatus("bandit.yml", (), None, "gh exit 127"),
)
for line in _render_report(live).splitlines():
assert line == line.rstrip(), f"trailing whitespace: {line!r}"
def test_committed_report_has_no_trailing_whitespace(self):
path = pathlib.Path(cih.ROOT) / ".github" / "CI_HEALTH.md"
text = path.read_text(encoding="utf-8")
for number, line in enumerate(text.splitlines(), 1):
assert line == line.rstrip(), (
f".github/CI_HEALTH.md:{number} has trailing whitespace; the "
"trailing-whitespace hook would strip it back out"
)
class TestHeaderMatchesContent:
"""The header claimed "not live run status" even while rendering one."""
def test_static_only_header_disclaims_live_status(self):
markdown = _render_report()
assert "not live run status" in markdown
assert "## Live scanner status" not in markdown
def test_live_header_announces_the_live_section(self):
run = cih.WorkflowRun(
1, "success", "schedule", "main", "2026-01-02T03:04:05Z"
)
markdown = _render_report(
(cih.LiveStatus("codeql.yml", (run,), "success", None),)
)
assert "## Live scanner status" in markdown
assert "not live run status" not in markdown, (
"the report denies carrying live status while printing a live table"
)
assert "plus a **live scanner status**" in markdown
assert "docs/ci/workflow-status.md" in markdown
class TestLiveEventFilter:
"""`--branch=main` matches the run's HEAD REF NAME. A fork's default
branch is also called `main`, so its pull-request runs pass that filter
while executing the contributor's tree, not this repository's."""
def test_fork_pull_request_run_named_main_is_excluded(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"failure",
databaseId=2,
event="pull_request",
headBranch="main",
),
_run_entry("success", databaseId=1, event="schedule"),
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert [run.database_id for run in live[0].runs] == [1]
assert live[0].latest_conclusion == "success"
assert findings == [], (
"a fork pull-request failure must not be reported as main being red"
)
def test_all_fetched_runs_filtered_out_is_not_idle(self, monkeypatch):
"""Five fork PRs from a branch named `main` can fill the whole query
page. That is a crowded window, not a scanner that never ran: the
entry must be unavailable (not `NO RUNS`), raise no idle finding,
and keep counting as coverage for its category."""
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"success",
databaseId=index,
event="pull_request",
headBranch="main",
)
for index in range(5, 0, -1)
]
),
)
findings: list = []
live = cih.analyze_live_status(
findings, ("zizmor-security.yml",), enabled=True
)
assert live[0].runs == ()
assert live[0].unavailable is not None
assert "pull-request" in live[0].unavailable
assert findings == [], "a crowded window is not an idle scanner"
category = cih.ScannerCategory(
"Workflow-Security",
True,
True,
("zizmor-security.yml",),
False,
)
[adjusted] = cih.discount_idle_scanners((category,), live, findings)
assert adjusted.satisfied is True
assert adjusted.workflows == ("zizmor-security.yml",)
assert findings == []
def test_pull_request_target_run_is_excluded(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"failure", databaseId=2, event="pull_request_target"
),
_run_entry("success", databaseId=1, event="push"),
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert [run.database_id for run in live[0].runs] == [1]
assert findings == []
def test_other_branch_run_is_excluded(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"failure",
databaseId=2,
event="push",
headBranch="release/1.2",
)
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].runs == ()
# The excluded run is not a failure of main, and a page that held
# only excluded runs is inconclusive, not idle.
assert findings == []
assert live[0].unavailable is not None
# release-gate.yml:64-65 gives `semgrep.yml` the job id `semgrep-scan`, and
# GitHub names every job a called workflow contributes to the caller's run
# "<caller job id> / <called job name>" -- verified on release-gate run
# 35810068506, whose job list holds `semgrep-scan / semgrep-scan`,
# `checkov-scan / checkov` and `owasp-zap-scan / ZAP API Scan` next to the
# roll-up jobs `Check Code Scanning Alerts` and `Release Gate Summary`.
_SEMGREP_JOB = "semgrep-scan / semgrep-scan"
_CHECKOV_JOB = "checkov-scan / checkov"
# The two jobs that failed on run 35810068506 while every scanner passed:
# a standing alert-inventory condition, not a scanner result.
_GATE_ROLLUP_FAILURES = [
_job("Check Code Scanning Alerts", "failure"),
_job("Release Gate Summary", "failure"),
]
class TestGatedScanners:
"""`docs/ci/workflow-status.md` already states the rule: a workflow with
no default-branch history of its own shows its GATED run, not its own
direct-run history (which is a stale dispatch log)."""
def test_workflow_call_only_scanner_is_read_through_its_gate(
self, monkeypatch
):
calls = _fake_run(
monkeypatch,
_gh_with_jobs(
[_run_entry("success")], [_job(_SEMGREP_JOB, "success")]
),
)
live = cih.analyze_live_status([], ("semgrep.yml",), enabled=True)
run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]
assert len(run_list) == 1
assert "--workflow=release-gate.yml" in run_list[0], (
"semgrep.yml is workflow_call/workflow_dispatch only; its own run "
"list is the manual-dispatch log, not evidence the scanner runs"
)
assert live[0].workflow == "semgrep.yml"
assert live[0].gated_by == "release-gate.yml"
def test_direct_failure_is_still_high(self, monkeypatch):
_fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))
findings: list = []
cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
# Also carries the "no successful run in the window" medium finding
# (BL3-1/R1): the lone run is a failure, so there is no success.
assert sorted(finding.severity for finding in findings) == [
"high",
"medium",
]
def test_self_triggering_scanner_is_queried_directly(self, monkeypatch):
calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))
live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)
run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]
assert "--workflow=codeql.yml" in run_list[0]
assert live[0].gated_by is None
def test_real_tree_resolves_the_documented_gated_scanners(self):
callers = cih._reusable_workflow_callers()
assert cih._gating_parent("semgrep.yml", callers) == "release-gate.yml"
assert cih._gating_parent("checkov.yml", callers) == "release-gate.yml"
assert cih._gating_parent("codeql.yml", callers) is None, (
"codeql.yml has its own push/PR/schedule triggers"
)
assert (
cih._gating_parent("owasp-zap-scan.yml", callers)
== "release-gate.yml"
)
def test_owasp_zap_scan_stays_gated_once_it_gains_a_pull_request_trigger(
self, monkeypatch
):
"""On `main` (unlike this PR's base tree), `owasp-zap-scan.yml` adds
a path-filtered `pull_request:` trigger next to `workflow_call:`/
`workflow_dispatch:` -- exactly the shape that previously fell back
to a dead direct-run log and reported STALE (see
TestGateResolution). Simulate that shape over the real tree (rather
than depend on `origin/main` being fetched in CI) by patching
`_workflow_on_block` for this one file; everything else --
`_reusable_workflow_callers()`'s real `uses:` scan included -- comes
from the actual repo tree."""
real_on_block = cih._workflow_on_block
def patched(path):
if path.name == "owasp-zap-scan.yml":
return {
"pull_request": None,
"workflow_call": None,
"workflow_dispatch": None,
}
return real_on_block(path)
monkeypatch.setattr(cih, "_workflow_on_block", patched)
callers = cih._reusable_workflow_callers()
assert (
cih._gating_parent("owasp-zap-scan.yml", callers)
== "release-gate.yml"
)
def test_gate_is_named_in_the_rendered_row(self):
run = cih.WorkflowRun(
1, "success", "schedule", "main", "2026-01-02T03:04:05Z"
)
markdown = _render_report(
(
cih.LiveStatus(
"semgrep.yml", (run,), "success", None, "release-gate.yml"
),
)
)
assert "gated by `release-gate.yml`" in _row_for(
markdown, "semgrep.yml"
)
class TestGatedScannersResolvePerJob:
"""A gate's conclusion is a roll-up over every job it runs, so it is not
this scanner's result in either direction. `release-gate.yml` concluded
`failure` on 19 of its last 20 `main` runs over a standing
`Check Code Scanning Alerts` condition while every scanner job in them
succeeded (run 35810068506). Each gate run is therefore resolved to the
scanner's own jobs."""
def test_failed_gate_summary_does_not_fail_the_scanners(self, monkeypatch):
calls = _fake_run(
monkeypatch,
_gh_with_jobs(
[_run_entry("failure", databaseId=900)],
[
_job(_SEMGREP_JOB, "success"),
_job(_CHECKOV_JOB, "success"),
*_GATE_ROLLUP_FAILURES,
],
),
)
findings: list = []
live = cih.analyze_live_status(
findings, ("checkov.yml", "semgrep.yml"), enabled=True
)
assert [entry.latest_conclusion for entry in live] == [
"success",
"success",
], "the gate's roll-up was printed under the scanners' names"
assert findings == []
views = [call for call in calls if call[:3] == ["gh", "run", "view"]]
assert [call[3] for call in views] == ["900"], (
"the jobs lookup must be memoised across the scanners sharing "
f"the gate run, got {views}"
)
markdown = _render_report(live)
for workflow in ("checkov.yml", "semgrep.yml"):
row = _row_for(markdown, workflow)
assert "| PASS |" in row, row
assert "FAIL" not in row, row
assert "this scanner's own jobs" in row
def test_failed_scanner_job_is_high_and_renders_fail(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_with_jobs(
[_run_entry("failure", databaseId=901)],
[_job(_SEMGREP_JOB, "failure"), *_GATE_ROLLUP_FAILURES],
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert live[0].latest_conclusion == "failure"
# A failed scanner job inside the gate IS a measurement of that
# scanner (high) -- and, with no success in the window, also the
# BL3-1/R1 "no successful run" medium finding.
highs = [f for f in findings if f.severity == "high"]
assert len(highs) == 1, findings
assert sorted(f.severity for f in findings) == ["high", "medium"]
assert "release-gate.yml" in highs[0].message
assert highs[0].file == "semgrep.yml"
row = _row_for(_render_report(live), "semgrep.yml")
assert "| FAIL |" in row, row
assert "latest run did not pass" in row
def test_unnamed_scanner_is_unresolved_not_failed(self, monkeypatch):
"""No job for this scanner in the gate run: the scanner was not
measured. Rendering FAIL would invent a failure; rendering PASS would
invent a success."""
_fake_run(
monkeypatch,
_gh_with_jobs(
[_run_entry("failure", databaseId=902)],
_GATE_ROLLUP_FAILURES,
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert live[0].latest_conclusion == "unresolved"
assert [finding.severity for finding in findings] == ["medium"]
assert "unknown, not passing" in findings[0].message
markdown = _render_report(live)
row = _row_for(markdown, "semgrep.yml")
assert "| UNRESOLVED |" in row, row
assert "FAIL" not in row and "PASS" not in row, row
assert "absence of evidence" in markdown
# This run WAS fetched and its job list genuinely lacked the
# scanner (no time-budget truncation, no unreadable response
# involved), so the row must say so -- this is the base case the
# "did not name this scanner" note exists for.
assert "did not name this scanner" in row, row
def test_settled_run_above_an_unresolved_run_raises_no_unresolved_medium(
self, monkeypatch
):
"""R5-4: the UNRESOLVED medium is keyed off the NEWEST run that
settled to anything, not off "any run in the row is UNRESOLVED". A
genuinely resolved PASS/FAIL newer than an older UNRESOLVED run
means the scanner WAS measured most recently; the older run's own
gap is not news."""
runs = [
_run_entry("success", databaseId=920, createdAt=_iso_days_ago(1)),
_run_entry("failure", databaseId=919, createdAt=_iso_days_ago(2)),
]
jobs_by_run = {
"920": [_job(_SEMGREP_JOB, "success"), *_GATE_ROLLUP_FAILURES],
"919": [_job("Other Job", "success"), *_GATE_ROLLUP_FAILURES],
}
def handler(command):
import json as _json
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
return (0, _json.dumps({"jobs": jobs_by_run[command[3]]}), "")
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert [run.conclusion for run in live[0].runs] == [
"success",
"unresolved",
]
assert findings == [], (
"a settled PASS newer than an UNRESOLVED run must not raise the "
f"UNRESOLVED medium: {findings}"
)
row = _row_for(_render_report(live), "semgrep.yml")
assert "| PASS |" in row, row
assert "UNRESOLVED" in row, row
def test_unreadable_job_list_is_unresolved_not_failed(self, monkeypatch):
"""R5-2: `gh run view` failing (exit != 0, timeout, malformed JSON)
means this scanner's job list was never READ -- distinct from a job
list that WAS read and simply didn't name this scanner. Saying "did
not name this scanner" here would be a guess about a list nothing is
known about."""
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
if command[:3] == ["gh", "run", "view"]:
return (1, "", "HTTP 403\n")
import json as _json
return (0, _json.dumps([_run_entry("failure", databaseId=903)]), "")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert live[0].latest_conclusion == "unresolved"
assert [finding.severity for finding in findings] == ["medium"]
row = _row_for(_render_report(live), "semgrep.yml")
assert "| UNRESOLVED |" in row, row
assert "the gate run's job list could not be read" in row, row
assert "did not name this scanner" not in row, row
def test_recent_runs_cell_shows_the_scanners_own_conclusions(
self, monkeypatch
):
runs = [
_run_entry("failure", databaseId=910),
_run_entry("failure", databaseId=909),
]
jobs_by_run = {
"910": [_job(_SEMGREP_JOB, "success"), *_GATE_ROLLUP_FAILURES],
"909": [_job(_SEMGREP_JOB, "failure"), *_GATE_ROLLUP_FAILURES],
}
def handler(command):
import json as _json
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0", "")
if command[:3] == ["gh", "run", "view"]:
return (0, _json.dumps({"jobs": jobs_by_run[command[3]]}), "")
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
live = cih.analyze_live_status([], ("semgrep.yml",), enabled=True)
assert [run.conclusion for run in live[0].runs] == [
"success",
"failure",
]
row = _row_for(_render_report(live), "semgrep.yml")
assert "| PASS FAIL |" in row, (
f"the recent-runs cell still shows the gate's history: {row}"
)
def test_worst_of_several_jobs_wins(self):
jobs = (
("owasp-zap-scan / ZAP Baseline Scan", "success"),
("owasp-zap-scan / ZAP API Scan", "failure"),
)
assert (
cih._scanner_conclusion_from_jobs(jobs, ("owasp-zap-scan",))
== "failure"
)
def test_nested_reusable_job_path_still_matches(self):
# A called workflow that itself calls one adds another segment.
jobs = (("osv-scan / scan / osv-scan", "success"),)
assert (
cih._scanner_conclusion_from_jobs(jobs, ("osv-scan",)) == "success"
)
def test_job_id_prefix_does_not_match_a_different_job(self):
jobs = (("grype-scan-extra / thing", "failure"),)
assert cih._scanner_conclusion_from_jobs(jobs, ("grype-scan",)) is None
def test_in_flight_gate_job_is_running_not_passing(self):
jobs = (("semgrep-scan / semgrep-scan", "unknown"),)
assert (
cih._scanner_conclusion_from_jobs(jobs, ("semgrep-scan",))
== "unknown"
)
def test_real_tree_maps_the_gate_jobs_to_their_scanners(self):
mapping = cih._gate_job_ids("release-gate.yml")
assert mapping.get("semgrep.yml") == ("semgrep-scan",)
assert mapping.get("gitleaks-main.yml") == ("gitleaks-scan",)
assert mapping.get("owasp-zap-scan.yml") == ("owasp-zap-scan",)
def test_gate_job_ids_prefix_is_the_jobs_name_when_set(
self, monkeypatch, tmp_path
):
"""R3: GitHub renders a called workflow's own jobs nested under the
caller job's DISPLAYED name -- its `name:`, when the job has one,
not the YAML key `_gate_job_ids` used to return unconditionally."""
_write_wf(
tmp_path,
"gate.yml",
{
"on": {"workflow_dispatch": {}},
"jobs": {
"gitleaks-scan": {
"name": "Gitleaks Scan",
"uses": "./.github/workflows/gitleaks-main.yml",
}
},
},
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
mapping = cih._gate_job_ids("gate.yml")
assert mapping.get("gitleaks-main.yml") == ("Gitleaks Scan",)
assert (
cih._scanner_conclusion_from_jobs(
(("Gitleaks Scan / gitleaks", "failure"),), ("Gitleaks Scan",)
)
== "failure"
)
def test_gate_job_ids_matrix_caller_prefix_matches(
self, monkeypatch, tmp_path
):
"""R3: a matrixed caller job's name gets its matrix value appended
in parens directly onto the same prefix (no `name:` override), e.g.
`owasp-zap-scan (staging) / ZAP API Scan`."""
_write_wf(
tmp_path,
"gate.yml",
{
"on": {"workflow_dispatch": {}},
"jobs": {
"owasp-zap-scan": {
"strategy": {"matrix": {"target": ["staging", "prod"]}},
"uses": "./.github/workflows/owasp-zap-scan.yml",
}
},
},
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
mapping = cih._gate_job_ids("gate.yml")
assert mapping.get("owasp-zap-scan.yml") == ("owasp-zap-scan",)
assert (
cih._scanner_conclusion_from_jobs(
(("owasp-zap-scan (staging) / ZAP API Scan", "failure"),),
("owasp-zap-scan",),
)
== "failure"
)
def test_budget_truncated_to_zero_resolved_runs_is_unresolved_not_no_runs(
self, monkeypatch
):
"""R2: `_resolve_gated_runs` breaking before resolving even the
newest of the five FOUND gate runs must not render as `NO RUNS`
(which claims no qualifying run existed at all -- false) and must
not drop the failing newest run silently."""
runs = [
_run_entry(
"failure", databaseId=100 + i, createdAt=_iso_days_ago(1 + i)
)
for i in range(5)
]
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
raise AssertionError(
"jobs call must not happen once the budget is spent"
)
import json as _json
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)
# Always inside the reserve window, so the very first run trips it.
monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 1000.0)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
entry = live[0]
assert entry.latest_conclusion == "unresolved"
assert any("time budget" in note for note in entry.notes)
assert any(
f.severity == "medium"
and f.message
== "gate history could not be resolved within the live time budget"
for f in findings
), findings
# R4-b: the run synthesized only because the budget ran out was
# never queried, so it must not ALSO raise the ordinary "could not
# resolve this scanner's jobs in ... run #N" medium naming a run
# number nothing was ever fetched for -- one medium, not two.
assert [f.severity for f in findings] == ["medium"], findings
row = _row_for(_render_report(live), "semgrep.yml")
assert "| UNRESOLVED |" in row, row
assert "NO RUNS" not in row, (
"five runs were found; the budget just couldn't resolve them"
)
# R4-c/R4-g: this run's job list was never checked (the budget ran
# out before it could be queried), so the row must not claim it
# "did not name this scanner" -- that would be a guess.
assert "did not name this scanner" not in row, row
def test_budget_truncated_to_some_keeps_the_resolved_runs(
self, monkeypatch
):
"""R2: truncated to >= 1 resolved run must keep those runs' real
conclusions (not synthesize UNRESOLVED) and still carry the
truncation note."""
runs = [
_run_entry("failure", databaseId=205, createdAt=_iso_days_ago(1)),
_run_entry("failure", databaseId=204, createdAt=_iso_days_ago(2)),
_run_entry("failure", databaseId=203, createdAt=_iso_days_ago(3)),
]
jobs_by_run = {
"205": [_job(_SEMGREP_JOB, "success")],
"204": [_job(_SEMGREP_JOB, "failure")],
}
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
rid = command[3]
if rid not in jobs_by_run:
raise AssertionError(
f"resolved past the truncation point: {rid}"
)
import json as _json
return (0, _json.dumps({"jobs": jobs_by_run[rid]}), "")
import json as _json
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
# `time.monotonic()` is called: once for the deadline, once for the
# outer per-workflow check, then once per run inside
# `_resolve_gated_runs` (205, 204, 203) -- land the 203 check inside
# the reserve window so only 205 and 204 get resolved.
from types import SimpleNamespace
seq = iter([0.0, 1.0, 2.0, 3.0, 90.0])
monkeypatch.setattr(
cih, "time", SimpleNamespace(monotonic=lambda: next(seq))
)
monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)
monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
entry = live[0]
assert [run.conclusion for run in entry.runs] == ["success", "failure"]
assert any("truncated to 2 of 3" in note for note in entry.notes)
def test_truncated_run_that_was_resolved_keeps_the_unnamed_scanner_note(
self, monkeypatch
):
"""R5-1: the newest gate run IS fetched and resolved (its job list
genuinely lacked this scanner), and the budget then runs out before
the OLDER run can be queried. Both facts are about this row and
neither should hide the other: the truncation note AND the
"did not name this scanner" note must both render."""
runs = [
_run_entry("failure", databaseId=10, createdAt=_iso_days_ago(1)),
_run_entry("failure", databaseId=9, createdAt=_iso_days_ago(2)),
]
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
assert command[3] == "10", command
import json as _json
return (
0,
_json.dumps({"jobs": [_job("Other Job", "success")]}),
"",
)
import json as _json
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
from types import SimpleNamespace
seq = iter([0.0, 1.0, 2.0, 90.0])
monkeypatch.setattr(
cih, "time", SimpleNamespace(monotonic=lambda: next(seq))
)
monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)
monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
row = _row_for(_render_report(live), "semgrep.yml")
assert "truncated to 1 of 2" in row, row
assert "did not name this scanner" in row, row
def test_gate_with_no_job_for_this_workflow_renders_one_note_not_two(
self, monkeypatch
):
"""R5-5: `_gate_job_ids` found no job in the gate calling this
workflow at all -- a per-gate fact, not a per-run one. The
entry-level note already says so; the per-run "did not name this
scanner" note would repeat the same cause under a different
sentence."""
_fake_run(
monkeypatch,
_gh_with_jobs(
[_run_entry("failure", databaseId=904)],
[_job("Other Job", "success")],
),
)
monkeypatch.setattr(cih, "_gate_job_ids", lambda gate: {})
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert live[0].latest_conclusion == "unresolved"
row = _row_for(_render_report(live), "semgrep.yml")
assert "| UNRESOLVED |" in row, row
assert row.count("has no job calling this workflow") == 1, row
assert "did not name this scanner" not in row, row
def test_in_flight_gate_run_with_unstarted_scanner_job_is_running(
self, monkeypatch
):
"""R5: the gate run itself is still in progress and its job list
(fetched successfully) simply does not have this scanner's job YET
-- that is "in flight", not "a completed run whose job list stayed
silent" (UNRESOLVED). The finding must come from the previous
CONCLUSIVE gate run, not be swallowed."""
jobs_by_run = {
"301": [_job("Other Job", "success")], # scanner's job absent
"300": [_job(_SEMGREP_JOB, "failure")],
}
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
import json as _json
return (
0,
_json.dumps({"jobs": jobs_by_run[command[3]]}),
"",
)
import json as _json
return (
0,
_json.dumps(
[
_run_entry(
None, databaseId=301, createdAt=_iso_days_ago(0)
),
_run_entry(
"failure",
databaseId=300,
createdAt=_iso_days_ago(1),
),
]
),
"",
)
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
entry = live[0]
assert entry.runs[0].conclusion == "unknown", (
"an unstarted scanner job in an in-flight gate run must stay "
f"'unknown' (RUNNING), not UNRESOLVED: {entry.runs[0]}"
)
row = _row_for(_render_report(live), "semgrep.yml")
assert "| RUNNING |" in row, row
assert "previous run FAIL" in row, row
# Finding(s) computed from the previous CONCLUSIVE gate run (#300,
# failure), not swallowed by the in-flight newest run.
assert findings, "the previous run's failure must not be swallowed"
assert any(
f.severity == "high" and "failure" in f.message for f in findings
)
def test_in_flight_gate_run_with_no_earlier_resolved_run_still_flags_unresolved(
self, monkeypatch
):
"""R4-d: the newest gate run is in flight (its job list, fetched
successfully, simply doesn't have this scanner's job YET -- stays
'unknown'/RUNNING, not UNRESOLVED) and NO earlier run ever resolved
to a conclusion either -- their job lists were fetched too and also
never named this scanner, so they are UNRESOLVED. The UNRESOLVED
medium must still fire, keyed off the newest run that resolved to
ANYTHING (not `latest`, which never resolves while in flight); the
row itself is unchanged (RUNNING label, UNRESOLVED chips beneath
it)."""
jobs_by_run = {
str(300 - i): [_job("Other Job", "success")] for i in range(5)
}
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
import json as _json
return (
0,
_json.dumps({"jobs": jobs_by_run[command[3]]}),
"",
)
import json as _json
runs = [
_run_entry(None, databaseId=300, createdAt=_iso_days_ago(0))
] + [
_run_entry(
"failure", databaseId=300 - i, createdAt=_iso_days_ago(i)
)
for i in range(1, 5)
]
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
entry = live[0]
assert entry.runs[0].conclusion == "unknown", entry.runs
assert entry.runs[1].conclusion == "unresolved", entry.runs
assert [f.severity for f in findings] == ["medium"], findings
assert "run #299" in findings[0].message, findings
row = _row_for(_render_report(live), "semgrep.yml")
assert "| RUNNING |" in row, row
assert "UNRESOLVED" in row, row
def test_in_flight_newest_plus_truncated_older_run_still_flags_unresolved(
self, monkeypatch
):
"""R5-3: the newest gate run is in flight (kept "unknown", RUNNING)
and the OLDER run -- a FAILURE -- never even gets its job list
queried because the live time budget runs out first. No run in the
row ever settles to anything, so neither the "not runs" empty-tuple
branch nor the newest-settled-is-UNRESOLVED branch fires; without
this fix that FAILURE is invisible with zero findings raised."""
runs = [
_run_entry(None, databaseId=10, createdAt=_iso_days_ago(0)),
_run_entry("failure", databaseId=9, createdAt=_iso_days_ago(1)),
]
def handler(command):
if command[:2] == ["gh", "--version"]:
return (0, "gh version 2.0.0\n", "")
if command[:3] == ["gh", "run", "view"]:
assert command[3] == "10", command
import json as _json
return (
0,
_json.dumps({"jobs": [_job("Other Job", "success")]}),
"",
)
import json as _json
return (0, _json.dumps(runs), "")
_fake_run(monkeypatch, handler)
from types import SimpleNamespace
seq = iter([0.0, 1.0, 2.0, 90.0])
monkeypatch.setattr(
cih, "time", SimpleNamespace(monotonic=lambda: next(seq))
)
monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)
monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)
findings: list = []
live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)
assert [run.conclusion for run in live[0].runs] == ["unknown"], (
"the in-flight newest run must stay 'unknown' (RUNNING), not be "
f"overwritten: {live[0].runs}"
)
assert [f.severity for f in findings] == ["medium"], findings
assert (
findings[0].message
== "gate history could not be resolved within the live time budget"
), findings
row = _row_for(_render_report(live), "semgrep.yml")
assert "| RUNNING |" in row, row
assert "truncated to 1 of 2" in row, row
class TestGateResolution:
"""Which gate a scanner is read through, and which workflows qualify."""
def _gate(self, tmp_path, monkeypatch, scanner_on, gates=("release-gate",)):
_write_wf(
tmp_path,
"scanner.yml",
{"on": scanner_on, "jobs": {"scan": {"steps": []}}},
)
for gate in gates:
_write_wf(
tmp_path,
f"{gate}.yml",
{
"on": {"schedule": [{"cron": "0 2 * * *"}]},
"jobs": {
"scanner": {"uses": "./.github/workflows/scanner.yml"}
},
},
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
return cih._reusable_workflow_callers()
def test_pull_request_only_scanner_is_still_read_through_its_gate(
self, monkeypatch, tmp_path
):
"""`owasp-zap-scan.yml` on `main` adds a path-filtered
`pull_request:` trigger to `workflow_call:`/`workflow_dispatch:`.
Pull-request runs are discarded by `_is_default_branch_run`, so that
trigger gives the workflow no default-branch history of its own --
treating it as one sent the scanner back to a dead dispatch log and
reported STALE while the gate ran it nightly."""
callers = self._gate(
tmp_path,
monkeypatch,
{
"pull_request": {"paths": ["src/**"]},
"workflow_call": {},
"workflow_dispatch": {},
},
)
assert callers["scanner.yml"] == ("release-gate.yml",)
assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"
def test_pull_request_target_only_scanner_is_gated_too(
self, monkeypatch, tmp_path
):
callers = self._gate(
tmp_path,
monkeypatch,
{"pull_request_target": {}, "workflow_call": {}},
)
assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"
def test_a_scanner_with_its_own_push_history_is_not_gated(
self, monkeypatch, tmp_path
):
callers = self._gate(
tmp_path,
monkeypatch,
{"push": {"branches": ["main"]}, "workflow_call": {}},
)
assert cih._gating_parent("scanner.yml", callers) is None, (
"a workflow that runs on pushes to main has its own history"
)
def test_release_gate_beats_ci_gate(self, monkeypatch, tmp_path):
"""`GATE_ROOTS` is an unordered set; alphabetical order would pick
`ci-gate.yml`, whose `main` history is two cancelled dispatches."""
callers = self._gate(
tmp_path,
monkeypatch,
{"workflow_call": {}},
gates=("ci-gate", "release-gate"),
)
assert callers["scanner.yml"] == ("ci-gate.yml", "release-gate.yml"), (
"the caller list is sorted, so gates[0] would be ci-gate.yml"
)
assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"
@pytest.mark.parametrize(
"uses",
[
"./.github/workflows/scanner.yml",
"'./.github/workflows/scanner.yml'",
'"./.github/workflows/scanner.yml"',
],
)
def test_quoted_uses_paths_are_recognised(
self, monkeypatch, tmp_path, uses
):
"""A quoted `uses:` value is the same reference; the line scanner
sees the quotes, the YAML parser does not."""
(tmp_path / "scanner.yml").write_text(
"on:\n workflow_call: {}\njobs:\n scan:\n steps: []\n",
encoding="utf-8",
)
(tmp_path / "release-gate.yml").write_text(
"on:\n schedule:\n - cron: '0 2 * * *'\n"
f"jobs:\n scanner:\n uses: {uses}\n",
encoding="utf-8",
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
callers = cih._reusable_workflow_callers()
assert callers.get("scanner.yml") == ("release-gate.yml",)
assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"
assert cih._gate_job_ids("release-gate.yml") == {
"scanner.yml": ("scanner",)
}
class TestLiveStaleness:
"""An eight-month-old success is not evidence that a scanner works."""
def test_old_run_is_stale_and_raises_a_finding(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok([_run_entry("success", createdAt="2020-01-01T00:00:00Z")]),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].stale
stale = [
finding
for finding in findings
if "staleness threshold" in finding.message
]
assert len(stale) == 1, f"no staleness finding in {findings}"
assert stale[0].file == "codeql.yml"
assert stale[0].area == "scanner-runtime"
assert "2020-01-01" in stale[0].message
def test_recent_run_is_not_stale(self, monkeypatch):
_fake_run(
monkeypatch,
_gh_ok([_run_entry("success", createdAt=_iso_days_ago(2))]),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert not live[0].stale
assert findings == []
def test_stale_entry_renders_stale_with_the_date_not_pass(self):
run = cih.WorkflowRun(
7, "success", "schedule", "main", "2020-01-01T00:00:00Z"
)
markdown = _render_report(
(cih.LiveStatus("codeql.yml", (run,), "success", None, None, True),)
)
row = _row_for(markdown, "codeql.yml")
assert "| STALE |" in row, f"stale run still reported as: {row}"
assert "| 2020-01-01 |" in row
def test_threshold_reuses_the_repository_rule(self):
rules = cih._status_rules()
assert rules is not None, (
"scripts/generate_workflow_status.py failed to load; the "
"staleness rule must not be a second, drifting copy"
)
crons = cih._cron_strings("codeql.yml")
assert crons, "codeql.yml lost its cron; pick another scheduled scanner"
expected = max(
cih._STALE_FLOOR_DAYS,
rules.STALE_MULTIPLIER
* min(rules.cron_cadence_days(cron) for cron in crons),
)
assert cih._stale_threshold_days("codeql.yml") == expected
def test_threshold_falls_back_to_the_floor_without_a_cron(self):
assert (
cih._stale_threshold_days("no-such-workflow.yml")
== cih._STALE_FLOOR_DAYS
)
def test_floor_is_the_siblings_floor_to_the_digit(self):
"""`classify_staleness` uses `max(60.0, ...)`
(scripts/generate_workflow_status.py:618). A different floor here
would let the two dashboards call the same workflow fresh and stale
on the same day."""
assert cih._STALE_FLOOR_DAYS == 60.0
@pytest.mark.parametrize(
("cron", "expected"),
[
# daily: 2 x 1 day is under the floor, so the floor wins
("0 3 * * *", 60.0),
# yearly: 2 x 365 days -- well clear of the floor, so this is the
# multiplier being exercised rather than the floor being restated
("0 3 1 1 *", 730.0),
],
)
def test_threshold_follows_the_cadence_not_only_the_floor(
self, monkeypatch, tmp_path, cron, expected
):
_write_wf(
tmp_path,
"scanner.yml",
{"on": {"schedule": [{"cron": cron}]}, "jobs": {}},
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
assert cih._stale_threshold_days("scanner.yml") == expected
def test_threshold_multiplies_a_cadence_above_the_floor(
self, monkeypatch, tmp_path
):
"""`cron_cadence_days()`'s real buckets are coarse (daily/weekly/
monthly=30/yearly=365), so a monthly cron (2 x 30 = 60) lands
exactly on the floor and cannot tell the multiplier apart from the
floor clamping it -- the previous "monthly" case here did exactly
that and proved nothing beyond the daily case above it. Patch
`cron_cadence_days` to a cadence a real cron can't express (45
days, comfortably clear of the floor) so `2 x 45 = 90` can only
come from the multiplier actually running."""
_write_wf(
tmp_path,
"scanner.yml",
{"on": {"schedule": [{"cron": "0 3 1 * *"}]}, "jobs": {}},
)
monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)
rules = cih._status_rules()
assert rules is not None
monkeypatch.setattr(rules, "cron_cadence_days", lambda cron: 45.0)
assert cih._stale_threshold_days("scanner.yml") == 90.0
def test_staleness_is_measured_on_the_last_successful_run(
self, monkeypatch
):
"""The sibling measures `last_success_iso`, not the newest run: a
scanner whose recent runs all failed is not fresh because it failed
recently."""
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry("failure", databaseId=2),
_run_entry(
"success",
databaseId=1,
createdAt="2020-01-01T00:00:00Z",
),
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].stale, (
"a recent failure made an eight-month-old success look fresh"
)
severities = sorted(finding.severity for finding in findings)
assert severities == ["high", "medium"], findings
row = _row_for(_render_report(live), "codeql.yml")
assert "| FAIL |" in row, (
f"the staleness label swallowed the failing latest run: {row}"
)
assert "last successful run 2020-01-01" in row
def test_unparseable_created_at_fails_closed_to_stale(self, monkeypatch):
"""A timestamp we cannot read is not evidence of a recent run."""
_fake_run(
monkeypatch, _gh_ok([_run_entry("success", createdAt="whenever")])
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].stale
assert [finding.severity for finding in findings] == ["medium"]
assert "unparseable" in findings[0].message
row = _row_for(_render_report(live), "codeql.yml")
assert "| STALE |" in row, row
assert "unparseable run timestamp" in row
def test_staleness_stays_below_the_check_gate(self, monkeypatch, tmp_path):
"""Absent evidence is a medium finding: it is reported, but it does
not fail --check the way a red scanner does."""
_fake_run(
monkeypatch,
_gh_ok([_run_entry("success", createdAt="2020-01-01T00:00:00Z")]),
)
out = tmp_path / "o.md"
assert _run_cli(["--check", "--live", "--out", str(out)]) == 0
assert "| STALE |" in out.read_text(encoding="utf-8")
class TestNoSuccessInWindowIsStaleUnconditionally:
"""BL3-1/R1: `generate_workflow_status.py:618-619`'s sibling rule is "no
success ⇒ stale unconditionally" -- not "fall back to the newest run of
any conclusion and call IT the measured success". A scanner whose recent
runs are all failures (or all cancelled, or any non-success mix) is
stale regardless of how young its newest failing run is, and the
finding/row must never claim a failure was a success."""
def test_five_failures_are_stale_with_the_no_success_message(
self, monkeypatch
):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"failure",
databaseId=500 + i,
createdAt=_iso_days_ago(400 + i),
)
for i in range(5)
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].stale
assert live[0].latest_conclusion == "failure"
no_success = [
f
for f in findings
if "no successful run in the query window" in f.message
]
assert len(no_success) == 1, findings
assert no_success[0].severity == "medium"
# The message must name the newest run's OWN conclusion, never
# relabel a failure as a success (BL3-1).
assert "failure" in no_success[0].message
assert "newest successful run" not in no_success[0].message
row = _row_for(_render_report(live), "codeql.yml")
# The row's label stays the latest conclusion (FAIL), never PASS --
# there was never a success to lie about.
assert "| FAIL |" in row, row
assert "| PASS |" not in row
assert "| STALE |" not in row
assert "no successful run in the query window" in row
assert "(newest run" in row, row
def test_five_cancelled_are_stale_too(self, monkeypatch):
"""Not a failure -- `cancelled` never raises a high finding -- but
still no evidence the scanner works, so still stale."""
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(
"cancelled",
databaseId=600 + i,
createdAt=_iso_days_ago(5 + i),
)
for i in range(5)
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].stale
assert live[0].latest_conclusion == "cancelled"
assert [f.severity for f in findings] == ["medium"], findings
assert "no successful run in the query window" in findings[0].message
row = _row_for(_render_report(live), "codeql.yml")
assert "| CANCELLED |" in row, row
assert "| PASS |" not in row
class TestInFlightAndEmptyHistory:
def test_in_flight_run_does_not_mask_a_failed_previous_run(
self, monkeypatch
):
_fake_run(
monkeypatch,
_gh_ok(
[
_run_entry(None, databaseId=2),
_run_entry("failure", databaseId=1),
]
),
)
findings: list = []
live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)
assert live[0].latest_conclusion == "unknown"
# Also carries the BL3-1/R1 "no successful run" medium finding: the
# in-flight run is inconclusive, and the previous CONCLUSIVE run
# (#1) was a failure, so the window has no success either.
highs = [f for f in findings if f.severity == "high"]
assert len(highs) == 1, (
f"a run racing the report suppressed the failure finding: {findings}"
)
assert sorted(f.severity for f in findings) == ["high", "medium"]
assert "run #1" in highs[0].message
def test_in_flight_renders_running_with_the_previous_conclusion(self):
runs = (
cih.WorkflowRun(
2, "unknown", "schedule", "main", "2026-02-02T00:00:00Z"
),
cih.WorkflowRun(
1, "failure", "schedule", "main", "2026-02-01T00:00:00Z"
),
)
row = _row_for(
_render_report(
(cih.LiveStatus("codeql.yml", runs, "unknown", None),)
),
"codeql.yml",
)
assert "| RUNNING |" in row
assert "previous run FAIL" in row
assert "latest run did not pass" in row
def test_empty_history_renders_no_runs_with_a_footnote(self):
markdown = _render_report(
(cih.LiveStatus("codeql.yml", (), None, None),)
)
row = _row_for(markdown, "codeql.yml")
assert "| NO RUNS |" in row, f"empty history rendered as: {row}"
assert "absence of evidence" in markdown
class TestWorkflowClaimsMatchWiring:
"""The workflow's comment described a gate reading the findings outputs.
No step ran ``--check``, and nothing read ``findings_*``."""
def _workflow_text(self) -> str:
path = (
pathlib.Path(cih.ROOT)
/ ".github"
/ "workflows"
/ "ci-health-report.yml"
)
return path.read_text(encoding="utf-8")
def test_no_step_gates_on_the_report_and_none_is_claimed(self):
"""Structural, so an explanatory comment cannot satisfy it: the old
form asserted `"--check" in text`, which the comment saying there is
no --check step made true."""
import yaml as _yaml
text = self._workflow_text()
workflow = _yaml.safe_load(text)
commands = [
step["run"]
for job in workflow["jobs"].values()
for step in job["steps"]
if isinstance(step, dict) and isinstance(step.get("run"), str)
]
assert commands, "no run: steps parsed out of the workflow"
assert any(
"generate_ci_health.py" in command for command in commands
), "no step runs the analyzer at all"
for command in commands:
assert "--check" not in command, (
f"a step gates on the report: {command!r}; either keep the "
"gating step and say so, or do not claim one"
)
assert "the gate" not in text.casefold(), (
"the workflow's prose names a gate that no step performs"
)
def test_every_emitted_output_is_consumed(self, tmp_path, monkeypatch):
gh_out = tmp_path / "gh_output"
monkeypatch.setenv("GITHUB_OUTPUT", str(gh_out))
assert (
_run_cli(["--github-output", "--out", str(tmp_path / "o.md")]) == 0
)
emitted = [
line.split("=", 1)[0]
for line in gh_out.read_text(encoding="utf-8").splitlines()
if "=" in line
]
text = self._workflow_text()
for key in emitted:
assert f"steps.gen.outputs.{key}" in text, (
f"{key} is written to $GITHUB_OUTPUT but no step reads it"
)