testing: move make_admin into MozReviewBoard class; r=smacleod
authorGregory Szorc <gps@mozilla.com>
Fri, 06 Mar 2015 12:49:34 -0800
changeset 360488 24f8668e52f2246378af2b0f3a7d8eaf077803e6
parent 360487 eb65e00a415aca2d28ee3c0f57df0e527a63e3dc
child 360489 59aadbfdf529b9b528f44d6476900b09a2fb1751
push id16998
push userrwood@mozilla.com
push dateMon, 02 May 2016 19:42:03 +0000
reviewerssmacleod
testing: move make_admin into MozReviewBoard class; r=smacleod We want to call this API from elsewhere. Move it out of an inline mach command and into a module. As part of this, we changed the _get_rb() function to take an optional path argument. It is a holdover from the days of old to pass a path into the reviewboard mach commands. This paves the road to getting rid of this pattern.
testing/vcttesting/reviewboard/__init__.py
testing/vcttesting/reviewboard/mach_commands.py
--- a/testing/vcttesting/reviewboard/__init__.py
+++ b/testing/vcttesting/reviewboard/__init__.py
@@ -1,23 +1,29 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+import logging
 import os
 import signal
+import sqlite3
 import subprocess
 import sys
 import time
 import urllib
 
 from contextlib import contextmanager
 
 import psutil
 
+
+logger = logging.getLogger(__name__)
+
+
 SETTINGS_LOCAL = """
 from __future__ import unicode_literals
 
 import os
 
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
@@ -92,16 +98,33 @@ class MozReviewBoard(object):
             bug_url = '%s/show_bug.cgi?id=%%s' % bugzilla_url
 
             from reviewboard.scmtools.models import Repository, Tool
             tool = Tool.objects.get(name__exact='Mercurial')
             r = Repository(name=name, path=url, tool=tool, bug_tracker=bug_url)
             r.save()
             return r.id
 
+    def make_admin(self, email):
+        """Make the user with the specified email an admin.
+
+        This grants superuser and staff privileges to the user.
+        """
+        db = os.path.join(self.path, 'reviewboard.db')
+        conn = sqlite3.connect(db)
+        with conn:
+            count = conn.execute('UPDATE auth_user SET is_superuser=1, is_staff=1 '
+                                 'WHERE email=?', (email,))
+
+            if not count:
+                raise Exception('Cannot make unknown user an admin: %s' % email)
+
+            conn.commit()
+            logger.info('made %s an admin' % email)
+
     def start(self, port):
         """Start the HTTP server on the specified port."""
         with wrap_env():
             self._setup_env()
             return self._start(port)
 
     def stop(self):
         path = os.path.join(self.path, 'rbserver.pid')
--- a/testing/vcttesting/reviewboard/mach_commands.py
+++ b/testing/vcttesting/reviewboard/mach_commands.py
@@ -161,22 +161,22 @@ class ReviewBoardCommands(object):
             pass
 
         return RBClient('http://localhost:%s/' % port, username=username,
                         password=password)
 
     def _get_root(self, port):
         return self._get_client(port).get_root()
 
-    def _get_rb(self, path):
+    def _get_rb(self, path=None):
         from vcttesting.reviewboard import MozReviewBoard
 
         if self.mr:
             return self.mr.get_reviewboard()
-        elif 'BUGZILLA_HOME' is os.environ:
+        elif 'BUGZILLA_HOME' in os.environ and path:
             return MozReviewBoard(path, os.environ['BUGZILLA_URL'],
                 pulse_host=os.environ.get('PULSE_HOST'),
                 pulse_port=os.environ.get('PULSE_PORT'))
         else:
             raise Exception('Do not know about Bugzilla URL. Cannot talk to '
                             'Review Board. Try running `mozreview start` and '
                             'setting MOZREVIEW_HOME.')
 
@@ -605,26 +605,18 @@ class ReviewBoardCommands(object):
             return 1
 
         url = self.mr.restart_reviewboard()
         print('Review Board accessible at %s' % url)
 
     @Command('make-admin', category='reviewboard',
         description='Make a user a superuser and staff user')
     @CommandArgument('email', help='Email address of user to modify')
-    @CommandArgument('--path', required=False,
-                     help='Path to Review Board install')
-    def make_admin(self, email, path=None):
-        import sqlite3
-        db = os.path.join(path or self.mr._path, 'reviewboard.db')
-        conn = sqlite3.connect(db)
-        with conn:
-            conn.execute('UPDATE auth_user SET is_superuser=1, is_staff=1 '
-                    'WHERE email=?', (email,))
-            conn.commit()
+    def make_admin(self, email):
+        self._get_rb().make_admin(email)
 
     @Command('dump-account-profile', category='reviewboard',
          description='Dump the contents of the auth_user table')
     @CommandArgument('username', help='Username whose info the print')
     @CommandArgument('--path', required=False,
                      help='Path to Review Board install')
     def dump_account_profile(self, username, path=None):
         import sqlite3