-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_fetcher.py
More file actions
95 lines (59 loc) · 2.13 KB
/
Copy pathgithub_fetcher.py
File metadata and controls
95 lines (59 loc) · 2.13 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
import requests
import os
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
HEADERS = {
"Accept": "application/vnd.github+json",
"User-Agent": "AI-GitHub-Code-Review-Assistant"
}
if GITHUB_TOKEN:
HEADERS["Authorization"] = f"Bearer {GITHUB_TOKEN}"
# -----------------------------
# Parse GitHub URL
# -----------------------------
def parse_github_url(repo_url):
repo_url = repo_url.strip()
if repo_url.endswith(".git"):
repo_url = repo_url[:-4]
repo_url = repo_url.rstrip("/")
parts = repo_url.split("/")
if len(parts) < 5:
raise Exception("Invalid GitHub repository URL")
owner = parts[3]
repo = parts[4]
return owner, repo
# -----------------------------
# Fetch Repository Files
# -----------------------------
def get_repo_files(repo_url):
owner, repo = parse_github_url(repo_url)
repo_api = f"https://api.github.com/repos/{owner}/{repo}"
repo_response = requests.get(repo_api, headers=HEADERS)
if repo_response.status_code != 200:
raise Exception(
f"GitHub API Error: {repo_response.status_code} - {repo_response.text}"
)
repo_data = repo_response.json()
default_branch = repo_data["default_branch"]
tree_api = f"https://api.github.com/repos/{owner}/{repo}/git/trees/{default_branch}?recursive=1"
tree_response = requests.get(tree_api, headers=HEADERS)
if tree_response.status_code != 200:
raise Exception(
f"GitHub Tree API Error: {tree_response.status_code} - {tree_response.text}"
)
tree_data = tree_response.json()
files = []
for item in tree_data["tree"]:
if item["type"] == "blob":
path = item["path"]
if path.endswith((".py", ".js", ".java", ".cpp")):
files.append(path)
return owner, repo, files
# -----------------------------
# Download File Content
# -----------------------------
def download_file(owner, repo, file_path):
raw_url = f"https://raw.githubusercontent.com/{owner}/{repo}/main/{file_path}"
response = requests.get(raw_url)
if response.status_code == 200:
return response.text
return None