Show More
@@ -212,7 +212,13 b' INPUT::' | |||||
212 | OUTPUT:: |
|
212 | OUTPUT:: | |
213 |
|
213 | |||
214 | id : <id_given_in_input> |
|
214 | id : <id_given_in_input> | |
215 | result : "User `<username>` set lock state for repo `<reponame>` to `true|false`" |
|
215 | result : { | |
|
216 | "repo": "<reponame>", | |||
|
217 | "locked": "<bool true|false>", | |||
|
218 | "locked_since": "<float lock_time>", | |||
|
219 | "locked_by": "<username>", | |||
|
220 | "msg": "User `<username>` set lock state for repo `<reponame>` to `<false|true>`" | |||
|
221 | } | |||
216 | error : null |
|
222 | error : null | |
217 |
|
223 | |||
218 |
|
224 |
@@ -25,6 +25,7 b'' | |||||
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 |
|
27 | |||
|
28 | import time | |||
28 | import traceback |
|
29 | import traceback | |
29 | import logging |
|
30 | import logging | |
30 |
|
31 | |||
@@ -265,27 +266,47 b' class ApiController(JSONRPCController):' | |||||
265 | lockobj = Repository.getlock(repo) |
|
266 | lockobj = Repository.getlock(repo) | |
266 |
|
267 | |||
267 | if lockobj[0] is None: |
|
268 | if lockobj[0] is None: | |
268 | return ('Repo `%s` not locked. Locked=`False`.' |
|
269 | _d = { | |
269 |
|
|
270 | 'repo': repo.repo_name, | |
|
271 | 'locked': False, | |||
|
272 | 'locked_since': None, | |||
|
273 | 'locked_by': None, | |||
|
274 | 'msg': 'Repo `%s` not locked.' % repo.repo_name | |||
|
275 | } | |||
|
276 | return _d | |||
270 | else: |
|
277 | else: | |
271 | userid, time_ = lockobj |
|
278 | userid, time_ = lockobj | |
272 | user = get_user_or_error(userid) |
|
279 | lock_user = get_user_or_error(userid) | |
|
280 | _d = { | |||
|
281 | 'repo': repo.repo_name, | |||
|
282 | 'locked': True, | |||
|
283 | 'locked_since': time_, | |||
|
284 | 'locked_by': lock_user.username, | |||
|
285 | 'msg': ('Repo `%s` locked by `%s`. ' | |||
|
286 | % (repo.repo_name, | |||
|
287 | json.dumps(time_to_datetime(time_)))) | |||
|
288 | } | |||
|
289 | return _d | |||
273 |
|
290 | |||
274 | return ('Repo `%s` locked by `%s`. Locked=`True`. ' |
|
291 | # force locked state through a flag | |
275 | 'Locked since: `%s`' |
|
|||
276 | % (repo.repo_name, user.username, |
|
|||
277 | json.dumps(time_to_datetime(time_)))) |
|
|||
278 |
|
||||
279 | else: |
|
292 | else: | |
280 | locked = str2bool(locked) |
|
293 | locked = str2bool(locked) | |
281 | try: |
|
294 | try: | |
282 | if locked: |
|
295 | if locked: | |
283 | Repository.lock(repo, user.user_id) |
|
296 | lock_time = time.time() | |
|
297 | Repository.lock(repo, user.user_id, lock_time) | |||
284 | else: |
|
298 | else: | |
|
299 | lock_time = None | |||
285 | Repository.unlock(repo) |
|
300 | Repository.unlock(repo) | |
286 |
|
301 | _d = { | ||
287 | return ('User `%s` set lock state for repo `%s` to `%s`' |
|
302 | 'repo': repo.repo_name, | |
288 | % (user.username, repo.repo_name, locked)) |
|
303 | 'locked': locked, | |
|
304 | 'locked_since': lock_time, | |||
|
305 | 'locked_by': user.username, | |||
|
306 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | |||
|
307 | % (user.username, repo.repo_name, locked)) | |||
|
308 | } | |||
|
309 | return _d | |||
289 | except Exception: |
|
310 | except Exception: | |
290 | log.error(traceback.format_exc()) |
|
311 | log.error(traceback.format_exc()) | |
291 | raise JSONRPCError( |
|
312 | raise JSONRPCError( |
@@ -1007,8 +1007,10 b' class Repository(Base, BaseModel):' | |||||
1007 | return data |
|
1007 | return data | |
1008 |
|
1008 | |||
1009 | @classmethod |
|
1009 | @classmethod | |
1010 | def lock(cls, repo, user_id): |
|
1010 | def lock(cls, repo, user_id, lock_time=None): | |
1011 | repo.locked = [user_id, time.time()] |
|
1011 | if not lock_time: | |
|
1012 | lock_time = time.time() | |||
|
1013 | repo.locked = [user_id, lock_time] | |||
1012 | Session().add(repo) |
|
1014 | Session().add(repo) | |
1013 | Session().commit() |
|
1015 | Session().commit() | |
1014 |
|
1016 |
@@ -12,6 +12,7 b' from rhodecode.model.repo import RepoMod' | |||||
12 | from rhodecode.model.meta import Session |
|
12 | from rhodecode.model.meta import Session | |
13 | from rhodecode.model.scm import ScmModel |
|
13 | from rhodecode.model.scm import ScmModel | |
14 | from rhodecode.model.db import Repository, User |
|
14 | from rhodecode.model.db import Repository, User | |
|
15 | from rhodecode.lib.utils2 import time_to_datetime | |||
15 |
|
16 | |||
16 |
|
17 | |||
17 | API_URL = '/_admin/api' |
|
18 | API_URL = '/_admin/api' | |
@@ -291,8 +292,15 b' class BaseTestApi(object):' | |||||
291 | repoid=self.REPO, |
|
292 | repoid=self.REPO, | |
292 | locked=True) |
|
293 | locked=True) | |
293 | response = api_call(self, params) |
|
294 | response = api_call(self, params) | |
294 | expected = ('User `%s` set lock state for repo `%s` to `%s`' |
|
295 | expected = { | |
295 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) |
|
296 | 'repo': self.REPO, | |
|
297 | 'locked': True, | |||
|
298 | 'locked_since': None, | |||
|
299 | 'locked_by': TEST_USER_ADMIN_LOGIN, | |||
|
300 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | |||
|
301 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | |||
|
302 | } | |||
|
303 | expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | |||
296 | self._compare_ok(id_, expected, given=response.body) |
|
304 | self._compare_ok(id_, expected, given=response.body) | |
297 |
|
305 | |||
298 | def test_api_lock_repo_lock_aquire_by_non_admin(self): |
|
306 | def test_api_lock_repo_lock_aquire_by_non_admin(self): | |
@@ -304,8 +312,15 b' class BaseTestApi(object):' | |||||
304 | repoid=repo_name, |
|
312 | repoid=repo_name, | |
305 | locked=True) |
|
313 | locked=True) | |
306 | response = api_call(self, params) |
|
314 | response = api_call(self, params) | |
307 | expected = ('User `%s` set lock state for repo `%s` to `%s`' |
|
315 | expected = { | |
308 | % (self.TEST_USER_LOGIN, repo_name, True)) |
|
316 | 'repo': repo_name, | |
|
317 | 'locked': True, | |||
|
318 | 'locked_since': None, | |||
|
319 | 'locked_by': self.TEST_USER_LOGIN, | |||
|
320 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | |||
|
321 | % (self.TEST_USER_LOGIN, repo_name, True)) | |||
|
322 | } | |||
|
323 | expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | |||
309 | self._compare_ok(id_, expected, given=response.body) |
|
324 | self._compare_ok(id_, expected, given=response.body) | |
310 | finally: |
|
325 | finally: | |
311 | fixture.destroy_repo(repo_name) |
|
326 | fixture.destroy_repo(repo_name) | |
@@ -339,8 +354,14 b' class BaseTestApi(object):' | |||||
339 | repoid=self.REPO, |
|
354 | repoid=self.REPO, | |
340 | locked=False) |
|
355 | locked=False) | |
341 | response = api_call(self, params) |
|
356 | response = api_call(self, params) | |
342 | expected = ('User `%s` set lock state for repo `%s` to `%s`' |
|
357 | expected = { | |
343 | % (TEST_USER_ADMIN_LOGIN, self.REPO, False)) |
|
358 | 'repo': self.REPO, | |
|
359 | 'locked': False, | |||
|
360 | 'locked_since': None, | |||
|
361 | 'locked_by': TEST_USER_ADMIN_LOGIN, | |||
|
362 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | |||
|
363 | % (TEST_USER_ADMIN_LOGIN, self.REPO, False)) | |||
|
364 | } | |||
344 | self._compare_ok(id_, expected, given=response.body) |
|
365 | self._compare_ok(id_, expected, given=response.body) | |
345 |
|
366 | |||
346 | def test_api_lock_repo_lock_aquire_optional_userid(self): |
|
367 | def test_api_lock_repo_lock_aquire_optional_userid(self): | |
@@ -348,19 +369,33 b' class BaseTestApi(object):' | |||||
348 | repoid=self.REPO, |
|
369 | repoid=self.REPO, | |
349 | locked=True) |
|
370 | locked=True) | |
350 | response = api_call(self, params) |
|
371 | response = api_call(self, params) | |
351 | expected = ('User `%s` set lock state for repo `%s` to `%s`' |
|
372 | expected = { | |
352 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) |
|
373 | 'repo': self.REPO, | |
|
374 | 'locked': True, | |||
|
375 | 'locked_since': None, | |||
|
376 | 'locked_by': TEST_USER_ADMIN_LOGIN, | |||
|
377 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | |||
|
378 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | |||
|
379 | } | |||
|
380 | expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | |||
353 | self._compare_ok(id_, expected, given=response.body) |
|
381 | self._compare_ok(id_, expected, given=response.body) | |
354 |
|
382 | |||
355 | def test_api_lock_repo_lock_optional_locked(self): |
|
383 | def test_api_lock_repo_lock_optional_locked(self): | |
356 | from rhodecode.lib.utils2 import time_to_datetime |
|
|||
357 | _locked_since = json.dumps(time_to_datetime(Repository\ |
|
|||
358 | .get_by_repo_name(self.REPO).locked[1])) |
|
|||
359 | id_, params = _build_data(self.apikey, 'lock', |
|
384 | id_, params = _build_data(self.apikey, 'lock', | |
360 | repoid=self.REPO) |
|
385 | repoid=self.REPO) | |
361 | response = api_call(self, params) |
|
386 | response = api_call(self, params) | |
362 | expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`' |
|
387 | time_ = json.loads(response.body)['result']['locked_since'] | |
363 | % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since)) |
|
388 | expected = { | |
|
389 | 'repo': self.REPO, | |||
|
390 | 'locked': True, | |||
|
391 | 'locked_since': None, | |||
|
392 | 'locked_by': TEST_USER_ADMIN_LOGIN, | |||
|
393 | 'msg': ('Repo `%s` locked by `%s`. ' | |||
|
394 | % (self.REPO, | |||
|
395 | json.dumps(time_to_datetime(time_)))) | |||
|
396 | ||||
|
397 | } | |||
|
398 | expected['locked_since'] = time_ | |||
364 | self._compare_ok(id_, expected, given=response.body) |
|
399 | self._compare_ok(id_, expected, given=response.body) | |
365 |
|
400 | |||
366 | @mock.patch.object(Repository, 'lock', crash) |
|
401 | @mock.patch.object(Repository, 'lock', crash) |
General Comments 0
You need to be logged in to leave comments.
Login now