def main( ctx: click.Context, options: types.GlobalOptions, git_url: str, since_commit: Optional[str], max_depth: int, branch: Optional[str], work_dir: Optional[str], ) -> Tuple[str, Optional[GitRepoScanner]]: """Automatically clone and scan a remote git repository.""" git_options = types.GitOptions( since_commit=since_commit, max_depth=max_depth, branch=branch, fetch=False ) repo_path: Optional[Path] = None if work_dir: # Make sure we clone into a sub-directory of the working directory # so that we don't inadvertently delete the working directory repo_name = urlparse(git_url).path.split("/")[-1] repo_path = Path(work_dir) / repo_name repo_path.mkdir(parents=True) scanner = None try: repo_path = util.clone_git_repo(git_url, repo_path) scanner = GitRepoScanner(options, git_options, str(repo_path)) scanner.scan() except types.GitException as exc: util.fail(f"Error cloning remote repo: {exc}", ctx) except types.TartufoException as exc: util.fail(str(exc), ctx) finally: if repo_path and repo_path.exists(): rmtree(str(repo_path), onerror=util.del_rw) return (git_url, scanner)
def main( ctx: click.Context, options: types.GlobalOptions, repo_path: str, since_commit: Optional[str], max_depth: int, branch: Optional[str], include_submodules: bool, ) -> GitRepoScanner: """Scan a repository already cloned to your local system.""" git_options = types.GitOptions( since_commit=since_commit, max_depth=max_depth, branch=branch, include_submodules=include_submodules, ) scanner = None try: scanner = GitRepoScanner(options, git_options, str(repo_path)) util.process_issues(repo_path, scanner, options) except types.GitLocalException: util.fail(f"{repo_path} is not a valid git repository.", ctx) except types.TartufoException as exc: util.fail(str(exc), ctx) return scanner # type: ignore
def main(ctx: click.Context, options: types.GlobalOptions) -> Tuple[str, GitPreCommitScanner]: """Scan staged changes in a pre-commit hook.""" # Assume that the current working directory is the appropriate git repo repo_path = pathlib.Path.cwd() scanner = None try: scanner = GitPreCommitScanner(options, str(repo_path)) scanner.scan() except types.ScanException as exc: util.fail(str(exc), ctx) return (str(repo_path), scanner) # type: ignore
def main( ctx: click.Context, options: types.GlobalOptions, include_submodules: bool ) -> GitPreCommitScanner: """Scan staged changes in a pre-commit hook.""" # Assume that the current working directory is the appropriate git repo repo_path = pathlib.Path.cwd() scanner = None try: scanner = GitPreCommitScanner(options, str(repo_path), include_submodules) util.process_issues(str(repo_path), scanner, options) except types.ScanException as exc: util.fail(str(exc), ctx) return scanner # type: ignore
def main(ctx: click.Context, options: types.GlobalOptions, target: str, recurse: bool) -> FolderScanner: """Scan a folder.""" try: resume: bool = True if util.path_contains_git(target) is True: resume = click.confirm( "This folder is a git repository, and should be scanned using the " "scan-local-repo command. Are you sure you wish to proceed?") if resume is False: sys.exit(0) scanner = FolderScanner(options, target, recurse) util.process_issues(target, scanner, options) except types.TartufoException as exc: util.fail(str(exc), ctx) return scanner # type: ignore
def main( ctx: click.Context, options: types.GlobalOptions, repo_path: str, since_commit: Optional[str], max_depth: int, branch: Optional[str], fetch: bool, ) -> Tuple[str, Optional[GitRepoScanner]]: """Scan a repository already cloned to your local system.""" git_options = types.GitOptions(since_commit=since_commit, max_depth=max_depth, branch=branch, fetch=fetch) scanner = None try: scanner = GitRepoScanner(options, git_options, str(repo_path)) scanner.scan() except types.GitLocalException as exc: util.fail(f"{repo_path} is not a valid git repository.", ctx) except types.GitRemoteException as exc: util.fail( f"There was an error fetching from the remote repository: {exc}", ctx) except types.TartufoException as exc: util.fail(str(exc), ctx) return (str(repo_path), scanner)
def main( ctx: click.Context, options: types.GlobalOptions, git_url: str, since_commit: Optional[str], max_depth: int, branch: Optional[str], work_dir: Optional[str], include_submodules: bool, ) -> GitRepoScanner: """Automatically clone and scan a remote git repository.""" git_options = types.GitOptions( since_commit=since_commit, max_depth=max_depth, branch=None, include_submodules=include_submodules, ) repo_path: Optional[Path] = None if work_dir: # Make sure we clone into a sub-directory of the working directory # so that we don't inadvertently delete the working directory repo_name = urlparse(git_url).path.split("/")[-1] repo_path = Path(work_dir) / repo_name repo_path.mkdir(parents=True) scanner = None try: repo_path, origin = util.clone_git_repo(git_url, repo_path) if branch: git_options.branch = f"{origin}/{branch}" scanner = GitRepoScanner(options, git_options, str(repo_path)) util.process_issues(git_url, scanner, options) except types.GitException as exc: util.fail(f"Error cloning remote repo: {exc}", ctx) except types.TartufoException as exc: util.fail(str(exc), ctx) finally: if repo_path and repo_path.exists(): rmtree(str(repo_path), onerror=util.del_rw) return scanner # type: ignore
def test_fail_exits_with_exit_code(self): mock_context = mock.MagicMock() util.fail("Foo!", mock_context, 42) mock_context.exit.assert_called_once_with(42)
def test_fail_echos_styled_error_message(self, mock_click, mock_style): util.fail("Foo!", mock.MagicMock(), 42) mock_style.assert_called_once_with("Foo!") mock_click.echo.assert_called_once_with(mock_style.return_value, err=True)