def execute( self, git_selected_repos: List[str] ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Batch `git clone` command. Args: git_selected_repos: list of configured repo names. Returns: str: output. str: error output. """ if self.err_output: return res.ResponseFailure(res.ResponseTypes.SYSTEM_ERROR, self.err_output) output = "" cwd = pathlib.Path().absolute() for repo_name in git_selected_repos: folder_name = repo_name.split("/")[1] clone_path = self.github_service.get_repo_url(repo_name) output += f"{folder_name}: " popen = subprocess.Popen( # noqa: S603, S607 ["git", "clone", clone_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, ) _, error = popen.communicate() # check for errors if popen.returncode == 0: output += "clone successful.\n" else: error_str = error.decode("utf-8") output += f"{error_str}" return res.ResponseSuccess(output)
def generate_response( self, output: str ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Create appropriate response object.""" if self.error: return res.ResponseFailure(res.ResponseTypes.PARAMETERS_ERROR, output) return res.ResponseSuccess(output)
def execute( self, request_object: Union[il.IssueListValidRequest, il.IssueListInvalidRequest], github_repo: str = "", ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Close issues.""" output = "" if github_repo: response = li.GhListIssueUseCase(self.config_manager, self.github_service).execute( request_object, github_repo) if isinstance(response, res.ResponseSuccess): output = self.call_github_service("close_issues_from_repo", output, github_repo, response.value) else: output = f"{github_repo}: no issues closed.\n" else: for github_repo in self.config_manager.config.github_selected_repos: response = li.GhListIssueUseCase(self.config_manager, self.github_service).execute( request_object, github_repo) if isinstance(response, res.ResponseSuccess): output = self.call_github_service("close_issues_from_repo", output, github_repo, response.value) else: output += f"{github_repo}: no issues closed.\n" return res.ResponseSuccess(output)
def execute( self, github_config: cs.GhConnectionSettings, selected_repos: List[str] ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Configuration of git repositories.""" self.config_manager.config.github_access_token = github_config.access_token self.config_manager.config.github_hostname = github_config.hostname self.config_manager.config.github_selected_repos = selected_repos self.config_manager.save_config() return res.ResponseSuccess( "gitp repositories successfully configured.")
def execute( self, request: Union[il.IssueListValidRequest, il.IssueListInvalidRequest], github_repo: str, ) -> Union[res.ResponseSuccess, res.ResponseFailure]: """Return a list of matching issues.""" if isinstance(request, il.IssueListInvalidRequest): return res.build_response_from_invalid_request(request) try: issues = self.github_service.list_issues_from_repo( github_repo, request) return res.ResponseSuccess(issues) except Exception as exc: return res.ResponseFailure(res.ResponseTypes.SYSTEM_ERROR, exc)
def test_config_init_success( mock_prompt_inquirer_prompter: MockerFixture, mock_config_manager: MockerFixture, mock_config_init_use_case: MockerFixture, runner: CliRunner, ) -> None: """It executes config init use case.""" config_manager = mock_config_manager.return_value mock_config_init_use_case( config_manager ).execute.return_value = res.ResponseSuccess("success message") result = runner.invoke(git_portfolio.__main__.configure, ["init"], prog_name="gitp") mock_config_init_use_case(config_manager).execute.assert_called_once() assert result.output == "success message\n"
def test_execute_for_specific_repo( mock_config_manager: MockerFixture, mock_github_service: MockerFixture, mock_gh_list_issue_use_case: MockerFixture, ) -> None: """It returns success.""" config_manager = mock_config_manager.return_value github_service = mock_github_service.return_value mock_gh_list_issue_use_case.return_value.execute.return_value = ( res.ResponseSuccess()) response = ghci.GhCloseIssueUseCase(config_manager, github_service).execute( REQUEST_ISSUES, REPO) assert bool(response) is True assert "success message\n" == response.value
def test_config_init_connection_error( mock_prompt_inquirer_prompter: MockerFixture, mock_config_manager: MockerFixture, mock_config_init_use_case: MockerFixture, runner: CliRunner, ) -> None: """It executes with connection error first then succeeds.""" config_manager = mock_config_manager.return_value mock_config_init_use_case(config_manager).execute.side_effect = [ res.ResponseFailure(res.ResponseTypes.SYSTEM_ERROR, "message"), res.ResponseSuccess("success message"), ] result = runner.invoke(git_portfolio.__main__.configure, ["init"], prog_name="gitp") assert mock_config_init_use_case(config_manager).execute.call_count == 2 assert result.output == "Error: message\nsuccess message\n"
def test_execute_link_issue( mock_config_manager: MockerFixture, mock_github_service: MockerFixture, mock_gh_list_issue_use_case: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It returns success.""" config_manager = mock_config_manager.return_value github_service = mock_github_service.return_value mock_gh_list_issue_use_case.return_value.execute.return_value = ( res.ResponseSuccess() ) request = domain_prs[1] response = ghcp.GhCreatePrUseCase(config_manager, github_service).execute( request, REQUEST_ISSUES ) assert bool(response) is True assert "success message\nsuccess message\n" == response.value
def execute( self, git_selected_repos: List[str], command: str, args: Tuple[str] ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Batch `git` command. Args: git_selected_repos: list of configured repo names. command: git command eg. checkout, pull, push... args: command arguments. Returns: Union[res.ResponseFailure, res.ResponseSuccess]: final result. """ if self.err_output: return res.ResponseFailure(res.ResponseTypes.SYSTEM_ERROR, self.err_output) output = "" cwd = pathlib.Path().absolute() for repo_name in git_selected_repos: folder_name = repo_name.split("/")[1] output += f"{folder_name}: " try: popen = subprocess.Popen( # noqa: S603, S607 ["git", command, *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.join(cwd, folder_name), ) stdout, error = popen.communicate() if popen.returncode == 0: # case for command with no output on success such as `git add .` if not stdout: output += f"{command} successful.\n" else: stdout_str = stdout.decode("utf-8") output += f"{stdout_str}\n" else: error_str = error.decode("utf-8") output += f"{error_str}" except FileNotFoundError as fnf_error: output += f"{fnf_error.strerror}: {fnf_error.filename}\n" return res.ResponseSuccess(output)
def test_response_success_is_true() -> None: """It has bool value of True.""" response = res.ResponseSuccess(SUCCESS_VALUE) assert bool(response) is True
def test_response_success_has_type_and_value() -> None: """It has success type and value.""" response = res.ResponseSuccess(SUCCESS_VALUE) assert response.type == res.ResponseTypes.SUCCESS assert response.value == SUCCESS_VALUE
def _() -> res.ResponseSuccess: return res.ResponseSuccess("success message")