##// END OF EJS Templates
calling lock function without lock attribute, will return lock state
marcink -
r3457:08e81155 beta
parent child Browse files
Show More
@@ -178,7 +178,8 b' lock'
178 ----
178 ----
179
179
180 Set locking state on given repository by given user. If userid param is skipped
180 Set locking state on given repository by given user. If userid param is skipped
181 , then it is set to id of user whos calling this method.
181 , then it is set to id of user whos calling this method. If locked param is skipped
182 then function shows current lock state of given repo.
182 This command can be executed only using api_key belonging to user with admin
183 This command can be executed only using api_key belonging to user with admin
183 rights or regular user that have admin or write access to repository.
184 rights or regular user that have admin or write access to repository.
184
185
@@ -190,7 +191,7 b' INPUT::'
190 args : {
191 args : {
191 "repoid" : "<reponame or repo_id>"
192 "repoid" : "<reponame or repo_id>"
192 "userid" : "<user_id or username = Optional(=apiuser)>",
193 "userid" : "<user_id or username = Optional(=apiuser)>",
193 "locked" : "<bool true|false>"
194 "locked" : "<bool true|false = Optional(=None)>"
194 }
195 }
195
196
196 OUTPUT::
197 OUTPUT::
@@ -27,14 +27,14 b''
27
27
28 import traceback
28 import traceback
29 import logging
29 import logging
30 from pylons.controllers.util import abort
31
30
32 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
31 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
33 from rhodecode.lib.auth import PasswordGenerator, AuthUser, \
32 from rhodecode.lib.auth import PasswordGenerator, AuthUser, \
34 HasPermissionAllDecorator, HasPermissionAnyDecorator, \
33 HasPermissionAllDecorator, HasPermissionAnyDecorator, \
35 HasPermissionAnyApi, HasRepoPermissionAnyApi
34 HasPermissionAnyApi, HasRepoPermissionAnyApi
36 from rhodecode.lib.utils import map_groups, repo2db_mapper
35 from rhodecode.lib.utils import map_groups, repo2db_mapper
37 from rhodecode.lib.utils2 import str2bool
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime
37 from rhodecode.lib import helpers as h
38 from rhodecode.model.meta import Session
38 from rhodecode.model.meta import Session
39 from rhodecode.model.scm import ScmModel
39 from rhodecode.model.scm import ScmModel
40 from rhodecode.model.repo import RepoModel
40 from rhodecode.model.repo import RepoModel
@@ -229,7 +229,8 b' class ApiController(JSONRPCController):'
229 'Error occurred during cache invalidation action'
229 'Error occurred during cache invalidation action'
230 )
230 )
231
231
232 def lock(self, apiuser, repoid, locked, userid=Optional(OAttr('apiuser'))):
232 def lock(self, apiuser, repoid, locked=Optional(None),
233 userid=Optional(OAttr('apiuser'))):
233 """
234 """
234 Set locking state on particular repository by given user, if
235 Set locking state on particular repository by given user, if
235 this command is runned by non-admin account userid is set to user
236 this command is runned by non-admin account userid is set to user
@@ -257,7 +258,25 b' class ApiController(JSONRPCController):'
257
258
258 if isinstance(userid, Optional):
259 if isinstance(userid, Optional):
259 userid = apiuser.user_id
260 userid = apiuser.user_id
261
260 user = get_user_or_error(userid)
262 user = get_user_or_error(userid)
263
264 if isinstance(locked, Optional):
265 lockobj = Repository.getlock(repo)
266
267 if lockobj[0] is None:
268 return ('Repo `%s` not locked. Locked=`False`.'
269 % (repo.repo_name))
270 else:
271 userid, time_ = lockobj
272 user = get_user_or_error(userid)
273
274 return ('Repo `%s` locked by `%s`. Locked=`True`. '
275 'Locked since: `%s`'
276 % (repo.repo_name, user.username,
277 h.fmt_date(time_to_datetime(time_))))
278
279 else:
261 locked = str2bool(locked)
280 locked = str2bool(locked)
262 try:
281 try:
263 if locked:
282 if locked:
@@ -1002,6 +1002,10 b' class Repository(Base, BaseModel):'
1002 Session().add(repo)
1002 Session().add(repo)
1003 Session().commit()
1003 Session().commit()
1004
1004
1005 @classmethod
1006 def getlock(cls, repo):
1007 return repo.locked
1008
1005 @property
1009 @property
1006 def last_db_change(self):
1010 def last_db_change(self):
1007 return self.updated_on
1011 return self.updated_on
@@ -370,6 +370,18 b' class BaseTestApi(object):'
370 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
370 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
371 self._compare_ok(id_, expected, given=response.body)
371 self._compare_ok(id_, expected, given=response.body)
372
372
373 def test_api_lock_repo_lock_optional_locked(self):
374 from rhodecode.lib import helpers
375 from rhodecode.lib.utils2 import time_to_datetime
376 _locked_since = helpers.fmt_date(time_to_datetime(Repository\
377 .get_by_repo_name(self.REPO).locked[1]))
378 id_, params = _build_data(self.apikey, 'lock',
379 repoid=self.REPO)
380 response = api_call(self, params)
381 expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`'
382 % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since))
383 self._compare_ok(id_, expected, given=response.body)
384
373 @mock.patch.object(Repository, 'lock', crash)
385 @mock.patch.object(Repository, 'lock', crash)
374 def test_api_lock_error(self):
386 def test_api_lock_error(self):
375 id_, params = _build_data(self.apikey, 'lock',
387 id_, params = _build_data(self.apikey, 'lock',
General Comments 0
You need to be logged in to leave comments. Login now