OLD | NEW |
| (Empty) |
1 # Copyright (c) 2013 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 from oauth2client.client import SignedJwtAssertionCredentials | |
6 import httplib2 | |
7 import model.app_config | |
8 import urllib | |
9 import util | |
10 | |
11 | |
12 EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' | |
13 | |
14 | |
15 class RietveldRequestError(Exception): | |
16 """Raised on request errors.""" | |
17 pass | |
18 | |
19 | |
20 class Rietveld(object): | |
21 """Implements a Python API to access rietveld via HTTP. | |
22 | |
23 Authentication is handled via an OAuth2 access token minted from an RSA key | |
24 associated with a service account (which can be created via the Google API | |
25 console). For this to work, the Rietveld instance to talk to must be | |
26 configured to allow the service account client ID as OAuth2 audience (see | |
27 Rietveld source). Both the RSA key and the server URL are provided via static | |
28 application configuration. | |
29 """ | |
30 | |
31 def __init__(self): | |
32 self.app_config = model.app_config.get() | |
33 | |
34 @util.lazy_property | |
35 def http(self): | |
36 http = httplib2.Http() | |
37 | |
38 creds = SignedJwtAssertionCredentials(self.app_config.client_id, | |
39 self.app_config.service_account_key, | |
40 EMAIL_SCOPE) | |
41 creds.authorize(http) | |
42 return http | |
43 | |
44 @util.lazy_property | |
45 def xsrf_token(self): | |
46 return self.make_request('xsrf_token', | |
47 headers={'X-Requesting-XSRF-Token': 1}) | |
48 | |
49 def make_request(self, req, *args, **kwargs): | |
50 resp, response = self.http.request( | |
51 '%s/%s' % (self.app_config.server_url, req), *args, **kwargs) | |
52 if resp.status != 200: | |
53 raise RietveldRequestError( | |
54 'Rietveld %s request failed: %s\n%s' % | |
55 (req, resp.status, str(resp)), resp, response) | |
56 | |
57 return response | |
58 | |
59 def post_data(self, req, payload=None): | |
60 actual_payload = dict(payload or {}) | |
61 actual_payload['xsrf_token'] = self.xsrf_token | |
62 | |
63 return self.make_request(req, method='POST', | |
64 body=urllib.urlencode(actual_payload)) | |
65 | |
66 def post_issue_data(self, issue, req, payload): | |
67 return self.post_data('%s/%s' % (issue, req), payload) | |
68 | |
69 def publish_inline_comments(self, issue, comment, reviewers, cc, | |
70 subject=None, send_mail=False): | |
71 publish_payload = { | |
72 'cc': cc, | |
73 'message': comment, | |
74 'message_only': 0, | |
75 'no_redirect': 1, | |
76 'reviewers': reviewers, | |
77 'send_mail': 1 if send_mail else 0, | |
78 } | |
79 if subject is not None: | |
80 publish_payload['subject'] = subject | |
81 self.post_issue_data(issue, 'publish', publish_payload) | |
82 | |
83 def add_inline_comment(self, issue_id, patchset_id, patch_id, line, a_or_b, | |
84 comment): | |
85 comment_payload = { | |
86 'snapshot': 'old' if a_or_b is 'a' else 'new', | |
87 'lineno': line, | |
88 'side': a_or_b, | |
89 'issue': issue_id, | |
90 'patchset': patchset_id, | |
91 'patch': patch_id, | |
92 'text': comment, | |
93 } | |
94 self.post_data('inline_draft', comment_payload) | |
OLD | NEW |