changeset 2697:4565e655ea2a beta

API: Added option to rescann repositories via api call
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 07 Aug 2012 02:55:15 +0200
parents 1cb10d6abd7b
children 4debfe3b50be
files docs/api/api.rst rhodecode/controllers/api/api.py rhodecode/tests/api/api_base.py
diffstat 3 files changed, 66 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/docs/api/api.rst	Tue Aug 07 00:07:10 2012 +0200
+++ b/docs/api/api.rst	Tue Aug 07 02:55:15 2012 +0200
@@ -127,6 +127,31 @@
     error :  null
 
 
+rescan_repos
+------------
+
+Dispatch rescan repositories action. If remove_obsolete is set
+RhodeCode will delete repos that are in database but not in the filesystem.
+This command can be executed only using api_key belonging to user with admin 
+rights.
+
+INPUT::
+
+    id : <id_for_response>
+    api_key : "<api_key>"
+    method :  "rescan_repos"
+    args :    {
+                "remove_obsolete" : "<boolean = Optional(False)>"
+              }
+
+OUTPUT::
+
+    id : <id_given_in_input>
+    result : "{'added': [<list of names of added repos>], 
+               'removed': [<list of names of removed repos>]}"
+    error :  null
+
+
 get_user
 --------
 
--- a/rhodecode/controllers/api/api.py	Tue Aug 07 00:07:10 2012 +0200
+++ b/rhodecode/controllers/api/api.py	Tue Aug 07 02:55:15 2012 +0200
@@ -31,7 +31,7 @@
 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
 from rhodecode.lib.auth import HasPermissionAllDecorator, \
     HasPermissionAnyDecorator, PasswordGenerator, AuthUser
-from rhodecode.lib.utils import map_groups
+from rhodecode.lib.utils import map_groups, repo2db_mapper
 from rhodecode.model.meta import Session
 from rhodecode.model.scm import ScmModel
 from rhodecode.model.repo import RepoModel
@@ -162,6 +162,28 @@
             )
 
     @HasPermissionAllDecorator('hg.admin')
+    def rescan_repos(self, apiuser, remove_obsolete=Optional(False)):
+        """
+        Dispatch rescan repositories action. If remove_obsolete is set
+        than also delete repos that are in database but not in the filesystem.
+        aka "clean zombies"
+
+        :param apiuser:
+        :param remove_obsolete:
+        """
+
+        try:
+            rm_obsolete = Optional.extract(remove_obsolete)
+            added, removed = repo2db_mapper(ScmModel().repo_scan(),
+                                            remove_obsolete=rm_obsolete)
+            return {'added': added, 'removed': removed}
+        except Exception:
+            log.error(traceback.format_exc())
+            raise JSONRPCError(
+                'Unable to rescan repositories'
+            )
+
+    @HasPermissionAllDecorator('hg.admin')
     def get_user(self, apiuser, userid):
         """"
         Get a user by username
--- a/rhodecode/tests/api/api_base.py	Tue Aug 07 00:07:10 2012 +0200
+++ b/rhodecode/tests/api/api_base.py	Tue Aug 07 02:55:15 2012 +0200
@@ -9,6 +9,7 @@
 from rhodecode.model.users_group import UsersGroupModel
 from rhodecode.model.repo import RepoModel
 from rhodecode.model.meta import Session
+from rhodecode.model.scm import ScmModel
 
 API_URL = '/_admin/api'
 
@@ -215,6 +216,23 @@
         expected = 'Unable to pull changes from `%s`' % self.REPO
         self._compare_error(id_, expected, given=response.body)
 
+    def test_api_rescan_repos(self):
+        id_, params = _build_data(self.apikey, 'rescan_repos')
+        response = self.app.post(API_URL, content_type='application/json',
+                                 params=params)
+
+        expected = {'added': [], 'removed': []}
+        self._compare_ok(id_, expected, given=response.body)
+
+    @mock.patch.object(ScmModel, 'repo_scan', crash)
+    def test_api_rescann_error(self):
+        id_, params = _build_data(self.apikey, 'rescan_repos',)
+        response = self.app.post(API_URL, content_type='application/json',
+                                 params=params)
+
+        expected = 'Unable to rescan repositories'
+        self._compare_error(id_, expected, given=response.body)
+
     def test_api_create_existing_user(self):
         id_, params = _build_data(self.apikey, 'create_user',
                                   username=TEST_USER_ADMIN_LOGIN,