mozreview: add API endpoint to enable/disable autoland (bug 1248592) r?smacleod draft
authorDan Minor <dminor@mozilla.com>
Wed, 17 Feb 2016 13:09:20 -0500
changeset 7427 aa21333adb1e03c6f94e44dc960a3ac58e90e11c
parent 7426 9733a44750b11e6e7046b08af8433d88cf320b22
push id677
push userdminor@mozilla.com
push dateThu, 03 Mar 2016 20:19:20 +0000
reviewerssmacleod
bugs1248592
mozreview: add API endpoint to enable/disable autoland (bug 1248592) r?smacleod MozReview-Commit-ID: 7wRNVIrURL5
pylib/mozreview/mozreview/autoland/resources.py
pylib/mozreview/mozreview/extension.py
pylib/mozreview/mozreview/models.py
testing/vcttesting/mozreview.py
--- a/pylib/mozreview/mozreview/autoland/resources.py
+++ b/pylib/mozreview/mozreview/autoland/resources.py
@@ -12,16 +12,17 @@ from djblets.webapi.decorators import (w
 from djblets.webapi.errors import (DOES_NOT_EXIST,
                                    INVALID_FORM_DATA,
                                    NOT_LOGGED_IN,
                                    PERMISSION_DENIED)
 import requests
 from reviewboard.changedescs.models import ChangeDescription
 from reviewboard.extensions.base import get_extension_manager
 from reviewboard.reviews.models import ReviewRequest
+from reviewboard.scmtools.models import Repository
 from reviewboard.site.urlresolvers import local_site_reverse
 from reviewboard.webapi.resources import WebAPIResource
 
 from mozreview.autoland.models import (AutolandEventLogEntry,
                                        AutolandRequest)
 from mozreview.decorators import webapi_scm_groups_required
 from mozreview.errors import (AUTOLAND_CONFIGURATION_ERROR,
                               AUTOLAND_ERROR,
@@ -517,8 +518,74 @@ class AutolandRequestUpdateResource(WebA
                                        autoland_request.repository_url,
                                        autoland_request.push_revision)
         release_lock(lock_id)
 
         return 200, {}
 
 
 autoland_request_update_resource = AutolandRequestUpdateResource()
+
+
+class AutolandEnableResource(WebAPIResource):
+    """Provides interface to enable or disable Autoland for a repository."""
+
+    name = 'autoland_enable'
+    uri_name = 'autoland_enable'
+    uri_object_key = 'repository'
+    allowed_methods = ('GET', 'PUT')
+
+    @webapi_response_errors(DOES_NOT_EXIST, PERMISSION_DENIED)
+    def get(self, request, *args, **kwargs):
+        try:
+            repo = Repository.objects.get(id=kwargs[self.uri_object_key])
+        except Repository.DoesNotExist:
+            return DOES_NOT_EXIST
+
+        try_enabled = repo.extra_data.get('autolanding_to_try_enabled', False)
+        enabled = repo.extra_data.get('autolanding_enabled', False)
+        return 200, {
+            'autolanding_to_try_enabled': try_enabled,
+            'autolanding_enabled': enabled,
+        }
+
+    @webapi_response_errors(DOES_NOT_EXIST, PERMISSION_DENIED)
+    @webapi_request_fields(
+        required={
+            'autolanding_to_try_enabled': {
+                'type': bool,
+                'description': 'Enable autolanding to try',
+            },
+            'autolanding_enabled': {
+                'type': bool,
+                'description': 'Enable autolanding',
+            },
+        },
+    )
+    def update(self, request, autolanding_to_try_enabled,
+               autolanding_enabled,*args, **kwargs):
+        if not request.user.has_perm('mozreview.enable_autoland'):
+            logger.info('Could not set autoland enable: permission '
+                        'denied for user: %s' % (request.user.id))
+            return PERMISSION_DENIED
+
+        try:
+            repo = Repository.objects.get(id=kwargs[self.uri_object_key])
+        except Repository.DoesNotExist:
+            logger.info('Could not set autoland enable: repository %s'
+                        'unknown.' % (kwargs[self.uri_object_key]))
+            return DOES_NOT_EXIST
+
+        logger.info('Setting autoland enable: repository %s: try: %s '
+                    'landing: %s at request of user: %s' % (
+                    kwargs[self.uri_object_key], autolanding_to_try_enabled,
+                    autolanding_enabled, request.user.id))
+        repo.extra_data['autolanding_to_try_enabled'] = autolanding_to_try_enabled
+        repo.extra_data['autolanding_enabled'] = autolanding_enabled
+        repo.save(update_fields=['extra_data'])
+
+        return 200, {
+            'autolanding_to_try_enabled': autolanding_to_try_enabled,
+            'autolanding_enabled': autolanding_enabled,
+        }
+
+
+autoland_enable_resource = AutolandEnableResource()
--- a/pylib/mozreview/mozreview/extension.py
+++ b/pylib/mozreview/mozreview/extension.py
@@ -18,16 +18,17 @@ from reviewboard.reviews.builtin_fields 
                                                 DependsOnField,
                                                 BlocksField)
 from reviewboard.reviews.fields import (get_review_request_field,
                                         get_review_request_fieldset)
 from reviewboard.urls import (diffviewer_url_names,
                               review_request_url_names)
 
 from mozreview.autoland.resources import (
+    autoland_enable_resource,
     autoland_request_update_resource,
     autoland_trigger_resource,
     try_autoland_trigger_resource,
 )
 from mozreview.batchreview.resources import (
     batch_review_resource,
 )
 from mozreview.extra_data import (
@@ -141,16 +142,17 @@ class MozReviewExtension(Extension):
         },
         'parent-review-requests': {
             'source_filenames': ['mozreview/js/parents.js'],
             'apply_to': review_request_url_names,
         },
     }
 
     resources = [
+        autoland_enable_resource,
         autoland_request_update_resource,
         autoland_trigger_resource,
         batch_review_request_resource,
         batch_review_resource,
         bugzilla_api_key_login_resource,
         commit_data_resource,
         commit_rewrite_resource,
         file_diff_reviewer_resource,
--- a/pylib/mozreview/mozreview/models.py
+++ b/pylib/mozreview/mozreview/models.py
@@ -66,9 +66,11 @@ class MozReviewUserProfile(models.Model)
         return query_scm_group(self.ldap_username, group)
 
 
     class Meta:
         app_label = 'mozreview'
         permissions = (
             ("modify_ldap_association",
              "Can change ldap assocation for all users"),
+            ("enable_autoland",
+             "Can enable or disable autoland for a repository"),
         )
--- a/testing/vcttesting/mozreview.py
+++ b/testing/vcttesting/mozreview.py
@@ -255,27 +255,30 @@ class MozReview(object):
         rb.login_user(bugzilla.username, bugzilla.password)
 
         # Ensure the mozreview hg user is present and has privileges.
         # This has to occur after the admin user is logged in to avoid
         # race conditions with user IDs.
         rb.create_local_user(self.hg_rb_username, self.hg_rb_email,
                              self.hg_rb_password)
 
-        with futures.ThreadPoolExecutor(6) as e:
+        with futures.ThreadPoolExecutor(7) as e:
             # Ensure admin user had admin privileges.
             e.submit(rb.make_admin, bugzilla.username)
 
-            # Ensure mozreview user has LDAP privileges.
+            # Ensure mozreview user has permissions for testing.
             e.submit(rb.grant_permission, self.hg_rb_username,
                      'Can change ldap assocation for all users')
 
             e.submit(rb.grant_permission, self.hg_rb_username,
                      'Can verify DiffSet legitimacy')
 
+            e.submit(rb.grant_permission, self.hg_rb_username,
+                     'Can enable or disable autoland for a repository')
+
             # Tell hgrb about URLs.
             e.submit(self._docker.execute, self.hgrb_id,
                      ['/set-urls', self.bugzilla_url, self.reviewboard_url])
 
             # Define site domain and hostname in rbweb. This is necessary so it
             # constructs self-referential URLs properly.
             e.submit(self._docker.execute, self.rbweb_id,
                      ['/set-site-url', self.reviewboard_url,