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

Unified Diff: appengine/findit/common/rietveld.py

Issue 2079773004: [Findit] Add a Rietveld client to post a message to codereview of a culprit. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Fix unittests. Created 4 years, 6 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « appengine/findit/common/http_client_appengine.py ('k') | appengine/findit/common/test/rietveld_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/findit/common/rietveld.py
diff --git a/appengine/findit/common/rietveld.py b/appengine/findit/common/rietveld.py
new file mode 100644
index 0000000000000000000000000000000000000000..bf7907177f8cd2a3e853d01bd2fd3032fc7e8241
--- /dev/null
+++ b/appengine/findit/common/rietveld.py
@@ -0,0 +1,86 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+import re
+import urlparse
+
+from common.codereview import CodeReview
+from common.http_client_appengine import HttpClientAppengine
+
+
+_RIETVELD_ISSUE_NUMBER_RE = re.compile('^/(\d+)/?.*')
+
+
+class Rietveld(CodeReview):
+ """The implementation of CodeReview interface for Rietveld."""
+ HTTP_CLIENT = HttpClientAppengine()
+
+ def _GetXsrfToken(self, rietveld_url):
+ """Returns the xsrf token for follow-up requests."""
+ headers = {
+ 'X-Requesting-XSRF-Token': '1',
+ 'Accept': 'text/plain',
+ }
+ url = '%s/xsrf_token' % rietveld_url
+ status_code, xsrf_token = self.HTTP_CLIENT.Post(
+ url, data=None, headers=headers)
+ if status_code != 200:
+ logging.error('Failed to get xsrf token from %s', rietveld_url)
+ xsrf_token = None
+ return xsrf_token
+
+ def _GetRietveldUrlAndIssueNumber(self, issue_url):
+ """Parses the given Rietveld issue url.
+
+ Args:
+ issue_url(str): The url to an issue on Rietveld.
+ Returns:
+ (rietveld_url, issue_number)
+ rietveld_url(str): The root url of the Rietveld app.
+ issue_number(str): The issue number.
+ """
+ u = urlparse.urlparse(issue_url)
+ rietveld_url = 'https://%s' % u.netloc # Enforces https.
+ issue_number = _RIETVELD_ISSUE_NUMBER_RE.match(u.path).group(1)
+ return rietveld_url, issue_number
+
+ def _EncodeMultipartFormData(self, fields):
+ """Encodes form fields for multipart/form-data"""
+ BOUNDARY = '-F-I-N-D-I-T-M-E-S-S-A-G-E-'
+ CRLF = '\r\n'
+ lines = []
+ for key, value in fields.iteritems():
+ lines.append('--' + BOUNDARY)
+ lines.append('Content-Disposition: form-data; name="%s"' % key)
+ lines.append('')
+ lines.append(str(value))
+ lines.append('--' + BOUNDARY + '--')
+ lines.append('')
+ body = CRLF.join(lines)
+ content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+ return content_type, body
+
+ def PostMessage(self, issue_url, message):
+ rietveld_url, issue_number = self._GetRietveldUrlAndIssueNumber(issue_url)
+ url = '%s/%s/publish' % (rietveld_url, issue_number)
+ xsrf_token = self._GetXsrfToken(rietveld_url)
+ if not xsrf_token:
+ return False
+ form_fields = {
+ 'xsrf_token': xsrf_token,
+ 'message': message,
+ 'message_only': 'True',
+ 'add_as_reviewer': 'False',
+ 'send_mail': 'True',
+ 'no_redirect': 'True',
+ }
+ content_type, body = self._EncodeMultipartFormData(form_fields)
+ headers = {
+ 'Content-Type': content_type,
+ 'Accept': 'text/plain',
+ }
+ status_code, content = self.HTTP_CLIENT.Post(
+ url, data=body, headers=headers)
+ return status_code == 200 and content == 'OK'
« no previous file with comments | « appengine/findit/common/http_client_appengine.py ('k') | appengine/findit/common/test/rietveld_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698