-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPull Request.py
More file actions
207 lines (165 loc) · 6.87 KB
/
Pull Request.py
File metadata and controls
207 lines (165 loc) · 6.87 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
import os, re
import tkinter as tk
from tkinter import messagebox
from collections import Counter
import lizard
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from git import Repo
# ---------------- VISUAL HELPERS ----------------
def bar(percent, size=10):
filled = int((percent / 100) * size)
return "█" * filled + "░" * (size - filled)
# ---------------- LANGUAGE DETECTION ----------------
EXT_LANG = {
".py":"Python",".js":"JavaScript",".ts":"TypeScript",".java":"Java",
".cpp":"C++",".c":"C",".h":"C/C++ Header",".go":"Go",".rs":"Rust",
".php":"PHP",".rb":"Ruby",".swift":"Swift",".kt":"Kotlin",
".cs":"C#",".scala":"Scala",".r":"R",".m":"MATLAB",
".sh":"Shell",".html":"HTML",".css":"CSS",".json":"JSON",".yaml":"YAML"
}
# ---------------- GITHUB HELPERS ----------------
def repo_parts(url):
parts = url.replace("https://github.com/","").split("/")
return parts[0], parts[1]
def shallow_clone(owner, repo):
path = f"temp_repo/{repo}"
if os.path.exists(path):
os.system(f"rm -rf {path}") # Clean old clone
os.makedirs("temp_repo", exist_ok=True)
Repo.clone_from(f"https://github.com/{owner}/{repo}.git", path, depth=1)
return path
# ---------------- README CLEAN + SUMMARY ----------------
def clean_readme(text):
text = re.sub(r'!\[.*?\]\(.*?\)', '', text)
text = re.sub(r'\[.*?\]\(.*?\)', '', text)
return text.strip()
def fetch_summary(owner, repo):
readme_path = f"temp_repo/{repo}/README.md"
if not os.path.exists(readme_path):
return "No README available."
with open(readme_path, errors="ignore") as f:
text = clean_readme(f.read())
sents = re.split(r'(?<=[.!?]) +', text)
if len(sents) < 5:
return text[:500]
tfidf = TfidfVectorizer(stop_words="english")
X = tfidf.fit_transform(sents)
scores = np.array(X.sum(axis=1)).flatten()
top = sorted(zip(scores, sents), reverse=True)[:5]
return " ".join(s for _, s in top)
# ---------------- ANALYSIS ----------------
def detect_languages(root):
c = Counter()
for r,_,files in os.walk(root):
for f in files:
ext = os.path.splitext(f)[1]
if ext in EXT_LANG:
c[EXT_LANG[ext]] += 1
return c
def file_level_analysis(root):
issues = []
total_complexity = 0
files = 0
for r,_,fs in os.walk(root):
for f in fs:
path = os.path.join(r,f)
if f.endswith(".py"):
try:
a = lizard.analyze_file(path)
c = sum(fn.cyclomatic_complexity for fn in a.function_list)
total_complexity += c
files += 1
if c > 10:
issues.append(f"{f} → High complexity")
except:
pass
if os.path.getsize(path) > 200_000:
issues.append(f"{f} → Very large file")
if f.endswith(".py"):
with open(path, errors="ignore") as fp:
content = fp.read()
if "def " in content and '"""' not in content:
issues.append(f"{f} → Missing documentation")
return issues, total_complexity, files
def health_score(root):
score = 100
missing = []
for f,p in [("README.md",20),("LICENSE",10),("tests",15),(".github",10)]:
if not os.path.exists(os.path.join(root,f)):
score -= p
missing.append(f)
return max(score,0), missing
def test_coverage(root):
src, tests = 0, 0
for r,_,files in os.walk(root):
for f in files:
if f.endswith(".py"):
if "test" in f.lower():
tests += 1
else:
src += 1
pct = int((tests / max(src,1)) * 100)
level = "Low" if pct < 40 else "Medium" if pct < 70 else "High"
return pct, level
def predict_trend(complexity, coverage):
if complexity > 70 and coverage < 40:
return "📉 Growing but risky (needs refactoring)"
if complexity < 50 and coverage > 60:
return "📈 Healthy and scalable"
return "➡ Stable but could improve"
# ---------------- MAIN ----------------
def analyze_repo():
url = repo_entry.get()
mode = mode_var.get()
if not url.startswith("https://github.com/"):
messagebox.showerror("Error","Invalid GitHub URL")
return
owner, repo = repo_parts(url)
output.delete("1.0",tk.END)
root_path = shallow_clone(owner, repo)
summary = fetch_summary(owner, repo)
langs = detect_languages(root_path)
issues, complexity, files = file_level_analysis(root_path)
health, missing = health_score(root_path)
coverage, cov_level = test_coverage(root_path)
difficulty = min(int((complexity / max(files,1)) * 10),100)
trend = predict_trend(difficulty, coverage)
# -------- OUTPUT --------
output.insert(tk.END,"📌 SUMMARY\n"+"-"*50+"\n"+summary+"\n\n")
output.insert(tk.END,"📊 DASHBOARD\n")
output.insert(tk.END,f"Code Difficulty {bar(difficulty)} {difficulty}%\n")
output.insert(tk.END,f"Project Health {bar(health)} {health}%\n")
output.insert(tk.END,f"Test Coverage {bar(coverage)} {coverage}% ({cov_level})\n\n")
output.insert(tk.END,"📂 LANGUAGES USED\n")
for l,c in langs.items():
output.insert(tk.END,f"• {l}: {c} files\n")
output.insert(tk.END,f"\n📈 REPO TREND\n{trend}\n")
if mode == "Advanced":
output.insert(tk.END,"\n🔍 FILE-LEVEL INSIGHTS\n")
if issues:
for i in issues:
output.insert(tk.END,f"• {i}\n")
else:
output.insert(tk.END,"• No major issues found\n")
output.insert(tk.END,"\n🧠 REFACTOR SUGGESTIONS\n")
if difficulty > 60:
output.insert(tk.END,"• Break large functions into smaller ones\n")
if coverage < 50:
output.insert(tk.END,"• Add unit tests for critical logic\n")
if missing:
output.insert(tk.END,"• Add missing repo files: "+", ".join(missing)+"\n")
# ---------------- GUI ----------------
root = tk.Tk()
root.title("GitHub Repository Analyzer – Shallow Clone")
root.geometry("900x650")
tk.Label(root,text="GitHub Repository URL").pack(pady=5)
repo_entry = tk.Entry(root,width=80)
repo_entry.pack()
mode_var = tk.StringVar(value="Beginner")
tk.Radiobutton(root,text="Beginner Mode",variable=mode_var,value="Beginner").pack()
tk.Radiobutton(root,text="Advanced Mode",variable=mode_var,value="Advanced").pack()
tk.Button(root,text="Run Analysis",command=analyze_repo).pack(pady=10)
output = tk.Text(root,wrap=tk.WORD)
output.pack(fill=tk.BOTH,expand=True,padx=10,pady=10)
root.mainloop()