Show More
@@ -558,26 +558,48 b' class ApiController(JSONRPCController):' | |||||
558 | log.error(traceback.format_exc()) |
|
558 | log.error(traceback.format_exc()) | |
559 | raise JSONRPCError('failed to create repository `%s`' % repo_name) |
|
559 | raise JSONRPCError('failed to create repository `%s`' % repo_name) | |
560 |
|
560 | |||
561 |
|
|
561 | @HasPermissionAnyDecorator('hg.admin') | |
562 |
|
|
562 | def fork_repo(self, apiuser, repoid, fork_name, owner, | |
563 | # repo = get_repo_or_error(repoid) |
|
563 | description=Optional(''), copy_permissions=Optional(False), | |
564 | # |
|
564 | private=Optional(False), landing_rev=Optional('tip')): | |
565 | # try: |
|
565 | repo = get_repo_or_error(repoid) | |
566 | # form_data = dict( |
|
566 | repo_name = repo.repo_name | |
567 | # |
|
567 | owner = get_user_or_error(owner) | |
568 | # ) |
|
568 | ||
569 | # RepoModel().create_fork(form_data, cur_user=apiuser) |
|
569 | _repo = RepoModel().get_by_repo_name(fork_name) | |
570 | # return dict( |
|
570 | if _repo: | |
571 | # msg='Created fork of `%s` as `%s`' % (repo.repo_name, |
|
571 | type_ = 'fork' if _repo.fork else 'repo' | |
572 | # fork_name), |
|
572 | raise JSONRPCError("%s `%s` already exist" % (type_, fork_name)) | |
573 | # success=True |
|
573 | ||
574 |
|
|
574 | try: | |
575 | # except Exception: |
|
575 | # create structure of groups and return the last group | |
576 | # log.error(traceback.format_exc()) |
|
576 | group = map_groups(fork_name) | |
577 | # raise JSONRPCError( |
|
577 | ||
578 | # 'failed to fork repository `%s` as `%s`' % (repo.repo_name, |
|
578 | form_data = dict( | |
579 | # fork_name) |
|
579 | repo_name=fork_name, | |
580 | # ) |
|
580 | repo_name_full=fork_name, | |
|
581 | repo_group=group, | |||
|
582 | repo_type=repo.repo_type, | |||
|
583 | description=Optional.extract(description), | |||
|
584 | private=Optional.extract(private), | |||
|
585 | copy_permissions=Optional.extract(copy_permissions), | |||
|
586 | landing_rev=Optional.extract(landing_rev), | |||
|
587 | update_after_clone=False, | |||
|
588 | fork_parent_id=repo.repo_id, | |||
|
589 | ) | |||
|
590 | RepoModel().create_fork(form_data, cur_user=owner) | |||
|
591 | return dict( | |||
|
592 | msg='Created fork of `%s` as `%s`' % (repo.repo_name, | |||
|
593 | fork_name), | |||
|
594 | success=True # cannot return the repo data here since fork | |||
|
595 | # cann be done async | |||
|
596 | ) | |||
|
597 | except Exception: | |||
|
598 | log.error(traceback.format_exc()) | |||
|
599 | raise JSONRPCError( | |||
|
600 | 'failed to fork repository `%s` as `%s`' % (repo_name, | |||
|
601 | fork_name) | |||
|
602 | ) | |||
581 |
|
603 | |||
582 | @HasPermissionAnyDecorator('hg.admin') |
|
604 | @HasPermissionAnyDecorator('hg.admin') | |
583 | def delete_repo(self, apiuser, repoid): |
|
605 | def delete_repo(self, apiuser, repoid): |
@@ -68,6 +68,15 b' def create_repo(repo_name, repo_type):' | |||||
68 | return r |
|
68 | return r | |
69 |
|
69 | |||
70 |
|
70 | |||
|
71 | def create_fork(fork_name, fork_type, fork_of): | |||
|
72 | fork = RepoModel(Session())._get_repo(fork_of) | |||
|
73 | r = create_repo(fork_name, fork_type) | |||
|
74 | r.fork = fork | |||
|
75 | Session().add(r) | |||
|
76 | Session().commit() | |||
|
77 | return r | |||
|
78 | ||||
|
79 | ||||
71 | def destroy_repo(repo_name): |
|
80 | def destroy_repo(repo_name): | |
72 | RepoModel().delete(repo_name) |
|
81 | RepoModel().delete(repo_name) | |
73 | Session().commit() |
|
82 | Session().commit() | |
@@ -571,6 +580,7 b' class BaseTestApi(object):' | |||||
571 | def test_api_delete_repo_exception_occurred(self): |
|
580 | def test_api_delete_repo_exception_occurred(self): | |
572 | repo_name = 'api_delete_me' |
|
581 | repo_name = 'api_delete_me' | |
573 | create_repo(repo_name, self.REPO_TYPE) |
|
582 | create_repo(repo_name, self.REPO_TYPE) | |
|
583 | try: | |||
574 | with mock.patch.object(RepoModel, 'delete', crash): |
|
584 | with mock.patch.object(RepoModel, 'delete', crash): | |
575 | id_, params = _build_data(self.apikey, 'delete_repo', |
|
585 | id_, params = _build_data(self.apikey, 'delete_repo', | |
576 | repoid=repo_name,) |
|
586 | repoid=repo_name,) | |
@@ -579,10 +589,89 b' class BaseTestApi(object):' | |||||
579 |
|
589 | |||
580 | expected = 'failed to delete repository `%s`' % repo_name |
|
590 | expected = 'failed to delete repository `%s`' % repo_name | |
581 | self._compare_error(id_, expected, given=response.body) |
|
591 | self._compare_error(id_, expected, given=response.body) | |
|
592 | finally: | |||
582 | destroy_repo(repo_name) |
|
593 | destroy_repo(repo_name) | |
583 |
|
594 | |||
584 | def test_api_fork_repo(self): |
|
595 | def test_api_fork_repo(self): | |
585 | self.failIf(False, 'TODO:') |
|
596 | fork_name = 'api-repo-fork' | |
|
597 | id_, params = _build_data(self.apikey, 'fork_repo', | |||
|
598 | repoid=self.REPO, | |||
|
599 | fork_name=fork_name, | |||
|
600 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
601 | ) | |||
|
602 | response = self.app.post(API_URL, content_type='application/json', | |||
|
603 | params=params) | |||
|
604 | ||||
|
605 | ret = { | |||
|
606 | 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, | |||
|
607 | fork_name), | |||
|
608 | 'success': True | |||
|
609 | } | |||
|
610 | expected = ret | |||
|
611 | self._compare_ok(id_, expected, given=response.body) | |||
|
612 | destroy_repo(fork_name) | |||
|
613 | ||||
|
614 | def test_api_fork_repo_unknown_owner(self): | |||
|
615 | fork_name = 'api-repo-fork' | |||
|
616 | owner = 'i-dont-exist' | |||
|
617 | id_, params = _build_data(self.apikey, 'fork_repo', | |||
|
618 | repoid=self.REPO, | |||
|
619 | fork_name=fork_name, | |||
|
620 | owner=owner, | |||
|
621 | ) | |||
|
622 | response = self.app.post(API_URL, content_type='application/json', | |||
|
623 | params=params) | |||
|
624 | expected = 'user `%s` does not exist' % owner | |||
|
625 | self._compare_error(id_, expected, given=response.body) | |||
|
626 | ||||
|
627 | def test_api_fork_repo_fork_exists(self): | |||
|
628 | fork_name = 'api-repo-fork' | |||
|
629 | create_fork(fork_name, self.REPO_TYPE, self.REPO) | |||
|
630 | ||||
|
631 | try: | |||
|
632 | fork_name = 'api-repo-fork' | |||
|
633 | ||||
|
634 | id_, params = _build_data(self.apikey, 'fork_repo', | |||
|
635 | repoid=self.REPO, | |||
|
636 | fork_name=fork_name, | |||
|
637 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
638 | ) | |||
|
639 | response = self.app.post(API_URL, content_type='application/json', | |||
|
640 | params=params) | |||
|
641 | ||||
|
642 | expected = "fork `%s` already exist" % fork_name | |||
|
643 | self._compare_error(id_, expected, given=response.body) | |||
|
644 | finally: | |||
|
645 | destroy_repo(fork_name) | |||
|
646 | ||||
|
647 | def test_api_fork_repo_repo_exists(self): | |||
|
648 | fork_name = self.REPO | |||
|
649 | ||||
|
650 | id_, params = _build_data(self.apikey, 'fork_repo', | |||
|
651 | repoid=self.REPO, | |||
|
652 | fork_name=fork_name, | |||
|
653 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
654 | ) | |||
|
655 | response = self.app.post(API_URL, content_type='application/json', | |||
|
656 | params=params) | |||
|
657 | ||||
|
658 | expected = "repo `%s` already exist" % fork_name | |||
|
659 | self._compare_error(id_, expected, given=response.body) | |||
|
660 | ||||
|
661 | @mock.patch.object(RepoModel, 'create_fork', crash) | |||
|
662 | def test_api_fork_repo_exception_occurred(self): | |||
|
663 | fork_name = 'api-repo-fork' | |||
|
664 | id_, params = _build_data(self.apikey, 'fork_repo', | |||
|
665 | repoid=self.REPO, | |||
|
666 | fork_name=fork_name, | |||
|
667 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
668 | ) | |||
|
669 | response = self.app.post(API_URL, content_type='application/json', | |||
|
670 | params=params) | |||
|
671 | ||||
|
672 | expected = 'failed to fork repository `%s` as `%s`' % (self.REPO, | |||
|
673 | fork_name) | |||
|
674 | self._compare_error(id_, expected, given=response.body) | |||
586 |
|
675 | |||
587 | def test_api_get_users_group(self): |
|
676 | def test_api_get_users_group(self): | |
588 | id_, params = _build_data(self.apikey, 'get_users_group', |
|
677 | id_, params = _build_data(self.apikey, 'get_users_group', |
General Comments 0
You need to be logged in to leave comments.
Login now