| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import base64 | |
| 6 import json | |
| 7 import httplib2 | |
| 8 import logging | |
| 9 | |
| 10 _log = logging.getLogger(__name__) | |
| 11 | |
| 12 | |
| 13 class GitHub(object): | |
| 14 | |
| 15 def __init__(self, host): | |
| 16 self.host = host | |
| 17 self.user = self.host.environ.get('GH_USER') | |
| 18 self.token = self.host.environ.get('GH_TOKEN') | |
| 19 | |
| 20 assert self.user and self.token, 'must have GH_USER and GH_TOKEN env var
s' | |
| 21 | |
| 22 def auth_token(self): | |
| 23 return base64.encodestring('{}:{}'.format(self.user, self.token)) | |
| 24 | |
| 25 def create_pr(self, local_branch_name, desc_title, body): | |
| 26 """Creates a PR on GitHub. | |
| 27 | |
| 28 API doc: https://developer.github.com/v3/pulls/#create-a-pull-request | |
| 29 | |
| 30 Returns: | |
| 31 A raw response object if successful, None if not. | |
| 32 """ | |
| 33 assert local_branch_name | |
| 34 assert desc_title | |
| 35 assert body | |
| 36 | |
| 37 pr_branch_name = '{}:{}'.format(self.user, local_branch_name) | |
| 38 | |
| 39 # TODO(jeffcarp): add HTTP to Host and use that here | |
| 40 conn = httplib2.Http() | |
| 41 headers = { | |
| 42 "Accept": "application/vnd.github.v3+json", | |
| 43 "Authorization": "Basic " + self.auth_token() | |
| 44 } | |
| 45 body = { | |
| 46 "title": desc_title, | |
| 47 "body": body, | |
| 48 "head": pr_branch_name, | |
| 49 "base": "master" | |
| 50 } | |
| 51 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf
orm-tests/pulls", | |
| 52 "POST", body=json.JSONEncoder().encode(body
), headers=headers) | |
| 53 _log.info("GitHub response: %s", content) | |
| 54 if resp["status"] != "201": | |
| 55 return None | |
| 56 return json.loads(content) | |
| OLD | NEW |