def ensure_code_commit_repo()

in servicecatalog_factory/commands/portfolios.py [0:0]


def ensure_code_commit_repo(details):
    logger.info(f"ensuring code commit rep")
    if details.get("Source", {}).get("Provider", "").lower() == "codecommit":
        logger.info(f"Codecommit repo defined, carrying on")
        configuration = details.get("Source").get("Configuration")
        branch_name = configuration.get("BranchName")
        repository_name = configuration.get("RepositoryName")
        with betterboto_client.ClientContextManager("codecommit") as codecommit:
            repo_empty = False
            all_branches = []
            try:
                all_branches = codecommit.list_branches_single_page(
                    repositoryName=repository_name
                ).get("branches")
                logger.info(f"Codecommit repo exists, carrying on")
            except codecommit.exceptions.RepositoryDoesNotExistException:
                logger.info(f"Codecommit does not exist, creating it")
                repo_empty = True
                codecommit.create_repository(repositoryName=repository_name)

            if branch_name not in all_branches:
                logger.info(f"Codecommit branch not found, creating it")
                if repo_empty:
                    logger.info(
                        f"Repo was empty, creating first commit on the branch: {branch_name}"
                    )
                    parent_commit_id = codecommit.create_commit(
                        repositoryName=repository_name,
                        branchName=branch_name,
                        commitMessage="Auto generated commit",
                        putFiles=[
                            {
                                "filePath": "product.template.yaml",
                                "fileMode": "NORMAL",
                                "fileContent": "",
                            }
                        ],
                    ).get("commitId")
                else:
                    if "master" not in all_branches:
                        raise Exception(
                            f"{repository_name} has no 'master' branch to branch"
                        )
                    logger.info(
                        f"Repo was not empty, go to create the branch: {branch_name} from master"
                    )
                    parent_commit_id = (
                        codecommit.get_branch(
                            repositoryName=repository_name, branchName="master",
                        )
                        .get("branch")
                        .get("commitId")
                    )

                logger.info(f"Creating the branch: {branch_name}")
                codecommit.create_branch(
                    repositoryName=repository_name,
                    branchName=branch_name,
                    commitId=parent_commit_id,
                )