# HG changeset patch # User Marcin Kuzminski # Date 1359238370 -3600 # Node ID d6029dacbcc456cea48ea7a2bf8fd45725a1dc05 # Parent 21cccfea18bf6229d8e02b665125a3423a6b40a0 API invalidate_cache function ref #733 diff -r 21cccfea18bf -r d6029dacbcc4 docs/api/api.rst --- a/docs/api/api.rst Sat Jan 26 23:11:40 2013 +0100 +++ b/docs/api/api.rst Sat Jan 26 23:12:50 2013 +0100 @@ -152,6 +152,28 @@ error : null +invalidate_cache +---------------- + +Invalidate cache for repository. +This command can be executed only using api_key belonging to user with admin +rights or regular user that have write or admin or write access to repository. + +INPUT:: + + id : + api_key : "" + method : "invalidate_cache" + args : { + "repoid" : "" + } + +OUTPUT:: + + id : + result : "Cache for repository `` was invalidated: invalidated cache keys: " + error : null + lock ---- diff -r 21cccfea18bf -r d6029dacbcc4 rhodecode/controllers/api/api.py --- a/rhodecode/controllers/api/api.py Sat Jan 26 23:11:40 2013 +0100 +++ b/rhodecode/controllers/api/api.py Sat Jan 26 23:12:50 2013 +0100 @@ -202,6 +202,32 @@ 'Error occurred during rescan repositories action' ) + def invalidate_cache(self, apiuser, repoid): + """ + Dispatch cache invalidation action on given repo + + :param apiuser: + :param repoid: + """ + repo = get_repo_or_error(repoid) + if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: + # check if we have admin permission for this repo ! + if HasRepoPermissionAnyApi('repository.admin', + 'repository.write')(user=apiuser, + repo_name=repo.repo_name) is False: + raise JSONRPCError('repository `%s` does not exist' % (repoid)) + + try: + invalidated_keys = ScmModel().mark_for_invalidation(repo.repo_name) + Session().commit() + return ('Cache for repository `%s` was invalidated: ' + 'invalidated cache keys: %s' % (repoid, invalidated_keys)) + except Exception: + log.error(traceback.format_exc()) + raise JSONRPCError( + 'Error occurred during cache invalidation action' + ) + def lock(self, apiuser, repoid, locked, userid=Optional(OAttr('apiuser'))): """ Set locking state on particular repository by given user, if diff -r 21cccfea18bf -r d6029dacbcc4 rhodecode/tests/api/api_base.py --- a/rhodecode/tests/api/api_base.py Sat Jan 26 23:11:40 2013 +0100 +++ b/rhodecode/tests/api/api_base.py Sat Jan 26 23:12:50 2013 +0100 @@ -286,6 +286,25 @@ expected = 'Error occurred during rescan repositories action' self._compare_error(id_, expected, given=response.body) + def test_api_invalidate_cache(self): + id_, params = _build_data(self.apikey, 'invalidate_cache', + repoid=self.REPO) + response = api_call(self, params) + + expected = ("Cache for repository `%s` was invalidated: " + "invalidated cache keys: %s" % (self.REPO, + [unicode(self.REPO)])) + self._compare_ok(id_, expected, given=response.body) + + @mock.patch.object(ScmModel, 'mark_for_invalidation', crash) + def test_api_invalidate_cache_error(self): + id_, params = _build_data(self.apikey, 'invalidate_cache', + repoid=self.REPO) + response = api_call(self, params) + + expected = 'Error occurred during cache invalidation action' + self._compare_error(id_, expected, given=response.body) + def test_api_lock_repo_lock_aquire(self): id_, params = _build_data(self.apikey, 'lock', userid=TEST_USER_ADMIN_LOGIN,