@gnost-cli/darwin-arm64 0.5.3-rc.1 → 0.6.0-rc.1
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.
- package/bundle/_internal/base_library.zip +0 -0
- package/bundle/_internal/bin/ruff +0 -0
- package/bundle/_internal/certifi/cacert.pem +3885 -0
- package/bundle/_internal/gnost/__init__.py +1 -1
- package/bundle/_internal/gnost/analysis/analyzers/readability_analyzer.py +4 -1
- package/bundle/_internal/gnost/analysis/utils/tool_command.py +8 -0
- package/bundle/_internal/gnost/changes/__init__.py +1 -0
- package/bundle/_internal/gnost/changes/analyzer.py +215 -0
- package/bundle/_internal/gnost/changes/findings.py +55 -0
- package/bundle/_internal/gnost/changes/impact.py +59 -0
- package/bundle/_internal/gnost/changes/refs.py +105 -0
- package/bundle/_internal/gnost/changes/render.py +172 -0
- package/bundle/_internal/gnost/changes/report.py +124 -0
- package/bundle/_internal/gnost/changes/snapshot.py +109 -0
- package/bundle/_internal/gnost/ci/__init__.py +1 -0
- package/bundle/_internal/gnost/ci/baseline.py +88 -0
- package/bundle/_internal/gnost/ci/exit_codes.py +19 -0
- package/bundle/_internal/gnost/ci/fingerprint.py +44 -0
- package/bundle/_internal/gnost/ci/gate.py +42 -0
- package/bundle/_internal/gnost/ci/paths.py +57 -0
- package/bundle/_internal/gnost/ci/run.py +135 -0
- package/bundle/_internal/gnost/ci/sarif.py +149 -0
- package/bundle/_internal/gnost/ci/scorecard.py +95 -0
- package/bundle/_internal/gnost/cli/app.py +406 -155
- package/bundle/_internal/gnost/cli/commands/changes.py +144 -0
- package/bundle/_internal/gnost/cli/commands/init.py +192 -0
- package/bundle/_internal/gnost/cli/commands/onboard.py +83 -27
- package/bundle/_internal/gnost/cli/commands/remote.py +53 -0
- package/bundle/_internal/gnost/cli/commands/show.py +15 -14
- package/bundle/_internal/gnost/config/languages.py +19 -4
- package/bundle/_internal/gnost/config/settings.py +127 -2
- package/bundle/_internal/gnost/core/flow.py +27 -0
- package/bundle/_internal/gnost/core/insight_builder.py +3 -1
- package/bundle/_internal/gnost/core/risk.py +115 -0
- package/bundle/_internal/gnost/reporters/insights_json.py +65 -1
- package/bundle/_internal/gnost/reporters/loc_summary.py +3 -3
- package/bundle/_internal/gnost/reporters/markdown.py +89 -10
- package/bundle/_internal/gnost/reporters/mermaid.py +7 -0
- package/bundle/_internal/gnost/reporters/readme.py +11 -6
- package/bundle/_internal/gnost/reporters/stats.py +3 -3
- package/bundle/_internal/gnost/reporters/summary.py +48 -1
- package/bundle/_internal/gnost/scanner/filters.py +45 -12
- package/bundle/_internal/gnost/scanner/loc.py +5 -0
- package/bundle/_internal/gnost/schemas/insights.schema.json +153 -0
- package/bundle/_internal/gnost/utils/footer.py +36 -0
- package/bundle/_internal/gnost/utils/integrations.py +346 -0
- package/bundle/_internal/gnost/utils/layout.py +111 -0
- package/bundle/_internal/gnost/utils/progress.py +6 -3
- package/bundle/_internal/gnost/utils/update_check.py +253 -0
- package/bundle/_internal/gnost/vcs/__init__.py +1 -0
- package/bundle/_internal/gnost/vcs/git.py +181 -0
- package/bundle/_internal/gnost/vcs/history.py +404 -0
- package/bundle/_internal/gnost/vcs/remote.py +204 -0
- package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/METADATA +7 -4
- package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/RECORD +88 -30
- package/bundle/_internal/gnost-0.6.0rc1.dist-info/REQUESTED +0 -0
- package/bundle/_internal/librt/base64.cpython-312-darwin.so +0 -0
- package/bundle/_internal/librt/internal.cpython-312-darwin.so +0 -0
- package/bundle/_internal/yaml/_yaml.cpython-312-darwin.so +0 -0
- package/bundle/gnost +0 -0
- package/package.json +1 -1
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info/REQUESTED → certifi/py.typed} +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/INSTALLER +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/WHEEL +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/direct_url.json +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/entry_points.txt +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/licenses/LICENSE +0 -0
- /package/bundle/_internal/{gnost-0.5.3rc1.dist-info → gnost-0.6.0rc1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""The result of `gnost changes`: plain data classes that the renderers and JSON share.
|
|
2
|
+
|
|
3
|
+
`ChangeReport` holds the changed files with their risk, the blast radius, routes, cycles and
|
|
4
|
+
findings. `to_dict` is the JSON written for tools; the Markdown and terminal views read the
|
|
5
|
+
same object so the three always agree.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import asdict, dataclass, field
|
|
11
|
+
from typing import Any, Dict, List, Optional
|
|
12
|
+
|
|
13
|
+
from gnost.changes.impact import AffectedFile
|
|
14
|
+
from gnost.changes.refs import ChangedFile
|
|
15
|
+
|
|
16
|
+
SCHEMA_VERSION = "1.0"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class FileRisk:
|
|
21
|
+
"""How fragile and how busy one changed file is."""
|
|
22
|
+
|
|
23
|
+
path: str
|
|
24
|
+
structural: float # caution score, 0..100
|
|
25
|
+
churn: Optional[float] = None # change-hotspot score, None without history
|
|
26
|
+
risk: Optional[float] = None # structural x churn, None without history
|
|
27
|
+
why: str = "" # strongest caution signals in plain words
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def level(self) -> float:
|
|
31
|
+
"""The number to rank by: the combined risk when known, else the structural score."""
|
|
32
|
+
return self.risk if self.risk is not None else self.structural
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class RouteChange:
|
|
37
|
+
"""An HTTP route touched by the change."""
|
|
38
|
+
|
|
39
|
+
method: str
|
|
40
|
+
path: str
|
|
41
|
+
handler: str
|
|
42
|
+
file: str
|
|
43
|
+
state: str # added, removed, changed (handler file changed) or affected
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class CycleChange:
|
|
48
|
+
"""A circular dependency that exists on one side only."""
|
|
49
|
+
|
|
50
|
+
files: List[str]
|
|
51
|
+
kind: str
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class FindingInChange:
|
|
56
|
+
"""An analyzer finding located in a changed file."""
|
|
57
|
+
|
|
58
|
+
file: str
|
|
59
|
+
line: int
|
|
60
|
+
severity: str
|
|
61
|
+
title: str
|
|
62
|
+
analyzer: str
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class ChangeReport:
|
|
67
|
+
"""Everything `gnost changes` found for one range."""
|
|
68
|
+
|
|
69
|
+
base: str
|
|
70
|
+
head: str
|
|
71
|
+
base_commit: str
|
|
72
|
+
head_commit: str
|
|
73
|
+
files: List[ChangedFile]
|
|
74
|
+
affected: List[AffectedFile]
|
|
75
|
+
depth: int
|
|
76
|
+
risks: List[FileRisk] = field(default_factory=list)
|
|
77
|
+
routes: List[RouteChange] = field(default_factory=list)
|
|
78
|
+
new_cycles: List[CycleChange] = field(default_factory=list)
|
|
79
|
+
fixed_cycles: List[CycleChange] = field(default_factory=list)
|
|
80
|
+
findings: Optional[List[FindingInChange]] = None # None: not asked for
|
|
81
|
+
cycles_compared: bool = True
|
|
82
|
+
notes: List[str] = field(default_factory=list)
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def label(self) -> str:
|
|
86
|
+
"""The range as shown to people, for example `main..feature`."""
|
|
87
|
+
return f"{self.base}..{self.head}"
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def counts(self) -> Dict[str, int]:
|
|
91
|
+
"""Changed files per status."""
|
|
92
|
+
out = {"added": 0, "modified": 0, "deleted": 0, "renamed": 0}
|
|
93
|
+
for f in self.files:
|
|
94
|
+
out[f.status] += 1
|
|
95
|
+
return out
|
|
96
|
+
|
|
97
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
98
|
+
"""The JSON form (schema version 1.0), stable key order."""
|
|
99
|
+
return {
|
|
100
|
+
"schema_version": SCHEMA_VERSION,
|
|
101
|
+
"range": {
|
|
102
|
+
"base": self.base,
|
|
103
|
+
"head": self.head,
|
|
104
|
+
"base_commit": self.base_commit,
|
|
105
|
+
"head_commit": self.head_commit,
|
|
106
|
+
},
|
|
107
|
+
"counts": self.counts,
|
|
108
|
+
"files": [asdict(f) for f in self.files],
|
|
109
|
+
"blast_radius": {
|
|
110
|
+
"depth": self.depth,
|
|
111
|
+
"affected": [asdict(a) for a in self.affected],
|
|
112
|
+
},
|
|
113
|
+
"risk": [asdict(r) for r in self.risks],
|
|
114
|
+
"routes": [asdict(r) for r in self.routes],
|
|
115
|
+
"cycles": {
|
|
116
|
+
"compared": self.cycles_compared,
|
|
117
|
+
"new": [asdict(c) for c in self.new_cycles],
|
|
118
|
+
"fixed": [asdict(c) for c in self.fixed_cycles],
|
|
119
|
+
},
|
|
120
|
+
"findings": (
|
|
121
|
+
None if self.findings is None else [asdict(f) for f in self.findings]
|
|
122
|
+
),
|
|
123
|
+
"notes": list(self.notes),
|
|
124
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""One scanned version of a project: its files, import graph, flow, routes and cycles.
|
|
2
|
+
|
|
3
|
+
`ProjectSnapshot.scan` runs the same steps as `gnost onboard` on a folder. `RefCheckout` exports
|
|
4
|
+
a git ref into a temporary folder so the base side of a change can be scanned the same way
|
|
5
|
+
without touching the working tree.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import shutil
|
|
12
|
+
import tempfile
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from types import TracebackType
|
|
15
|
+
from typing import List, Optional, Sequence, Set, Type
|
|
16
|
+
|
|
17
|
+
from gnost.core.cycles import Cycle
|
|
18
|
+
from gnost.core.flow import FlowBuilder, FlowResult
|
|
19
|
+
from gnost.core.graph import DependencyGraph
|
|
20
|
+
from gnost.languages.base import LanguageAdapter
|
|
21
|
+
from gnost.languages.java import JavaAdapter
|
|
22
|
+
from gnost.languages.javascript import JavaScriptAdapter
|
|
23
|
+
from gnost.languages.python import PythonAdapter
|
|
24
|
+
from gnost.languages.typescript import TypeScriptAdapter
|
|
25
|
+
from gnost.routes import Route, find_routes
|
|
26
|
+
from gnost.scanner.engine import ScannerEngine
|
|
27
|
+
from gnost.scanner.models import ScanResult
|
|
28
|
+
from gnost.vcs.git import GitRepository
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def default_adapters() -> List[LanguageAdapter]:
|
|
32
|
+
"""The language adapters every GNOST scan uses."""
|
|
33
|
+
return [PythonAdapter(), JavaScriptAdapter(), TypeScriptAdapter(), JavaAdapter()]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class ProjectSnapshot:
|
|
38
|
+
"""A scanned folder with the structures built from it."""
|
|
39
|
+
|
|
40
|
+
root: str
|
|
41
|
+
scan: ScanResult
|
|
42
|
+
graph: DependencyGraph
|
|
43
|
+
flow: FlowResult
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def scan_folder(
|
|
47
|
+
cls, root: str, exclude: Optional[Sequence[str]] = None
|
|
48
|
+
) -> "ProjectSnapshot":
|
|
49
|
+
"""Scan root and build the dependency graph, flow and routes."""
|
|
50
|
+
scan = ScannerEngine(
|
|
51
|
+
adapters=default_adapters(), exclude=list(exclude or [])
|
|
52
|
+
).scan(root)
|
|
53
|
+
scan.routes = find_routes(scan)
|
|
54
|
+
graph = DependencyGraph.from_scan(scan)
|
|
55
|
+
flow = FlowBuilder(graph=graph, scan=scan).build()
|
|
56
|
+
return cls(root=root, scan=scan, graph=graph, flow=flow)
|
|
57
|
+
|
|
58
|
+
def relative(self, path: str) -> str:
|
|
59
|
+
"""A scanned file's path relative to the root, with forward slashes."""
|
|
60
|
+
return os.path.relpath(path, self.scan.root).replace(os.sep, "/")
|
|
61
|
+
|
|
62
|
+
def files(self) -> Set[str]:
|
|
63
|
+
"""Relative paths of every scanned file."""
|
|
64
|
+
return {self.relative(f.path) for f in self.scan.files}
|
|
65
|
+
|
|
66
|
+
def cycle_keys(self) -> Set[frozenset]:
|
|
67
|
+
"""Each circular dependency as the set of its relative file paths."""
|
|
68
|
+
return {self.cycle_key(c) for c in self.flow.cycles}
|
|
69
|
+
|
|
70
|
+
def cycle_key(self, cycle: Cycle) -> frozenset:
|
|
71
|
+
"""The identity of one cycle: which files are in it."""
|
|
72
|
+
return frozenset(self.relative(f) for f in cycle.files)
|
|
73
|
+
|
|
74
|
+
def routes(self) -> List[Route]:
|
|
75
|
+
"""The routes found in this version."""
|
|
76
|
+
return list(self.scan.routes)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class RefCheckout:
|
|
80
|
+
"""Exports a git ref into a temporary folder; removes it on exit, even after Ctrl+C.
|
|
81
|
+
|
|
82
|
+
`with RefCheckout(repo, commit, subpath) as folder:` gives the folder to scan.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
def __init__(self, repo: GitRepository, ref: str, subpath: str = ".") -> None:
|
|
86
|
+
self.repo = repo
|
|
87
|
+
self.ref = ref
|
|
88
|
+
self.subpath = subpath
|
|
89
|
+
self._temp: Optional[str] = None
|
|
90
|
+
|
|
91
|
+
def __enter__(self) -> str:
|
|
92
|
+
self._temp = tempfile.mkdtemp(prefix="gnost-base-")
|
|
93
|
+
try:
|
|
94
|
+
self.repo.checkout_to(self.ref, self._temp, self.subpath)
|
|
95
|
+
except BaseException:
|
|
96
|
+
self.__exit__(None, None, None)
|
|
97
|
+
raise
|
|
98
|
+
folder = os.path.join(self._temp, self.subpath)
|
|
99
|
+
return os.path.realpath(folder if self.subpath not in ("", ".") else self._temp)
|
|
100
|
+
|
|
101
|
+
def __exit__(
|
|
102
|
+
self,
|
|
103
|
+
exc_type: Optional[Type[BaseException]],
|
|
104
|
+
exc: Optional[BaseException],
|
|
105
|
+
tb: Optional[TracebackType],
|
|
106
|
+
) -> None:
|
|
107
|
+
if self._temp:
|
|
108
|
+
shutil.rmtree(self._temp, ignore_errors=True)
|
|
109
|
+
self._temp = None
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""CI features of `gnost analyze`: score gate, baseline, SARIF output and exit codes."""
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""A saved list of known findings, so CI can report only the new ones.
|
|
2
|
+
|
|
3
|
+
`gnost analyze --write-baseline` records how many findings of each fingerprint exist today;
|
|
4
|
+
`--baseline` then treats up to that many of each as known and reports the rest. Counting (not
|
|
5
|
+
a plain yes/no) means fixing one of three similar findings never hides a newly added one.
|
|
6
|
+
The file is small, sorted JSON, meant to be committed.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
from typing import Dict, Iterable, List, Tuple
|
|
14
|
+
|
|
15
|
+
from gnost import __version__
|
|
16
|
+
from gnost.analysis.core.models import UnifiedFinding
|
|
17
|
+
from gnost.ci.fingerprint import FindingFingerprint
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class BaselineError(Exception):
|
|
21
|
+
"""The baseline file is missing or cannot be read; the message says what to do."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Baseline:
|
|
25
|
+
"""Known findings as a count per fingerprint."""
|
|
26
|
+
|
|
27
|
+
VERSION = 1
|
|
28
|
+
|
|
29
|
+
def __init__(self, counts: Dict[str, int]) -> None:
|
|
30
|
+
self.counts = counts
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def from_findings(
|
|
34
|
+
cls, findings: Iterable[UnifiedFinding], fingerprint: FindingFingerprint
|
|
35
|
+
) -> "Baseline":
|
|
36
|
+
"""The baseline that makes every one of these findings known."""
|
|
37
|
+
counts: Dict[str, int] = {}
|
|
38
|
+
for finding in findings:
|
|
39
|
+
key = fingerprint.of(finding)
|
|
40
|
+
counts[key] = counts.get(key, 0) + 1
|
|
41
|
+
return cls(counts)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def load(cls, path: str) -> "Baseline":
|
|
45
|
+
"""Read a baseline file; raises BaselineError if it is missing or not a baseline."""
|
|
46
|
+
try:
|
|
47
|
+
with open(path, encoding="utf-8") as f:
|
|
48
|
+
data = json.load(f)
|
|
49
|
+
except OSError:
|
|
50
|
+
raise BaselineError(
|
|
51
|
+
f"baseline file not found: {path} (create it with --write-baseline)"
|
|
52
|
+
) from None
|
|
53
|
+
except ValueError as exc:
|
|
54
|
+
raise BaselineError(f"{path} is not valid JSON ({exc})") from exc
|
|
55
|
+
counts = data.get("fingerprints") if isinstance(data, dict) else None
|
|
56
|
+
if not isinstance(counts, dict) or not all(
|
|
57
|
+
isinstance(v, int) for v in counts.values()
|
|
58
|
+
):
|
|
59
|
+
raise BaselineError(f"{path} is not a gnost baseline file")
|
|
60
|
+
return cls({str(k): int(v) for k, v in counts.items()})
|
|
61
|
+
|
|
62
|
+
def save(self, path: str) -> None:
|
|
63
|
+
"""Write the baseline (sorted, so the file only changes when the findings do)."""
|
|
64
|
+
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
|
|
65
|
+
data = {
|
|
66
|
+
"version": self.VERSION,
|
|
67
|
+
"generator": f"gnost {__version__}",
|
|
68
|
+
"total": sum(self.counts.values()),
|
|
69
|
+
"fingerprints": dict(sorted(self.counts.items())),
|
|
70
|
+
}
|
|
71
|
+
with open(path, "w", encoding="utf-8") as f:
|
|
72
|
+
json.dump(data, f, indent=2)
|
|
73
|
+
f.write("\n")
|
|
74
|
+
|
|
75
|
+
def split(
|
|
76
|
+
self, findings: List[UnifiedFinding], fingerprint: FindingFingerprint
|
|
77
|
+
) -> Tuple[List[UnifiedFinding], List[UnifiedFinding]]:
|
|
78
|
+
"""(new, known): per fingerprint the first N by line are known, the rest are new."""
|
|
79
|
+
groups: Dict[str, List[UnifiedFinding]] = {}
|
|
80
|
+
for finding in findings:
|
|
81
|
+
groups.setdefault(fingerprint.of(finding), []).append(finding)
|
|
82
|
+
known_ids = set()
|
|
83
|
+
for key, group in groups.items():
|
|
84
|
+
ordered = sorted(group, key=lambda f: f.location.line_number or 0)
|
|
85
|
+
known_ids.update(id(f) for f in ordered[: self.counts.get(key, 0)])
|
|
86
|
+
new = [f for f in findings if id(f) not in known_ids]
|
|
87
|
+
known = [f for f in findings if id(f) in known_ids]
|
|
88
|
+
return new, known
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""The exit codes of the gnost command line, documented for scripts and CI.
|
|
2
|
+
|
|
3
|
+
0 means success. 1 is an unexpected failure while running. 2 is a mistake in how the command or
|
|
4
|
+
the config was written (argparse uses 2 as well). 3 means the quality gate failed.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from enum import IntEnum
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ExitCode(IntEnum):
|
|
13
|
+
"""Process exit codes; compare with `int(ExitCode.GATE_FAILED)` or use directly in returns."""
|
|
14
|
+
|
|
15
|
+
OK = 0
|
|
16
|
+
ERROR = 1
|
|
17
|
+
USAGE = 2
|
|
18
|
+
GATE_FAILED = 3
|
|
19
|
+
INTERRUPTED = 130
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""A stable identity for a finding, so the same problem is recognised across runs.
|
|
2
|
+
|
|
3
|
+
The fingerprint uses what the finding is (analyzer, rule, normalised title, resolved file,
|
|
4
|
+
function and class) and never the line number or numbers in the message, which change
|
|
5
|
+
whenever code above or around it moves. Used by the baseline and as the SARIF fingerprint.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
from gnost.analysis.core.models import UnifiedFinding
|
|
14
|
+
from gnost.ci.paths import PathResolver
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class FindingFingerprint:
|
|
18
|
+
"""Computes a fingerprint for findings using one path resolver."""
|
|
19
|
+
|
|
20
|
+
VERSION = "v1"
|
|
21
|
+
_DIGITS = re.compile(r"\d+")
|
|
22
|
+
_SPACES = re.compile(r"\s+")
|
|
23
|
+
|
|
24
|
+
def __init__(self, resolver: PathResolver) -> None:
|
|
25
|
+
self.resolver = resolver
|
|
26
|
+
|
|
27
|
+
def of(self, finding: UnifiedFinding) -> str:
|
|
28
|
+
"""A 32-character hex fingerprint, the same for the same problem in the same place."""
|
|
29
|
+
location = finding.location
|
|
30
|
+
parts = [
|
|
31
|
+
self.VERSION,
|
|
32
|
+
finding.source_analyzer,
|
|
33
|
+
finding.rule_id or "",
|
|
34
|
+
self.normalise_title(finding.title),
|
|
35
|
+
self.resolver.resolve(location.file_path),
|
|
36
|
+
location.function_name or "",
|
|
37
|
+
location.class_name or "",
|
|
38
|
+
]
|
|
39
|
+
return hashlib.sha256("|".join(parts).encode("utf-8")).hexdigest()[:32]
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def normalise_title(cls, title: str) -> str:
|
|
43
|
+
"""Lower case, numbers replaced, spaces collapsed: 'Function 12 is long' == 'function # is long'."""
|
|
44
|
+
return cls._SPACES.sub(" ", cls._DIGITS.sub("#", title.lower())).strip()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""The quality gate: decide from the score and the new findings whether a CI run passes.
|
|
2
|
+
|
|
3
|
+
`QualityGate` holds the two optional rules (`--fail-under`, `--fail-on-new`) and returns a
|
|
4
|
+
`GateResult` with a message per broken rule, ready to print and to turn into an exit code.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import List, Optional
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class GateResult:
|
|
15
|
+
"""Whether the run passed, and one sentence for each rule it broke."""
|
|
16
|
+
|
|
17
|
+
passed: bool
|
|
18
|
+
messages: List[str] = field(default_factory=list)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class QualityGate:
|
|
22
|
+
"""Fails a run when the score is under a threshold or findings are new against a baseline."""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self, fail_under: Optional[float] = None, fail_on_new: bool = False
|
|
26
|
+
) -> None:
|
|
27
|
+
self.fail_under = fail_under
|
|
28
|
+
self.fail_on_new = fail_on_new
|
|
29
|
+
|
|
30
|
+
def evaluate(self, score: float, new_findings: Optional[int]) -> GateResult:
|
|
31
|
+
"""Check the final score (0 to 10) and the number of new findings (None without a baseline)."""
|
|
32
|
+
messages: List[str] = []
|
|
33
|
+
if self.fail_under is not None and score < self.fail_under:
|
|
34
|
+
messages.append(
|
|
35
|
+
f"score {score:.2f} is below the required {self.fail_under:.2f} (--fail-under)"
|
|
36
|
+
)
|
|
37
|
+
if self.fail_on_new and new_findings:
|
|
38
|
+
noun = "finding" if new_findings == 1 else "findings"
|
|
39
|
+
messages.append(
|
|
40
|
+
f"{new_findings} new {noun} not in the baseline (--fail-on-new)"
|
|
41
|
+
)
|
|
42
|
+
return GateResult(passed=not messages, messages=messages)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Maps the file paths in findings back to real files, relative to the repository root.
|
|
2
|
+
|
|
3
|
+
Analyzers report paths in different forms: absolute, relative, or only the last two parts
|
|
4
|
+
(`core/file_utils.py`). SARIF and fingerprints need one stable form, so `PathResolver`
|
|
5
|
+
matches each reported path against the files that were analyzed.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from typing import Dict, Iterable, List
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PathResolver:
|
|
15
|
+
"""Resolves a reported path to a repository-relative path with forward slashes."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, files: Iterable[str], root: str) -> None:
|
|
18
|
+
self.root = os.path.realpath(root)
|
|
19
|
+
self._known = {self._relative(f) for f in files}
|
|
20
|
+
self._by_suffix: Dict[str, List[str]] = {}
|
|
21
|
+
for rel in sorted(self._known):
|
|
22
|
+
parts = rel.split("/")
|
|
23
|
+
for i in range(len(parts)):
|
|
24
|
+
self._by_suffix.setdefault("/".join(parts[i:]), []).append(rel)
|
|
25
|
+
|
|
26
|
+
def resolve(self, reported: str) -> str:
|
|
27
|
+
"""The repository-relative path for a reported one, or the reported text if unknown.
|
|
28
|
+
|
|
29
|
+
Matches the longest tail of the path that is exactly one analyzed file.
|
|
30
|
+
"""
|
|
31
|
+
text = reported.replace("\\", "/")
|
|
32
|
+
if os.path.isabs(reported):
|
|
33
|
+
return self._relative(
|
|
34
|
+
reported
|
|
35
|
+
) # symlinks such as /var -> /private/var are resolved
|
|
36
|
+
text = text[2:] if text.startswith("./") else text
|
|
37
|
+
parts = text.split("/")
|
|
38
|
+
for start in range(
|
|
39
|
+
len(parts)
|
|
40
|
+
): # longest tail first; an extra leading folder is dropped
|
|
41
|
+
matches = self._by_suffix.get("/".join(parts[start:]), [])
|
|
42
|
+
if matches:
|
|
43
|
+
return matches[0] if len(matches) == 1 else text
|
|
44
|
+
return text
|
|
45
|
+
|
|
46
|
+
def exists(self, relative: str) -> bool:
|
|
47
|
+
"""True if the resolved path is a real file under the root."""
|
|
48
|
+
return relative in self._known or os.path.isfile(
|
|
49
|
+
os.path.join(self.root, relative)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def _relative(self, path: str) -> str:
|
|
53
|
+
"""A path made relative to the root, comparing real paths (unchanged if outside it)."""
|
|
54
|
+
absolute = os.path.realpath(path)
|
|
55
|
+
if absolute == self.root or absolute.startswith(self.root + os.sep):
|
|
56
|
+
return os.path.relpath(absolute, self.root).replace(os.sep, "/")
|
|
57
|
+
return absolute.replace(os.sep, "/")
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Applies the CI options to a finished analysis: baseline, SARIF file and quality gate.
|
|
2
|
+
|
|
3
|
+
`CiOptions` holds what was asked for; `CiRun.process` does it in one place and returns a
|
|
4
|
+
`CiOutcome` with the report to show, the score, the gate result and notes to print.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import dataclasses
|
|
11
|
+
import os
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from typing import Any, Dict, List, Optional, Sequence
|
|
14
|
+
|
|
15
|
+
from gnost.analysis.core.models import ConsolidatedReport
|
|
16
|
+
from gnost.ci.baseline import Baseline
|
|
17
|
+
from gnost.ci.fingerprint import FindingFingerprint
|
|
18
|
+
from gnost.ci.gate import GateResult, QualityGate
|
|
19
|
+
from gnost.ci.paths import PathResolver
|
|
20
|
+
from gnost.ci.sarif import SarifWriter
|
|
21
|
+
from gnost.ci.scorecard import ScoreCard
|
|
22
|
+
from gnost.config.settings import GnostConfig
|
|
23
|
+
from gnost.utils.layout import OutputLayout
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class CiOptions:
|
|
28
|
+
"""What the user asked for on the command line or in `[analyze]` of .gnost.toml."""
|
|
29
|
+
|
|
30
|
+
fail_under: Optional[float] = None
|
|
31
|
+
fail_on_new: bool = False
|
|
32
|
+
baseline: Optional[str] = None # file to compare against
|
|
33
|
+
write_baseline: Optional[str] = None # file to create from today's findings
|
|
34
|
+
sarif: Optional[str] = None # SARIF file to write
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def from_args(
|
|
38
|
+
cls, args: argparse.Namespace, config: GnostConfig, layout: OutputLayout
|
|
39
|
+
) -> "CiOptions":
|
|
40
|
+
"""Combine flags (first), the config file and the default paths in the output folder."""
|
|
41
|
+
configured = config.get("analyze", "baseline")
|
|
42
|
+
baseline = cls._path(
|
|
43
|
+
getattr(args, "baseline", None),
|
|
44
|
+
layout.baseline_json,
|
|
45
|
+
os.path.join(layout.root, configured) if configured else None,
|
|
46
|
+
)
|
|
47
|
+
return cls(
|
|
48
|
+
fail_under=config.pick(
|
|
49
|
+
"analyze", "fail_under", getattr(args, "fail_under", None), None
|
|
50
|
+
),
|
|
51
|
+
fail_on_new=bool(getattr(args, "fail_on_new", False)),
|
|
52
|
+
baseline=baseline,
|
|
53
|
+
write_baseline=cls._path(
|
|
54
|
+
getattr(args, "write_baseline", None), layout.baseline_json
|
|
55
|
+
),
|
|
56
|
+
sarif=cls._path(getattr(args, "sarif", None), layout.analysis_sarif),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def problem(self) -> Optional[str]:
|
|
60
|
+
"""A mistake in the combination of options, as one sentence, or None."""
|
|
61
|
+
if self.fail_on_new and not self.baseline:
|
|
62
|
+
return "--fail-on-new needs a baseline to compare with (--baseline)"
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
@staticmethod
|
|
66
|
+
def _path(
|
|
67
|
+
given: Optional[str], default: str, configured: Optional[str] = None
|
|
68
|
+
) -> Optional[str]:
|
|
69
|
+
"""None when not asked for, the default for a bare flag (""), else the path given."""
|
|
70
|
+
if given is None:
|
|
71
|
+
return configured
|
|
72
|
+
return given or default
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class CiOutcome:
|
|
77
|
+
"""The result of applying the CI options to one report."""
|
|
78
|
+
|
|
79
|
+
report: ConsolidatedReport # what to print and write (new findings only with a baseline)
|
|
80
|
+
card: ScoreCard # scores of the full report
|
|
81
|
+
gate: GateResult
|
|
82
|
+
new_findings: Optional[int] = None # None when no baseline is used
|
|
83
|
+
known_findings: int = 0
|
|
84
|
+
notes: List[str] = field(default_factory=list)
|
|
85
|
+
|
|
86
|
+
def baseline_summary(self) -> Optional[Dict[str, Any]]:
|
|
87
|
+
"""Counts to add to the JSON payload when a baseline hid findings."""
|
|
88
|
+
if self.new_findings is None:
|
|
89
|
+
return None
|
|
90
|
+
return {
|
|
91
|
+
"new_findings": self.new_findings,
|
|
92
|
+
"known_findings_hidden": self.known_findings,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class CiRun:
|
|
97
|
+
"""Runs the baseline, SARIF and gate steps for one analysis."""
|
|
98
|
+
|
|
99
|
+
def __init__(self, options: CiOptions, root: str, files: Sequence[str]) -> None:
|
|
100
|
+
self.options = options
|
|
101
|
+
self.resolver = PathResolver(files, root)
|
|
102
|
+
self.fingerprint = FindingFingerprint(self.resolver)
|
|
103
|
+
|
|
104
|
+
def process(self, report: ConsolidatedReport) -> CiOutcome:
|
|
105
|
+
"""Apply the options. Raises BaselineError if a baseline file cannot be used."""
|
|
106
|
+
notes: List[str] = []
|
|
107
|
+
opts = self.options
|
|
108
|
+
if opts.write_baseline:
|
|
109
|
+
Baseline.from_findings(report.findings, self.fingerprint).save(
|
|
110
|
+
opts.write_baseline
|
|
111
|
+
)
|
|
112
|
+
notes.append(
|
|
113
|
+
f"Baseline written to {opts.write_baseline} ({len(report.findings)} findings)"
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
visible, new, known = report, None, 0
|
|
117
|
+
if opts.baseline:
|
|
118
|
+
fresh, old = Baseline.load(opts.baseline).split(
|
|
119
|
+
report.findings, self.fingerprint
|
|
120
|
+
)
|
|
121
|
+
visible = dataclasses.replace(report, findings=fresh)
|
|
122
|
+
new, known = len(fresh), len(old)
|
|
123
|
+
notes.append(f"Baseline: {known} known findings hidden, {new} new")
|
|
124
|
+
|
|
125
|
+
if opts.sarif:
|
|
126
|
+
SarifWriter(self.resolver, self.fingerprint).write(
|
|
127
|
+
opts.sarif, visible.findings
|
|
128
|
+
)
|
|
129
|
+
notes.append(f"SARIF written to {opts.sarif}")
|
|
130
|
+
|
|
131
|
+
card = ScoreCard.from_report(report)
|
|
132
|
+
gate = QualityGate(opts.fail_under, opts.fail_on_new).evaluate(
|
|
133
|
+
card.final_score, new
|
|
134
|
+
)
|
|
135
|
+
return CiOutcome(visible, card, gate, new, known, notes)
|