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 post_comment(self, issue, comment, submit_inline_comments=False): |
| 70 publish_payload = { |
| 71 'message_only': 0 if submit_inline_comments else 1, |
| 72 'send_mail': 1, |
| 73 'add_as_reviewer': 0, |
| 74 'message': comment, |
| 75 'no_redirect': 1, |
| 76 } |
| 77 self.post_issue_data(issue, 'publish', publish_payload) |
| 78 |
| 79 def add_inline_comment(self, issue_id, patchset_id, patch_id, line, a_or_b, |
| 80 comment): |
| 81 comment_payload = { |
| 82 'snapshot': 'old' if a_or_b is 'a' else 'new', |
| 83 'lineno': line, |
| 84 'side': a_or_b, |
| 85 'issue': issue_id, |
| 86 'patchset': patchset_id, |
| 87 'patch': patch_id, |
| 88 'text': comment, |
| 89 } |
| 90 self.post_data('inline_draft', comment_payload) |
OLD | NEW |