forked from pyvec/python.cz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
79 lines (58 loc) · 1.95 KB
/
Copy pathgithub.py
File metadata and controls
79 lines (58 loc) · 1.95 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
# -*- coding: utf-8 -*-
from datetime import datetime
import requests
from werkzeug.contrib.cache import FileSystemCache
from .. import app
__all__ = ('get_issues',)
cache = FileSystemCache(app.config['CACHE_DIR'], default_timeout=3600)
def get_issues(org_names):
issues = cache.get('github-issues')
if issues is None:
issues = []
for org_name in org_names:
for issue in _get_issues_for_org(org_name):
issues.append(_enhance_issue(issue))
issues = sorted(issues, key=_get_issue_sort_key, reverse=True)
cache.set('github-issues', issues)
return issues
def _get_issues_for_org(org_name):
now = datetime.now()
user_agent = ('pythoncz/{now.year}-{now.month} '
'(+https://python.cz)').format(now=now)
session = requests.Session()
session.headers.update({
'User-Agent': user_agent,
'Authorization': 'token {}'.format(app.config['GITHUB_TOKEN']),
})
search_conditions = [
'is:open',
'is:issue',
'org:{}'.format(org_name)
]
page = 1
while True:
res = session.get('https://api.github.com/search/issues', params={
'q': ' '.join(search_conditions),
'per_page': 100,
'page': page,
})
res.raise_for_status()
items = res.json().get('items', [])
if items:
yield from items
page += 1
else:
break
def _enhance_issue(issue):
repo_url_segments = issue['repository_url'].split('/')
repo_full_name = '{}/{}'.format(
repo_url_segments[-2],
repo_url_segments[-1]
)
issue['repository_name'] = repo_url_segments[-1]
issue['organization_name'] = repo_url_segments[-2]
issue['repository_full_name'] = repo_full_name
issue['repository_url_html'] = 'https://github.com/' + repo_full_name
return issue
def _get_issue_sort_key(issue):
return issue.get('updated_at')