quality-graph-core 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quality-graph-core
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Platform-independent Quality Graph contracts and decision logic
5
5
  Project-URL: Homepage, https://quality-graph.xyz/
6
6
  Project-URL: Documentation, https://quality-graph.xyz/
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "quality-graph-core"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  description = "Platform-independent Quality Graph contracts and decision logic"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -4,13 +4,16 @@ from __future__ import annotations
4
4
 
5
5
  import hashlib
6
6
  import json
7
+ from collections import Counter
7
8
  from dataclasses import dataclass, replace
8
9
  from typing import TYPE_CHECKING, cast
9
10
 
10
11
  from defusedxml import ElementTree
11
12
  from defusedxml.common import DefusedXmlException
12
13
 
14
+ from quality_graph_core.json_values import JsonValidator
13
15
  from quality_graph_core.result import (
16
+ MAX_FINDINGS,
14
17
  Annotation,
15
18
  Diagnostic,
16
19
  DiagnosticKind,
@@ -53,13 +56,16 @@ if TYPE_CHECKING:
53
56
  MAX_REPORT_BYTES = 10 * 1024 * 1024
54
57
  MAX_SUMMARY_CHARACTERS = 60_000
55
58
  MAX_JUNIT_DIAGNOSTICS = 100
56
- MAX_JUNIT_FINDINGS = 10_000
59
+ MAX_JUNIT_FINDINGS = MAX_FINDINGS
57
60
 
58
61
 
59
62
  class AdapterError(ValueError):
60
63
  """Represent deterministic failure to read or translate a report."""
61
64
 
62
65
 
66
+ JSON = JsonValidator(AdapterError)
67
+
68
+
63
69
  @dataclass(frozen=True)
64
70
  class AdapterContext:
65
71
  """Provide trusted node and workflow metadata to an adapter."""
@@ -97,7 +103,7 @@ def adapt_exit(context: AdapterContext, output: str = "") -> Result:
97
103
  def adapt_native(context: AdapterContext, report: bytes) -> Result:
98
104
  """Validate a native result and bind it to trusted execution metadata."""
99
105
  try:
100
- data = _object(_decode_json(report, "Native result"), "native result")
106
+ data = JSON.object(_decode_json(report, "Native result"), "native result")
101
107
  if "reportVersion" in data:
102
108
  data = _bind_producer(context, data)
103
109
  result = Result.from_value(data)
@@ -110,23 +116,42 @@ def adapt_native(context: AdapterContext, report: bytes) -> Result:
110
116
  if result.provenance != context.provenance:
111
117
  message = "Native result provenance does not match the current workflow attempt"
112
118
  raise AdapterError(message)
113
- return _reconcile_command(context, result)
119
+ result = _reconcile_command(context, result)
120
+ if result.status is ResultStatus.PASSED and result.omitted_findings:
121
+ return replace(result, status=ResultStatus.FAILED, failure_kind=FailureKind.QUALITY)
122
+ return result
114
123
 
115
124
 
116
125
  def adapt_sarif(context: AdapterContext, report: bytes) -> Result:
117
126
  """Translate SARIF findings and locations into the shared protocol."""
127
+ try:
128
+ return _adapt_sarif(context, report)
129
+ except (TypeError, ValueError) as error:
130
+ message = f"SARIF result is invalid: {error}"
131
+ raise AdapterError(message) from error
132
+
133
+
134
+ def _adapt_sarif(context: AdapterContext, report: bytes) -> Result:
118
135
  data = _decode_json(report, "SARIF")
119
- root = _object(data, "SARIF report")
120
- runs = _array(root.get("runs"), "SARIF runs")
136
+ root = JSON.object(data, "SARIF report")
137
+ runs = JSON.array(root.get("runs"), "SARIF runs")
138
+ entries: dict[str, tuple[Finding, Annotation | None]] = {}
139
+ for run_value in runs:
140
+ run = JSON.object(run_value, "SARIF run")
141
+ for result_value in JSON.array(run.get("results", []), "SARIF results"):
142
+ identity = json.dumps({"tool": run.get("tool"), "result": result_value}, sort_keys=True)
143
+ entries[identity] = _sarif_finding(JSON.object(result_value, "SARIF result"))
144
+ counts = Counter(finding.id for finding, _ in entries.values())
121
145
  findings: list[Finding] = []
122
146
  annotations: list[Annotation] = []
123
- for run_value in runs:
124
- run = _object(run_value, "SARIF run")
125
- for result_value in _array(run.get("results", []), "SARIF results"):
126
- finding, annotation = _sarif_finding(_object(result_value, "SARIF result"))
127
- findings.append(finding)
128
- if annotation is not None:
129
- annotations.append(annotation)
147
+ for identity, (finding, annotation) in entries.items():
148
+ resolved = finding
149
+ if counts[finding.id] > 1:
150
+ fingerprint = hashlib.sha256(identity.encode()).hexdigest()
151
+ resolved = replace(finding, id=f"sarif-{fingerprint[:24]}", fingerprint=fingerprint)
152
+ findings.append(resolved)
153
+ if annotation is not None and len(findings) <= MAX_FINDINGS:
154
+ annotations.append(annotation)
130
155
  errors = sum(finding.severity is Severity.ERROR for finding in findings)
131
156
  status = ResultStatus.FAILED if errors or not context.command_succeeded else ResultStatus.PASSED
132
157
  result = Result(
@@ -137,8 +162,9 @@ def adapt_sarif(context: AdapterContext, report: bytes) -> Result:
137
162
  FailureKind.QUALITY if status is ResultStatus.FAILED else None,
138
163
  f"Found {len(findings)} SARIF findings ({errors} errors).",
139
164
  (Metric("Findings", str(len(findings))), Metric("Errors", str(errors))),
140
- tuple(findings),
165
+ tuple(findings[:MAX_FINDINGS]),
141
166
  tuple(annotations),
167
+ omitted_findings=max(0, len(findings) - MAX_FINDINGS),
142
168
  )
143
169
  return _reconcile_command(context, result)
144
170
 
@@ -196,6 +222,7 @@ def junit_report(report: bytes) -> dict[str, JsonValue]:
196
222
  else [],
197
223
  }
