diff --git a/lib/tool_shed/util/repository_util.py b/lib/tool_shed/util/repository_util.py index e698bbb345ad..91629ee9b53d 100644 --- a/lib/tool_shed/util/repository_util.py +++ b/lib/tool_shed/util/repository_util.py @@ -194,39 +194,39 @@ def create_repository( long_description=long_description, user_id=user_id, ) + sa_session.add(repository) if category_ids: # Create category associations for category_id in category_ids: category = sa_session.query(app.model.Category).get(app.security.decode_id(category_id)) rca = app.model.RepositoryCategoryAssociation(repository, category) sa_session.add(rca) - sa_session.add(repository) + # Create an admin role for the repository. + create_repository_admin_role(app, repository) + # Create a temporary repo_path on disk. + with tempfile.TemporaryDirectory( + dir=app.config.file_path, prefix="f{repository.user.username}-{repository.name}" + ) as repository_path: + # Create the local repository. + init_repository(repo_path=repository_path) + # Create a .hg/hgrc file for the local repository. + create_hgrc_file(app, repository) + # Add an entry in the hgweb.config file for the local repository. + lhs = f"{app.config.hgweb_repo_prefix}{repository.user.username}/{repository.name}" + # Flush to get the id. session = sa_session() with transaction(session): - # Create an admin role for the repository. - create_repository_admin_role(app, repository) - # Create a temporary repo_path on disk. - with tempfile.TemporaryDirectory( - dir=app.config.file_path, prefix="f{repository.user.username}-{repository.name}" - ) as repository_path: - # Create the local repository. - init_repository(repo_path=repository_path) - # Create a .hg/hgrc file for the local repository. - create_hgrc_file(app, repository) - # Add an entry in the hgweb.config file for the local repository. - lhs = f"{app.config.hgweb_repo_prefix}{repository.user.username}/{repository.name}" - # Flush to get the id. - session.commit() - dir = os.path.join(app.config.file_path, *util.directory_hash_id(repository.id)) - # Define repo name inside hashed directory. - final_repository_path = os.path.join(dir, "repo_%d" % repository.id) - # Create final repository directory. - if not os.path.exists(repository_path): - os.makedirs(repository_path) - os.rename(repository_path, final_repository_path) - app.hgweb_config_manager.add_entry(lhs, final_repository_path) - # Update the repository registry. - app.repository_registry.add_entry(repository) + session.commit() + dir = os.path.join(app.config.file_path, *util.directory_hash_id(repository.id)) + # Define repo name inside hashed directory. + final_repository_path = os.path.join(dir, "repo_%d" % repository.id) + # Create final repository directory. + if not os.path.exists(repository_path): + os.makedirs(repository_path) + os.rename(repository_path, final_repository_path) + app.hgweb_config_manager.add_entry(lhs, final_repository_path) + # Update the repository registry. + app.repository_registry.add_entry(repository) message = f"Repository {escape(str(repository.name))} has been created." return repository, message