Chromium Code Reviews| 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 logging | 7 import logging |
| 8 import urllib2 | 8 import urllib2 |
| 9 | 9 |
| 10 | 10 |
| 11 _log = logging.getLogger(__name__) | 11 _log = logging.getLogger(__name__) |
| 12 API_BASE = 'https://api.github.com' | 12 API_BASE = 'https://api.github.com' |
| 13 EXPORT_LABEL = 'chromium-export' | 13 EXPORT_LABEL = 'chromium-export' |
| 14 | 14 |
| 15 | 15 |
| 16 class WPTGitHub(object): | 16 class WPTGitHub(object): |
| 17 | 17 |
| 18 def __init__(self, host, user, token): | 18 def __init__(self, host, user, token): |
| 19 self.host = host | 19 self.host = host |
| 20 self.user = user | 20 self.user = user |
| 21 self.token = token | 21 self.token = token |
| 22 assert self.user and self.token | 22 assert self.user and self.token |
| 23 | 23 |
| 24 def auth_token(self): | 24 def auth_token(self): |
| 25 return base64.encodestring('{}:{}'.format(self.user, self.token)).strip( ) | 25 return base64.encodestring('{}:{}'.format(self.user, self.token)).replac e('\n', '') |
|
qyearsley
2017/02/13 23:29:12
Or, as just discussed: base64.b64encode('{}:{}'.fo
| |
| 26 | 26 |
| 27 def request(self, path, method, body=None): | 27 def request(self, path, method, body=None): |
| 28 assert path.startswith('/') | 28 assert path.startswith('/') |
| 29 if body: | 29 if body: |
| 30 body = json.dumps(body) | 30 body = json.dumps(body) |
| 31 opener = urllib2.build_opener(urllib2.HTTPHandler) | 31 opener = urllib2.build_opener(urllib2.HTTPHandler) |
| 32 request = urllib2.Request(url=API_BASE + path, data=body) | 32 request = urllib2.Request(url=API_BASE + path, data=body) |
| 33 request.add_header('Accept', 'application/vnd.github.v3+json') | 33 request.add_header('Accept', 'application/vnd.github.v3+json') |
| 34 request.add_header('Authorization', 'Basic {}'.format(self.auth_token()) ) | 34 request.add_header('Authorization', 'Basic {}'.format(self.auth_token()) ) |
| 35 request.get_method = lambda: method | 35 request.get_method = lambda: method |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 def delete_remote_branch(self, remote_branch_name): | 96 def delete_remote_branch(self, remote_branch_name): |
| 97 # TODO(jeffcarp): Unit test this method | 97 # TODO(jeffcarp): Unit test this method |
| 98 path = '/repos/w3c/web-platform-tests/git/refs/heads/%s' % remote_branch _name | 98 path = '/repos/w3c/web-platform-tests/git/refs/heads/%s' % remote_branch _name |
| 99 data, status_code = self.request(path, method='DELETE') | 99 data, status_code = self.request(path, method='DELETE') |
| 100 | 100 |
| 101 if status_code != 204: | 101 if status_code != 204: |
| 102 # TODO(jeffcarp): Raise more specific exception (create MergeError c lass?) | 102 # TODO(jeffcarp): Raise more specific exception (create MergeError c lass?) |
| 103 raise Exception('Received non-204 status code attempting to delete r emote branch: {}'.format(status_code)) | 103 raise Exception('Received non-204 status code attempting to delete r emote branch: {}'.format(status_code)) |
| 104 | 104 |
| 105 return data | 105 return data |
| OLD | NEW |