reviewboard: move review submission logic out of pushhooks.py; r=smacleod
This code does very little now and doesn't need to exist in
pushhooks.py. Move it to proto.py.
Yes, the future Git implementation will need to reinvent this wheel. But
the code is mostly boilerplate: I'm not concerned at the moment.
--- a/hgext/reviewboard/hgrb/proto.py
+++ b/hgext/reviewboard/hgrb/proto.py
@@ -81,26 +81,58 @@ class NoAPITokenAuthError(ServerError):
def __str__(self):
return 'error %s' % API_KEY_NEEDED
# Wrap reviewboardmods and error types because we don't want to require the
# clients to import rbtools.
def post_reviews(*args, **kwargs):
- from reviewboardmods.pushhooks import post_reviews as pr
from rbtools.api import errors
try:
- return pr(*args, **kwargs)
+ return submit_reviews(*args, **kwargs)
except errors.AuthorizationError as e:
raise AuthorizationError(e, **kwargs)
except errors.BadRequestError as e:
raise BadRequestError(e)
+
+def submit_reviews(url, repoid, identifier, commits, hgresp,
+ username=None, apikey=None):
+ """Submit commits to Review Board."""
+ import json
+ from reviewboardmods.pushhooks import ReviewBoardClient
+
+ with ReviewBoardClient(url, username=username, apikey=apikey) as client:
+ root = client.get_root()
+
+ batch_request_resource = root.get_extension(
+ extension_name='mozreview.extension.MozReviewExtension')\
+ .get_batch_review_requests()
+ series_result = batch_request_resource.create(
+ # This assumes that we pushed to the repository/URL that Review Board is
+ # configured to use. This assumption may not always hold.
+ repo_id=repoid,
+ identifier=identifier,
+ commits=json.dumps(commits, encoding='utf-8'))
+
+ for w in series_result.warnings:
+ hgresp.append(b'display %s' % w.encode('utf-8'))
+
+ nodes = {node.encode('utf-8'): str(rid)
+ for node, rid in series_result.nodes.iteritems()}
+
+ return (
+ str(series_result.squashed_rr),
+ nodes,
+ series_result.review_requests,
+ )
+
+
def associate_ldap_username(*args, **kwargs):
from reviewboardmods.pushhooks import associate_ldap_username as alu
return alu(*args, **kwargs)
def getpayload(proto, args):
# HTTP and SSH behave differently here. In SSH, the data is
# passed as an argument. In HTTP, the data is on a stream which
--- a/pylib/reviewboardmods/reviewboardmods/pushhooks.py
+++ b/pylib/reviewboardmods/reviewboardmods/pushhooks.py
@@ -5,55 +5,23 @@ Mercurial, etc) and adding them to Revie
It is intended for this module to be generic and applicable to any
Review Board install. Please abstract away Mozilla implementation
details.
"""
from contextlib import contextmanager
import os
-import json
import tempfile
from rbtools.api.client import RBClient
from rbtools.api.errors import APIError
from rbtools.api.transport.sync import SyncTransport
-def post_reviews(url, repoid, identifier, commits, hgresp,
- username=None, apikey=None):
- """Post a set of commits to Review Board.
-
- Repository hooks can use this function to post a set of pushed commits
- to Review Board.
- """
- with ReviewBoardClient(url, username=username, apikey=apikey) as rbc:
- root = rbc.get_root()
- return _post_reviews(root, repoid, identifier, commits, hgresp)
-
-def _post_reviews(api_root, repoid, identifier, commits, hgresp):
- batch_request_resource = api_root.get_extension(
- extension_name='mozreview.extension.MozReviewExtension')\
- .get_batch_review_requests()
- series_result = batch_request_resource.create(
- # This assumes that we pushed to the repository/URL that Review Board is
- # configured to use. This assumption may not always hold.
- repo_id=repoid,
- identifier=identifier,
- commits=json.dumps(commits, encoding='utf-8'))
-
- for w in series_result.warnings:
- hgresp.append(b'display %s' % w.encode('utf-8'))
-
- nodes = {node.encode('utf-8'): str(rid)
- for node, rid in series_result.nodes.iteritems()}
-
- return str(series_result.squashed_rr), nodes, series_result.review_requests
-
-
def associate_ldap_username(url, ldap_username, privileged_username,
privileged_password, username, apikey):
"""Associate a Review Board user with an ldap username.
Will return True if an ldap_username is successfully associated
with a Review Board account, False otherwise.
This function does not prove ownership over the provided