##// END OF EJS Templates
recursive forks detach...
marcink -
r3641:b4497964 beta
parent child Browse files
Show More
@@ -848,8 +848,10 b' OUTPUT::'
848 delete_repo
848 delete_repo
849 -----------
849 -----------
850
850
851 Deletes a repository. This command can be executed only using api_key belonging to user with admin
851 Deletes a repository. This command can be executed only using api_key belonging
852 rights or regular user that have admin access to repository.
852 to user with admin rights or regular user that have admin access to repository.
853 When `forks` param is set it's possible to detach or delete forks of deleting
854 repository
853
855
854
856
855 INPUT::
857 INPUT::
@@ -858,7 +860,8 b' INPUT::'
858 api_key : "<api_key>"
860 api_key : "<api_key>"
859 method : "delete_repo"
861 method : "delete_repo"
860 args: {
862 args: {
861 "repoid" : "<reponame or repo_id>"
863 "repoid" : "<reponame or repo_id>",
864 "forks" : "`delete` or `detach` = Optional(None)"
862 }
865 }
863
866
864 OUTPUT::
867 OUTPUT::
@@ -50,6 +50,7 b' from rhodecode.model.scm import ScmModel'
50 from rhodecode.model.repo import RepoModel
50 from rhodecode.model.repo import RepoModel
51 from rhodecode.lib.compat import json
51 from rhodecode.lib.compat import json
52 from sqlalchemy.sql.expression import func
52 from sqlalchemy.sql.expression import func
53 from rhodecode.lib.exceptions import AttachedForksError
53
54
54 log = logging.getLogger(__name__)
55 log = logging.getLogger(__name__)
55
56
@@ -302,38 +303,26 b' class ReposController(BaseRepoController'
302 return redirect(url('repos'))
303 return redirect(url('repos'))
303 try:
304 try:
304 _forks = repo.forks.count()
305 _forks = repo.forks.count()
306 handle_forks = None
305 if _forks and request.POST.get('forks'):
307 if _forks and request.POST.get('forks'):
306 do = request.POST['forks']
308 do = request.POST['forks']
307 if do == 'detach_forks':
309 if do == 'detach_forks':
308 for r in repo.forks:
310 handle_forks = 'detach'
309 log.debug('Detaching fork %s from repo %s' % (r, repo))
310 r.fork = None
311 Session().add(r)
312 h.flash(_('Detached %s forks') % _forks, category='success')
311 h.flash(_('Detached %s forks') % _forks, category='success')
313 elif do == 'delete_forks':
312 elif do == 'delete_forks':
314 for r in repo.forks:
313 handle_forks = 'delete'
315 log.debug('Deleting fork %s of repo %s' % (r, repo))
316 repo_model.delete(r)
317 h.flash(_('Deleted %s forks') % _forks, category='success')
314 h.flash(_('Deleted %s forks') % _forks, category='success')
315 repo_model.delete(repo, forks=handle_forks)
318 action_logger(self.rhodecode_user, 'admin_deleted_repo',
316 action_logger(self.rhodecode_user, 'admin_deleted_repo',
319 repo_name, self.ip_addr, self.sa)
317 repo_name, self.ip_addr, self.sa)
320 repo_model.delete(repo)
321 invalidate_cache('get_repo_cached_%s' % repo_name)
318 invalidate_cache('get_repo_cached_%s' % repo_name)
322 h.flash(_('Deleted repository %s') % repo_name, category='success')
319 h.flash(_('Deleted repository %s') % repo_name, category='success')
323 Session().commit()
320 Session().commit()
324 except IntegrityError, e:
321 except AttachedForksError:
325 if e.message.find('repositories_fork_id_fkey') != -1:
322 h.flash(_('Cannot delete %s it still contains attached forks')
326 log.error(traceback.format_exc())
323 % repo_name, category='warning')
327 h.flash(_('Cannot delete %s it still contains attached '
328 'forks') % repo_name,
329 category='warning')
330 else:
331 log.error(traceback.format_exc())
332 h.flash(_('An error occurred during '
333 'deletion of %s') % repo_name,
334 category='error')
335
324
336 except Exception, e:
325 except Exception:
337 log.error(traceback.format_exc())
326 log.error(traceback.format_exc())
338 h.flash(_('An error occurred during deletion of %s') % repo_name,
327 h.flash(_('An error occurred during deletion of %s') % repo_name,
339 category='error')
328 category='error')
@@ -853,12 +853,13 b' class ApiController(JSONRPCController):'
853 fork_name)
853 fork_name)
854 )
854 )
855
855
856 def delete_repo(self, apiuser, repoid):
856 def delete_repo(self, apiuser, repoid, forks=Optional(None)):
857 """
857 """
858 Deletes a given repository
858 Deletes a given repository
859
859
860 :param apiuser:
860 :param apiuser:
861 :param repoid:
861 :param repoid:
862 :param forks: detach or delete, what do do with attached forks for repo
862 """
863 """
863 repo = get_repo_or_error(repoid)
864 repo = get_repo_or_error(repoid)
864
865
@@ -866,13 +867,26 b' class ApiController(JSONRPCController):'
866 # check if we have admin permission for this repo !
867 # check if we have admin permission for this repo !
867 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser,
868 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser,
868 repo_name=repo.repo_name) is False:
869 repo_name=repo.repo_name) is False:
869 raise JSONRPCError('repository `%s` does not exist' % (repoid))
870 raise JSONRPCError('repository `%s` does not exist' % (repoid))
870
871
871 try:
872 try:
872 RepoModel().delete(repo)
873 handle_forks = Optional.extract(forks)
874 _forks_msg = ''
875 _forks = [f for f in repo.forks]
876 if handle_forks == 'detach':
877 _forks_msg = ' ' + _('Detached %s forks') % len(_forks)
878 elif handle_forks == 'delete':
879 _forks_msg = ' ' + _('Deleted %s forks') % len(_forks)
880 elif _forks:
881 raise JSONRPCError(
882 'Cannot delete `%s` it still contains attached forks'
883 % repo.repo_name
884 )
885
886 RepoModel().delete(repo, forks=forks)
873 Session().commit()
887 Session().commit()
874 return dict(
888 return dict(
875 msg='Deleted repository `%s`' % repo.repo_name,
889 msg='Deleted repository `%s`%s' % (repo.repo_name, _forks_msg),
876 success=True
890 success=True
877 )
891 )
878 except Exception:
892 except Exception:
@@ -58,6 +58,10 b' class StatusChangeOnClosedPullRequestErr'
58 pass
58 pass
59
59
60
60
61 class AttachedForksError(Exception):
62 pass
63
64
61 class HTTPLockedRC(HTTPClientError):
65 class HTTPLockedRC(HTTPClientError):
62 """
66 """
63 Special Exception For locked Repos in RhodeCode, the return code can
67 Special Exception For locked Repos in RhodeCode, the return code can
@@ -42,6 +42,7 b' from rhodecode.model.db import Repositor'
42 RhodeCodeSetting, RepositoryField
42 RhodeCodeSetting, RepositoryField
43 from rhodecode.lib import helpers as h
43 from rhodecode.lib import helpers as h
44 from rhodecode.lib.auth import HasRepoPermissionAny
44 from rhodecode.lib.auth import HasRepoPermissionAny
45 from rhodecode.lib.exceptions import AttachedForksError
45
46
46 log = logging.getLogger(__name__)
47 log = logging.getLogger(__name__)
47
48
@@ -465,9 +466,27 b' class RepoModel(BaseModel):'
465 from rhodecode.lib.celerylib import tasks, run_task
466 from rhodecode.lib.celerylib import tasks, run_task
466 run_task(tasks.create_repo_fork, form_data, cur_user)
467 run_task(tasks.create_repo_fork, form_data, cur_user)
467
468
468 def delete(self, repo):
469 def delete(self, repo, forks=None):
470 """
471 Delete given repository, forks parameter defines what do do with
472 attached forks. Throws AttachedForksError if deleted repo has attached
473 forks
474
475 :param repo:
476 :param forks: str 'delete' or 'detach'
477 """
469 repo = self._get_repo(repo)
478 repo = self._get_repo(repo)
470 if repo:
479 if repo:
480 if forks == 'detach':
481 for r in repo.forks:
482 r.fork = None
483 self.sa.add(r)
484 elif forks == 'delete':
485 for r in repo.forks:
486 self.delete(r, forks='delete')
487 elif [f for f in repo.forks]:
488 raise AttachedForksError()
489
471 old_repo_dict = repo.get_dict()
490 old_repo_dict = repo.get_dict()
472 owner = repo.user
491 owner = repo.user
473 try:
492 try:
General Comments 0
You need to be logged in to leave comments. Login now