@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
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "0.
|
|
1
|
+
__version__ = "0.6.0rc1"
|
|
@@ -13,7 +13,7 @@ from collections import defaultdict
|
|
|
13
13
|
from typing import List, Dict, Any, Tuple
|
|
14
14
|
|
|
15
15
|
from gnost.utils.paths import tail
|
|
16
|
-
from gnost.analysis.utils.tool_command import tool_command
|
|
16
|
+
from gnost.analysis.utils.tool_command import tool_command, untrusted_repo
|
|
17
17
|
from gnost.analysis.core.interfaces import QualityAnalyzer
|
|
18
18
|
from gnost.analysis.core.file_utils import find_python_files
|
|
19
19
|
from gnost.analysis.core.models import (
|
|
@@ -607,6 +607,9 @@ class ReadabilityAnalyzer(QualityAnalyzer):
|
|
|
607
607
|
proc = await asyncio.create_subprocess_exec(
|
|
608
608
|
*tool_command("ruff"),
|
|
609
609
|
"check",
|
|
610
|
+
*(
|
|
611
|
+
["--isolated"] if untrusted_repo() else []
|
|
612
|
+
), # ignore the repo's ruff config
|
|
610
613
|
os.fspath(file_path),
|
|
611
614
|
"--select",
|
|
612
615
|
",".join(selected),
|
|
@@ -8,9 +8,17 @@ from pathlib import Path
|
|
|
8
8
|
from typing import List, Optional
|
|
9
9
|
|
|
10
10
|
RUN_TOOL_FLAG = "--run-tool"
|
|
11
|
+
UNTRUSTED_ENV = (
|
|
12
|
+
"GNOST_UNTRUSTED_REPO" # set when the code being read is not the user's own
|
|
13
|
+
)
|
|
11
14
|
PYTHON_TOOLS = ("radon", "bandit", "mypy")
|
|
12
15
|
|
|
13
16
|
|
|
17
|
+
def untrusted_repo() -> bool:
|
|
18
|
+
"""True when the analyzed code comes from a remote repository nobody has reviewed."""
|
|
19
|
+
return os.environ.get(UNTRUSTED_ENV) == "1"
|
|
20
|
+
|
|
21
|
+
|
|
14
22
|
def is_frozen() -> bool:
|
|
15
23
|
"""Return True when running inside a PyInstaller bundle."""
|
|
16
24
|
return bool(getattr(sys, "frozen", False))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Change intelligence: what changed between two git refs and what it touches (`gnost changes`)."""
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""Builds a ChangeReport: diff, blast radius, risk, routes and circular dependencies.
|
|
2
|
+
|
|
3
|
+
`ChangeAnalyzer` scans the current folder (the head side), optionally exports the base commit to
|
|
4
|
+
a temporary folder and scans that too, then compares the two. Nothing in the working tree is
|
|
5
|
+
modified. History and risk reuse the same classes as `gnost onboard`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from typing import Dict, List, Optional, Sequence, Set
|
|
12
|
+
|
|
13
|
+
from gnost.changes.findings import ChangedFindings
|
|
14
|
+
from gnost.changes.impact import DEFAULT_DEPTH, AffectedFile, BlastRadius
|
|
15
|
+
from gnost.changes.refs import ChangedFile, DiffReader, RefRange
|
|
16
|
+
from gnost.changes.report import (
|
|
17
|
+
ChangeReport,
|
|
18
|
+
CycleChange,
|
|
19
|
+
FileRisk,
|
|
20
|
+
RouteChange,
|
|
21
|
+
)
|
|
22
|
+
from gnost.changes.snapshot import ProjectSnapshot, RefCheckout
|
|
23
|
+
from gnost.core.caution import CautionSettings, rank_files
|
|
24
|
+
from gnost.core.risk import REASON_WORDS, RiskCalculator, RiskSettings
|
|
25
|
+
from gnost.vcs.git import GitRepository
|
|
26
|
+
from gnost.vcs.history import HistoryReader, HistorySettings
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ChangeAnalyzer:
|
|
30
|
+
"""Compares HEAD with the point where it split from BASE for one project folder."""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
repo: GitRepository,
|
|
35
|
+
scan_root: str,
|
|
36
|
+
exclude: Optional[Sequence[str]] = None,
|
|
37
|
+
depth: int = DEFAULT_DEPTH,
|
|
38
|
+
compare_base: bool = True,
|
|
39
|
+
findings: bool = False,
|
|
40
|
+
caution: Optional[CautionSettings] = None,
|
|
41
|
+
history: Optional[HistorySettings] = None,
|
|
42
|
+
risk: Optional[RiskSettings] = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
self.repo = repo
|
|
45
|
+
self.scan_root = scan_root
|
|
46
|
+
self.exclude = list(exclude or [])
|
|
47
|
+
self.depth = depth
|
|
48
|
+
self.compare_base = compare_base
|
|
49
|
+
self.findings = findings
|
|
50
|
+
self.caution = caution or CautionSettings()
|
|
51
|
+
self.history = history or HistorySettings()
|
|
52
|
+
self.risk = risk or RiskSettings()
|
|
53
|
+
|
|
54
|
+
def analyze(self, refs: RefRange) -> ChangeReport:
|
|
55
|
+
"""The full report for a range; the head side is the current working folder."""
|
|
56
|
+
files = DiffReader(self.repo).read(refs, self.scan_root)
|
|
57
|
+
head = ProjectSnapshot.scan_folder(self.scan_root, self.exclude)
|
|
58
|
+
base = self._scan_base(refs) if self.compare_base and files else None
|
|
59
|
+
|
|
60
|
+
changed_now = [f.path for f in files if f.status != "deleted"]
|
|
61
|
+
report = ChangeReport(
|
|
62
|
+
base=refs.base,
|
|
63
|
+
head=refs.head,
|
|
64
|
+
base_commit=refs.base_commit,
|
|
65
|
+
head_commit=refs.head_commit,
|
|
66
|
+
files=files,
|
|
67
|
+
affected=self._affected(head, base, files),
|
|
68
|
+
depth=self.depth,
|
|
69
|
+
cycles_compared=base is not None,
|
|
70
|
+
)
|
|
71
|
+
report.risks = self._risks(head, changed_now)
|
|
72
|
+
report.routes = self._routes(head, base, files, report.affected)
|
|
73
|
+
if base is not None:
|
|
74
|
+
self._compare_cycles(report, head, base)
|
|
75
|
+
elif self.compare_base and files:
|
|
76
|
+
report.notes.append(
|
|
77
|
+
"Circular dependencies were not compared (base scan failed)."
|
|
78
|
+
)
|
|
79
|
+
if self.findings:
|
|
80
|
+
absolute = [os.path.join(self.scan_root, p) for p in changed_now]
|
|
81
|
+
report.findings = ChangedFindings(self.scan_root).run(absolute)
|
|
82
|
+
if not self._head_is_checked_out(refs):
|
|
83
|
+
report.notes.append(
|
|
84
|
+
f"`{refs.head}` is not the commit checked out here; "
|
|
85
|
+
"the working folder was scanned for the blast radius, risk and routes."
|
|
86
|
+
)
|
|
87
|
+
return report
|
|
88
|
+
|
|
89
|
+
# ------------------------------------------------------------ sections
|
|
90
|
+
|
|
91
|
+
def _scan_base(self, refs: RefRange) -> Optional[ProjectSnapshot]:
|
|
92
|
+
"""Scan the base commit in a temporary folder; None if it cannot be exported."""
|
|
93
|
+
subpath = os.path.relpath(self.scan_root, self.repo.root).replace(os.sep, "/")
|
|
94
|
+
try:
|
|
95
|
+
with RefCheckout(self.repo, refs.base_commit, subpath) as folder:
|
|
96
|
+
return ProjectSnapshot.scan_folder(folder, self.exclude)
|
|
97
|
+
except Exception:
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
def _affected(
|
|
101
|
+
self,
|
|
102
|
+
head: ProjectSnapshot,
|
|
103
|
+
base: Optional[ProjectSnapshot],
|
|
104
|
+
files: Sequence[ChangedFile],
|
|
105
|
+
) -> List[AffectedFile]:
|
|
106
|
+
"""Dependents of changed files; for deleted files, dependents in the base version."""
|
|
107
|
+
found = {
|
|
108
|
+
a.path: a
|
|
109
|
+
for a in BlastRadius(head.graph, head.relative).of(
|
|
110
|
+
[f.path for f in files if f.status != "deleted"], self.depth
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
if base is not None:
|
|
114
|
+
gone = [
|
|
115
|
+
f.old_path or f.path
|
|
116
|
+
for f in files
|
|
117
|
+
if f.status in ("deleted", "renamed")
|
|
118
|
+
]
|
|
119
|
+
now = head.files()
|
|
120
|
+
for a in BlastRadius(base.graph, base.relative).of(gone, self.depth):
|
|
121
|
+
if a.path in now and a.path not in found:
|
|
122
|
+
found[a.path] = a
|
|
123
|
+
changed = {f.path for f in files}
|
|
124
|
+
items = [a for a in found.values() if a.path not in changed]
|
|
125
|
+
return sorted(items, key=lambda a: (a.depth, a.path))
|
|
126
|
+
|
|
127
|
+
def _risks(self, head: ProjectSnapshot, changed: Sequence[str]) -> List[FileRisk]:
|
|
128
|
+
"""Caution score (and risk, where history exists) of each changed file, riskiest first."""
|
|
129
|
+
wanted: Set[str] = set(changed)
|
|
130
|
+
structural = [
|
|
131
|
+
s
|
|
132
|
+
for s in rank_files(head.scan, head.graph, head.flow.cycles, self.caution)
|
|
133
|
+
if head.relative(s.path) in wanted
|
|
134
|
+
]
|
|
135
|
+
insights = HistoryReader(self.history).analyze(self.scan_root, head.files())
|
|
136
|
+
everything = RiskSettings(0.0, 10**6, self.risk.churn_weight)
|
|
137
|
+
by_path = {
|
|
138
|
+
r.path: r
|
|
139
|
+
for r in RiskCalculator(everything).rank(
|
|
140
|
+
structural, insights.hotspots, self.scan_root
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
out: List[FileRisk] = []
|
|
144
|
+
for s in structural:
|
|
145
|
+
rel = head.relative(s.path)
|
|
146
|
+
scored = by_path.get(rel)
|
|
147
|
+
why = ", ".join(REASON_WORDS.get(f.name, f.name) for f in s.factors[:2])
|
|
148
|
+
out.append(
|
|
149
|
+
FileRisk(
|
|
150
|
+
path=rel,
|
|
151
|
+
structural=s.score,
|
|
152
|
+
churn=scored.churn if scored else None,
|
|
153
|
+
risk=scored.risk if scored else None,
|
|
154
|
+
why=why,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
out.sort(key=lambda r: (-r.level, r.path))
|
|
158
|
+
return [r for r in out if r.level >= self.risk.threshold]
|
|
159
|
+
|
|
160
|
+
def _routes(
|
|
161
|
+
self,
|
|
162
|
+
head: ProjectSnapshot,
|
|
163
|
+
base: Optional[ProjectSnapshot],
|
|
164
|
+
files: Sequence[ChangedFile],
|
|
165
|
+
affected: Sequence[AffectedFile],
|
|
166
|
+
) -> List[RouteChange]:
|
|
167
|
+
"""Routes added or removed (when the base is known) and routes in touched files."""
|
|
168
|
+
changed = {f.path for f in files}
|
|
169
|
+
near = {a.path for a in affected}
|
|
170
|
+
key = lambda r: (r.method, r.path) # noqa: E731
|
|
171
|
+
old: Dict = {key(r): r for r in base.routes()} if base else {}
|
|
172
|
+
new: Dict = {key(r): r for r in head.routes()}
|
|
173
|
+
out: List[RouteChange] = []
|
|
174
|
+
for k, r in sorted(new.items()):
|
|
175
|
+
rel = head.relative(r.file)
|
|
176
|
+
if base is not None and k not in old:
|
|
177
|
+
state = "added"
|
|
178
|
+
elif rel in changed:
|
|
179
|
+
state = "changed"
|
|
180
|
+
elif rel in near:
|
|
181
|
+
state = "affected"
|
|
182
|
+
else:
|
|
183
|
+
continue
|
|
184
|
+
out.append(RouteChange(r.method, r.path, r.handler, rel, state))
|
|
185
|
+
for k, r in sorted(old.items()):
|
|
186
|
+
if k not in new and base is not None:
|
|
187
|
+
out.append(
|
|
188
|
+
RouteChange(
|
|
189
|
+
r.method, r.path, r.handler, base.relative(r.file), "removed"
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
return out
|
|
193
|
+
|
|
194
|
+
@staticmethod
|
|
195
|
+
def _compare_cycles(
|
|
196
|
+
report: ChangeReport, head: ProjectSnapshot, base: ProjectSnapshot
|
|
197
|
+
) -> None:
|
|
198
|
+
"""Fill new and fixed cycles by comparing the file sets on each side."""
|
|
199
|
+
before = base.cycle_keys()
|
|
200
|
+
after = head.cycle_keys()
|
|
201
|
+
for c in head.flow.cycles:
|
|
202
|
+
if head.cycle_key(c) not in before:
|
|
203
|
+
report.new_cycles.append(CycleChange(sorted(head.cycle_key(c)), c.kind))
|
|
204
|
+
for c in base.flow.cycles:
|
|
205
|
+
if base.cycle_key(c) not in after:
|
|
206
|
+
report.fixed_cycles.append(
|
|
207
|
+
CycleChange(sorted(base.cycle_key(c)), c.kind)
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
def _head_is_checked_out(self, refs: RefRange) -> bool:
|
|
211
|
+
"""Whether the head ref is the commit the working folder is on."""
|
|
212
|
+
try:
|
|
213
|
+
return self.repo.resolve("HEAD") == refs.head_commit
|
|
214
|
+
except Exception:
|
|
215
|
+
return True
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Analyzer findings that sit in changed files only.
|
|
2
|
+
|
|
3
|
+
`ChangedFindings` runs the normal `gnost analyze` analyzers on just the changed Python files
|
|
4
|
+
and maps each finding back to a path relative to the project. It is slower than the rest of
|
|
5
|
+
`gnost changes`, so it only runs when asked for (`--findings`).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
from typing import List, Sequence
|
|
12
|
+
|
|
13
|
+
from gnost.analysis.core.models import AnalysisConfiguration, SeverityLevel
|
|
14
|
+
from gnost.changes.report import FindingInChange
|
|
15
|
+
from gnost.ci.paths import PathResolver
|
|
16
|
+
from gnost.cli.commands.analysis import Analysis
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ChangedFindings:
|
|
20
|
+
"""Runs the analyzers on a list of files and returns their findings."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, root: str, timeout: int = 900, max_findings: int = 1000) -> None:
|
|
23
|
+
self.root = root
|
|
24
|
+
self.timeout = timeout
|
|
25
|
+
self.max_findings = max_findings
|
|
26
|
+
|
|
27
|
+
def run(self, files: Sequence[str]) -> List[FindingInChange]:
|
|
28
|
+
"""Findings for these absolute file paths (Python only), worst first."""
|
|
29
|
+
python = [f for f in files if f.endswith(".py")]
|
|
30
|
+
if not python:
|
|
31
|
+
return []
|
|
32
|
+
analysis = Analysis()
|
|
33
|
+
analysis.initialize_analyzers()
|
|
34
|
+
cfg = AnalysisConfiguration(
|
|
35
|
+
target_path=self.root,
|
|
36
|
+
enabled_analyzers=set(),
|
|
37
|
+
severity_threshold=SeverityLevel.INFO,
|
|
38
|
+
timeout_seconds=self.timeout,
|
|
39
|
+
max_findings_per_analyzer=self.max_findings,
|
|
40
|
+
files=python,
|
|
41
|
+
)
|
|
42
|
+
report = asyncio.run(analysis.run_async(cfg, show_progress=False))
|
|
43
|
+
resolver = PathResolver(python, self.root)
|
|
44
|
+
order = [s.value for s in SeverityLevel]
|
|
45
|
+
found = [
|
|
46
|
+
FindingInChange(
|
|
47
|
+
file=resolver.resolve(f.location.file_path),
|
|
48
|
+
line=f.location.line_number or 0,
|
|
49
|
+
severity=f.severity.value,
|
|
50
|
+
title=f.title,
|
|
51
|
+
analyzer=f.source_analyzer or "",
|
|
52
|
+
)
|
|
53
|
+
for f in report.findings
|
|
54
|
+
]
|
|
55
|
+
return sorted(found, key=lambda x: (order.index(x.severity), x.file, x.line))
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Blast radius: the files that import a changed file, directly or through other files.
|
|
2
|
+
|
|
3
|
+
`BlastRadius` walks the import graph backwards from the changed files, breadth first, up to a
|
|
4
|
+
depth. Every affected file records its distance and the changed file it was reached from.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections import deque
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Callable, Deque, Dict, Iterable, List, Set, Tuple
|
|
12
|
+
|
|
13
|
+
from gnost.core.graph import DependencyGraph
|
|
14
|
+
|
|
15
|
+
DEFAULT_DEPTH = 3
|
|
16
|
+
Relative = Callable[[str], str] # turns a graph node into a path relative to the folder
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class AffectedFile:
|
|
21
|
+
"""A file that depends on the change."""
|
|
22
|
+
|
|
23
|
+
path: str # relative to the scanned folder
|
|
24
|
+
depth: int # 1 = imports a changed file directly
|
|
25
|
+
via: str # the changed file it was reached from
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class BlastRadius:
|
|
29
|
+
"""Finds dependents of a set of files in one dependency graph."""
|
|
30
|
+
|
|
31
|
+
def __init__(self, graph: DependencyGraph, relative: Relative) -> None:
|
|
32
|
+
self.graph = graph
|
|
33
|
+
self.relative = relative
|
|
34
|
+
|
|
35
|
+
def of(
|
|
36
|
+
self, changed: Iterable[str], depth: int = DEFAULT_DEPTH
|
|
37
|
+
) -> List[AffectedFile]:
|
|
38
|
+
"""Files affected by `changed` (relative paths), nearest first, then by path."""
|
|
39
|
+
by_rel = {self.relative(n): n for n in self.graph.all_nodes()}
|
|
40
|
+
seeds = [by_rel[c] for c in sorted(set(changed)) if c in by_rel]
|
|
41
|
+
seen: Set[str] = set(seeds)
|
|
42
|
+
found: Dict[str, Tuple[int, str]] = {}
|
|
43
|
+
queue: Deque[Tuple[str, int, str]] = deque(
|
|
44
|
+
(s, 0, self.relative(s)) for s in seeds
|
|
45
|
+
)
|
|
46
|
+
while queue:
|
|
47
|
+
node, distance, origin = queue.popleft()
|
|
48
|
+
if distance >= depth:
|
|
49
|
+
continue
|
|
50
|
+
for parent in sorted(self.graph.dependents_of(node)):
|
|
51
|
+
if parent in seen:
|
|
52
|
+
continue
|
|
53
|
+
seen.add(parent)
|
|
54
|
+
found[parent] = (distance + 1, origin)
|
|
55
|
+
queue.append((parent, distance + 1, origin))
|
|
56
|
+
items = [
|
|
57
|
+
AffectedFile(self.relative(n), d, via) for n, (d, via) in found.items()
|
|
58
|
+
]
|
|
59
|
+
return sorted(items, key=lambda a: (a.depth, a.path))
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""The two refs a change is measured between, and the files that differ.
|
|
2
|
+
|
|
3
|
+
`RefRange` parses `BASE`, `BASE..HEAD` or `BASE...HEAD`. Like a pull request, the comparison
|
|
4
|
+
starts at the commit where HEAD split from BASE. `DiffReader` lists the changed files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import List, Optional
|
|
11
|
+
|
|
12
|
+
from gnost.vcs.git import GitError, GitRepository
|
|
13
|
+
|
|
14
|
+
STATUS_NAMES = {
|
|
15
|
+
"A": "added",
|
|
16
|
+
"C": "added",
|
|
17
|
+
"M": "modified",
|
|
18
|
+
"T": "modified",
|
|
19
|
+
"D": "deleted",
|
|
20
|
+
"R": "renamed",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RefError(GitError):
|
|
25
|
+
"""A ref could not be understood; the message says which one."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class RefRange:
|
|
30
|
+
"""BASE and HEAD as typed, and the commits they resolve to."""
|
|
31
|
+
|
|
32
|
+
base: str
|
|
33
|
+
head: str
|
|
34
|
+
base_commit: str # where the two sides split
|
|
35
|
+
head_commit: str
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def parse(cls, text: str, repo: GitRepository) -> "RefRange":
|
|
39
|
+
"""Read `BASE`, `BASE..HEAD` or `BASE...HEAD`; HEAD defaults to the current commit."""
|
|
40
|
+
base, _, head = text.replace("...", "..").partition("..")
|
|
41
|
+
base, head = base.strip(), (head.strip() or "HEAD")
|
|
42
|
+
if not base:
|
|
43
|
+
raise RefError("give a base ref, for example `gnost changes origin/main`")
|
|
44
|
+
base_commit = cls._commit(repo, base)
|
|
45
|
+
head_commit = cls._commit(repo, head)
|
|
46
|
+
try:
|
|
47
|
+
split = repo.merge_base(base_commit, head_commit)
|
|
48
|
+
except GitError as exc:
|
|
49
|
+
raise RefError(f"`{base}` and `{head}` have no common history") from exc
|
|
50
|
+
return cls(base, head, split, head_commit)
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def _commit(repo: GitRepository, ref: str) -> str:
|
|
54
|
+
try:
|
|
55
|
+
commit = repo.resolve(ref)
|
|
56
|
+
except GitError as exc:
|
|
57
|
+
raise RefError(
|
|
58
|
+
f"unknown ref `{ref}`. If this is a CI checkout, fetch the base branch "
|
|
59
|
+
"first (in GitHub Actions: `fetch-depth: 0`)."
|
|
60
|
+
) from exc
|
|
61
|
+
if not commit:
|
|
62
|
+
raise RefError(f"unknown ref `{ref}`")
|
|
63
|
+
return commit
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def label(self) -> str:
|
|
67
|
+
"""The range as shown to people, for example `main..feature`."""
|
|
68
|
+
return f"{self.base}..{self.head}"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class ChangedFile:
|
|
73
|
+
"""One file that differs between the two sides."""
|
|
74
|
+
|
|
75
|
+
path: str # relative to the scanned folder; the new name for a rename
|
|
76
|
+
status: str # added, modified, deleted or renamed
|
|
77
|
+
old_path: Optional[str] = None # the previous name of a renamed file
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class DiffReader:
|
|
81
|
+
"""Lists the files that changed in a range, limited to the scanned folder."""
|
|
82
|
+
|
|
83
|
+
def __init__(self, repo: GitRepository) -> None:
|
|
84
|
+
self.repo = repo
|
|
85
|
+
|
|
86
|
+
def read(self, refs: RefRange, scan_root: str) -> List[ChangedFile]:
|
|
87
|
+
"""Changed files under scan_root, sorted by path, with paths relative to scan_root."""
|
|
88
|
+
args = ["diff", "--name-status", "-M", "--relative"]
|
|
89
|
+
# --relative limits the list to scan_root and makes the paths relative to it
|
|
90
|
+
text = self.repo.run([*args, refs.base_commit, refs.head_commit], cwd=scan_root)
|
|
91
|
+
files = [f for f in (self._parse(line) for line in text.splitlines()) if f]
|
|
92
|
+
return sorted(files, key=lambda f: f.path)
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def _parse(line: str) -> Optional[ChangedFile]:
|
|
96
|
+
"""One `git diff --name-status` line: `M<TAB>path` or `R90<TAB>old<TAB>new`."""
|
|
97
|
+
parts = line.split("\t")
|
|
98
|
+
if len(parts) < 2:
|
|
99
|
+
return None
|
|
100
|
+
status = STATUS_NAMES.get(parts[0][:1])
|
|
101
|
+
if status is None:
|
|
102
|
+
return None
|
|
103
|
+
if status == "renamed" and len(parts) >= 3:
|
|
104
|
+
return ChangedFile(parts[2], status, parts[1])
|
|
105
|
+
return ChangedFile(parts[-1], status)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Turns a ChangeReport into Markdown (also the pull request comment) and terminal text.
|
|
2
|
+
|
|
3
|
+
`MarkdownRenderer` starts with a hidden marker so the GitHub Action can find and update its own
|
|
4
|
+
comment, and ends with a footer linking to GNOST. `TextRenderer` is the short terminal summary.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Iterable, List
|
|
10
|
+
|
|
11
|
+
from gnost.changes.report import ChangeReport
|
|
12
|
+
from gnost.utils.footer import Footer
|
|
13
|
+
|
|
14
|
+
COMMENT_MARKER = "<!-- gnost-changes -->"
|
|
15
|
+
LIST_LIMIT = 15
|
|
16
|
+
ICONS = {"added": "A", "modified": "M", "deleted": "D", "renamed": "R"}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MarkdownRenderer:
|
|
20
|
+
"""Writes the report as Markdown that GitHub shows well."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, footer: Footer = Footer()) -> None:
|
|
23
|
+
self.footer = footer
|
|
24
|
+
|
|
25
|
+
def render(self, report: ChangeReport) -> str:
|
|
26
|
+
"""The whole document, ending with a newline."""
|
|
27
|
+
parts: List[str] = [
|
|
28
|
+
COMMENT_MARKER,
|
|
29
|
+
f"## GNOST: what changed in `{report.label}`",
|
|
30
|
+
self._summary(report),
|
|
31
|
+
self._risk(report),
|
|
32
|
+
self._cycles(report),
|
|
33
|
+
self._routes(report),
|
|
34
|
+
self._blast(report),
|
|
35
|
+
self._findings(report),
|
|
36
|
+
self._files(report),
|
|
37
|
+
"\n".join(f"> {n}" for n in report.notes),
|
|
38
|
+
self.footer.line(),
|
|
39
|
+
]
|
|
40
|
+
return "\n\n".join(p for p in parts if p).strip() + "\n"
|
|
41
|
+
|
|
42
|
+
def _summary(self, r: ChangeReport) -> str:
|
|
43
|
+
c = r.counts
|
|
44
|
+
bits = [f"{n} {name}" for name, n in c.items() if n]
|
|
45
|
+
head = f"**{len(r.files)} files changed** ({', '.join(bits) or 'none'})"
|
|
46
|
+
if r.affected:
|
|
47
|
+
return f"{head}; **{len(r.affected)}** other files depend on them."
|
|
48
|
+
return head + "."
|
|
49
|
+
|
|
50
|
+
def _risk(self, r: ChangeReport) -> str:
|
|
51
|
+
if not r.risks:
|
|
52
|
+
return "**Risk:** no changed file is fragile enough to flag."
|
|
53
|
+
rows = [
|
|
54
|
+
"**Risky files in this change**",
|
|
55
|
+
"",
|
|
56
|
+
"| File | Risk | Why |",
|
|
57
|
+
"|---|---|---|",
|
|
58
|
+
]
|
|
59
|
+
for x in r.risks[:LIST_LIMIT]:
|
|
60
|
+
rows.append(f"| `{x.path}` | {x.level:.0f} | {x.why or '-'} |")
|
|
61
|
+
return "\n".join(rows)
|
|
62
|
+
|
|
63
|
+
def _cycles(self, r: ChangeReport) -> str:
|
|
64
|
+
if not r.cycles_compared:
|
|
65
|
+
return ""
|
|
66
|
+
lines = [
|
|
67
|
+
f"- New circular dependency ({c.kind}): {', '.join(f'`{f}`' for f in c.files)}"
|
|
68
|
+
for c in r.new_cycles
|
|
69
|
+
] + [
|
|
70
|
+
f"- Fixed circular dependency: {', '.join(f'`{f}`' for f in c.files)}"
|
|
71
|
+
for c in r.fixed_cycles
|
|
72
|
+
]
|
|
73
|
+
return "**Circular dependencies**\n\n" + "\n".join(lines) if lines else ""
|
|
74
|
+
|
|
75
|
+
def _routes(self, r: ChangeReport) -> str:
|
|
76
|
+
if not r.routes:
|
|
77
|
+
return ""
|
|
78
|
+
lines = [
|
|
79
|
+
f"- `{x.method} {x.path}` ({x.state}) in `{x.file}`"
|
|
80
|
+
for x in r.routes[:LIST_LIMIT]
|
|
81
|
+
]
|
|
82
|
+
return "**Routes touched**\n\n" + "\n".join(lines)
|
|
83
|
+
|
|
84
|
+
def _blast(self, r: ChangeReport) -> str:
|
|
85
|
+
if not r.affected:
|
|
86
|
+
return ""
|
|
87
|
+
lines = [
|
|
88
|
+
f"- `{a.path}` (distance {a.depth}, via `{a.via}`)"
|
|
89
|
+
for a in r.affected[:LIST_LIMIT]
|
|
90
|
+
]
|
|
91
|
+
more = len(r.affected) - LIST_LIMIT
|
|
92
|
+
if more > 0:
|
|
93
|
+
lines.append(f"- … and {more} more")
|
|
94
|
+
return (
|
|
95
|
+
f"<details><summary>Blast radius: {len(r.affected)} files</summary>\n\n"
|
|
96
|
+
+ "\n".join(lines)
|
|
97
|
+
+ "\n\n</details>"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def _findings(self, r: ChangeReport) -> str:
|
|
101
|
+
if r.findings is None:
|
|
102
|
+
return ""
|
|
103
|
+
if not r.findings:
|
|
104
|
+
return "**Findings in changed files:** none."
|
|
105
|
+
lines = [
|
|
106
|
+
f"- {f.severity}: {f.title} (`{f.file}:{f.line}`)"
|
|
107
|
+
for f in r.findings[:LIST_LIMIT]
|
|
108
|
+
]
|
|
109
|
+
return f"**Findings in changed files ({len(r.findings)})**\n\n" + "\n".join(
|
|
110
|
+
lines
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def _files(self, r: ChangeReport) -> str:
|
|
114
|
+
lines = [
|
|
115
|
+
f"- {ICONS[f.status]} `{f.old_path} → {f.path}`"
|
|
116
|
+
if f.old_path
|
|
117
|
+
else f"- {ICONS[f.status]} `{f.path}`"
|
|
118
|
+
for f in r.files[: LIST_LIMIT * 4]
|
|
119
|
+
]
|
|
120
|
+
return (
|
|
121
|
+
f"<details><summary>Changed files ({len(r.files)})</summary>\n\n"
|
|
122
|
+
+ "\n".join(lines)
|
|
123
|
+
+ "\n\n</details>"
|
|
124
|
+
if lines
|
|
125
|
+
else ""
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class TextRenderer:
|
|
130
|
+
"""The short summary printed in the terminal."""
|
|
131
|
+
|
|
132
|
+
def render(self, report: ChangeReport) -> str:
|
|
133
|
+
"""Plain text lines, no colors, so it also works in CI logs."""
|
|
134
|
+
c = report.counts
|
|
135
|
+
out = [
|
|
136
|
+
f"Changes {report.label}: {len(report.files)} files "
|
|
137
|
+
f"({c['added']} added, {c['modified']} modified, {c['deleted']} deleted, {c['renamed']} renamed)",
|
|
138
|
+
f"Blast radius: {len(report.affected)} files depend on them (depth {report.depth})",
|
|
139
|
+
]
|
|
140
|
+
out += self._section(
|
|
141
|
+
"Risky files",
|
|
142
|
+
(f"{x.path} risk {x.level:.0f} {x.why}" for x in report.risks),
|
|
143
|
+
)
|
|
144
|
+
out += self._section(
|
|
145
|
+
"New circular dependencies",
|
|
146
|
+
(" -> ".join(c.files) for c in report.new_cycles),
|
|
147
|
+
)
|
|
148
|
+
out += self._section(
|
|
149
|
+
"Fixed circular dependencies",
|
|
150
|
+
(" -> ".join(c.files) for c in report.fixed_cycles),
|
|
151
|
+
)
|
|
152
|
+
out += self._section(
|
|
153
|
+
"Routes touched",
|
|
154
|
+
(f"{x.method} {x.path} ({x.state})" for x in report.routes),
|
|
155
|
+
)
|
|
156
|
+
if report.findings is not None:
|
|
157
|
+
out += self._section(
|
|
158
|
+
f"Findings in changed files ({len(report.findings)})",
|
|
159
|
+
(
|
|
160
|
+
f"{f.severity}: {f.title} ({f.file}:{f.line})"
|
|
161
|
+
for f in report.findings[:10]
|
|
162
|
+
),
|
|
163
|
+
)
|
|
164
|
+
out += [f"Note: {n}" for n in report.notes]
|
|
165
|
+
return "\n".join(out)
|
|
166
|
+
|
|
167
|
+
@staticmethod
|
|
168
|
+
def _section(title: str, lines: Iterable[str]) -> List[str]:
|
|
169
|
+
rows = [f" {x}" for x in lines]
|
|
170
|
+
if len(rows) > 10:
|
|
171
|
+
rows = rows[:10] + [f" ... and {len(rows) - 10} more (see changes.md)"]
|
|
172
|
+
return [f"{title}:", *rows] if rows else []
|