198
224
  if failures > MAX_JUNIT_FINDINGS:
225
+ value["omittedFindings"] = failures - MAX_JUNIT_FINDINGS
199
226
  notes = cast("list[JsonValue]", value["notes"])
200
227
  notes.append(f"{failures - MAX_JUNIT_FINDINGS} additional findings omitted.")
201
228
  if findings:
@@ -254,6 +281,12 @@ def adapter_failure(context: AdapterContext, error: AdapterError) -> Result:
254
281
 
255
282
 
256
283
  def _reconcile_command(context: AdapterContext, result: Result) -> Result:
284
+ if (
285
+ context.command_succeeded
286
+ and result.omitted_findings
287
+ and result.status is ResultStatus.PASSED
288
+ ):
289
+ result = replace(result, status=ResultStatus.FAILED, failure_kind=FailureKind.QUALITY)
257
290
  if context.command_succeeded or result.status in {ResultStatus.FAILED, ResultStatus.CANCELLED}:
258
291
  return result
259
292
  diagnostic = Diagnostic(
@@ -290,11 +323,11 @@ def _decode_json(value: bytes, context: str) -> JsonValue:
290
323
 
291
324
 
292
325
  def _sarif_finding(data: dict[str, JsonValue]) -> tuple[Finding, Annotation | None]:
293
- rule_id = _optional_string(data.get("ruleId"), "SARIF rule id")
294
- message = _sarif_message(_object(data.get("message"), "SARIF message"))
295
- severity = _sarif_severity(_optional_string(data.get("level"), "SARIF level"))
326
+ rule_id = JSON.optional_string(data.get("ruleId"), "SARIF rule id")
327
+ message = _sarif_message(JSON.object(data.get("message"), "SARIF message"))
328
+ severity = _sarif_severity(JSON.optional_string(data.get("level"), "SARIF level"))
296
329
  location = _sarif_location(data.get("locations"))
297
- partial = _optional_object(data.get("partialFingerprints"), "SARIF partial fingerprints")
330
+ partial = JSON.optional_object(data.get("partialFingerprints"), "SARIF partial fingerprints")
298
331
  fingerprint = _sarif_fingerprint(rule_id, message, location, partial)
299
332
  finding_id = f"sarif-{fingerprint[:24]}"
300
333
  finding = Finding(
@@ -311,7 +344,7 @@ def _sarif_finding(data: dict[str, JsonValue]) -> tuple[Finding, Annotation | No
311
344
 
312
345
  def _sarif_message(data: dict[str, JsonValue]) -> str:
313
346
  text = data.get("text", data.get("markdown"))
314
- return _string(text, "SARIF message text")
347
+ return JSON.string(text, "SARIF message text")
315
348
 
316
349
 
317
350
  def _sarif_severity(value: str | None) -> Severity:
@@ -323,21 +356,21 @@ def _sarif_severity(value: str | None) -> Severity:
323
356
 
324
357
 
325
358
  def _sarif_location(value: JsonValue) -> SourceLocation | None:
326
- locations = _array(value if value is not None else [], "SARIF locations")
359
+ locations = JSON.array(value if value is not None else [], "SARIF locations")
327
360
  if not locations:
328
361
  return None
329
- location = _object(locations[0], "SARIF location")
330
- physical = _object(location.get("physicalLocation"), "SARIF physical location")
331
- artifact = _object(physical.get("artifactLocation"), "SARIF artifact location")
332
- region = _object(physical.get("region"), "SARIF region")
333
- start_line = _integer(region.get("startLine"), "SARIF start line")
334
- end_line = _optional_integer(region.get("endLine"), "SARIF end line") or start_line
362
+ location = JSON.object(locations[0], "SARIF location")
363
+ physical = JSON.object(location.get("physicalLocation"), "SARIF physical location")
364
+ artifact = JSON.object(physical.get("artifactLocation"), "SARIF artifact location")
365
+ region = JSON.object(physical.get("region"), "SARIF region")
366
+ start_line = JSON.integer(region.get("startLine"), "SARIF start line")
367
+ end_line = JSON.optional_integer(region.get("endLine"), "SARIF end line") or start_line
335
368
  return SourceLocation(
336
- _string(artifact.get("uri"), "SARIF artifact URI"),
369
+ JSON.string(artifact.get("uri"), "SARIF artifact URI"),
337
370
  start_line,
338
371
  end_line,
339
- _optional_integer(region.get("startColumn"), "SARIF start column"),
340
- _optional_integer(region.get("endColumn"), "SARIF end column"),
372
+ JSON.optional_integer(region.get("startColumn"), "SARIF start column"),
373
+ JSON.optional_integer(region.get("endColumn"), "SARIF end column"),
341
374
  )
342
375
 
343
376
 
@@ -349,7 +382,8 @@ def _sarif_fingerprint(
349
382
  ) -> str:
350
383
  if partial:
351
384
  semantic = "\n".join(
352
- f"{key}={_string(value, 'SARIF fingerprint')}" for key, value in sorted(partial.items())
385
+ f"{key}={JSON.string(value, 'SARIF fingerprint')}"
386
+ for key, value in sorted(partial.items())
353
387
  )
354
388
  else:
355
389
  semantic = "\n".join((rule_id or "", message, location.path if location else ""))
@@ -373,43 +407,3 @@ def _junit_finding(case: XmlElement, failure: XmlElement) -> Finding:
373
407
  group=class_name or None,
374
408
  location=SourceLocation(case.get("file"), 1, 1) if case.get("file") else None,
375
409
  )
376
-
377
-
378
- def _object(value: JsonValue, context: str) -> dict[str, JsonValue]:
379
- if not isinstance(value, dict):
380
- message = f"{context} must be an object"
381
- raise AdapterError(message)
382
- return value
383
-
384
-
385
- def _optional_object(value: JsonValue, context: str) -> dict[str, JsonValue] | None:
386
- return None if value is None else _object(value, context)
387
-
388
-
389
- def _array(value: JsonValue, context: str) -> list[JsonValue]:
390
- if not isinstance(value, list):
391
- message = f"{context} must be an array"
392
- raise AdapterError(message)
393
- return value
394
-
395
-
396
- def _string(value: JsonValue, context: str) -> str:
397
- if not isinstance(value, str):
398
- message = f"{context} must be a string"
399
- raise AdapterError(message)
400
- return value
401
-
402
-
403
- def _optional_string(value: JsonValue, context: str) -> str | None:
404
- return None if value is None else _string(value, context)
405
-
406
-
407
- def _integer(value: JsonValue, context: str) -> int:
408
- if not isinstance(value, int) or isinstance(value, bool):
409
- message = f"{context} must be an integer"
410
- raise AdapterError(message)
411
- return value
412
-
413
-
414
- def _optional_integer(value: JsonValue, context: str) -> int | None:
415
- return None if value is None else _integer(value, context)
@@ -0,0 +1,52 @@
1
+ """Collect bounded reports consistently for every hosting provider."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import replace
6
+ from typing import TYPE_CHECKING
7
+
8
+ from quality_graph_core.adapters import (
9
+ AdapterContext,
10
+ AdapterError,
11
+ adapt_exit,
12
+ adapt_junit,
13
+ adapt_native,
14
+ adapt_sarif,
15
+ adapter_failure,
16
+ read_report,
17
+ )
18
+ from quality_graph_core.graph import AdapterKind
19
+
20
+ if TYPE_CHECKING:
21
+ from pathlib import Path
22
+
23
+ from quality_graph_core.result import Result
24
+
25
+
26
+ def collect_report(
27
+ context: AdapterContext, kind: AdapterKind, workspace: Path, report_path: str | None = None
28
+ ) -> Result:
29
+ """Read and adapt one existing report into trusted execution context."""
30
+ try:
31
+ if kind is AdapterKind.EXIT_CODE:
32
+ output = ""
33
+ if report_path is not None:
34
+ try:
35
+ output = read_report(workspace, report_path).decode()
36
+ except UnicodeDecodeError as error:
37
+ message = "Exit-code command output must be UTF-8"
38
+ raise AdapterError(message) from error
39
+ return adapt_exit(context, output)
40
+ if report_path is None:
41
+ message = f"{kind.value} adapter requires a report path"
42
+ raise AdapterError(message)
43
+ report = read_report(workspace, report_path)
44
+ adapters = {
45
+ AdapterKind.NATIVE: adapt_native,
46
+ AdapterKind.JUNIT: adapt_junit,
47
+ AdapterKind.SARIF: adapt_sarif,
48
+ }
49
+ result = adapters[kind](context, report)
50
+ return replace(result, controls=())
51
+ except (OSError, TypeError, ValueError) as error:
52
+ return adapter_failure(context, AdapterError(str(error)))