Exemplo n.º 1
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('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

    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)
    remote_url = result['repositoryMetadata']['cloneUrlHttp']
    source_control.setup_codecommit_remote_repo(remote_url=remote_url)

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            create_codecommit_branch(source_control, 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, remote_url)
        if not succesful_branch:
            io.echo(
                "Could not set CodeCommit branch, run with '--debug' to get the full error"
            )
            return None

    return branch_name
Exemplo n.º 2
0
    def test_list_branches(self, make_api_call_mock):
        make_api_call_mock.return_value = mock_responses.LIST_BRANCHES_RESPONSE

        self.assertEqual(
            mock_responses.LIST_BRANCHES_RESPONSE,
            codecommit.list_branches('my-repository', next_token='next-token'))

        make_api_call_mock.assert_called_once_with(
            'codecommit',
            'list_branches',
            nextToken='next-token',
            repositoryName='my-repository')
Exemplo n.º 3
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('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

    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)
    remote_url = result['repositoryMetadata']['cloneUrlHttp']
    source_control.setup_codecommit_remote_repo(remote_url=remote_url)

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            create_codecommit_branch(source_control, 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, remote_url)
        if not succesful_branch:
            io.echo("Could not set CodeCommit branch, run with '--debug' to get the full error")
            return None

    return branch_name
Exemplo n.º 4
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