-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.py
53 lines (44 loc) · 1.58 KB
/
github.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
import os
import requests
github_auth = (None, 'x-oauth-basic')
github_api_url = "https://api.github.com"
def set_config(api_key, api_url):
global github_auth
global github_api_url
if api_key is not None:
github_auth = (api_key, github_auth[1])
if api_url is not None:
github_api_url = api_url
all_issues_url = '%s/repos/%s/%s/issues?per_page=100&state=all'
all_commits_url = '%s/repos/%s/%s/commits?per_page=100'
all_pull_requests_url = '%s/repos/%s/%s/pulls?per_page=100&state=all'
all_issues_cache = {}
def get_paged_data(url, auth=None):
data = []
current_url = url
headers = {
'accept': 'application/vnd.github.v3+json',
'user-agent': 'https://github.com/jschementi/changelog'
}
while True:
r = requests.get(current_url, auth=auth, headers=headers)
r.raise_for_status()
data.extend(r.json())
if not ('next' in r.links):
break
current_url = r.links['next']['url']
return data
def get_all_issues(owner, repo):
global all_issues_cache
cache_key = "%s/%s" % (owner, repo)
if not cache_key in all_issues_cache:
all_issues = get_paged_data(all_issues_url % (github_api_url, owner, repo), auth=github_auth)
all_issues_cache[cache_key] = all_issues
return all_issues_cache[cache_key]
def get_issue_index(issues):
def index_issue_by_number(m, v):
m[v['number']] = v
return m
return reduce(index_issue_by_number, issues, {})
def get_repo_path(repo_url):
return os.path.splitext(repo_url)[0].split('github.com')[1][1:].split('/')