-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
48 lines (37 loc) · 1.67 KB
/
analyzer.py
File metadata and controls
48 lines (37 loc) · 1.67 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
# analyzer.py
import requests
class RepoAnalysisError(ValueError):
"""Raised when repository analysis cannot be completed."""
def analyze_repo(repo_url):
repo_url = repo_url.strip().rstrip("/")
if "github.com/" not in repo_url:
raise RepoAnalysisError("Provide a valid GitHub repo URL like https://github.com/owner/repo")
# Split out owner/repo safely
parts = repo_url.split("github.com/")[-1].split("/")
if len(parts) < 2 or not parts[0] or not parts[1]:
raise RepoAnalysisError("Repository URL should include both owner and repo name")
owner, repo = parts[0], parts[1]
api = f"https://api.github.com/repos/{owner}/{repo}"
def _fetch(url):
try:
resp = requests.get(url, timeout=10)
except requests.RequestException as exc:
raise RepoAnalysisError(f"Network error contacting GitHub: {exc}") from exc
if resp.status_code >= 400:
raise RepoAnalysisError(f"GitHub API error {resp.status_code}: {resp.text[:200]}")
return resp.json()
repo_data = _fetch(api)
contents_resp = _fetch(api + "/contents")
commits_resp = _fetch(api + "/commits")
languages = _fetch(api + "/languages")
contents = contents_resp if isinstance(contents_resp, list) else []
commits = commits_resp if isinstance(commits_resp, list) else []
files = [item.get("name", "") for item in contents if isinstance(item, dict)]
return {
"name": repo,
"languages": list(languages.keys()),
"file_count": len(files),
"has_readme": "README.md" in files,
"has_tests": any("test" in f.lower() for f in files),
"commit_count": len(commits),
}