Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/w3c/wpt_github.py

Issue 2557963002: Convert WPT export usage of httplib2 to urllib2 (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 os 8 import os
9 import sys 9 import sys
10 import urllib2
10 11
11 from webkitpy.common.system.filesystem import FileSystem 12 from webkitpy.common.system.filesystem import FileSystem
12 from webkitpy.common.webkit_finder import WebKitFinder 13 from webkitpy.common.webkit_finder import WebKitFinder
13 14
14 # httplib2 is part of the hermetic depot_tools install,
15 # but we first need to find a path to it.
16 # TODO(dglazkov): libhttp2 needs to be imported into thirdparty dir.
17 # See http://crbug.com/670987 for details.
18 _DEPOT_TOOLS_ROOT = WebKitFinder(FileSystem()).depot_tools_base()
19 sys.path.append(os.path.join(_DEPOT_TOOLS_ROOT, 'third_party'))
20
21 import httplib2
22 15
23 _log = logging.getLogger(__name__) 16 _log = logging.getLogger(__name__)
24 API_BASE = 'https://api.github.com' 17 API_BASE = 'https://api.github.com'
25 EXPORT_LABEL = 'chromium-export' 18 EXPORT_LABEL = 'chromium-export'
26 19
27 20
28 class WPTGitHub(object): 21 class WPTGitHub(object):
29 22
30 def __init__(self, host): 23 def __init__(self, host):
31 self.host = host 24 self.host = host
32 self.user = self.host.environ.get('GH_USER') 25 self.user = self.host.environ.get('GH_USER')
33 self.token = self.host.environ.get('GH_TOKEN') 26 self.token = self.host.environ.get('GH_TOKEN')
34 27
35 assert self.user and self.token, 'must have GH_USER and GH_TOKEN env var s' 28 assert self.user and self.token, 'must have GH_USER and GH_TOKEN env var s'
36 29
37 def auth_token(self): 30 def auth_token(self):
38 return base64.encodestring('{}:{}'.format(self.user, self.token)) 31 return base64.encodestring('{}:{}'.format(self.user, self.token)).strip( )
39 32
40 def create_pr(self, local_branch_name, desc_title, body): 33 def create_pr(self, local_branch_name, desc_title, body):
41 """Creates a PR on GitHub. 34 """Creates a PR on GitHub.
42 35
43 API doc: https://developer.github.com/v3/pulls/#create-a-pull-request 36 API doc: https://developer.github.com/v3/pulls/#create-a-pull-request
44 37
45 Returns: 38 Returns:
46 A raw response object if successful, None if not. 39 A raw response object if successful, None if not.
47 """ 40 """
48 assert local_branch_name 41 assert local_branch_name
49 assert desc_title 42 assert desc_title
50 assert body 43 assert body
51 44
52 pr_branch_name = '{}:{}'.format(self.user, local_branch_name) 45 pr_branch_name = '{}:{}'.format(self.user, local_branch_name)
53 46
54 # TODO(jeffcarp): CC foolip and qyearsley on all PRs for now 47 # TODO(jeffcarp): CC foolip and qyearsley on all PRs for now
55 # TODO(jeffcarp): add HTTP to Host and use that here 48 # TODO(jeffcarp): add HTTP to Host and use that here
56 conn = httplib2.Http() 49 path = '/repos/w3c/web-platform-tests/pulls'
57 headers = {
58 "Accept": "application/vnd.github.v3+json",
59 "Authorization": "Basic " + self.auth_token()
60 }
61 body = { 50 body = {
62 "title": desc_title, 51 "title": desc_title,
63 "body": body, 52 "body": body,
64 "head": pr_branch_name, 53 "head": pr_branch_name,
65 "base": 'master', 54 "base": 'master',
66 "labels": [EXPORT_LABEL] 55 "labels": [EXPORT_LABEL]
67 } 56 }
68 resp, content = conn.request("https://api.github.com/repos/w3c/web-platf orm-tests/pulls", 57 data, status_code = self.request(path, body)
69 "POST", body=json.JSONEncoder().encode(body ), headers=headers) 58
70 _log.info("GitHub response: %s", content) 59 if status_code != 201:
71 if resp["status"] != "201":
72 return None 60 return None
73 return json.loads(content) 61
62 return data
74 63
75 def in_flight_pull_requests(self): 64 def in_flight_pull_requests(self):
76 url_encoded_label = EXPORT_LABEL.replace(' ', '%20') 65 url_encoded_label = EXPORT_LABEL.replace(' ', '%20')
77 path = '/search/issues?q=repo:w3c/web-platform-tests%20is:open%20type:pr %20labels:{}'.format(url_encoded_label) 66 path = '/search/issues?q=repo:w3c/web-platform-tests%20is:open%20type:pr %20labels:{}'.format(url_encoded_label)
78 response, content = self.request(path) 67 data, status_code = self.request(path)
79 if response['status'] == '200': 68 if status_code == 200:
80 data = json.loads(content)
81 return data['items'] 69 return data['items']
82 else: 70 else:
83 raise Exception('Non-200 status code (%s): %s' % (response['status'] , content)) 71 raise Exception('Non-200 status code (%s): %s' % (status_code, data) )
84 72
85 def request(self, path, body=None): 73 def request(self, path, body=None):
86 assert path.startswith('/') 74 assert path.startswith('/')
87 75
88 # Not used yet since only hitting public API 76 if body:
89 # headers = { 77 body = json.dumps(body)
90 # "Accept": "application/vnd.github.v3+json",
91 # "Authorization": "Basic " + self.auth_token()
92 # }
93 78
94 if body: 79 req = urllib2.Request(url=API_BASE + path, data=body)
95 json_body = json.dumps(body) 80 req.add_header('Accept', 'application/vnd.github.v3+json')
96 else: 81 req.add_header('Authorization', 'Basic {}'.format(self.auth_token()))
97 json_body = None 82 res = urllib2.urlopen(req)
98 83 status_code = res.getcode()
99 conn = httplib2.Http() 84 return json.load(res), status_code
100 return conn.request(API_BASE + path, body=json_body)
101 85
102 def merge_pull_request(self, pull_request_number): 86 def merge_pull_request(self, pull_request_number):
103 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
104 body = {} 88 body = {}
105 response, content = self.request(path, body) 89 response, content = self.request(path, body)
106 90
107 if response['status'] == '200': 91 if response['status'] == '200':
108 return json.loads(content) 92 return json.loads(content)
109 else: 93 else:
110 raise Exception('PR could not be merged: %d' % pull_request_number) 94 raise Exception('PR could not be merged: %d' % pull_request_number)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698