forked from Harmageddon/gh-file-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.py
More file actions
64 lines (38 loc) · 1.46 KB
/
github_api.py
File metadata and controls
64 lines (38 loc) · 1.46 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
import requests
from datetime import datetime
import os.path
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
headers = {}
if os.path.exists('.oauth-token'):
with open('.oauth-token', 'r') as fp:
headers['Authorization'] = 'token ' + fp.read()
def get_prs(repo, branch, page=1):
url = 'https://api.github.com/repos/{}/pulls?base={}&sort=updated&direction=desc&page={:d}'.format(repo, branch, page)
return api_call(url)
def get_pr_files(repo, number):
url = 'https://api.github.com/repos/{}/pulls/{:d}/files'.format(repo, number)
return api_call(url)
def get_prs_since(repository, branch, since):
prs = get_prs(repository, branch)
i_filtered = 0
page = 1
while datetime.strptime(prs[i_filtered]['updated_at'], DATE_FORMAT) > since:
i_filtered += 1
if i_filtered >= len(prs):
page += 1
response = get_prs(repository, branch, page)
if len(response) == 0:
break
prs.extend(response)
return prs[:i_filtered]
def get_file(repo, file):
url = 'https://api.github.com/repos/{}/contents/{}'.format(repo, file)
return api_call(url)
def get_file_commits(repo, file):
url = 'https://api.github.com/repos/{}/commits?path={}'.format(repo, file)
return api_call(url)
def api_call(url):
response = requests.get(url, headers=headers)
if response.status_code >= 400:
raise PermissionError(response.json()['message'])
return response.json()