-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_client.py
More file actions
179 lines (148 loc) · 5.66 KB
/
github_client.py
File metadata and controls
179 lines (148 loc) · 5.66 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
"""
GitHub Repository client for extracting repo metadata, README, and release notes
from public GitHub repositories via the REST API.
"""
import re
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Optional
import requests
logger = logging.getLogger(__name__)
GITHUB_API_BASE = "https://api.github.com"
GITHUB_REPO_PATTERN = re.compile(
r"(?:https?://)?(?:www\.)?github\.com/([a-zA-Z0-9\-_.]+)/([a-zA-Z0-9\-_.]+)(?:/.*)?$"
)
_REQUEST_TIMEOUT = 15
def is_github_repo_url(url: str) -> bool:
"""Return True if *url* points to a GitHub repository (not gists, orgs, etc.)."""
if not url:
return False
match = GITHUB_REPO_PATTERN.match(url.strip().rstrip("/"))
if not match:
return False
owner, repo = match.group(1), match.group(2)
# Exclude GitHub special paths that aren't repos
excluded_owners = {
"settings", "notifications", "explore", "topics",
"trending", "collections", "sponsors", "login", "signup",
"orgs", "marketplace", "features", "security", "enterprise",
"pricing", "about", "team", "customer-stories", "readme",
}
if owner.lower() in excluded_owners:
return False
return True
def parse_github_repo_url(url: str) -> tuple[str, str]:
"""Extract (owner, repo) from a GitHub repo URL.
Raises ValueError if the URL doesn't match.
"""
match = GITHUB_REPO_PATTERN.match(url.strip().rstrip("/"))
if not match:
raise ValueError(f"Not a valid GitHub repo URL: {url}")
owner = match.group(1)
repo = match.group(2)
# Strip .git suffix if present
if repo.endswith(".git"):
repo = repo[:-4]
return owner, repo
def _api_get(path: str, accept: str = "application/vnd.github+json") -> requests.Response:
headers = {
"Accept": accept,
"X-GitHub-Api-Version": "2022-11-28",
}
return requests.get(
f"{GITHUB_API_BASE}{path}",
headers=headers,
timeout=_REQUEST_TIMEOUT,
)
def _fetch_repo_metadata(owner: str, repo: str) -> Optional[dict]:
try:
resp = _api_get(f"/repos/{owner}/{repo}")
resp.raise_for_status()
return resp.json()
except Exception as exc:
logger.warning("Failed to fetch repo metadata for %s/%s: %s", owner, repo, exc)
return None
def _fetch_readme(owner: str, repo: str) -> Optional[str]:
try:
resp = _api_get(f"/repos/{owner}/{repo}/readme", accept="application/vnd.github.raw")
resp.raise_for_status()
return resp.text
except Exception as exc:
logger.debug("No README for %s/%s: %s", owner, repo, exc)
return None
def _fetch_latest_release(owner: str, repo: str) -> Optional[dict]:
try:
resp = _api_get(f"/repos/{owner}/{repo}/releases/latest")
if resp.status_code == 404:
return None
resp.raise_for_status()
return resp.json()
except Exception as exc:
logger.debug("No releases for %s/%s: %s", owner, repo, exc)
return None
def fetch_github_repo(owner: str, repo: str) -> dict:
"""Fetch repo metadata, README, and latest release in parallel.
Returns a dict with keys: title, description, content, og_image, metadata.
Raises RuntimeError if the repo metadata cannot be fetched.
"""
futures = {}
with ThreadPoolExecutor(max_workers=3) as pool:
futures["meta"] = pool.submit(_fetch_repo_metadata, owner, repo)
futures["readme"] = pool.submit(_fetch_readme, owner, repo)
futures["release"] = pool.submit(_fetch_latest_release, owner, repo)
meta = futures["meta"].result()
readme_text = futures["readme"].result()
release = futures["release"].result()
if meta is None:
raise RuntimeError(f"Could not fetch repository {owner}/{repo}. It may not exist or may be private.")
full_name = meta.get("full_name", f"{owner}/{repo}")
description = meta.get("description") or ""
language = meta.get("language") or "Unknown"
stars = meta.get("stargazers_count", 0)
forks = meta.get("forks_count", 0)
topics = meta.get("topics") or []
homepage = meta.get("homepage") or ""
avatar = meta.get("owner", {}).get("avatar_url") or ""
# Build the assembled content blob
lines = [
f"Repository: {full_name}",
f"Description: {description}",
f"Language: {language} | Stars: {stars:,} | Forks: {forks:,}",
]
if topics:
lines.append(f"Topics: {', '.join(topics)}")
if homepage:
lines.append(f"Homepage: {homepage}")
lines.append("")
if readme_text:
lines.append("--- README ---")
lines.append(readme_text.strip())
lines.append("")
if release:
tag = release.get("tag_name", "")
release_name = release.get("name", tag)
release_body = release.get("body") or ""
header = f"--- Latest Release: {release_name} ({tag}) ---" if release_name != tag else f"--- Latest Release: {tag} ---"
lines.append(header)
if release_body:
lines.append(release_body.strip())
lines.append("")
content = "\n".join(lines)
# Truncate extremely large READMEs to keep content manageable
max_content = 50_000
if len(content) > max_content:
content = content[:max_content] + "\n\n[Content truncated]"
return {
"title": full_name,
"description": description,
"content": content,
"og_image": avatar,
"metadata": {
"language": language,
"stars": stars,
"forks": forks,
"topics": topics,
"homepage": homepage,
"latest_release": release.get("tag_name") if release else None,
},
}