Show More
@@ -212,7 +212,13 b' INPUT::' | |||
|
212 | 212 | OUTPUT:: |
|
213 | 213 | |
|
214 | 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 | 222 | error : null |
|
217 | 223 | |
|
218 | 224 |
@@ -25,6 +25,7 b'' | |||
|
25 | 25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | 26 | # MA 02110-1301, USA. |
|
27 | 27 | |
|
28 | import time | |
|
28 | 29 | import traceback |
|
29 | 30 | import logging |
|
30 | 31 | |
@@ -265,27 +266,47 b' class ApiController(JSONRPCController):' | |||
|
265 | 266 | lockobj = Repository.getlock(repo) |
|
266 | 267 | |
|
267 | 268 | if lockobj[0] is None: |
|
268 | return ('Repo `%s` not locked. Locked=`False`.' | |
|
269 |
|
|
|
269 | _d = { | |
|
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 | 277 | else: |
|
271 | 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`. ' | |
|
275 | 'Locked since: `%s`' | |
|
276 | % (repo.repo_name, user.username, | |
|
277 | json.dumps(time_to_datetime(time_)))) | |
|
278 | ||
|
291 | # force locked state through a flag | |
|
279 | 292 | else: |
|
280 | 293 | locked = str2bool(locked) |
|
281 | 294 | try: |
|
282 | 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 | 298 | else: |
|
299 | lock_time = None | |
|
285 | 300 | Repository.unlock(repo) |
|
286 | ||
|
287 | return ('User `%s` set lock state for repo `%s` to `%s`' | |
|
288 | % (user.username, repo.repo_name, locked)) | |
|
301 | _d = { | |
|
302 | 'repo': repo.repo_name, | |
|
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 | 310 | except Exception: |
|
290 | 311 | log.error(traceback.format_exc()) |
|
291 | 312 | raise JSONRPCError( |
@@ -1007,8 +1007,10 b' class Repository(Base, BaseModel):' | |||
|
1007 | 1007 | return data |
|
1008 | 1008 | |
|
1009 | 1009 | @classmethod |
|
1010 | def lock(cls, repo, user_id): | |
|
1011 | repo.locked = [user_id, time.time()] | |
|
1010 | def lock(cls, repo, user_id, lock_time=None): | |
|
1011 | if not lock_time: | |
|
1012 | lock_time = time.time() | |
|
1013 | repo.locked = [user_id, lock_time] | |
|
1012 | 1014 | Session().add(repo) |
|
1013 | 1015 | Session().commit() |
|
1014 | 1016 |
@@ -12,6 +12,7 b' from rhodecode.model.repo import RepoMod' | |||
|
12 | 12 | from rhodecode.model.meta import Session |
|
13 | 13 | from rhodecode.model.scm import ScmModel |
|
14 | 14 | from rhodecode.model.db import Repository, User |
|
15 | from rhodecode.lib.utils2 import time_to_datetime | |
|
15 | 16 | |
|
16 | 17 | |
|
17 | 18 | API_URL = '/_admin/api' |
@@ -291,8 +292,15 b' class BaseTestApi(object):' | |||
|
291 | 292 | repoid=self.REPO, |
|
292 | 293 | locked=True) |
|
293 | 294 | response = api_call(self, params) |
|
294 | expected = ('User `%s` set lock state for repo `%s` to `%s`' | |
|
295 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | |
|
295 | expected = { | |
|
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 | 304 | self._compare_ok(id_, expected, given=response.body) |
|
297 | 305 | |
|
298 | 306 | def test_api_lock_repo_lock_aquire_by_non_admin(self): |
@@ -304,8 +312,15 b' class BaseTestApi(object):' | |||
|
304 | 312 | repoid=repo_name, |
|
305 | 313 | locked=True) |
|
306 | 314 | response = api_call(self, params) |
|
307 | expected = ('User `%s` set lock state for repo `%s` to `%s`' | |
|
308 | % (self.TEST_USER_LOGIN, repo_name, True)) | |
|
315 | expected = { | |
|
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 | 324 | self._compare_ok(id_, expected, given=response.body) |
|
310 | 325 | finally: |
|
311 | 326 | fixture.destroy_repo(repo_name) |
@@ -339,8 +354,14 b' class BaseTestApi(object):' | |||
|
339 | 354 | repoid=self.REPO, |
|
340 | 355 | locked=False) |
|
341 | 356 | response = api_call(self, params) |
|
342 | expected = ('User `%s` set lock state for repo `%s` to `%s`' | |
|
343 | % (TEST_USER_ADMIN_LOGIN, self.REPO, False)) | |
|
357 | expected = { | |
|
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 | 365 | self._compare_ok(id_, expected, given=response.body) |
|
345 | 366 | |
|
346 | 367 | def test_api_lock_repo_lock_aquire_optional_userid(self): |
@@ -348,19 +369,33 b' class BaseTestApi(object):' | |||
|
348 | 369 | repoid=self.REPO, |
|
349 | 370 | locked=True) |
|
350 | 371 | response = api_call(self, params) |
|
351 | expected = ('User `%s` set lock state for repo `%s` to `%s`' | |
|
352 | % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | |
|
372 | expected = { | |
|
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 | 381 | self._compare_ok(id_, expected, given=response.body) |
|
354 | 382 | |
|
355 | 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 | 384 | id_, params = _build_data(self.apikey, 'lock', |
|
360 | 385 | repoid=self.REPO) |
|
361 | 386 | response = api_call(self, params) |
|
362 | expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`' | |
|
363 | % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since)) | |
|
387 | time_ = json.loads(response.body)['result']['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 | 399 | self._compare_ok(id_, expected, given=response.body) |
|
365 | 400 | |
|
366 | 401 | @mock.patch.object(Repository, 'lock', crash) |
General Comments 0
You need to be logged in to leave comments.
Login now