-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.py
More file actions
245 lines (206 loc) · 11.2 KB
/
Copy pathgithub_api.py
File metadata and controls
245 lines (206 loc) · 11.2 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import requests
import logging
from datetime import datetime
from typing import Dict, List, Optional
logger = logging.getLogger(__name__)
class GitHubAPI:
def __init__(self, token: str):
self.token = token
self.headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
self.base_url = 'https://api.github.com'
def get_repo_info(self, repo_full_name: str) -> Optional[Dict]:
try:
url = f'{self.base_url}/repos/{repo_full_name}'
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
logger.info(f"Successfully fetched repo info for: {repo_full_name}")
return response.json()
elif response.status_code == 404:
logger.warning(f"Repository not found: {repo_full_name}")
elif response.status_code == 403:
logger.error(f"Forbidden access to: {repo_full_name}")
elif response.status_code == 401:
logger.error(f"Invalid authentication for: {repo_full_name}")
elif response.status_code == 429:
logger.error(f"Rate limit exceeded for: {repo_full_name}")
else:
logger.error(f"GitHub API error {response.status_code} for {repo_full_name}: {response.text[:100]}")
except requests.exceptions.Timeout:
logger.error(f"Request timeout for {repo_full_name}")
except requests.exceptions.ConnectionError:
logger.error(f"Connection error for {repo_full_name}")
except requests.exceptions.RequestException as e:
logger.error(f"Request exception for {repo_full_name}: {str(e)}")
except Exception as e:
logger.error(f"Unexpected error for {repo_full_name}: {str(e)}")
return None
def get_latest_commits(self, repo_full_name: str, branch: str = 'main', since: datetime = None) -> List[Dict]:
try:
url = f'{self.base_url}/repos/{repo_full_name}/commits'
params = {'sha': branch, 'per_page': 20}
if since:
params['since'] = since.strftime('%Y-%m-%dT%H:%M:%SZ')
logger.info(f"Fetching commits for {repo_full_name} (branch: {branch}) since {since if since else 'beginning'}")
response = requests.get(url, headers=self.headers, params=params, timeout=15)
if response.status_code == 200:
commits = response.json()
parsed_commits = []
logger.info(f"Retrieved {len(commits)} commits from {repo_full_name}")
for commit in commits:
try:
commit_info = {
'sha': commit['sha'],
'message': commit['commit']['message'].strip(),
'author_name': commit['commit']['author']['name'],
'author_email': commit['commit']['author']['email'],
'date': datetime.strptime(
commit['commit']['author']['date'],
'%Y-%m-%dT%H:%M:%SZ'
),
'url': commit['html_url'],
'repo_full_name': repo_full_name,
'added': 0,
'removed': 0,
'modified': 0,
'files': []
}
try:
detail_url = f'{self.base_url}/repos/{repo_full_name}/commits/{commit["sha"]}'
detail_resp = requests.get(detail_url, headers=self.headers, timeout=10)
if detail_resp.status_code == 200:
detail = detail_resp.json()
if 'files' in detail:
for f in detail['files']:
status = f.get('status', '')
if status == 'added':
commit_info['added'] += 1
elif status == 'removed':
commit_info['removed'] += 1
elif status == 'modified':
commit_info['modified'] += 1
commit_info['files'] = [f['filename'] for f in detail['files'][:5]]
logger.debug(f"Got file details for commit {commit['sha'][:7]}: "
f"+{commit_info['added']} -{commit_info['removed']} ~{commit_info['modified']}")
except Exception as e:
logger.warning(f"Failed to get commit details for {commit['sha'][:7]}: {str(e)}")
parsed_commits.append(commit_info)
except KeyError as e:
logger.warning(f"Missing field in commit data for {repo_full_name}: {str(e)}")
continue
except ValueError as e:
logger.warning(f"Date parsing error for commit in {repo_full_name}: {str(e)}")
continue
except Exception as e:
logger.warning(f"Error parsing commit in {repo_full_name}: {str(e)}")
continue
logger.info(f"Successfully parsed {len(parsed_commits)} commits for {repo_full_name}")
return parsed_commits
elif response.status_code == 404:
logger.error(f"Repository {repo_full_name} not found")
elif response.status_code == 409:
logger.error(f"Repository {repo_full_name} is empty")
elif response.status_code == 403:
logger.error(f"Access forbidden to {repo_full_name}")
else:
logger.error(f"Error getting commits for {repo_full_name}: {response.status_code} - {response.text[:200]}")
except requests.exceptions.Timeout:
logger.error(f"Request timeout for commits in {repo_full_name}")
except requests.exceptions.ConnectionError:
logger.error(f"Connection error for commits in {repo_full_name}")
except requests.exceptions.RequestException as e:
logger.error(f"Request exception for commits in {repo_full_name}: {str(e)}")
except Exception as e:
logger.error(f"Unexpected error for commits in {repo_full_name}: {str(e)}")
return []
def test_connection(self) -> bool:
try:
url = f'{self.base_url}/user'
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
user_data = response.json()
logger.info(f"Successfully connected to GitHub as: {user_data.get('login')}")
return True
elif response.status_code == 401:
logger.error("Invalid GitHub token")
elif response.status_code == 403:
logger.error("Token access is restricted")
else:
logger.error(f"Connection error: {response.status_code}")
return False
except requests.exceptions.Timeout:
logger.error("Connection to GitHub API timed out")
return False
except requests.exceptions.ConnectionError:
logger.error("Connection error to GitHub API")
return False
except Exception as e:
logger.error(f"Unexpected error testing connection: {str(e)}")
return False
def get_rate_limit(self) -> Dict:
try:
url = f'{self.base_url}/rate_limit'
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
return response.json()
return {}
except Exception as e:
logger.error(f"Error getting rate limit: {str(e)}")
return {}
def get_branches(self, repo_full_name: str) -> List[Dict]:
try:
url = f'{self.base_url}/repos/{repo_full_name}/branches'
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
branches = response.json()
logger.info(f"Retrieved {len(branches)} branches for {repo_full_name}")
return branches
else:
logger.error(f"Error getting branches for {repo_full_name}: {response.status_code}")
return []
except requests.exceptions.Timeout:
logger.error(f"Timeout getting branches for {repo_full_name}")
return []
except requests.exceptions.ConnectionError:
logger.error(f"Connection error getting branches for {repo_full_name}")
return []
except Exception as e:
logger.error(f"Unexpected error getting branches for {repo_full_name}: {str(e)}")
return []
if __name__ == "__main__":
import sys
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
if len(sys.argv) > 1:
token = sys.argv[1]
github = GitHubAPI(token)
logger.info("Testing GitHub API connection...")
if github.test_connection():
logger.info("Connection successful")
logger.info("Testing repository info retrieval...")
repo_info = github.get_repo_info("octocat/Hello-World")
if repo_info:
logger.info(f"Repository: {repo_info['full_name']}")
logger.info(f"Description: {repo_info.get('description', 'No description')}")
logger.info(f"Default branch: {repo_info.get('default_branch', 'main')}")
logger.info("Testing commits retrieval...")
commits = github.get_latest_commits("octocat/Hello-World", "main")
if commits:
logger.info(f"{len(commits)} commits retrieved")
logger.info(f"Latest commit: {commits[0]['message'][:50]}...")
logger.info("Testing rate limit status...")
rate_limit = github.get_rate_limit()
if rate_limit:
core = rate_limit.get('resources', {}).get('core', {})
remaining = core.get('remaining', 0)
limit = core.get('limit', 0)
logger.info(f"Rate limit remaining: {remaining}/{limit}")
else:
logger.error("Connection failed")
else:
logger.warning("Please provide a GitHub Token as an argument:")
logger.warning(" python github_api.py YOUR_GITHUB_TOKEN")