From ac10ea381e740b246a304d6694f99773bc9070b5 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Oct 08 2018 08:10:04 +0000 Subject: Allow admins to select to ignore existing repositories Signed-off-by: Patrick Uiterwijk --- diff --git a/doc/configuration.rst b/doc/configuration.rst index eb35b98..6b1c056 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1062,6 +1062,17 @@ prevent users from deleting branches in their git repositories. Defaults to: ``True``. +ALLOW_ADMIN_IGNORE_EXISTING_REPOS +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This enables a checkbox "Ignore existing repos" for admins when creating a new +project. When this is checkbox is checked, existing repositories will not cause +project creation to fail. +This could be used to assume responsibility of existing repositories. + +Defaults to: ``False``. + + LOCAL_SSH_KEY ~~~~~~~~~~~~~ diff --git a/pagure/default_config.py b/pagure/default_config.py index 05b4c86..45569d8 100644 --- a/pagure/default_config.py +++ b/pagure/default_config.py @@ -78,6 +78,9 @@ PRIVATE_PROJECTS = True # Enable / Disable deleting branches in the UI ALLOW_DELETE_BRANCH = True +# Allow admins to ignore existing repos when creating a new project +ALLOW_ADMIN_IGNORE_EXISTING_REPOS = False + # Enable / Disable having pagure manage the user's ssh keys LOCAL_SSH_KEY = True diff --git a/pagure/forms.py b/pagure/forms.py index c91879b..5e5ff3b 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -167,6 +167,11 @@ class ProjectForm(ProjectFormSimplified): choices=[], coerce=convert_value, ) + ignore_existing_repos = wtforms.BooleanField( + "Ignore existing repositories", + [wtforms.validators.optional()], + false_values=FALSE_VALUES, + ) repospanner_region = wtforms.SelectField( "repoSpanner Region", [wtforms.validators.optional()], @@ -202,6 +207,13 @@ class ProjectForm(ProjectFormSimplified): ] if not pagure_config.get("USER_NAMESPACE", False): self.namespace.choices.insert(0, ("", "")) + + if not ( + is_admin() + and pagure_config.get("ALLOW_ADMIN_IGNORE_EXISTING_REPOS") + ): + self.ignore_existing_repos = None + if not ( is_admin() and pagure_config.get("REPOSPANNER_NEW_REPO_ADMIN_OVERRIDE") diff --git a/pagure/lib/git.py b/pagure/lib/git.py index cfc5598..5a8429b 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -2280,6 +2280,8 @@ def _create_project_repo(project, region, templ, ignore_existing, repotype): resp = resp.json() _log.debug("Response json: %s", resp) if not resp["Success"]: + if ignore_existing and "already exists" in resp["Error"]: + return None raise Exception( "Error in repoSpanner API call: %s" % resp["Error"] ) @@ -2292,10 +2294,13 @@ def _create_project_repo(project, region, templ, ignore_existing, repotype): if repodir is None: # This repo type is disabled return None - if os.path.exists(repodir) and not ignore_existing: - raise pagure.exceptions.RepoExistsException( - "The %s repo %s already exists" % (repotype, project.path) - ) + if os.path.exists(repodir): + if not ignore_existing: + raise pagure.exceptions.RepoExistsException( + "The %s repo %s already exists" % (repotype, project.path) + ) + else: + return None if repotype == "main": pygit2.init_repository(repodir, bare=True, template_path=templ) diff --git a/pagure/ui/app.py b/pagure/ui/app.py index 51e46c2..d07ad6c 100644 --- a/pagure/ui/app.py +++ b/pagure/ui/app.py @@ -1034,6 +1034,10 @@ def new_project(): repospanner_region = form.repospanner_region.data else: repospanner_region = None + if form.ignore_existing_repos: + ignore_existing_repos = form.ignore_existing_repos.data + else: + ignore_existing_repos = False try: task = pagure.lib.new_project( @@ -1054,6 +1058,7 @@ def new_project(): "OLD_VIEW_COMMIT_ENABLED", False ), user_ns=pagure_config.get("USER_NAMESPACE", False), + ignore_existing_repo=ignore_existing_repos, ) flask.g.session.commit() return pagure.utils.wait_for_task(task)