Beispiel #1
0
def _raise_if_unsupported(repo_path: Path) -> None:
    repo = bento.git.repo(repo_path)
    if repo is None:
        raise NotAGitRepoException()

    if not is_ci_provider_supported(repo_path):
        raise UnsupportedCIProviderException()
Beispiel #2
0
def uninstall_autorun(context: Context) -> None:
    """
    Configures Bento to NOT run automatically on commits.

    Autorun is only removed for the project from which this
    command is run.
    """
    import git  # import inside def for performance

    # Get hook path
    repo = bento.git.repo(context.base_path)
    if repo is None:
        raise NotAGitRepoException()

    hook_path = Path(git.index.fun.hook_path("pre-commit", repo.git_dir))

    if not _is_bento_precommit(hook_path):
        echo_warning(
            "Not uninstalling autorun: Bento is not configured for autorun on this project."
        )
        sys.exit(1)
    else:
        # Put back legacy hook if exits
        legacy_hook_path = Path(f"{hook_path}.pre-bento")
        if legacy_hook_path.exists():
            shutil.move(legacy_hook_path, hook_path)
        else:
            hook_path.unlink()

        echo_success("Uninstalled Bento autorun.")
        echo_next_step("To enable autorun", "bento enable autorun")
Beispiel #3
0
def install_autorun(context: Context, block: bool) -> None:
    """
    Configures Bento to automatically run on commits.

    Autorun is configured only for you; it does not affect
    other contributors to this project.

    Autorun is only configured for the project from which this
    command is run.

    By default, Bento will block commits if it finds an issue.
    To prevent autorun from blocking commits, run:

        $ bento enable autorun --no-block

    """
    import git  # import inside def for performance

    # Get hook path
    repo = bento.git.repo(context.base_path)
    if repo is None:
        raise NotAGitRepoException()

    _configure_block(context, block)

    hook_path = Path(git.index.fun.hook_path("pre-commit", repo.git_dir))

    if _is_bento_precommit(hook_path):
        _notify_install(context, block)

    else:
        legacy_hook_path = Path(f"{hook_path}.pre-bento")
        if hook_path.exists():
            # If pre-commit hook already exists move it over
            if legacy_hook_path.exists():
                raise ExistingGitHookException(str(hook_path))
            else:
                # Check that
                shutil.move(hook_path, legacy_hook_path)

        # Ensure .git/hooks directory exists
        # note that we can (and should) assume .git dir exists since
        # project must be a git project at this point in the code
        hook_path.parent.mkdir(exist_ok=True)

        # Copy pre-commit script template to hook_path
        template_location = os.path.join(os.path.dirname(__file__),
                                         "../configs/pre-commit.template")
        shutil.copyfile(template_location, hook_path)

        # Make file executable
        original_mode = hook_path.stat().st_mode
        os.chmod(hook_path,
                 original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

        _notify_install(context, block)
Beispiel #4
0
def uninstall_ci(context: Context) -> None:
    """
    Configures Bento to NOT run in CI.
    """
    repo = bento.git.repo(context.base_path)
    if repo is None:
        raise NotAGitRepoException()

    if not context.gh_actions_file_path.exists():
        echo_warning(
            "Not uninstalling CI config: Bento is not configured for CI on this project."
        )
        sys.exit(1)

    _delete_gh_actions_config(path=context.gh_actions_file_path,
                              root_path=context.base_path)

    echo_success("Uninstalled Bento from CI.")
    echo_next_step("To re-enable CI integration", "bento enable ci")
Beispiel #5
0
 def _identify_git(self) -> None:
     repo = bento.git.repo(self.context.base_path)
     if repo is None:
         raise NotAGitRepoException()