Exemple #1
0
    def _getGitNamespaceExtras(self, path, requester):
        """Get the namespace, repository name, and callback for the path.

        If the path defines a full Git repository path including the owner
        and repository name, then the namespace that is returned is the
        namespace for the owner and the repository target specified.

        If the path uses a shortcut name, then we only allow the requester
        to create a repository if they have permission to make the newly
        created repository the default for the shortcut target.  If there is
        an existing default repository, then GitRepositoryExists is raised.
        The repository name that is used is determined by the namespace as
        the first unused name starting with the leaf part of the namespace
        name.  In this case, the repository owner will be set to the
        namespace owner, and distribution source package namespaces are
        currently disallowed due to the complexities of ownership there.
        """
        try:
            namespace_name, repository_name = split_git_unique_name(path)
        except InvalidNamespace:
            namespace_name = path
            repository_name = None
        owner, target, repository = getUtility(IGitTraverser).traverse_path(
            namespace_name)
        # split_git_unique_name should have left us without a repository name.
        assert repository is None
        if owner is None:
            if not get_git_namespace(target, None).allow_push_to_set_default:
                raise GitRepositoryCreationForbidden(
                    "Cannot automatically set the default repository for this "
                    "target; push to a named repository instead.")
            repository_owner = target.owner
        else:
            repository_owner = owner
        namespace = get_git_namespace(target, repository_owner)
        if repository_name is None and not namespace.has_defaults:
            raise InvalidNamespace(path)
        if repository_name is None:
            def default_func(new_repository):
                if owner is None:
                    self.repository_set.setDefaultRepository(
                        target, new_repository)
                if (owner is not None or
                    self.repository_set.getDefaultRepositoryForOwner(
                        repository_owner, target) is None):
                    self.repository_set.setDefaultRepositoryForOwner(
                        repository_owner, target, new_repository, requester)

            repository_name = namespace.findUnusedName(target.name)
            return namespace, repository_name, default_func
        else:
            return namespace, repository_name, None
 def default_information_type(self):
     """The default information type for new repos."""
     if self.user is None:
         return None
     namespace = get_git_namespace(self.target, self.user)
     policy = IGitNamespacePolicy(namespace)
     return policy.getDefaultInformationType(self.user)
    def assertPolicyCheckRaises(self, error, creator, owner):
        """Assert that the policy check raises an exception.

        :param error: The exception class that should be raised.
        :param creator: The user creating the repository.
        :param owner: The person or team that will be the owner of the
            repository.
        """
        policy = IGitNamespacePolicy(get_git_namespace(owner, owner))
        self.assertRaises(error, policy.validateRegistrant, registrant=creator)
 def test_owner_default_to_personal(self):
     # Moving an owner_default repository to a personal namespace is
     # permitted, and the flag is cleared.
     repository = self.factory.makeGitRepository()
     repository.setOwnerDefault(True)
     namespace = get_git_namespace(repository.owner, repository.owner)
     namespace.moveRepository(
         repository, getUtility(ILaunchpadCelebrities).admin.teamowner)
     self.assertNamespacesEqual(namespace, repository.namespace)
     self.assertFalse(repository.owner_default)
    def assertPublic(self, creator, owner):
        """Assert that the policy check would result in a public repository.

        :param creator: The user creating the repository.
        :param owner: The person or team that will be the owner of the
            repository.
        """
        namespace = get_git_namespace(owner, owner)
        self.assertNotIn(
            InformationType.PROPRIETARY,
            namespace.getAllowedInformationTypes())
Exemple #6
0
    def new(self,
            registrant,
            context,
            branch_name,
            rcs_type,
            target_rcs_type,
            url=None,
            cvs_root=None,
            cvs_module=None,
            review_status=None,
            owner=None):
        """See `ICodeImportSet`."""
        if rcs_type == RevisionControlSystems.CVS:
            assert cvs_root is not None and cvs_module is not None
            assert url is None
        elif rcs_type in NON_CVS_RCS_TYPES:
            assert cvs_root is None and cvs_module is None
            assert url is not None
        else:
            raise AssertionError(
                "Don't know how to sanity check source details for unknown "
                "rcs_type %s" % rcs_type)
        if owner is None:
            owner = registrant
        if target_rcs_type == TargetRevisionControlSystems.BZR:
            # XXX cjwatson 2016-10-15: Testing
            # IHasBranches.providedBy(context) would seem more in line with
            # the Git case, but for some reason ProductSeries doesn't
            # provide that.  We should sync this up somehow.
            try:
                target = IBranchTarget(context)
            except TypeError:
                raise CodeImportInvalidTargetType(context, target_rcs_type)
            namespace = target.getNamespace(owner)
        elif target_rcs_type == TargetRevisionControlSystems.GIT:
            if not IHasGitRepositories.providedBy(context):
                raise CodeImportInvalidTargetType(context, target_rcs_type)
            if rcs_type != RevisionControlSystems.GIT:
                raise AssertionError(
                    "Can't import rcs_type %s into a Git repository" %
                    rcs_type)
            target = namespace = get_git_namespace(context, owner)
        else:
            raise AssertionError("Can't import to target_rcs_type %s" %
                                 target_rcs_type)
        if review_status is None:
            # Auto approve imports.
            review_status = CodeImportReviewStatus.REVIEWED
        if not target.supports_code_imports:
            raise AssertionError("%r doesn't support code imports" % target)
        # Create the branch for the CodeImport.
        if target_rcs_type == TargetRevisionControlSystems.BZR:
            import_target = namespace.createBranch(
                branch_type=BranchType.IMPORTED,
                name=branch_name,
                registrant=registrant)
        else:
            import_target = namespace.createRepository(
                repository_type=GitRepositoryType.IMPORTED,
                name=branch_name,
                registrant=registrant)
            hosting_path = import_target.getInternalPath()
            getUtility(IGitHostingClient).create(hosting_path)

        code_import = CodeImport(registrant=registrant,
                                 owner=owner,
                                 target=import_target,
                                 rcs_type=rcs_type,
                                 url=url,
                                 cvs_root=cvs_root,
                                 cvs_module=cvs_module,
                                 review_status=review_status)

        getUtility(ICodeImportEventSet).newCreate(code_import, registrant)
        notify(ObjectCreatedEvent(code_import))

        # If created in the reviewed state, create a job.
        if review_status == CodeImportReviewStatus.REVIEWED:
            CodeImportJobWorkflow().newJob(code_import)

        return code_import
 def test_get_package(self):
     person = self.factory.makePerson()
     dsp = self.factory.makeDistributionSourcePackage()
     namespace = get_git_namespace(dsp, person)
     self.assertIsInstance(namespace, PackageGitNamespace)
 def test_get_project(self):
     person = self.factory.makePerson()
     project = self.factory.makeProduct()
     namespace = get_git_namespace(project, person)
     self.assertIsInstance(namespace, ProjectGitNamespace)
 def test_get_personal(self):
     person = self.factory.makePerson()
     namespace = get_git_namespace(person, person)
     self.assertIsInstance(namespace, PersonalGitNamespace)
 def getNamespace(self, person=None):
     if person is None:
         person = self.factory.makePerson()
     return get_git_namespace(
         self.factory.makeDistributionSourcePackage(), person)
 def getNamespace(self, person=None):
     if person is None:
         person = self.factory.makePerson()
     return get_git_namespace(self.factory.makeProduct(), person)