OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import base64 | 5 import base64 |
6 import json | 6 import json |
7 import httplib2 | 7 import httplib2 |
8 import logging | 8 import logging |
9 | 9 |
10 _log = logging.getLogger(__name__) | 10 _log = logging.getLogger(__name__) |
11 API_BASE = 'https://api.github.com' | 11 API_BASE = 'https://api.github.com' |
12 EXPORT_LABEL = 'chromium-export' | 12 EXPORT_LABEL = 'chromium-export' |
13 | 13 |
14 | 14 |
15 class WPTGitHub(object): | 15 class WPTGitHub(object): |
16 | 16 |
17 def __init__(self, host): | 17 def __init__(self, host): |
18 self.host = host | 18 self.host = host |
19 self.user = self.host.environ.get('GH_USER') | 19 self.user = self.host.environ.get('GH_USER') |
20 self.token = self.host.environ.get('GH_TOKEN') | 20 self.token = self.host.environ.get('GH_TOKEN') |
21 | 21 |
22 assert self.user and self.token, 'must have GH_USER and GH_TOKEN env var
s' | 22 assert self.user and self.token, 'must have GH_USER and GH_TOKEN env var
s' |
23 | 23 |
24 def auth_token(self): | 24 def auth_token(self): |
25 return base64.encodestring('{}:{}'.format(self.user, self.token)) | 25 return base64.encodestring('{}:{}'.format(self.user, self.token)) |
26 | 26 |
27 def create_pr(self, local_branch_name, desc_title, body): | 27 def create_pr(self, remote_branch_name, desc_title, body): |
28 """Creates a PR on GitHub. | 28 """Creates a PR on GitHub. |
29 | 29 |
30 API doc: https://developer.github.com/v3/pulls/#create-a-pull-request | 30 API doc: https://developer.github.com/v3/pulls/#create-a-pull-request |
31 | 31 |
32 Returns: | 32 Returns: |
33 A raw response object if successful, None if not. | 33 A raw response object if successful, None if not. |
34 """ | 34 """ |
35 assert local_branch_name | 35 assert remote_branch_name |
36 assert desc_title | 36 assert desc_title |
37 assert body | 37 assert body |
38 | 38 |
39 pr_branch_name = '{}:{}'.format(self.user, local_branch_name) | |
40 | |
41 # TODO(jeffcarp): CC foolip and qyearsley on all PRs for now | 39 # TODO(jeffcarp): CC foolip and qyearsley on all PRs for now |
42 # TODO(jeffcarp): add HTTP to Host and use that here | 40 # TODO(jeffcarp): add HTTP to Host and use that here |
43 conn = httplib2.Http() | 41 conn = httplib2.Http() |
44 headers = { | 42 headers = { |
45 "Accept": "application/vnd.github.v3+json", | 43 "Accept": "application/vnd.github.v3+json", |
46 "Authorization": "Basic " + self.auth_token() | 44 "Authorization": "Basic " + self.auth_token() |
47 } | 45 } |
48 body = { | 46 body = { |
49 "title": desc_title, | 47 "title": desc_title, |
50 "body": body, | 48 "body": body, |
51 "head": pr_branch_name, | 49 "head": remote_branch_name, |
52 "base": 'master', | 50 "base": 'master', |
| 51 # TODO(jeffcarp): it looks like this is not being added |
53 "labels": [EXPORT_LABEL] | 52 "labels": [EXPORT_LABEL] |
54 } | 53 } |
55 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf
orm-tests/pulls", | 54 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf
orm-tests/pulls", |
56 "POST", body=json.JSONEncoder().encode(body
), headers=headers) | 55 "POST", body=json.JSONEncoder().encode(body
), headers=headers) |
57 _log.info("GitHub response: %s", content) | 56 _log.info("GitHub response: %s", content) |
58 if resp["status"] != "201": | 57 if resp["status"] != "201": |
59 return None | 58 return None |
60 return json.loads(content) | 59 return json.loads(content) |
61 | 60 |
62 def in_flight_pull_requests(self): | 61 def in_flight_pull_requests(self): |
63 url_encoded_label = EXPORT_LABEL.replace(' ', '%20') | 62 path = '/search/issues?q=repo:w3c/web-platform-tests%20is:open%20type:pr
%20label:{}'.format(EXPORT_LABEL) |
64 path = '/search/issues?q=repo:w3c/web-platform-tests%20is:open%20type:pr
%20labels:{}'.format(url_encoded_label) | |
65 response, content = self.request(path) | 63 response, content = self.request(path) |
66 if response['status'] == '200': | 64 if response['status'] == '200': |
67 data = json.loads(content) | 65 data = json.loads(content) |
68 return data['items'] | 66 return data['items'] |
69 else: | 67 else: |
70 raise Exception('Non-200 status code (%s): %s' % (response['status']
, content)) | 68 raise Exception('Non-200 status code (%s): %s' % (response['status']
, content)) |
71 | 69 |
72 def request(self, path, body=None): | 70 def request(self, path, body=None, method='GET'): |
73 assert path.startswith('/') | 71 assert path.startswith('/') |
74 | 72 |
75 # Not used yet since only hitting public API | 73 headers = { |
76 # headers = { | 74 "Accept": "application/vnd.github.v3+json", |
77 # "Accept": "application/vnd.github.v3+json", | 75 "Authorization": "Basic " + self.auth_token() |
78 # "Authorization": "Basic " + self.auth_token() | 76 } |
79 # } | |
80 | 77 |
81 if body: | 78 if body: |
82 json_body = json.dumps(body) | 79 json_body = json.dumps(body) |
83 else: | 80 else: |
84 json_body = None | 81 json_body = None |
85 | 82 |
86 conn = httplib2.Http() | 83 conn = httplib2.Http() |
87 return conn.request(API_BASE + path, body=json_body) | 84 return conn.request(API_BASE + path, method, body=json_body, headers=hea
ders) |
88 | 85 |
89 def merge_pull_request(self, pull_request_number): | 86 def merge_pull_request(self, pull_request_number): |
90 path = '/repos/w3c/web-platform-tests/pulls/%d/merge' % pull_request_num
ber | 87 path = '/repos/w3c/web-platform-tests/pulls/%d/merge' % pull_request_num
ber |
91 body = {} | 88 body = {} |
92 response, content = self.request(path, body) | 89 response, content = self.request(path, body=body, method='PUT') |
93 | 90 |
94 if response['status'] == '200': | 91 if response['status'] == '200': |
95 return json.loads(content) | 92 return json.loads(content) |
96 else: | 93 else: |
97 raise Exception('PR could not be merged: %d' % pull_request_number) | 94 raise Exception('PR could not be merged: %d' % pull_request_number) |
OLD | NEW |