-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_extractor.py
More file actions
78 lines (59 loc) · 2.12 KB
/
Copy pathdiff_extractor.py
File metadata and controls
78 lines (59 loc) · 2.12 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
import difflib
from azdo_client import AzDOClient
MAX_FILES = 20
SKIP_CHANGE_TYPES = {"delete"}
def extract_unified_diffs(client: AzDOClient, commit_diff: dict, base_commit: str, target_commit: str) -> list[dict]:
results = []
for change in commit_diff.get("changes", []):
item = change.get("item", {})
path = item.get("path")
change_type = change.get("changeType", "")
if not path or item.get("isFolder"):
continue
if change_type in SKIP_CHANGE_TYPES:
continue
try:
base_lines = (
client.get_file_content(path, base_commit).splitlines(keepends=True)
if change_type != "add" else []
)
target_lines = client.get_file_content(path, target_commit).splitlines(keepends=True)
except Exception as e:
print(f" Skipping {path}: {e}")
continue
diff = list(difflib.unified_diff(
base_lines, target_lines,
fromfile=f"a{path}",
tofile=f"b{path}",
))
if diff:
results.append({
"file": path,
"diff": "".join(diff),
})
if len(results) >= MAX_FILES:
break
return results
# Usage: set AZDO_PR_REVIEWER_PAT and fill in your own org/project/repo/PR ID
if __name__ == "__main__":
ORG = "<org>"
PROJECT = "<project>"
REPO = "<repo>"
PR_ID = "<pr_id>"
print("Fetching PR diff from Azure DevOps...")
client = AzDOClient(ORG, PROJECT, REPO)
pr = client.get_pull_request(PR_ID)
base_commit = pr["lastMergeTargetCommit"]["commitId"]
target_commit = pr["lastMergeSourceCommit"]["commitId"]
commit_diff = client.get_commit_diff(base_commit, target_commit)
print()
print("Extracting unified diffs...\n")
diffs = extract_unified_diffs(client, commit_diff, base_commit, target_commit)
for i, d in enumerate(diffs, start=1):
print("=" * 80)
print(f"[{i}] FILE: {d['file']}")
print("-" * 80)
print(d["diff"])
print()
if not diffs:
print("No diffs extracted.")