forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr_genius_rules.py
More file actions
214 lines (187 loc) · 6.88 KB
/
Copy pathpr_genius_rules.py
File metadata and controls
214 lines (187 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""PR Genius rule engine — repo-agnostic core + repo-specific config.
Layer 1 (repo-agnostic): portable rules built into the engine.
Layer 2 (repo-specific): loaded from .pr-genius.yaml path_rules + custom_patterns.
"""
from __future__ import annotations
import re
from typing import Any
# ── Repo-agnostic rules (Layer 1) ──────────────────────────────────────────
REPO_AGNOSTIC_RULES: list[dict[str, Any]] = [
{
"id": "pr_too_large",
"layer": "core",
"severity": "high",
"enabled": True,
"description": "PR exceeds max line threshold",
},
{
"id": "missing_tests",
"layer": "core",
"severity": "medium",
"enabled": True,
"description": "Code changed but no test files updated",
},
{
"id": "doc_code_mismatch",
"layer": "core",
"severity": "low",
"enabled": True,
"description": "Docs-only PR with no code or test changes",
},
{
"id": "mixed_concerns",
"layer": "core",
"severity": "medium",
"enabled": True,
"description": "PR touches 3+ unrelated component areas",
},
{
"id": "no_issue_reference",
"layer": "core",
"severity": "low",
"enabled": True,
"description": "No linked issue found in PR body",
},
{
"id": "missing_dco",
"layer": "core",
"severity": "medium",
"enabled": True,
"description": "One or more commits lack Signed-off-by",
},
{
"id": "draft_pr",
"layer": "core",
"severity": "info",
"enabled": True,
"description": "PR is in draft state",
},
{
"id": "review_stale",
"layer": "core",
"severity": "medium",
"enabled": True,
"description": "PR has been open >14 days without approval",
},
]
# ── Rule engine ────────────────────────────────────────────────────────────
def build_rule_list(config: dict[str, Any]) -> list[dict[str, Any]]:
"""Merge repo-agnostic rules with repo-specific config.
Returns ordered list of rule dicts with id, layer, severity, enabled,
and optional trigger (path glob pattern).
"""
cfg_rules = config.get("rules", {})
pattern_cfg = cfg_rules.get("patterns", {})
# Start with repo-agnostic rules, apply config overrides
rules: list[dict[str, Any]] = []
for rule in REPO_AGNOSTIC_RULES:
merged = dict(rule)
override = pattern_cfg.get(rule["id"], {})
if override:
merged["enabled"] = override.get("enabled", rule["enabled"])
merged["severity"] = override.get("severity", rule["severity"])
rules.append(merged)
# Add repo-specific path_rules (Layer 2)
path_rules = cfg_rules.get("path_rules", [])
for pr in path_rules:
if not isinstance(pr, dict):
continue
rules.append({
"id": pr.get("id", "custom_path_rule"),
"layer": "repo",
"severity": pr.get("severity", "medium"),
"enabled": pr.get("enabled", True),
"description": pr.get("description", ""),
"trigger": pr.get("trigger", ""), # path glob pattern
"message": pr.get("message", ""),
})
# Add repo-specific custom_patterns (Layer 2)
custom_patterns = cfg_rules.get("custom_patterns", [])
for cp in custom_patterns:
if not isinstance(cp, dict):
continue
rules.append({
"id": cp.get("id", "custom_pattern"),
"layer": "repo",
"severity": cp.get("severity", "medium"),
"enabled": cp.get("enabled", True),
"description": cp.get("description", ""),
"pattern": cp.get("pattern", ""), # regex on PR body/title
"message": cp.get("message", ""),
})
return rules
def _match_path(path: str, pattern: str) -> bool:
"""Match a file path against a glob pattern.
Supports ``**`` for recursive directory matching and ``*``/``?``
for single-segment wildcards.
"""
import fnmatch
if "**" not in pattern:
return fnmatch.fnmatch(path, pattern)
# Split into prefix (before **) and suffix (after **)
prefix, _, suffix = pattern.partition("**")
prefix = prefix.rstrip("/")
suffix = suffix.lstrip("/")
# Path must start with prefix
if prefix and not path.startswith(prefix):
return False
remaining = path[len(prefix):].lstrip("/")
# If no suffix, ** at end matches everything
if not suffix:
return True
# Check if any path segment (or the whole remaining) matches suffix
# migrations/**/*.sql → suffix = *.sql
# Match against each possible suffix of the remaining path
parts = remaining.split("/")
for i in range(len(parts)):
subpath = "/".join(parts[i:])
if fnmatch.fnmatch(subpath, suffix):
return True
return False
def evaluate_path_rules(
rules: list[dict[str, Any]], paths: list[str]
) -> list[dict[str, str]]:
"""Evaluate path-triggered rules against changed file paths."""
findings: list[dict[str, str]] = []
for rule in rules:
if not rule.get("enabled") or rule.get("layer") != "repo":
continue
trigger = rule.get("trigger", "")
if not trigger:
continue
matched = [p for p in paths if _match_path(p, trigger)]
if matched:
findings.append({
"rule": rule["id"],
"severity": rule.get("severity", "medium"),
"detail": rule.get("message") or rule.get("description", ""),
"matched_files": ", ".join(matched[:5]),
})
return findings
def evaluate_body_rules(
rules: list[dict[str, Any]], body: str, title: str = ""
) -> list[dict[str, str]]:
"""Evaluate regex pattern rules against PR body and title."""
combined = f"{title}\n{body}"
findings: list[dict[str, str]] = []
for rule in rules:
if not rule.get("enabled") or rule.get("layer") != "repo":
continue
pattern = rule.get("pattern", "")
if not pattern:
continue
if re.search(pattern, combined, re.IGNORECASE):
findings.append({
"rule": rule["id"],
"severity": rule.get("severity", "medium"),
"detail": rule.get("message") or rule.get("description", ""),
})
return findings
def get_enabled_rules(
rules: list[dict[str, Any]], layer: str | None = None
) -> list[dict[str, Any]]:
"""Return enabled rules, optionally filtered by layer."""
return [
r for r in rules
if r.get("enabled", True) and (layer is None or r.get("layer") == layer)
]