-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_client.py
69 lines (54 loc) · 2.13 KB
/
github_client.py
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
import base64
import requests
import os
class GithubClient(object):
def __init__(self):
token = os.environ.get("GITHUB_TOKEN")
self.session = requests.Session()
if len(token):
self.session.headers.update({'Authorization': f'Bearer {token}'})
self.base_url = 'https://api.github.com'
def _get(self, url, params=None):
try:
return self.session.get(url, params=params)
except Exception as e:
print(e)
def get_most_recent_commit(self, repo: str, timestamp: str, branch: str) -> requests.models.Response:
api_url = f"{self.base_url}/repos/{repo}/commits"
params = {
"per_page": 1,
"until": timestamp,
"order": "desc",
"sha": branch
}
response = self._get(api_url, params=params)
if response.status_code == 200:
commits = response.json()
if len(commits) > 0:
most_recent_commit = commits[0]
return most_recent_commit['sha']
else:
print("No commits found.")
return None
else:
print(f"Error: {response.status_code}")
return None
def get_repository_at_commit(self, repository: str, commit_sha: str):
api_url = f"{self.base_url}/repos/{repository}/git/trees/{commit_sha}?recursive=1"
response = self._get(api_url)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code}")
return None
def get_file_at_commit(self, repository: str, filepath: str, commit_sha: str):
api_url = f"{self.base_url}/repos/{repository}/contents/{filepath}"
response = self._get(api_url, params={"ref": commit_sha})
if response.status_code == 200:
# File content is base64 encoded, decode it
content = response.json().get("content", "")
content = base64.b64decode(content)
return str(content, encoding='utf-8')
else:
print(f"Error: {response.status_code}")
return None