Show More
@@ -1,141 +1,141 | |||
|
1 | 1 | """Functions for Github API requests.""" |
|
2 | 2 | |
|
3 | 3 | try: |
|
4 | 4 | input = raw_input |
|
5 | 5 | except NameError: |
|
6 | 6 | pass |
|
7 | 7 | |
|
8 | 8 | import re |
|
9 | 9 | import sys |
|
10 | 10 | |
|
11 | 11 | import requests |
|
12 | 12 | import getpass |
|
13 | 13 | import json |
|
14 | 14 | |
|
15 | 15 | try: |
|
16 | 16 | import requests_cache |
|
17 | 17 | except ImportError: |
|
18 | 18 | print("cache not available, install `requests_cache` for caching.", file=sys.stderr) |
|
19 | 19 | else: |
|
20 | 20 | requests_cache.install_cache("gh_api", expire_after=3600) |
|
21 | 21 | |
|
22 | 22 | # Keyring stores passwords by a 'username', but we're not storing a username and |
|
23 | 23 | # password |
|
24 | 24 | import socket |
|
25 | 25 | fake_username = 'ipython_tools_%s' % socket.gethostname().replace('.','_').replace('-','_') |
|
26 | 26 | |
|
27 | 27 | class Obj(dict): |
|
28 | 28 | """Dictionary with attribute access to names.""" |
|
29 | 29 | def __getattr__(self, name): |
|
30 | 30 | try: |
|
31 | 31 | return self[name] |
|
32 | 32 | except KeyError as e: |
|
33 | 33 | raise AttributeError(name) from e |
|
34 | 34 | |
|
35 | 35 | def __setattr__(self, name, val): |
|
36 | 36 | self[name] = val |
|
37 | 37 | |
|
38 | 38 | token = None |
|
39 | 39 | def get_auth_token(): |
|
40 | 40 | global token |
|
41 | 41 | |
|
42 | 42 | if token is not None: |
|
43 | 43 | return token |
|
44 | 44 | |
|
45 | 45 | import keyring |
|
46 | 46 | token = keyring.get_password('github', fake_username) |
|
47 | 47 | if token is not None: |
|
48 | 48 | return token |
|
49 | 49 | |
|
50 | 50 | print( |
|
51 | "Get a token fom https://github.com/settings/tokens with public repo and gist." | |
|
51 | "Get a token from https://github.com/settings/tokens with public repo and gist." | |
|
52 | 52 | ) |
|
53 | 53 | token = getpass.getpass("Token: ", stream=sys.stderr) |
|
54 | 54 | |
|
55 | 55 | keyring.set_password('github', fake_username, token) |
|
56 | 56 | return token |
|
57 | 57 | |
|
58 | 58 | def make_auth_header(): |
|
59 | 59 | return {'Authorization': 'token ' + get_auth_token()} |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | def get_pull_request(project, num, auth=False): |
|
63 | 63 | """get pull request info by number |
|
64 | 64 | """ |
|
65 | 65 | url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num) |
|
66 | 66 | if auth: |
|
67 | 67 | header = make_auth_header() |
|
68 | 68 | else: |
|
69 | 69 | header = None |
|
70 | 70 | print("fetching %s" % url, file=sys.stderr) |
|
71 | 71 | response = requests.get(url, headers=header) |
|
72 | 72 | response.raise_for_status() |
|
73 | 73 | return json.loads(response.text, object_hook=Obj) |
|
74 | 74 | |
|
75 | 75 | element_pat = re.compile(r'<(.+?)>') |
|
76 | 76 | rel_pat = re.compile(r'rel=[\'"](\w+)[\'"]') |
|
77 | 77 | |
|
78 | 78 | def get_paged_request(url, headers=None, **params): |
|
79 | 79 | """get a full list, handling APIv3's paging""" |
|
80 | 80 | results = [] |
|
81 | 81 | params.setdefault("per_page", 100) |
|
82 | 82 | while True: |
|
83 | 83 | if '?' in url: |
|
84 | 84 | params = None |
|
85 | 85 | print("fetching %s" % url, file=sys.stderr) |
|
86 | 86 | else: |
|
87 | 87 | print("fetching %s with %s" % (url, params), file=sys.stderr) |
|
88 | 88 | response = requests.get(url, headers=headers, params=params) |
|
89 | 89 | response.raise_for_status() |
|
90 | 90 | results.extend(response.json()) |
|
91 | 91 | if 'next' in response.links: |
|
92 | 92 | url = response.links['next']['url'] |
|
93 | 93 | else: |
|
94 | 94 | break |
|
95 | 95 | return results |
|
96 | 96 | |
|
97 | 97 | def get_issues_list(project, auth=False, **params): |
|
98 | 98 | """get issues list""" |
|
99 | 99 | params.setdefault("state", "closed") |
|
100 | 100 | url = "https://api.github.com/repos/{project}/issues".format(project=project) |
|
101 | 101 | if auth: |
|
102 | 102 | headers = make_auth_header() |
|
103 | 103 | else: |
|
104 | 104 | headers = None |
|
105 | 105 | pages = get_paged_request(url, headers=headers, **params) |
|
106 | 106 | return pages |
|
107 | 107 | |
|
108 | 108 | def get_milestones(project, auth=False, **params): |
|
109 | 109 | params.setdefault('state', 'all') |
|
110 | 110 | url = "https://api.github.com/repos/{project}/milestones".format(project=project) |
|
111 | 111 | if auth: |
|
112 | 112 | headers = make_auth_header() |
|
113 | 113 | else: |
|
114 | 114 | headers = None |
|
115 | 115 | milestones = get_paged_request(url, headers=headers, **params) |
|
116 | 116 | return milestones |
|
117 | 117 | |
|
118 | 118 | def get_milestone_id(project, milestone, auth=False, **params): |
|
119 | 119 | milestones = get_milestones(project, auth=auth, **params) |
|
120 | 120 | for mstone in milestones: |
|
121 | 121 | if mstone['title'] == milestone: |
|
122 | 122 | return mstone['number'] |
|
123 | 123 | else: |
|
124 | 124 | raise ValueError("milestone %s not found" % milestone) |
|
125 | 125 | |
|
126 | 126 | def is_pull_request(issue): |
|
127 | 127 | """Return True if the given issue is a pull request.""" |
|
128 | 128 | return bool(issue.get('pull_request', {}).get('html_url', None)) |
|
129 | 129 | |
|
130 | 130 | def get_authors(pr): |
|
131 | 131 | print("getting authors for #%i" % pr['number'], file=sys.stderr) |
|
132 | 132 | h = make_auth_header() |
|
133 | 133 | r = requests.get(pr['commits_url'], headers=h) |
|
134 | 134 | r.raise_for_status() |
|
135 | 135 | commits = r.json() |
|
136 | 136 | authors = [] |
|
137 | 137 | for commit in commits: |
|
138 | 138 | author = commit['commit']['author'] |
|
139 | 139 | authors.append("%s <%s>" % (author['name'], author['email'])) |
|
140 | 140 | return authors |
|
141 | 141 |
General Comments 0
You need to be logged in to leave comments.
Login now