| 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' |
| 12 EXPORT_LABEL = 'chromium-export' |
| 11 | 13 |
| 12 | 14 |
| 13 class GitHub(object): | 15 class WPTGitHub(object): |
| 14 | 16 |
| 15 def __init__(self, host): | 17 def __init__(self, host): |
| 16 self.host = host | 18 self.host = host |
| 17 self.user = self.host.environ.get('GH_USER') | 19 self.user = self.host.environ.get('GH_USER') |
| 18 self.token = self.host.environ.get('GH_TOKEN') | 20 self.token = self.host.environ.get('GH_TOKEN') |
| 19 | 21 |
| 20 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' |
| 21 | 23 |
| 22 def auth_token(self): | 24 def auth_token(self): |
| 23 return base64.encodestring('{}:{}'.format(self.user, self.token)) | 25 return base64.encodestring('{}:{}'.format(self.user, self.token)) |
| 24 | 26 |
| 25 def create_pr(self, local_branch_name, desc_title, body): | 27 def create_pr(self, local_branch_name, desc_title, body): |
| 26 """Creates a PR on GitHub. | 28 """Creates a PR on GitHub. |
| 27 | 29 |
| 28 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 |
| 29 | 31 |
| 30 Returns: | 32 Returns: |
| 31 A raw response object if successful, None if not. | 33 A raw response object if successful, None if not. |
| 32 """ | 34 """ |
| 33 assert local_branch_name | 35 assert local_branch_name |
| 34 assert desc_title | 36 assert desc_title |
| 35 assert body | 37 assert body |
| 36 | 38 |
| 37 pr_branch_name = '{}:{}'.format(self.user, local_branch_name) | 39 pr_branch_name = '{}:{}'.format(self.user, local_branch_name) |
| 38 | 40 |
| 41 # TODO(jeffcarp): CC foolip and qyearsley on all PRs for now |
| 39 # TODO(jeffcarp): add HTTP to Host and use that here | 42 # TODO(jeffcarp): add HTTP to Host and use that here |
| 40 conn = httplib2.Http() | 43 conn = httplib2.Http() |
| 41 headers = { | 44 headers = { |
| 42 "Accept": "application/vnd.github.v3+json", | 45 "Accept": "application/vnd.github.v3+json", |
| 43 "Authorization": "Basic " + self.auth_token() | 46 "Authorization": "Basic " + self.auth_token() |
| 44 } | 47 } |
| 45 body = { | 48 body = { |
| 46 "title": desc_title, | 49 "title": desc_title, |
| 47 "body": body, | 50 "body": body, |
| 48 "head": pr_branch_name, | 51 "head": pr_branch_name, |
| 49 "base": "master" | 52 "base": 'master', |
| 53 "labels": [EXPORT_LABEL] |
| 50 } | 54 } |
| 51 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf
orm-tests/pulls", | 55 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf
orm-tests/pulls", |
| 52 "POST", body=json.JSONEncoder().encode(body
), headers=headers) | 56 "POST", body=json.JSONEncoder().encode(body
), headers=headers) |
| 53 _log.info("GitHub response: %s", content) | 57 _log.info("GitHub response: %s", content) |
| 54 if resp["status"] != "201": | 58 if resp["status"] != "201": |
| 55 return None | 59 return None |
| 56 return json.loads(content) | 60 return json.loads(content) |
| 61 |
| 62 def in_flight_pull_requests(self): |
| 63 url_encoded_label = EXPORT_LABEL.replace(' ', '%20') |
| 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) |
| 66 if response['status'] == '200': |
| 67 data = json.loads(content) |
| 68 return data['items'] |
| 69 else: |
| 70 raise Exception('Non-200 status code (%s): %s' % (response['status']
, content)) |
| 71 |
| 72 def request(self, path, body=None): |
| 73 assert path.startswith('/') |
| 74 |
| 75 # Not used yet since only hitting public API |
| 76 # headers = { |
| 77 # "Accept": "application/vnd.github.v3+json", |
| 78 # "Authorization": "Basic " + self.auth_token() |
| 79 # } |
| 80 |
| 81 if body: |
| 82 json_body = json.dumps(body) |
| 83 else: |
| 84 json_body = None |
| 85 |
| 86 conn = httplib2.Http() |
| 87 return conn.request(API_BASE + path, body=json_body) |
| 88 |
| 89 def merge_pull_request(self, pull_request_number): |
| 90 path = '/repos/w3c/web-platform-tests/pulls/%d/merge' % pull_request_num
ber |
| 91 body = {} |
| 92 response, content = self.request(path, body) |
| 93 |
| 94 if response['status'] == '200': |
| 95 return json.loads(content) |
| 96 else: |
| 97 raise Exception('PR could not be merged: %d' % pull_request_number) |
| OLD | NEW |