Show More
@@ -1,104 +1,104 b'' | |||||
1 | """Functions for Github authorisation.""" |
|
1 | """Functions for Github authorisation.""" | |
2 | from __future__ import print_function |
|
2 | from __future__ import print_function | |
3 |
|
3 | |||
4 | try: |
|
4 | try: | |
5 | input = raw_input |
|
5 | input = raw_input | |
6 | except NameError: |
|
6 | except NameError: | |
7 | pass |
|
7 | pass | |
8 |
|
8 | |||
9 | import requests |
|
9 | import requests | |
10 | import getpass |
|
10 | import getpass | |
11 | import json |
|
11 | import json | |
12 |
|
12 | |||
13 | # Keyring stores passwords by a 'username', but we're not storing a username and |
|
13 | # Keyring stores passwords by a 'username', but we're not storing a username and | |
14 | # password |
|
14 | # password | |
15 | fake_username = 'ipython_tools' |
|
15 | fake_username = 'ipython_tools' | |
16 |
|
16 | |||
17 | token = None |
|
17 | token = None | |
18 | def get_auth_token(): |
|
18 | def get_auth_token(): | |
19 | global token |
|
19 | global token | |
20 |
|
20 | |||
21 | if token is not None: |
|
21 | if token is not None: | |
22 | return token |
|
22 | return token | |
23 |
|
23 | |||
24 | import keyring |
|
24 | import keyring | |
25 | token = keyring.get_password('github', fake_username) |
|
25 | token = keyring.get_password('github', fake_username) | |
26 | if token is not None: |
|
26 | if token is not None: | |
27 | return token |
|
27 | return token | |
28 |
|
28 | |||
29 | print("Please enter your github username and password. These are not " |
|
29 | print("Please enter your github username and password. These are not " | |
30 | "stored, only used to get an oAuth token. You can revoke this at " |
|
30 | "stored, only used to get an oAuth token. You can revoke this at " | |
31 | "any time on Github.") |
|
31 | "any time on Github.") | |
32 | user = input("Username: ") |
|
32 | user = input("Username: ") | |
33 | pw = getpass.getpass("Password: ") |
|
33 | pw = getpass.getpass("Password: ") | |
34 |
|
34 | |||
35 | auth_request = { |
|
35 | auth_request = { | |
36 | "scopes": [ |
|
36 | "scopes": [ | |
37 | "public_repo", |
|
37 | "public_repo", | |
38 | "gist" |
|
38 | "gist" | |
39 | ], |
|
39 | ], | |
40 | "note": "IPython tools", |
|
40 | "note": "IPython tools", | |
41 | "note_url": "https://github.com/ipython/ipython/tree/master/tools", |
|
41 | "note_url": "https://github.com/ipython/ipython/tree/master/tools", | |
42 | } |
|
42 | } | |
43 | response = requests.post('https://api.github.com/authorizations', |
|
43 | response = requests.post('https://api.github.com/authorizations', | |
44 | auth=(user, pw), data=json.dumps(auth_request)) |
|
44 | auth=(user, pw), data=json.dumps(auth_request)) | |
45 | response.raise_for_status() |
|
45 | response.raise_for_status() | |
46 | token = json.loads(response.text)['token'] |
|
46 | token = json.loads(response.text)['token'] | |
47 | keyring.set_password('github', fake_username, token) |
|
47 | keyring.set_password('github', fake_username, token) | |
48 | return token |
|
48 | return token | |
49 |
|
49 | |||
50 | def make_auth_header(): |
|
50 | def make_auth_header(): | |
51 | return {'Authorization': 'token ' + get_auth_token()} |
|
51 | return {'Authorization': 'token ' + get_auth_token()} | |
52 |
|
52 | |||
53 | def post_issue_comment(project, num, body): |
|
53 | def post_issue_comment(project, num, body): | |
54 | url = 'https://api.github.com/repos/{project}/issues/{num}/comments'.format(project=project, num=num) |
|
54 | url = 'https://api.github.com/repos/{project}/issues/{num}/comments'.format(project=project, num=num) | |
55 | payload = json.dumps({'body': body}) |
|
55 | payload = json.dumps({'body': body}) | |
56 | r = requests.post(url, data=payload, headers=make_auth_header()) |
|
56 | r = requests.post(url, data=payload, headers=make_auth_header()) | |
57 |
|
57 | |||
58 | def post_gist(content, description='', filename='file', auth=False): |
|
58 | def post_gist(content, description='', filename='file', auth=False): | |
59 | """Post some text to a Gist, and return the URL.""" |
|
59 | """Post some text to a Gist, and return the URL.""" | |
60 | post_data = json.dumps({ |
|
60 | post_data = json.dumps({ | |
61 | "description": description, |
|
61 | "description": description, | |
62 | "public": True, |
|
62 | "public": True, | |
63 | "files": { |
|
63 | "files": { | |
64 | filename: { |
|
64 | filename: { | |
65 | "content": content |
|
65 | "content": content | |
66 | } |
|
66 | } | |
67 | } |
|
67 | } | |
68 | }).encode('utf-8') |
|
68 | }).encode('utf-8') | |
69 |
|
69 | |||
70 | headers = make_auth_header() if auth else {} |
|
70 | headers = make_auth_header() if auth else {} | |
71 | response = requests.post("https://api.github.com/gists", data=post_data, headers=headers) |
|
71 | response = requests.post("https://api.github.com/gists", data=post_data, headers=headers) | |
72 | response.raise_for_status() |
|
72 | response.raise_for_status() | |
73 | response_data = json.loads(response.text) |
|
73 | response_data = json.loads(response.text) | |
74 | return response_data['html_url'] |
|
74 | return response_data['html_url'] | |
75 |
|
75 | |||
76 | def get_pull_request(project, num, github_api=3): |
|
76 | def get_pull_request(project, num, github_api=3): | |
77 | """get pull request info by number |
|
77 | """get pull request info by number | |
78 |
|
78 | |||
79 | github_api : version of github api to use |
|
79 | github_api : version of github api to use | |
80 | """ |
|
80 | """ | |
81 | if github_api==2 : |
|
81 | if github_api==2 : | |
82 | url = "http://github.com/api/v2/json/pulls/{project}/{num}".format(project=project, num=num) |
|
82 | url = "http://github.com/api/v2/json/pulls/{project}/{num}".format(project=project, num=num) | |
83 | elif github_api == 3: |
|
83 | elif github_api == 3: | |
84 | url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num) |
|
84 | url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num) | |
85 | response = requests.get(url) |
|
85 | response = requests.get(url) | |
86 | response.raise_for_status() |
|
86 | response.raise_for_status() | |
87 |
if |
|
87 | if github_api == 2 : | |
88 | return json.loads(response.text)['pull'] |
|
88 | return json.loads(response.text)['pull'] | |
89 | return json.loads(response.text) |
|
89 | return json.loads(response.text) | |
90 |
|
90 | |||
91 | def get_pulls_list(project, github_api=3): |
|
91 | def get_pulls_list(project, github_api=3): | |
92 | """get pull request list |
|
92 | """get pull request list | |
93 |
|
93 | |||
94 | github_api : version of github api to use |
|
94 | github_api : version of github api to use | |
95 | """ |
|
95 | """ | |
96 | if github_api == 3 : |
|
96 | if github_api == 3 : | |
97 | url = "https://api.github.com/repos/{project}/pulls".format(project=project) |
|
97 | url = "https://api.github.com/repos/{project}/pulls".format(project=project) | |
98 | else : |
|
98 | else : | |
99 | url = "http://github.com/api/v2/json/pulls/{project}".format(project=project) |
|
99 | url = "http://github.com/api/v2/json/pulls/{project}".format(project=project) | |
100 | response = requests.get(url) |
|
100 | response = requests.get(url) | |
101 | response.raise_for_status() |
|
101 | response.raise_for_status() | |
102 |
if |
|
102 | if github_api == 2 : | |
103 | return json.loads(response.text)['pulls'] |
|
103 | return json.loads(response.text)['pulls'] | |
104 | return json.loads(response.text) |
|
104 | return json.loads(response.text) |
General Comments 0
You need to be logged in to leave comments.
Login now