|
2 | 2 | """ |
3 | 3 | Mining report aggregator — corpus mining (see docs/notes/mining.md). |
4 | 4 |
|
5 | | -Reads the human-format findings that `own-check` prints over a real C# repo and |
6 | | -turns them into a structured Markdown summary: counts by OWN code, severity |
7 | | -(errors = candidate leaks, warnings = advisory / OWN050 "unchecked"), resource |
8 | | -kind, the noisiest files, and a triage list of the error-severity findings to |
9 | | -eyeball. |
| 5 | +Reads own-check findings — its human text **or** a `--format sarif` log — over a |
| 6 | +real C# repo and turns them into a structured Markdown summary: counts by OWN |
| 7 | +code, severity (errors = candidate leaks, warnings = advisory / OWN050 |
| 8 | +"unchecked"), resource kind, the noisiest files, and a triage list of the |
| 9 | +error-severity findings to eyeball. |
10 | 10 |
|
11 | 11 | This is evaluation tooling for the analyser itself: a clean run is a precision |
12 | 12 | signal; a pile of OWN001s is either real bugs or a false-positive pattern to |
13 | 13 | harden; a high OWN050 count flags a coverage gap (unresolved external refs). |
14 | 14 |
|
15 | | -dotnet-free: the extractor (own-check) runs upstream; this only reads its text. |
| 15 | +dotnet-free: the extractor (own-check) runs upstream; this only reads its output. |
16 | 16 |
|
17 | 17 | Usage: |
18 | | - own-check.sh --format human -- <repo> | mine_report.py --repo owner/name --commit SHA |
19 | | - mine_report.py findings.txt --repo owner/name --commit SHA [--json out.json] |
| 18 | + own-check.sh --format sarif -- <repo> | mine_report.py --repo owner/name --commit SHA |
| 19 | + mine_report.py findings.{txt,sarif} --repo owner/name --commit SHA [--json out.json] |
20 | 20 | mine_report.py --selftest |
21 | 21 | """ |
22 | 22 |
|
@@ -72,15 +72,62 @@ def _net_open(s: str) -> int: |
72 | 72 | + s.count("[") - s.count("]")) |
73 | 73 |
|
74 | 74 |
|
| 75 | +def _sarif_finding(res: dict[str, Any]) -> dict[str, Any]: |
| 76 | + """Turn one SARIF result into the same finding dict the human-text parser yields |
| 77 | + (file / line / severity / code / message / kind), so the aggregation is |
| 78 | + format-agnostic. The SARIF level maps to the human severity (`error` -> error, |
| 79 | + `warning`/`note` -> warning), so an advisory OWN050 `note` and an injected-source |
| 80 | + `warning` both count as advisory exactly as in the text path; the resource kind |
| 81 | + comes from `properties.resourceKind`, and a trailing ` [resource: kind]` is split |
| 82 | + off the message for parity with the human format.""" |
| 83 | + loc = (res.get("locations") or [{}])[0].get("physicalLocation") or {} |
| 84 | + uri = (loc.get("artifactLocation") or {}).get("uri", "") |
| 85 | + line = (loc.get("region") or {}).get("startLine", 0) |
| 86 | + msg = (res.get("message") or {}).get("text") or "" |
| 87 | + kind = (res.get("properties") or {}).get("resourceKind", "") |
| 88 | + tail = _RESOURCE_TAIL.search(msg) |
| 89 | + if tail: |
| 90 | + kind = kind or tail["kind"] |
| 91 | + msg = msg[:tail.start()].rstrip() |
| 92 | + return { |
| 93 | + "file": uri, |
| 94 | + "line": line if isinstance(line, int) else 0, |
| 95 | + "severity": "error" if res.get("level") == "error" else "warning", |
| 96 | + "code": res.get("ruleId", ""), |
| 97 | + "message": msg, |
| 98 | + "kind": kind, |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +def _parse_sarif(doc: dict[str, Any]) -> list[dict[str, Any]]: |
| 103 | + """Every result across every run of a SARIF log, as finding dicts.""" |
| 104 | + return [_sarif_finding(res) |
| 105 | + for run in doc.get("runs", []) |
| 106 | + for res in run.get("results", [])] |
| 107 | + |
| 108 | + |
75 | 109 | def parse(text: str) -> tuple[list[dict[str, Any]], int]: |
76 | | - """Parse own-check human output into finding dicts; return (findings, unparsed). |
77 | | - A finding may span several lines — an inline lambda handler body is echoed across |
78 | | - lines, with the trailing `[resource: kind]` tag (when present) on the last. A |
79 | | - non-header line extends the current finding's message ONLY while that message's |
80 | | - brackets are still unbalanced (i.e. we are inside the lambda body); once balanced, |
81 | | - the finding is complete and a further non-matching line is stray drift, counted — |
82 | | - not silently absorbed. Build chatter shouldn't reach here (own-check sends it to |
83 | | - stderr); a stray line that does and isn't chatter is counted, not dropped.""" |
| 110 | + """Parse own-check output into finding dicts; return (findings, unparsed). |
| 111 | +
|
| 112 | + Accepts either format own-check emits. **SARIF** (`--format sarif`, a `{`-leading |
| 113 | + log with a `runs` array) is read structurally — no regex, `unparsed` is always 0. |
| 114 | + Otherwise the **human text**: a finding may span several lines — an inline lambda |
| 115 | + handler body is echoed across lines, with the trailing `[resource: kind]` tag |
| 116 | + (when present) on the last. A non-header line extends the current finding's message |
| 117 | + ONLY while that message's brackets are still unbalanced (i.e. we are inside the |
| 118 | + lambda body); once balanced, the finding is complete and a further non-matching |
| 119 | + line is stray drift, counted — not silently absorbed. Build chatter shouldn't reach |
| 120 | + here (own-check sends it to stderr); a stray line that does and isn't chatter is |
| 121 | + counted, not dropped.""" |
| 122 | + if text.lstrip().startswith("{"): |
| 123 | + try: |
| 124 | + doc = json.loads(text) |
| 125 | + except json.JSONDecodeError: |
| 126 | + doc = None |
| 127 | + if isinstance(doc, dict) and isinstance(doc.get("runs"), list): |
| 128 | + return _parse_sarif(doc), 0 |
| 129 | + # {-leading but not a SARIF log: fall through to the text parser, which |
| 130 | + # surfaces it as unparsed drift rather than silently reporting "clean". |
84 | 131 | findings: list[dict[str, Any]] = [] |
85 | 132 | unparsed = 0 |
86 | 133 | cur: dict[str, Any] | None = None |
@@ -248,9 +295,45 @@ def _selftest() -> int: |
248 | 295 | # the --stats coverage line is surfaced when supplied (header + clean note). |
249 | 296 | if "42 methods" not in render_md([], 0, "o/r", "abc123", "coverage: 1/42 methods"): |
250 | 297 | fails.append("coverage line not rendered") |
| 298 | + |
| 299 | + # own-check can also feed SARIF (--format sarif); parse() sniffs it and yields the |
| 300 | + # SAME finding dicts as the human path, so the aggregation is identical. The shape |
| 301 | + # mirrors ownlang.ownir.build_sarif (note level = advisory OWN050). |
| 302 | + def _res(code: str, level: str, uri: str, line: int, kind: str) -> dict[str, Any]: |
| 303 | + return {"ruleId": code, "level": level, |
| 304 | + "message": {"text": f"a leak [resource: {kind}]"}, |
| 305 | + "locations": [{"physicalLocation": { |
| 306 | + "artifactLocation": {"uri": uri}, "region": {"startLine": line}}}], |
| 307 | + "properties": {"resourceKind": kind}} |
| 308 | + sarif_text = json.dumps({"version": "2.1.0", "runs": [{"tool": {"driver": |
| 309 | + {"name": "Own.NET"}}, "results": [ |
| 310 | + _res("OWN001", "error", "src/A.cs", 12, "disposable"), |
| 311 | + _res("OWN001", "error", "src/A.cs", 40, "disposable"), |
| 312 | + _res("OWN002", "error", "src/A.cs", 30, "disposable"), |
| 313 | + _res("OWN050", "note", "src/B.cs", 5, "unresolved reference"), |
| 314 | + ]}]}) |
| 315 | + sf, sf_unparsed = parse(sarif_text) |
| 316 | + sagg = aggregate(sf) |
| 317 | + if sf_unparsed != 0: |
| 318 | + fails.append(f"SARIF input must not report unparsed, got {sf_unparsed}") |
| 319 | + if sagg["by_code"] != {"OWN001": 2, "OWN002": 1, "OWN050": 1}: |
| 320 | + fails.append(f"SARIF by_code wrong: {sagg['by_code']}") |
| 321 | + if (sagg["errors"], sagg["advisories"]) != (3, 1): |
| 322 | + fails.append(f"SARIF severity split wrong: {sagg['errors']}/{sagg['advisories']}") |
| 323 | + if sagg["by_kind"].get("disposable") != 3 or sagg["by_file"].get("src/A.cs") != 3: |
| 324 | + fails.append(f"SARIF by_kind/by_file wrong: {sagg['by_kind']} {sagg['by_file']}") |
| 325 | + a0 = next(f for f in sf if f["code"] == "OWN050") |
| 326 | + if a0["kind"] != "unresolved reference" or "[resource:" in a0["message"]: |
| 327 | + fails.append(f"SARIF message/kind split wrong: {a0}") |
| 328 | + # a {-leading input that is NOT a SARIF log falls through to the text parser and |
| 329 | + # surfaces as unparsed drift, not a silent clean read. |
| 330 | + _, ns_drift = parse('{"notruns": 1}\n') |
| 331 | + if ns_drift != 1: |
| 332 | + fails.append(f"non-SARIF JSON should surface as drift, got {ns_drift} unparsed") |
| 333 | + |
251 | 334 | for f in fails: |
252 | 335 | print(f"MINE SELFTEST FAIL: {f}") |
253 | | - print(f"mine_report selftest: {8 - len(fails)}/8 checks passed") |
| 336 | + print(f"mine_report selftest: {14 - len(fails)}/14 checks passed") |
254 | 337 | return 1 if fails else 0 |
255 | 338 |
|
256 | 339 |
|
|
0 commit comments