repository: add check preventing of removal of repo with attached pull requests....
marcink -
r3089:e665305d default
Not Reviewed
Show More
Add another comment
TODOs: 0 unresolved 0 Resolved
COMMENTS: 0 General 0 Inline
@@ -29,7 +29,7
29 from rhodecode.lib.auth import (
29 from rhodecode.lib.auth import (
30 LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired,
30 LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired,
31 HasRepoPermissionAny)
31 HasRepoPermissionAny)
32 from rhodecode.lib.exceptions import AttachedForksError
32 from rhodecode.lib.exceptions import AttachedForksError, AttachedPullRequestsError
33 from rhodecode.lib.utils2 import safe_int
33 from rhodecode.lib.utils2 import safe_int
34 from rhodecode.lib.vcs import RepositoryError
34 from rhodecode.lib.vcs import RepositoryError
35 from rhodecode.model.db import Session, UserFollowing, User, Repository
35 from rhodecode.model.db import Session, UserFollowing, User, Repository
@@ -80,22 +80,23
80 """
80 """
81 _ = self.request.translate
81 _ = self.request.translate
82 handle_forks = self.request.POST.get('forks', None)
82 handle_forks = self.request.POST.get('forks', None)
83 if handle_forks == 'detach_forks':
84 handle_forks = 'detach'
85 elif handle_forks == 'delete_forks':
86 handle_forks = 'delete'
83
87
84 try:
88 try:
89 old_data = self.db_repo.get_api_data()
90 RepoModel().delete(self.db_repo, forks=handle_forks)
91
85 _forks = self.db_repo.forks.count()
92 _forks = self.db_repo.forks.count()
86 if _forks and handle_forks:
93 if _forks and handle_forks:
87 if handle_forks == 'detach_forks':
94 if handle_forks == 'detach_forks':
88 handle_forks = 'detach'
89 h.flash(_('Detached %s forks') % _forks, category='success')
95 h.flash(_('Detached %s forks') % _forks, category='success')
90 elif handle_forks == 'delete_forks':
96 elif handle_forks == 'delete_forks':
91 handle_forks = 'delete'
92 h.flash(_('Deleted %s forks') % _forks, category='success')
97 h.flash(_('Deleted %s forks') % _forks, category='success')
93
98
94 old_data = self.db_repo.get_api_data()
99 repo = audit_logger.RepoWrap(repo_id=None, repo_name=self.db_repo.repo_name)
95 RepoModel().delete(self.db_repo, forks=handle_forks)
96
97 repo = audit_logger.RepoWrap(repo_id=None,
98 repo_name=self.db_repo.repo_name)
99 audit_logger.store_web(
100 audit_logger.store_web(
100 'repo.delete', action_data={'old_data': old_data},
101 'repo.delete', action_data={'old_data': old_data},
101 user=self._rhodecode_user, repo=repo)
102 user=self._rhodecode_user, repo=repo)
@@ -118,6 +119,20
118 # redirect to advanced for forks handle action ?
119 # redirect to advanced for forks handle action ?
119 raise HTTPFound(repo_advanced_url)
120 raise HTTPFound(repo_advanced_url)
120
121
122 except AttachedPullRequestsError:
123 repo_advanced_url = h.route_path(
124 'edit_repo_advanced', repo_name=self.db_repo_name,
125 _anchor='advanced-delete')
126 attached_prs = len(self.db_repo.pull_requests_source +
127 self.db_repo.pull_requests_target)
128 h.flash(
129 _('Cannot delete `{repo}` it still contains {num} attached pull requests. '
130 'Consider archiving the repository instead.').format(
131 repo=self.db_repo_name, num=attached_prs), category='warning')
132
133 # redirect to advanced for forks handle action ?
134 raise HTTPFound(repo_advanced_url)
135
121 except Exception:
136 except Exception:
122 log.exception("Exception during deletion of repository")
137 log.exception("Exception during deletion of repository")
123 h.flash(_('An error occurred during deletion of `%s`')
138 h.flash(_('An error occurred during deletion of `%s`')
@@ -70,6 +70,10
70 pass
70 pass
71
71
72
72
73 class AttachedPullRequestsError(Exception):
74 pass
75
76
73 class RepoGroupAssignmentError(Exception):
77 class RepoGroupAssignmentError(Exception):
74 pass
78 pass
75
79
@@ -32,7 +32,7
32 from rhodecode import events
32 from rhodecode import events
33 from rhodecode.lib.auth import HasUserGroupPermissionAny
33 from rhodecode.lib.auth import HasUserGroupPermissionAny
34 from rhodecode.lib.caching_query import FromCache
34 from rhodecode.lib.caching_query import FromCache
35 from rhodecode.lib.exceptions import AttachedForksError
35 from rhodecode.lib.exceptions import AttachedForksError, AttachedPullRequestsError
36 from rhodecode.lib.hooks_base import log_delete_repository
36 from rhodecode.lib.hooks_base import log_delete_repository
37 from rhodecode.lib.user_log_filter import user_log_filter
37 from rhodecode.lib.user_log_filter import user_log_filter
38 from rhodecode.lib.utils import make_db_config
38 from rhodecode.lib.utils import make_db_config
@@ -608,7 +608,7
608 from rhodecode.lib.celerylib import tasks, run_task
608 from rhodecode.lib.celerylib import tasks, run_task
609 return run_task(tasks.create_repo_fork, form_data, cur_user)
609 return run_task(tasks.create_repo_fork, form_data, cur_user)
610
610
611 def delete(self, repo, forks=None, fs_remove=True, cur_user=None):
611 def delete(self, repo, forks=None, pull_requests=None, fs_remove=True, cur_user=None):
612 """
612 """
613 Delete given repository, forks parameter defines what do do with
613 Delete given repository, forks parameter defines what do do with
614 attached forks. Throws AttachedForksError if deleted repo has attached
614 attached forks. Throws AttachedForksError if deleted repo has attached
@@ -632,6 +632,12
632 elif [f for f in repo.forks]:
632 elif [f for f in repo.forks]:
633 raise AttachedForksError()
633 raise AttachedForksError()
634
634
635 # check for pull requests
636 pr_sources = repo.pull_requests_source
637 pr_targets = repo.pull_requests_target
638 if pull_requests != 'delete' and (pr_sources or pr_targets):
639 raise AttachedPullRequestsError()
640
635 old_repo_dict = repo.get_dict()
641 old_repo_dict = repo.get_dict()
636 events.trigger(events.RepoPreDeleteEvent(repo))
642 events.trigger(events.RepoPreDeleteEvent(repo))
637 try:
643 try:
@@ -7,6 +7,8
7 (_('Updated on'), h.format_date(c.rhodecode_db_repo.updated_on), '', ''),
7 (_('Updated on'), h.format_date(c.rhodecode_db_repo.updated_on), '', ''),
8 (_('Cached Commit id'), lambda: h.link_to(c.rhodecode_db_repo.changeset_cache.get('short_id'), h.route_path('repo_commit',repo_name=c.repo_name,commit_id=c.rhodecode_db_repo.changeset_cache.get('raw_id'))), '', ''),
8 (_('Cached Commit id'), lambda: h.link_to(c.rhodecode_db_repo.changeset_cache.get('short_id'), h.route_path('repo_commit',repo_name=c.repo_name,commit_id=c.rhodecode_db_repo.changeset_cache.get('raw_id'))), '', ''),
9 (_('Attached scoped tokens'), len(c.rhodecode_db_repo.scoped_tokens), '', [x.user for x in c.rhodecode_db_repo.scoped_tokens]),
9 (_('Attached scoped tokens'), len(c.rhodecode_db_repo.scoped_tokens), '', [x.user for x in c.rhodecode_db_repo.scoped_tokens]),
10 (_('Pull requests source'), len(c.rhodecode_db_repo.pull_requests_source), '', ['pr_id:{}, repo:{}'.format(x.pull_request_id,x.source_repo.repo_name) for x in c.rhodecode_db_repo.pull_requests_source]),
11 (_('Pull requests target'), len(c.rhodecode_db_repo.pull_requests_target), '', ['pr_id:{}, repo:{}'.format(x.pull_request_id,x.target_repo.repo_name) for x in c.rhodecode_db_repo.pull_requests_target]),
10 ]
12 ]
11 %>
13 %>
12
14
@@ -131,6 +133,18
131 %endif
133 %endif
132 </td>
134 </td>
133 </tr>
135 </tr>
136 <% attached_prs = len(c.rhodecode_db_repo.pull_requests_source + c.rhodecode_db_repo.pull_requests_target) %>
137 % if c.rhodecode_db_repo.pull_requests_source or c.rhodecode_db_repo.pull_requests_target:
138 <tr>
139 <td>
140 ${_ungettext('This repository has %s attached pull request.', 'This repository has %s attached pull requests.', attached_prs) % attached_prs}
141 <br/>
142 ${_('Consider to archive this repository instead.')}
143 </td>
144 <td></td>
145 <td></td>
146 </tr>
147 % endif
134 </table>
148 </table>
135 <div style="margin: 0 0 20px 0" class="fake-space"></div>
149 <div style="margin: 0 0 20px 0" class="fake-space"></div>
136
150
@@ -226,7 +226,7
226 return r
226 return r
227
227
228 def destroy_repo(self, repo_name, **kwargs):
228 def destroy_repo(self, repo_name, **kwargs):
229 RepoModel().delete(repo_name, **kwargs)
229 RepoModel().delete(repo_name, pull_requests='delete', **kwargs)
230 Session().commit()
230 Session().commit()
231
231
232 def destroy_repo_on_filesystem(self, repo_name):
232 def destroy_repo_on_filesystem(self, repo_name):
Comments 0
You need to be logged in to leave comments. Login now