Ejemplo n.º 1
0
    def test_create_branch(self, make_api_call_mock):
        codecommit.create_branch('my-repository', 'my-branch',
                                 '068f60ebd5b7d9a0ad071b8a20ccdf8178491295')

        make_api_call_mock.assert_called_once_with(
            'codecommit',
            'create_branch',
            branchName='my-branch',
            commitId='068f60ebd5b7d9a0ad071b8a20ccdf8178491295',
            repositoryName='my-repository')
Ejemplo n.º 2
0
def get_branch_interactive(repository):
    source_control = SourceControl.get_source_control()
    # Give list of code commit branches to use
    new_branch = False
    branch_list = codecommit.list_branches(repository)["branches"]
    current_branch = source_control.get_current_branch()

    # If there are existing branches prompt the user to pick one
    if len(branch_list) > 0:
        io.echo()
        io.echo('Select a branch')
        new_branch_option = '[ Create new Branch with local HEAD ]'
        branch_list.append(new_branch_option)
        try:
            default_option = branch_list.index(current_branch) + 1
        except ValueError:
            default_option = len(branch_list)
        branch_name = utils.prompt_for_item_in_list(branch_list,
                                                    default=default_option)
        if branch_name == new_branch_option:
            new_branch = True

    # Create a new branch if the user specifies or there are no existing branches
    current_commit = source_control.get_current_commit()
    if len(branch_list) == 0 or new_branch:
        new_branch = True
        io.echo()
        io.echo('Enter Branch Name')
        io.echo(
            '***** Must have at least one commit to create a new branch with CodeCommit *****'
        )
        unique_name = utils.get_unique_name(current_branch, branch_list)
        branch_name = io.prompt_for_unique_name(unique_name, branch_list)

    # Setup git to push to this repo
    result = codecommit.get_repository(repository)
    source_control.setup_codecommit_remote_repo(
        remote_url=result['repositoryMetadata']['cloneUrlHttp'])

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            # Creating the branch requires that we setup the remote branch first
            # to ensure the code commit branch is synced with the local branch
            if current_commit is None:
                # TODO: Test on windows for weird empty returns with the staged files
                staged_files = source_control.get_list_of_staged_files()
                if staged_files is None or not staged_files:
                    source_control.create_initial_commit()
                else:
                    LOG.debug(
                        "Cannot create placeholder commit because there are staged files: {0}"
                        .format(staged_files))
                    io.echo(
                        "Could not set create a commit with staged files; cannot setup CodeCommit branch without a commit"
                    )
                    return None

                current_commit = source_control.get_current_commit()

            source_control.setup_new_codecommit_branch(branch_name=branch_name)
            codecommit.create_branch(repository, branch_name, current_commit)
            io.echo("Successfully created branch: {0}".format(branch_name))
        except ServiceError:
            io.echo(
                "Could not set CodeCommit branch with the current commit, run with '--debug' to get the full error"
            )
            return None
    elif not new_branch:
        LOG.debug("Setting up an existing branch")
        succesful_branch = source_control.setup_existing_codecommit_branch(
            branch_name)
        if not succesful_branch:
            io.echo(
                "Could not set CodeCommit branch, run with '--debug' to get the full error"
            )
            return None

    return branch_name