adre-cli 0.1.0__py3-none-any.whl
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.
- adre/__init__.py +5 -0
- adre/analyzers/__init__.py +1 -0
- adre/analyzers/architecture.py +253 -0
- adre/analyzers/findings.py +457 -0
- adre/analyzers/inference.py +163 -0
- adre/cli.py +923 -0
- adre/core/__init__.py +1 -0
- adre/core/accepted.py +225 -0
- adre/core/adaptive.py +221 -0
- adre/core/baseline.py +1165 -0
- adre/core/config.py +60 -0
- adre/core/decision_engine.py +361 -0
- adre/core/declared.py +313 -0
- adre/core/evolution_intelligence.py +390 -0
- adre/core/explainer.py +413 -0
- adre/core/git_intelligence.py +392 -0
- adre/core/github_check.py +212 -0
- adre/core/incremental.py +288 -0
- adre/core/layers.py +306 -0
- adre/core/pr_intelligence.py +261 -0
- adre/core/pr_review.py +454 -0
- adre/core/prioritization.py +196 -0
- adre/core/project.py +41 -0
- adre/core/refactor_intelligence.py +201 -0
- adre/core/repositories.py +312 -0
- adre/core/review.py +96 -0
- adre/core/rules_shared.py +75 -0
- adre/core/sarif.py +141 -0
- adre/core/scope.py +141 -0
- adre/core/snapshots.py +242 -0
- adre/core/stability_intelligence.py +361 -0
- adre/graph/__init__.py +0 -0
- adre/graph/dependencies.py +379 -0
- adre/parsers/__init__.py +87 -0
- adre/parsers/go.py +140 -0
- adre/parsers/javascript.py +364 -0
- adre/parsers/jvm.py +127 -0
- adre/parsers/python.py +153 -0
- adre/reports/__init__.py +1 -0
- adre/reports/architecture.py +349 -0
- adre/rules.py +12 -0
- adre/web.py +3731 -0
- adre_cli-0.1.0.dist-info/METADATA +343 -0
- adre_cli-0.1.0.dist-info/RECORD +48 -0
- adre_cli-0.1.0.dist-info/WHEEL +5 -0
- adre_cli-0.1.0.dist-info/entry_points.txt +2 -0
- adre_cli-0.1.0.dist-info/licenses/LICENSE +202 -0
- adre_cli-0.1.0.dist-info/top_level.txt +1 -0
adre/core/pr_review.py
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import shutil
|
|
4
|
+
import subprocess
|
|
5
|
+
import tempfile
|
|
6
|
+
|
|
7
|
+
from adre.analyzers.architecture import analyze_project_architecture
|
|
8
|
+
from adre.core.accepted import (
|
|
9
|
+
accepted_fail_on_stale,
|
|
10
|
+
accepted_rel_path,
|
|
11
|
+
apply_accepted,
|
|
12
|
+
load_accepted,
|
|
13
|
+
stale_accepted_rules,
|
|
14
|
+
)
|
|
15
|
+
from adre.core.baseline import (
|
|
16
|
+
compare_to_baseline,
|
|
17
|
+
default_baseline_path,
|
|
18
|
+
load_architecture_baseline,
|
|
19
|
+
validate_architecture_baseline,
|
|
20
|
+
)
|
|
21
|
+
from adre.core.declared import declared_rel_path, load_declared_text
|
|
22
|
+
from adre.core.config import BASELINE_DIRNAME, BASELINE_FILENAME
|
|
23
|
+
from adre.core.git_intelligence import get_repo_root, run_git, run_git_command
|
|
24
|
+
from adre.parsers import SUPPORTED_EXTENSIONS
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
PASS = "PASS"
|
|
28
|
+
DRIFT = "DRIFT"
|
|
29
|
+
ERROR = "ERROR"
|
|
30
|
+
BASELINE_REQUIRED = "BASELINE_REQUIRED"
|
|
31
|
+
BASELINE_INVALID = "BASELINE_INVALID"
|
|
32
|
+
|
|
33
|
+
EXIT_CODES = {
|
|
34
|
+
PASS: 0,
|
|
35
|
+
DRIFT: 2,
|
|
36
|
+
BASELINE_REQUIRED: 3,
|
|
37
|
+
BASELINE_INVALID: 1,
|
|
38
|
+
ERROR: 1,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _safe_ref(ref):
|
|
43
|
+
value = (ref or "").strip()
|
|
44
|
+
if not value or value.startswith("-") or "\x00" in value:
|
|
45
|
+
raise ValueError("Unsafe git ref.")
|
|
46
|
+
if any(char in value for char in "|;&$\n\r"):
|
|
47
|
+
raise ValueError("Unsafe git ref.")
|
|
48
|
+
return value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _safe_git_path(rel_path):
|
|
52
|
+
value = (rel_path or "").replace("\\", "/").lstrip("/")
|
|
53
|
+
if not value or value.startswith("-") or ".." in value.split("/"):
|
|
54
|
+
raise ValueError("Unsafe git path.")
|
|
55
|
+
return value
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def verify_ref(repo_root, ref):
|
|
59
|
+
safe = _safe_ref(ref)
|
|
60
|
+
run_git(repo_root, ["rev-parse", "--verify", "{0}^{{commit}}".format(safe)])
|
|
61
|
+
return run_git(repo_root, ["rev-parse", safe])
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def changed_files_between(repo_root, base_ref, head_ref):
|
|
65
|
+
output = run_git(
|
|
66
|
+
repo_root,
|
|
67
|
+
["diff", "--name-only", "{0}...{1}".format(_safe_ref(base_ref), _safe_ref(head_ref))],
|
|
68
|
+
)
|
|
69
|
+
return [line.strip().replace("\\", "/") for line in output.splitlines() if line.strip()]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _project_prefix(repo_root, project_path):
|
|
73
|
+
rel = os.path.relpath(project_path, repo_root).replace("\\", "/")
|
|
74
|
+
return "" if rel in {"", "."} else rel.rstrip("/") + "/"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def renamed_files_between(repo_root, base_ref, head_ref, project_prefix=""):
|
|
78
|
+
"""Return {old_path: new_path} for files git detects as renamed.
|
|
79
|
+
|
|
80
|
+
Paths are relative to the analyzed project directory. Renames that move a
|
|
81
|
+
file into or out of the project are ignored.
|
|
82
|
+
"""
|
|
83
|
+
output = run_git_command(
|
|
84
|
+
repo_root,
|
|
85
|
+
[
|
|
86
|
+
"diff", "-M", "--name-status", "-z", "--no-color",
|
|
87
|
+
"{0}...{1}".format(_safe_ref(base_ref), _safe_ref(head_ref)),
|
|
88
|
+
],
|
|
89
|
+
).stdout
|
|
90
|
+
tokens = output.split("\0")
|
|
91
|
+
renames = {}
|
|
92
|
+
index = 0
|
|
93
|
+
while index < len(tokens):
|
|
94
|
+
status = tokens[index]
|
|
95
|
+
if not status:
|
|
96
|
+
index += 1
|
|
97
|
+
continue
|
|
98
|
+
if status[0] in {"R", "C"}:
|
|
99
|
+
if index + 2 >= len(tokens):
|
|
100
|
+
break
|
|
101
|
+
old, new = tokens[index + 1], tokens[index + 2]
|
|
102
|
+
index += 3
|
|
103
|
+
if status[0] != "R":
|
|
104
|
+
continue
|
|
105
|
+
if project_prefix:
|
|
106
|
+
if not (old.startswith(project_prefix) and new.startswith(project_prefix)):
|
|
107
|
+
continue
|
|
108
|
+
old, new = old[len(project_prefix):], new[len(project_prefix):]
|
|
109
|
+
renames[old] = new
|
|
110
|
+
else:
|
|
111
|
+
index += 2
|
|
112
|
+
return renames
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def analyze_tree_at_ref(repo_root, ref, project_prefix=""):
|
|
116
|
+
"""Analyze the project as it exists at `ref`, in a throwaway clone.
|
|
117
|
+
|
|
118
|
+
The parse cache is neither read nor written, and the user's checkout is
|
|
119
|
+
never touched.
|
|
120
|
+
"""
|
|
121
|
+
temp_root = tempfile.mkdtemp(prefix="adre-base-")
|
|
122
|
+
clone_path = os.path.join(temp_root, "tree")
|
|
123
|
+
try:
|
|
124
|
+
run_git_command(
|
|
125
|
+
None,
|
|
126
|
+
["clone", "--quiet", "--no-checkout", "--shared", "--template=", repo_root, clone_path],
|
|
127
|
+
)
|
|
128
|
+
run_git_command(clone_path, ["checkout", "--quiet", "--detach", _safe_ref(ref)])
|
|
129
|
+
analysis_path = os.path.join(clone_path, project_prefix) if project_prefix else clone_path
|
|
130
|
+
if not os.path.isdir(analysis_path):
|
|
131
|
+
return {"architecture": {"dependencies": [], "cycles": [], "persistence_access": []}}
|
|
132
|
+
return analyze_project_architecture(
|
|
133
|
+
analysis_path,
|
|
134
|
+
persist_cache=False,
|
|
135
|
+
reuse_cache=False,
|
|
136
|
+
)
|
|
137
|
+
finally:
|
|
138
|
+
shutil.rmtree(temp_root, ignore_errors=True)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def load_declared_from_git(repo_root, ref, rel_path):
|
|
142
|
+
git_path = _safe_git_path(rel_path)
|
|
143
|
+
result = run_git_command(
|
|
144
|
+
repo_root,
|
|
145
|
+
["show", "{0}:{1}".format(_safe_ref(ref), git_path)],
|
|
146
|
+
check=False,
|
|
147
|
+
)
|
|
148
|
+
if result.returncode != 0:
|
|
149
|
+
return None
|
|
150
|
+
return load_declared_text(result.stdout, "{0}:{1}".format(ref, git_path))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def load_baseline_from_git(repo_root, ref, rel_path=None):
|
|
154
|
+
git_path = _safe_git_path(rel_path or "{0}/{1}".format(BASELINE_DIRNAME, BASELINE_FILENAME))
|
|
155
|
+
result = run_git_command(
|
|
156
|
+
repo_root,
|
|
157
|
+
["show", "{0}:{1}".format(_safe_ref(ref), git_path)],
|
|
158
|
+
check=False,
|
|
159
|
+
)
|
|
160
|
+
if result.returncode != 0:
|
|
161
|
+
return None
|
|
162
|
+
try:
|
|
163
|
+
data = json.loads(result.stdout)
|
|
164
|
+
except json.JSONDecodeError:
|
|
165
|
+
raise ValueError("Baseline in {0} is not valid JSON.".format(ref))
|
|
166
|
+
return validate_architecture_baseline(data, source="{0}:{1}".format(ref, git_path))
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def resolve_pr_baseline(repo_root, project_path, base_ref, explicit_path=None):
|
|
170
|
+
if explicit_path:
|
|
171
|
+
path = explicit_path if os.path.isabs(explicit_path) else os.path.join(project_path, explicit_path)
|
|
172
|
+
if not os.path.isfile(path):
|
|
173
|
+
return None, "explicit", path
|
|
174
|
+
return load_architecture_baseline(path), "explicit", path
|
|
175
|
+
|
|
176
|
+
git_rel = "{0}/{1}".format(BASELINE_DIRNAME, BASELINE_FILENAME)
|
|
177
|
+
from_base = load_baseline_from_git(repo_root, base_ref, git_rel)
|
|
178
|
+
if from_base is not None:
|
|
179
|
+
return from_base, "base_ref", "{0}:{1}".format(base_ref, git_rel)
|
|
180
|
+
|
|
181
|
+
working = default_baseline_path(project_path)
|
|
182
|
+
if os.path.isfile(working):
|
|
183
|
+
return load_architecture_baseline(working), "working_tree", working
|
|
184
|
+
return None, "missing", git_rel
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _normalize_name(value):
|
|
188
|
+
return (value or "").replace("\\", "/")
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _path_matches(candidate, changed_path):
|
|
192
|
+
if not candidate or not changed_path:
|
|
193
|
+
return False
|
|
194
|
+
if candidate == changed_path:
|
|
195
|
+
return True
|
|
196
|
+
return changed_path.endswith("/" + candidate) or candidate.endswith("/" + changed_path)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def finding_touches_changes(finding, changed_files):
|
|
200
|
+
if not changed_files:
|
|
201
|
+
return True
|
|
202
|
+
changed = {_normalize_name(item) for item in changed_files}
|
|
203
|
+
candidates = []
|
|
204
|
+
for key in ("file", "imported"):
|
|
205
|
+
value = _normalize_name(finding.get(key))
|
|
206
|
+
if value:
|
|
207
|
+
candidates.append(value)
|
|
208
|
+
for item in finding.get("affected_files") or []:
|
|
209
|
+
value = _normalize_name(item)
|
|
210
|
+
if value:
|
|
211
|
+
candidates.append(value)
|
|
212
|
+
for candidate in candidates:
|
|
213
|
+
for path in changed:
|
|
214
|
+
if _path_matches(candidate, path):
|
|
215
|
+
return True
|
|
216
|
+
return False
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def filter_pr_drift(drift, changed_files):
|
|
220
|
+
source_changes = [
|
|
221
|
+
path for path in changed_files
|
|
222
|
+
if os.path.splitext(path)[1].lower() in SUPPORTED_EXTENSIONS
|
|
223
|
+
]
|
|
224
|
+
if not source_changes:
|
|
225
|
+
return []
|
|
226
|
+
return [item for item in drift if finding_touches_changes(item, source_changes)]
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def review_pull_request(project_path=".", base_ref="HEAD", head_ref="HEAD", baseline_path=None):
|
|
230
|
+
project_path = os.path.realpath(os.path.abspath(project_path))
|
|
231
|
+
try:
|
|
232
|
+
repo_root = get_repo_root(project_path)
|
|
233
|
+
base_sha = verify_ref(repo_root, base_ref)
|
|
234
|
+
head_sha = verify_ref(repo_root, head_ref)
|
|
235
|
+
checked_out = run_git(repo_root, ["rev-parse", "HEAD"])
|
|
236
|
+
except (OSError, subprocess.CalledProcessError, ValueError) as exc:
|
|
237
|
+
return {
|
|
238
|
+
"status": ERROR,
|
|
239
|
+
"title": "ADRE",
|
|
240
|
+
"summary": "Could not resolve git refs: {0}".format(exc),
|
|
241
|
+
"drift": [],
|
|
242
|
+
"changed_files": [],
|
|
243
|
+
"error": str(exc),
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if checked_out != head_sha:
|
|
247
|
+
return {
|
|
248
|
+
"status": ERROR,
|
|
249
|
+
"title": "ADRE",
|
|
250
|
+
"summary": (
|
|
251
|
+
"Working tree HEAD {0} does not match requested head {1} ({2}). "
|
|
252
|
+
"Check out the pull request head (or merge commit) before running pr-review."
|
|
253
|
+
).format(checked_out, head_ref, head_sha),
|
|
254
|
+
"drift": [],
|
|
255
|
+
"changed_files": [],
|
|
256
|
+
"error": "head_mismatch",
|
|
257
|
+
"base_ref": base_ref,
|
|
258
|
+
"head_ref": head_ref,
|
|
259
|
+
"base_sha": base_sha,
|
|
260
|
+
"head_sha": head_sha,
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
try:
|
|
264
|
+
changed = changed_files_between(repo_root, base_sha, head_sha)
|
|
265
|
+
dirty = bool(run_git(repo_root, ["status", "--porcelain"]))
|
|
266
|
+
except subprocess.CalledProcessError as exc:
|
|
267
|
+
return {
|
|
268
|
+
"status": ERROR,
|
|
269
|
+
"title": "ADRE",
|
|
270
|
+
"summary": "Could not compute PR diff: {0}".format(exc),
|
|
271
|
+
"drift": [],
|
|
272
|
+
"changed_files": [],
|
|
273
|
+
"error": str(exc),
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
try:
|
|
277
|
+
baseline, baseline_source, baseline_location = resolve_pr_baseline(
|
|
278
|
+
repo_root,
|
|
279
|
+
project_path,
|
|
280
|
+
base_sha,
|
|
281
|
+
explicit_path=baseline_path,
|
|
282
|
+
)
|
|
283
|
+
except (OSError, ValueError, json.JSONDecodeError) as exc:
|
|
284
|
+
return {
|
|
285
|
+
"status": BASELINE_INVALID,
|
|
286
|
+
"title": "ADRE",
|
|
287
|
+
"summary": "Saved baseline could not be loaded: {0}".format(exc),
|
|
288
|
+
"drift": [],
|
|
289
|
+
"changed_files": changed,
|
|
290
|
+
"baseline_source": "invalid",
|
|
291
|
+
"error": str(exc),
|
|
292
|
+
"working_tree_dirty": dirty,
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
project_prefix = _project_prefix(repo_root, project_path)
|
|
296
|
+
declared_file = project_prefix + declared_rel_path()
|
|
297
|
+
try:
|
|
298
|
+
# Declared rules are policy: they take effect from the base commit, so
|
|
299
|
+
# a pull request cannot loosen the rules it is checked against.
|
|
300
|
+
declared = load_declared_from_git(repo_root, base_sha, declared_file)
|
|
301
|
+
except ValueError as exc:
|
|
302
|
+
return {
|
|
303
|
+
"status": ERROR,
|
|
304
|
+
"title": "ADRE",
|
|
305
|
+
"summary": "Declared architecture is invalid at the base commit: {0}".format(exc),
|
|
306
|
+
"drift": [],
|
|
307
|
+
"changed_files": changed,
|
|
308
|
+
"error": "declared_invalid",
|
|
309
|
+
"base_ref": base_ref,
|
|
310
|
+
"head_ref": head_ref,
|
|
311
|
+
"base_sha": base_sha,
|
|
312
|
+
"head_sha": head_sha,
|
|
313
|
+
"working_tree_dirty": dirty,
|
|
314
|
+
}
|
|
315
|
+
declared_file_changed = declared_file in changed
|
|
316
|
+
|
|
317
|
+
if baseline is None and declared is not None:
|
|
318
|
+
# Rules-only mode: no learned baseline yet, check the declared rules.
|
|
319
|
+
baseline = {"enough_structure": False, "layers": [], "scope": {}, "dependencies": [], "cycles": []}
|
|
320
|
+
baseline_source = "declared_only"
|
|
321
|
+
baseline_location = "{0}:{1}".format(base_ref, declared_file)
|
|
322
|
+
|
|
323
|
+
if baseline is None:
|
|
324
|
+
return {
|
|
325
|
+
"status": BASELINE_REQUIRED,
|
|
326
|
+
"title": "ADRE",
|
|
327
|
+
"summary": (
|
|
328
|
+
"No accepted architecture baseline was found. "
|
|
329
|
+
"Save one on the default branch with `adre baseline .` "
|
|
330
|
+
"and commit `.adre/baseline.json`."
|
|
331
|
+
),
|
|
332
|
+
"drift": [],
|
|
333
|
+
"changed_files": changed,
|
|
334
|
+
"baseline_source": baseline_source,
|
|
335
|
+
"baseline_location": baseline_location,
|
|
336
|
+
"base_ref": base_ref,
|
|
337
|
+
"head_ref": head_ref,
|
|
338
|
+
"base_sha": base_sha,
|
|
339
|
+
"head_sha": head_sha,
|
|
340
|
+
"working_tree_dirty": dirty,
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
reference = None
|
|
344
|
+
renames = {}
|
|
345
|
+
base_analysis_error = None
|
|
346
|
+
try:
|
|
347
|
+
renames = renamed_files_between(repo_root, base_sha, head_sha, project_prefix)
|
|
348
|
+
reference = analyze_tree_at_ref(repo_root, base_sha, project_prefix)
|
|
349
|
+
except (OSError, ValueError, TypeError, KeyError, RecursionError, subprocess.CalledProcessError) as exc:
|
|
350
|
+
# Fall back to the saved baseline's edges. That comparison is stricter
|
|
351
|
+
# (it can report drift that already exists on the base branch), so it
|
|
352
|
+
# never hides drift.
|
|
353
|
+
reference = None
|
|
354
|
+
base_analysis_error = str(exc) or exc.__class__.__name__
|
|
355
|
+
|
|
356
|
+
try:
|
|
357
|
+
analysis = analyze_project_architecture(project_path)
|
|
358
|
+
comparison = compare_to_baseline(
|
|
359
|
+
baseline, analysis, reference=reference, renames=renames, declared=declared,
|
|
360
|
+
)
|
|
361
|
+
except (OSError, ValueError, TypeError, KeyError, RecursionError) as exc:
|
|
362
|
+
return {
|
|
363
|
+
"status": ERROR,
|
|
364
|
+
"title": "ADRE",
|
|
365
|
+
"summary": "Architecture analysis failed: {0}".format(exc),
|
|
366
|
+
"drift": [],
|
|
367
|
+
"changed_files": changed,
|
|
368
|
+
"baseline_source": baseline_source,
|
|
369
|
+
"baseline_location": baseline_location,
|
|
370
|
+
"error": str(exc),
|
|
371
|
+
"working_tree_dirty": dirty,
|
|
372
|
+
"base_ref": base_ref,
|
|
373
|
+
"head_ref": head_ref,
|
|
374
|
+
"base_sha": base_sha,
|
|
375
|
+
"head_sha": head_sha,
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
raw_drift = comparison.get("drift") or []
|
|
379
|
+
drift = filter_pr_drift(raw_drift, changed)
|
|
380
|
+
accepted_file = project_prefix + accepted_rel_path()
|
|
381
|
+
accepted_file_changed = accepted_file in changed
|
|
382
|
+
try:
|
|
383
|
+
accepted_rules = load_accepted(project_path)
|
|
384
|
+
except ValueError as exc:
|
|
385
|
+
return {
|
|
386
|
+
"status": ERROR,
|
|
387
|
+
"title": "ADRE",
|
|
388
|
+
"summary": "Accepted exceptions file is invalid: {0}".format(exc),
|
|
389
|
+
"drift": drift,
|
|
390
|
+
"changed_files": changed,
|
|
391
|
+
"error": "accepted_invalid",
|
|
392
|
+
"base_ref": base_ref,
|
|
393
|
+
"head_ref": head_ref,
|
|
394
|
+
"base_sha": base_sha,
|
|
395
|
+
"head_sha": head_sha,
|
|
396
|
+
"working_tree_dirty": dirty,
|
|
397
|
+
}
|
|
398
|
+
drift, accepted_drift = apply_accepted(drift, accepted_rules)
|
|
399
|
+
stale_accepted = stale_accepted_rules(accepted_rules, analysis)
|
|
400
|
+
fail_on_stale = accepted_fail_on_stale(project_path)
|
|
401
|
+
if drift:
|
|
402
|
+
status = DRIFT
|
|
403
|
+
summary = "{0} new architectural drift finding(s) versus the accepted baseline.".format(len(drift))
|
|
404
|
+
elif stale_accepted and fail_on_stale:
|
|
405
|
+
status = DRIFT
|
|
406
|
+
summary = "{0} stale entr(y/ies) in {1} no longer match any dependency; remove them.".format(
|
|
407
|
+
len(stale_accepted), accepted_rel_path()
|
|
408
|
+
)
|
|
409
|
+
else:
|
|
410
|
+
status = PASS
|
|
411
|
+
summary = "No new architectural drift detected."
|
|
412
|
+
if accepted_drift:
|
|
413
|
+
summary = "No new architectural drift detected; {0} finding(s) accepted in {1}.".format(
|
|
414
|
+
len(accepted_drift), accepted_rel_path()
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
return {
|
|
418
|
+
"status": status,
|
|
419
|
+
"title": "ADRE",
|
|
420
|
+
"summary": summary,
|
|
421
|
+
"drift": drift,
|
|
422
|
+
"raw_drift_count": len(raw_drift),
|
|
423
|
+
"accepted_drift": accepted_drift,
|
|
424
|
+
"accepted_rules": len(accepted_rules),
|
|
425
|
+
"accepted_file_changed": accepted_file_changed,
|
|
426
|
+
"stale_accepted": stale_accepted,
|
|
427
|
+
"declared_rules": declared is not None,
|
|
428
|
+
"declared_file_changed": declared_file_changed,
|
|
429
|
+
"changed_files": changed,
|
|
430
|
+
"baseline_source": baseline_source,
|
|
431
|
+
"baseline_location": baseline_location,
|
|
432
|
+
"baseline_summary_path": comparison.get("baseline_summary_path") or baseline.get("summary_path") or "",
|
|
433
|
+
"current_summary_path": comparison.get("current_summary_path") or analysis.get("established_pattern") or "",
|
|
434
|
+
"baseline_enough_structure": bool(baseline.get("enough_structure")),
|
|
435
|
+
"current_enough_structure": bool((analysis.get("architecture") or {}).get("enough_structure")),
|
|
436
|
+
"incremental": analysis.get("incremental") or {},
|
|
437
|
+
"architecture": analysis.get("architecture") or {},
|
|
438
|
+
"base_ref": base_ref,
|
|
439
|
+
"head_ref": head_ref,
|
|
440
|
+
"base_sha": base_sha,
|
|
441
|
+
"head_sha": head_sha,
|
|
442
|
+
"project_path": project_path,
|
|
443
|
+
"project_prefix": project_prefix,
|
|
444
|
+
"comparison_mode": comparison.get("comparison_mode"),
|
|
445
|
+
"base_analysis_error": base_analysis_error,
|
|
446
|
+
"renamed_files": comparison.get("renamed_files") or [],
|
|
447
|
+
"added_dependencies": comparison.get("added_dependencies") or [],
|
|
448
|
+
"removed_dependencies": comparison.get("removed_dependencies") or [],
|
|
449
|
+
"working_tree_dirty": dirty,
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def exit_code_for(result):
|
|
454
|
+
return EXIT_CODES.get(result.get("status"), 1)
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
SEVERITY_PRIORITY = {
|
|
5
|
+
"critical": 55,
|
|
6
|
+
"high": 40,
|
|
7
|
+
"medium": 24,
|
|
8
|
+
"low": 10,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def clamp_score(value, minimum=0, maximum=100):
|
|
13
|
+
return max(minimum, min(maximum, int(round(value))))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def priority_level(score):
|
|
17
|
+
if score >= 80:
|
|
18
|
+
return "critical"
|
|
19
|
+
if score >= 60:
|
|
20
|
+
return "high"
|
|
21
|
+
if score >= 35:
|
|
22
|
+
return "medium"
|
|
23
|
+
return "low"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def inbound_dependency_counts(analysis):
|
|
27
|
+
counts = defaultdict(int)
|
|
28
|
+
resolved_graph = analysis.get("resolved_graph") or {}
|
|
29
|
+
|
|
30
|
+
if resolved_graph:
|
|
31
|
+
for targets in resolved_graph.values():
|
|
32
|
+
for target in targets:
|
|
33
|
+
counts[target] += 1
|
|
34
|
+
return counts
|
|
35
|
+
|
|
36
|
+
dependency_graph = analysis.get("dependency_graph", {})
|
|
37
|
+
for source, data in dependency_graph.items():
|
|
38
|
+
for imported in data.get("imports", []):
|
|
39
|
+
if imported in dependency_graph:
|
|
40
|
+
counts[imported] += 1
|
|
41
|
+
counts[source] += 0
|
|
42
|
+
return counts
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def cycle_file_set(analysis):
|
|
46
|
+
files = set()
|
|
47
|
+
for cycle in analysis.get("cycles", []):
|
|
48
|
+
files.update(file for file in cycle if file)
|
|
49
|
+
return files
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def hotspot_file_set(analysis):
|
|
53
|
+
return {
|
|
54
|
+
item.get("file")
|
|
55
|
+
for item in analysis.get("hotspot_files", [])
|
|
56
|
+
if item.get("file")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def issue_layers(issue):
|
|
61
|
+
return {
|
|
62
|
+
layer
|
|
63
|
+
for layer in [issue.get("layer"), issue.get("imported_layer")]
|
|
64
|
+
if layer and layer != "unknown"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def layer_violation_impact(issue):
|
|
69
|
+
if issue.get("issue_type") == "cycle":
|
|
70
|
+
return 0
|
|
71
|
+
layers = issue_layers(issue)
|
|
72
|
+
if issue.get("imported_layer") == "multiple":
|
|
73
|
+
return 15
|
|
74
|
+
if len(layers) >= 2:
|
|
75
|
+
return 10
|
|
76
|
+
if len(layers) == 1:
|
|
77
|
+
return 5
|
|
78
|
+
return 0
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def issue_priority_score(issue, inbound_counts, hotspots, cycle_files):
|
|
82
|
+
file = issue.get("file")
|
|
83
|
+
imported = issue.get("imported")
|
|
84
|
+
severity_score = SEVERITY_PRIORITY.get(issue.get("severity"), 8)
|
|
85
|
+
centrality_score = min(18, inbound_counts.get(file, 0) * 6)
|
|
86
|
+
|
|
87
|
+
hotspot_score = 0
|
|
88
|
+
if file in hotspots:
|
|
89
|
+
hotspot_score += 12
|
|
90
|
+
if imported in hotspots:
|
|
91
|
+
hotspot_score += 6
|
|
92
|
+
|
|
93
|
+
cycle_score = 0
|
|
94
|
+
if issue.get("issue_type") == "cycle":
|
|
95
|
+
cycle_score += 18
|
|
96
|
+
if file in cycle_files:
|
|
97
|
+
cycle_score += 12
|
|
98
|
+
if imported in cycle_files:
|
|
99
|
+
cycle_score += 8
|
|
100
|
+
|
|
101
|
+
score = (
|
|
102
|
+
severity_score
|
|
103
|
+
+ centrality_score
|
|
104
|
+
+ hotspot_score
|
|
105
|
+
+ cycle_score
|
|
106
|
+
+ layer_violation_impact(issue)
|
|
107
|
+
)
|
|
108
|
+
return clamp_score(score)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def rank_architecture_issues(analysis):
|
|
112
|
+
inbound_counts = inbound_dependency_counts(analysis)
|
|
113
|
+
hotspots = hotspot_file_set(analysis)
|
|
114
|
+
cycle_files = cycle_file_set(analysis)
|
|
115
|
+
|
|
116
|
+
ranked = []
|
|
117
|
+
for issue in analysis.get("issues", []):
|
|
118
|
+
score = issue_priority_score(issue, inbound_counts, hotspots, cycle_files)
|
|
119
|
+
enriched_issue = dict(issue)
|
|
120
|
+
enriched_issue["priority_score"] = score
|
|
121
|
+
enriched_issue["priority_level"] = priority_level(score)
|
|
122
|
+
ranked.append(enriched_issue)
|
|
123
|
+
|
|
124
|
+
return sorted(
|
|
125
|
+
ranked,
|
|
126
|
+
key=lambda item: (
|
|
127
|
+
-item["priority_score"],
|
|
128
|
+
item.get("file", ""),
|
|
129
|
+
item.get("imported", ""),
|
|
130
|
+
item.get("issue_type", ""),
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def add_reason(reasons, reason):
|
|
136
|
+
if reason not in reasons:
|
|
137
|
+
reasons.append(reason)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def build_file_risk_ranking(analysis):
|
|
141
|
+
inbound_counts = inbound_dependency_counts(analysis)
|
|
142
|
+
hotspots = hotspot_file_set(analysis)
|
|
143
|
+
cycle_files = cycle_file_set(analysis)
|
|
144
|
+
dependency_graph = analysis.get("dependency_graph", {})
|
|
145
|
+
scores = defaultdict(int)
|
|
146
|
+
reasons_by_file = defaultdict(list)
|
|
147
|
+
|
|
148
|
+
for file, count in inbound_counts.items():
|
|
149
|
+
if count:
|
|
150
|
+
scores[file] += min(30, count * 8)
|
|
151
|
+
if count >= 3:
|
|
152
|
+
add_reason(reasons_by_file[file], "high dependency centrality")
|
|
153
|
+
else:
|
|
154
|
+
add_reason(reasons_by_file[file], "dependency centrality")
|
|
155
|
+
|
|
156
|
+
for file in hotspots:
|
|
157
|
+
scores[file] += 24
|
|
158
|
+
add_reason(reasons_by_file[file], "hotspot")
|
|
159
|
+
|
|
160
|
+
for file in cycle_files:
|
|
161
|
+
scores[file] += 32
|
|
162
|
+
add_reason(reasons_by_file[file], "cycle participant")
|
|
163
|
+
|
|
164
|
+
for issue in analysis.get("issues", []):
|
|
165
|
+
file = issue.get("file")
|
|
166
|
+
if not file:
|
|
167
|
+
continue
|
|
168
|
+
severity = issue.get("severity", "low")
|
|
169
|
+
scores[file] += {
|
|
170
|
+
"critical": 24,
|
|
171
|
+
"high": 18,
|
|
172
|
+
"medium": 10,
|
|
173
|
+
"low": 4,
|
|
174
|
+
}.get(severity, 4)
|
|
175
|
+
if issue.get("issue_type") == "cycle":
|
|
176
|
+
add_reason(reasons_by_file[file], "cycle participant")
|
|
177
|
+
else:
|
|
178
|
+
add_reason(reasons_by_file[file], "layer rule violation")
|
|
179
|
+
|
|
180
|
+
for file in dependency_graph:
|
|
181
|
+
scores[file] += 0
|
|
182
|
+
|
|
183
|
+
ranking = []
|
|
184
|
+
for file, score in scores.items():
|
|
185
|
+
if score <= 0:
|
|
186
|
+
continue
|
|
187
|
+
ranking.append({
|
|
188
|
+
"file": file,
|
|
189
|
+
"risk_score": clamp_score(score),
|
|
190
|
+
"reasons": reasons_by_file[file],
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
return sorted(
|
|
194
|
+
ranking,
|
|
195
|
+
key=lambda item: (-item["risk_score"], item["file"]),
|
|
196
|
+
)
|
adre/core/project.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from adre.core.config import SKIP_DIRS
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def scan_project_structure(project_root):
|
|
7
|
+
structure = []
|
|
8
|
+
|
|
9
|
+
for root, dirs, files in os.walk(project_root):
|
|
10
|
+
dirs[:] = [d for d in dirs if d not in SKIP_DIRS and not d.startswith(".")]
|
|
11
|
+
|
|
12
|
+
for d in dirs:
|
|
13
|
+
structure.append({
|
|
14
|
+
"type": "dir",
|
|
15
|
+
"path": os.path.relpath(os.path.join(root, d), project_root)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
for f in files:
|
|
19
|
+
if f.startswith("."):
|
|
20
|
+
continue
|
|
21
|
+
structure.append({
|
|
22
|
+
"type": "file",
|
|
23
|
+
"path": os.path.relpath(os.path.join(root, f), project_root)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
"project_root": project_root,
|
|
28
|
+
"total_items": len(structure),
|
|
29
|
+
"structure": structure
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def analyze_project_drift(project_root):
|
|
34
|
+
structure = scan_project_structure(project_root)
|
|
35
|
+
return {
|
|
36
|
+
"score": None,
|
|
37
|
+
"violations": [],
|
|
38
|
+
"total_violations": 0,
|
|
39
|
+
"total_items": structure["total_items"],
|
|
40
|
+
"note": "Directory-name drift checks were removed. Use architecture analysis for dependency and layer findings.",
|
|
41
|
+
}
|