def test_delete_branch_from_repo_branch_not_found( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_branch: str, ) -> None: """It gives the message error returned from the API.""" def _initiate_mocked_exception( self: github3.exceptions.NotFoundError, ) -> None: self.msg = "Not found" mocker.patch.object( github3.exceptions.NotFoundError, "__init__", _initiate_mocked_exception ) repo = mock_github3_login.return_value.repositories.return_value[1] repo.ref.side_effect = github3.exceptions.NotFoundError response = gc.GithubService(domain_gh_conn_settings[0]).delete_branch_from_repo( "staticdev/omg", domain_branch ) assert response == "staticdev/omg: Not found."
def test_create_pull_request_from_repo_no_commits_unpatched_exception( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It gives the message error from the exception.""" mocked_response = mocker.Mock() mocked_response.json.return_value = { "message": "Validation Failed", "errors": [{"message": "No commits between master and new-branch"}], } repo = mock_github3_login.return_value.repositories.return_value[1] repo.create_pull.side_effect = github3.exceptions.UnprocessableEntity( mocked_response ) response = gc.GithubService( domain_gh_conn_settings[0] ).create_pull_request_from_repo("staticdev/omg", domain_prs[1]) assert ( response == "staticdev/omg: Validation Failed. No commits between master and new-branch." )
def test_link_issue_success( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It succeeds.""" label1 = mocker.Mock() label1.name = "dontinherit" label2 = mocker.Mock() label2.name = "enhancement" label3 = mocker.Mock() label3.name = "testing" issue1 = mocker.Mock(title="issue title", number=3) issue1.labels.return_value = [] issue2 = mocker.Mock(title="doesnt match title", number=4) issue2.labels.return_value = [label1] issue3 = mocker.Mock(title="match issue title", number=5) issue3.labels.return_value = [label2, label3] repo = mock_github3_login.return_value.repositories.return_value[1] repo.issues.return_value = [ issue1, issue2, issue3, ] gc.GithubService(domain_gh_conn_settings[0]).link_issues( "staticdev/omg", domain_prs[1] ) assert domain_prs[1].body == "my body\n\nCloses #3\nCloses #5\n" case = unittest.TestCase() case.assertCountEqual(domain_prs[1].labels, {"testing", "refactor", "enhancement"})
def test_create_pull_request_from_repo_invalid_branch( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It gives the message error informing the invalid field head.""" def _initiate_mocked_exception( self: github3.exceptions.UnprocessableEntity, ) -> None: self.errors = [{"field": "head"}] self.msg = "Validation Failed" mocker.patch.object( github3.exceptions.UnprocessableEntity, "__init__", _initiate_mocked_exception ) repo = mock_github3_login.return_value.repositories.return_value[1] repo.create_pull.side_effect = github3.exceptions.UnprocessableEntity response = gc.GithubService( domain_gh_conn_settings[0] ).create_pull_request_from_repo("staticdev/omg", domain_prs[1]) assert response == "staticdev/omg: Validation Failed. Invalid field head."
def test_list_issues_from_repo_no_filter_request( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], mock_github3_login: MockerFixture, ) -> None: """It returns empty result.""" issue1 = mocker.Mock(title="issue title", number=3) issue1.labels.return_value = [] issue2 = mocker.Mock(title="doesnt match title", number=4) issue2.labels.return_value = [] repo = mock_github3_login.return_value.repositories.return_value[1] repo.issues.return_value = [ issue1, issue2, ] response = gc.GithubService( domain_gh_conn_settings[0]).list_issues_from_repo( REPO, NO_FILTER_REQUEST_ISSUES) assert len(response) == 2 assert response[0].number == domain_issues[3].number assert response[1].number == domain_issues[2].number assert response[0].title == domain_issues[3].title assert response[1].title == domain_issues[2].title
def test_create_pull_request_from_repo_no_commits( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It gives the message error from the exception.""" def _initiate_mocked_exception( self: github3.exceptions.UnprocessableEntity, ) -> None: self.errors = [{"message": "No commits between master and new-branch"}] self.msg = "Validation Failed" mocker.patch.object( github3.exceptions.UnprocessableEntity, "__init__", _initiate_mocked_exception ) repo = mock_github3_login.return_value.repositories.return_value[1] repo.create_pull.side_effect = github3.exceptions.UnprocessableEntity response = gc.GithubService( domain_gh_conn_settings[0] ).create_pull_request_from_repo("staticdev/omg", domain_prs[1]) assert ( response == "staticdev/omg: Validation Failed. No commits between master and new-branch." )
def test_get_config( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns service config.""" expected = domain_gh_conn_settings[0] result = gc.GithubService(domain_gh_conn_settings[0]).get_config() assert result == expected
def test_get_repo_no_repo( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings] ) -> None: """It returns None.""" mock = mocker.patch("github3.login", autospec=True) mock.return_value.repositories.return_value = [] with pytest.raises(NameError): gc.GithubService(domain_gh_conn_settings[0])._get_repo("staticdev/omg")
def test_get_repo_names( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns list of strings.""" expected = ["staticdev/nope", "staticdev/omg"] result = gc.GithubService(domain_gh_conn_settings[0]).get_repo_names() assert result == expected
def test_link_issue_no_link( domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], domain_prs: List[pr.PullRequest], mock_github3_login: MockerFixture, ) -> None: """It succeeds without any linked issue.""" gc.GithubService(domain_gh_conn_settings[0]).link_issues(domain_prs[1], []) assert domain_prs[1].body == "my pr body 2"
def test_init_connection_error( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It succeeds.""" mock_github3_login.return_value.me.side_effect = github3.exceptions.ConnectionError( mocker.Mock()) with pytest.raises(ConnectionError): gc.GithubService(domain_gh_conn_settings[0])
def test_get_username( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns user name.""" expected = "staticdev" mock_github3_login.return_value.me.return_value.login = "******" result = gc.GithubService(domain_gh_conn_settings[0]).get_username() assert result == expected
def test_get_repo_url_success( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns url.""" expected = f"[email protected]:{REPO}.git" result = gc.GithubService(domain_gh_conn_settings[0]).get_repo_url(REPO) assert result == expected
def test_create_pull_request_from_repo_with_labels( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It succeeds.""" response = gc.GithubService( domain_gh_conn_settings[0] ).create_pull_request_from_repo("staticdev/omg", domain_prs[1]) assert response == "staticdev/omg: PR created successfully."
def test_create_issue_from_repo_success( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_issue: i.Issue, ) -> None: """It succeeds.""" response = gc.GithubService(domain_gh_conn_settings[0]).create_issue_from_repo( "staticdev/omg", domain_issue ) assert response == "staticdev/omg: issue created successfully."
def test_create_issue_from_repo_success( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_issues: List[i.Issue], ) -> None: """It succeeds.""" response = gc.GithubService( domain_gh_conn_settings[0]).create_issue_from_repo( REPO, domain_issues[0]) assert response == f"{REPO}: create issue successful.\n"
def test_init_wrong_token( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It succeeds.""" mock_github3_login.return_value.me.side_effect = ( github3.exceptions.AuthenticationFailed(mocker.Mock()) ) with pytest.raises(AttributeError): gc.GithubService(domain_gh_conn_settings[0])
def test_list_issues_from_repo_invalid_request( domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], mock_github3_login: MockerFixture, ) -> None: """It returns empty result.""" response = gc.GithubService( domain_gh_conn_settings[0]).list_issues_from_repo( REPO, INVALID_REQUEST_ISSUES) assert response == []
def test_create_pull_request_from_repo_success( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_prs: List[pr.PullRequest], ) -> None: """It succeeds.""" response = gc.GithubService( domain_gh_conn_settings[0]).create_pull_request_from_repo( REPO, domain_prs[0]) assert response == f"{REPO}: create PR successful.\n"
def test_close_issues_from_repo_no_issue( domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], mock_github3_login: MockerFixture, ) -> None: """It returns a not issue message.""" mock_github3_login.return_value.issue().is_closed.return_value = False response = gc.GithubService( domain_gh_conn_settings[0]).close_issues_from_repo(REPO, []) assert response == f"{REPO}: no issues match.\n"
def test_delete_branch_from_repo_success( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_branch: str, ) -> None: """It succeeds.""" response = gc.GithubService( domain_gh_conn_settings[0]).delete_branch_from_repo( REPO, domain_branch) assert response == f"{REPO}: delete branch successful.\n"
def test_close_issues_from_repo_already_closed( domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], mock_github3_login: MockerFixture, ) -> None: """It succeeds.""" mock_github3_login.return_value.issue().is_closed.return_value = True response = gc.GithubService( domain_gh_conn_settings[0]).close_issues_from_repo( REPO, domain_issues) assert response == f"{REPO}: close issues successful.\n"
def test_init_invalid_token( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns invalid token message.""" mock_github3_login.return_value.me.side_effect = ( github3.exceptions.AuthenticationFailed(mocker.Mock())) with pytest.raises(AttributeError) as excinfo: gc.GithubService(domain_gh_conn_settings[0]) assert "Invalid token." == str(excinfo.value)
def test_delete_branch_from_repo_success( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_branch: str, ) -> None: """It succeeds.""" response = gc.GithubService(domain_gh_conn_settings[0]).delete_branch_from_repo( "staticdev/omg", domain_branch ) assert response == "staticdev/omg: branch deleted successfully."
def test_merge_pull_request_from_repo_not_found( domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_mpr: mpr.PullRequestMerge, ) -> None: """It gives error message.""" repo = mock_github3_login.return_value.repositories.return_value[1] repo.pull_requests.return_value = [] response = gc.GithubService( domain_gh_conn_settings[0] ).merge_pull_request_from_repo("staticdev/omg", domain_mpr) assert response == "staticdev/omg: no open PR found for branch:main."
def test_init_invalid_token_scope( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, ) -> None: """It returns invalid response message.""" mock_github3_login.return_value.me.side_effect = ( github3.exceptions.IncompleteResponse(mocker.Mock(), mocker.Mock())) with pytest.raises(AttributeError) as excinfo: gc.GithubService(domain_gh_conn_settings[0]) assert "Invalid response. Your token might not be properly scoped." == str( excinfo.value)
def _get_github_service(config: c.Config) -> ghs.GithubService: settings = cs.GhConnectionSettings(config.github_access_token, config.github_hostname) try: return ghs.GithubService(settings) except AttributeError: response = res.ResponseFailure.build_parameters_error( "Wrong GitHub permissions. Please check your token.") except ConnectionError: response = res.ResponseFailure.build_system_error(( "Unable to reach server. Please check you network and credentials and " "try again.")) _echo_outputs(response) raise click.ClickException("")
def test_merge_pull_request_from_repo_ambiguous( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_mpr: mpr.PullRequestMerge, ) -> None: """It gives error message.""" repo = mock_github3_login.return_value.repositories.return_value[1] repo.pull_requests.return_value = [mocker.Mock(), mocker.Mock()] response = gc.GithubService( domain_gh_conn_settings[0] ).merge_pull_request_from_repo("staticdev/omg", domain_mpr) assert response == "staticdev/omg: unexpected number of PRs for branch:main."
def test_merge_pull_request_from_repo_success( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], mock_github3_login: MockerFixture, domain_mpr: mpr.PullRequestMerge, ) -> None: """It succeeds.""" repo = mock_github3_login.return_value.repositories.return_value[1] repo.pull_requests.return_value = [mocker.Mock()] response = gc.GithubService( domain_gh_conn_settings[0] ).merge_pull_request_from_repo("staticdev/omg", domain_mpr) assert response == "staticdev/omg: PR merged successfully."
def execute( self, request: gcs.GhConnectionSettings ) -> Union[res.ResponseFailure, res.ResponseSuccess]: """Initialize app configuration.""" try: new_github_service = ghs.GithubService(request) except AttributeError: return res.ResponseFailure.build_parameters_error() except ConnectionError: return res.ResponseFailure.build_system_error() repo_names = new_github_service.get_repo_names() selected_repos = p.InquirerPrompter.select_repos(repo_names) cr.ConfigReposUseCase(self.config_manager).execute(selected_repos) return res.ResponseSuccess("gitp successfully configured.")