def _getBranchNamespaceExtras(self, path, requester):
        """Get the branch namespace, branch name and callback for the path.

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

        If the path uses an lp short name, then we only allow the requester to
        create a branch if they have permission to link the newly created
        branch to the short name target.  If there is an existing branch
        already linked, then BranchExists is raised.  The branch name that is
        used is determined by the namespace as the first unused name starting
        with 'trunk'.
        """
        if path.startswith(BRANCH_ALIAS_PREFIX + '/'):
            path = path[len(BRANCH_ALIAS_PREFIX) + 1:]
            if not path.startswith('~'):
                context = getUtility(ILinkedBranchTraverser).traverse(path)
                target = IBranchTarget(context)
                namespace = target.getNamespace(requester)
                branch_name = namespace.findUnusedName('trunk')

                def link_func(new_branch):
                    link = ICanHasLinkedBranch(context)
                    link.setBranch(new_branch, requester)
                return namespace, branch_name, link_func, path
        namespace_name, branch_name = split_unique_name(path)
        namespace = lookup_branch_namespace(namespace_name)
        return namespace, branch_name, None, path
Beispiel #2
0
    def _getBranchNamespaceExtras(self, path, requester):
        """Get the branch namespace, branch name and callback for the path.

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

        If the path uses an lp short name, then we only allow the requester to
        create a branch if they have permission to link the newly created
        branch to the short name target.  If there is an existing branch
        already linked, then BranchExists is raised.  The branch name that is
        used is determined by the namespace as the first unused name starting
        with 'trunk'.
        """
        if path.startswith(BRANCH_ALIAS_PREFIX + '/'):
            path = path[len(BRANCH_ALIAS_PREFIX) + 1:]
            if not path.startswith('~'):
                context = getUtility(ILinkedBranchTraverser).traverse(path)
                target = IBranchTarget(context)
                namespace = target.getNamespace(requester)
                branch_name = namespace.findUnusedName('trunk')

                def link_func(new_branch):
                    link = ICanHasLinkedBranch(context)
                    link.setBranch(new_branch, requester)

                return namespace, branch_name, link_func, path
        namespace_name, branch_name = split_unique_name(path)
        namespace = lookup_branch_namespace(namespace_name)
        return namespace, branch_name, None, path
Beispiel #3
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