-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.py
More file actions
215 lines (195 loc) · 9.02 KB
/
github_api.py
File metadata and controls
215 lines (195 loc) · 9.02 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
#!/usr/bin/env python3
import base64
import requests
import os
class GitHubAPI:
"""GitHub API wrapper with comprehensive functionality."""
def __init__(self, token):
self.token = token
self.headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json",
}
def validate_token(self):
"""Validate the token by fetching the user profile."""
try:
r = requests.get("https://api.github.com/user", headers=self.headers)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}: Token invalid"
except Exception as e:
return False, str(e)
def get_user_info(self, username):
"""Get information about a GitHub user."""
try:
r = requests.get(f"https://api.github.com/users/{username}", headers=self.headers)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}"
except Exception as e:
return False, str(e)
def search_users(self, query):
"""Search for GitHub users by username."""
try:
r = requests.get(f"https://api.github.com/search/users?q={query}", headers=self.headers)
if r.status_code == 200:
return True, r.json().get('items', [])
return False, f"Error {r.status_code}"
except Exception as e:
return False, str(e)
def follow_user(self, user):
"""Follow a GitHub user."""
try:
r = requests.put(f"https://api.github.com/user/following/{user}", headers=self.headers)
return (r.status_code == 204), (f"Followed {user}" if r.status_code == 204 else f"Error {r.status_code}")
except Exception as e:
return False, str(e)
def unfollow_user(self, user):
"""Unfollow a GitHub user."""
try:
r = requests.delete(f"https://api.github.com/user/following/{user}", headers=self.headers)
return (r.status_code == 204), (f"Unfollowed {user}" if r.status_code == 204 else f"Error {r.status_code}")
except Exception as e:
return False, str(e)
def star_repo(self, owner, repo):
"""Star a GitHub repository."""
try:
r = requests.put(f"https://api.github.com/user/starred/{owner}/{repo}", headers=self.headers)
return (r.status_code == 204), (f"Starred {owner}/{repo}" if r.status_code == 204 else f"Error {r.status_code}")
except Exception as e:
return False, str(e)
def unstar_repo(self, owner, repo):
"""Unstar a GitHub repository."""
try:
r = requests.delete(f"https://api.github.com/user/starred/{owner}/{repo}", headers=self.headers)
return (r.status_code == 204), (f"Unstarred {owner}/{repo}" if r.status_code == 204 else f"Error {r.status_code}")
except Exception as e:
return False, str(e)
def get_following(self):
"""Get list of users being followed."""
try:
r = requests.get("https://api.github.com/user/following", headers=self.headers)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}"
except Exception as e:
return False, str(e)
def get_repos(self):
"""Get list of user repositories."""
try:
r = requests.get("https://api.github.com/user/repos?per_page=100", headers=self.headers)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}"
except Exception as e:
return False, str(e)
def create_repo(self, name, desc, private):
"""Create a new repository."""
data = {"name": name, "description": desc, "private": private}
try:
r = requests.post("https://api.github.com/user/repos", headers=self.headers, json=data)
if r.status_code == 201:
return True, r.json()
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)
def upload_file(self, owner, repo, path, content):
"""Upload a file to a repository."""
try:
enc = base64.b64encode(content).decode()
fn = os.path.basename(path)
up_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{fn}"
data = {"message": f"Add {fn}", "content": enc}
r = requests.put(up_url, headers=self.headers, json=data)
if r.status_code in [200, 201]:
return True, f"Uploaded {fn}"
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)
def get_contents(self, owner, repo, path=""):
"""Get contents of path within the repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
try:
r = requests.get(url, headers=self.headers)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)
def update_file(self, owner, repo, path, message, new_content, sha):
"""Update an existing file in a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
data = {
"message": message,
"content": base64.b64encode(new_content.encode()).decode(),
"sha": sha
}
try:
r = requests.put(url, headers=self.headers, json=data)
if r.status_code in [200, 201]:
return True, r.json()
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)
def delete_file(self, owner, repo, path, message, sha):
"""Delete a file in a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
data = {"message": message, "sha": sha}
try:
r = requests.delete(url, headers=self.headers, json=data)
if r.status_code == 200:
return True, f"File '{path}' deleted"
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)
def enable_wiki(self, owner, repo):
"""Enable wiki for a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}"
data = {"has_wiki": True}
r = requests.patch(url, headers=self.headers, json=data)
if r.status_code == 200:
return True, "Wiki enabled."
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
def disable_wiki(self, owner, repo):
"""Disable wiki for a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}"
data = {"has_wiki": False}
r = requests.patch(url, headers=self.headers, json=data)
if r.status_code == 200:
return True, "Wiki disabled."
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
def create_branch(self, owner, repo, branch_name, base_sha):
"""Create a new branch in a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/git/refs"
data = {
"ref": f"refs/heads/{branch_name}",
"sha": base_sha
}
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 201:
return True, f"Branch '{branch_name}' created."
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
def delete_branch(self, owner, repo, branch_name):
"""Delete a branch in a repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/git/refs/heads/{branch_name}"
r = requests.delete(url, headers=self.headers)
if r.status_code == 204:
return True, f"Branch '{branch_name}' deleted."
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
def update_profile(self, name, bio, company, location, blog):
"""Update user profile information."""
url = "https://api.github.com/user"
data = {
"name": name,
"bio": bio,
"company": company,
"location": location,
"blog": blog
}
try:
r = requests.patch(url, headers=self.headers, json=data)
if r.status_code == 200:
return True, r.json()
return False, f"Error {r.status_code}: {r.json().get('message', '')}"
except Exception as e:
return False, str(e)