diff --git a/baldrick/github/github_api.py b/baldrick/github/github_api.py index 683b4f4..6848a8e 100644 --- a/baldrick/github/github_api.py +++ b/baldrick/github/github_api.py @@ -10,7 +10,8 @@ from ttldict import TTLOrderedDict from baldrick.config import loads -from baldrick.github.github_auth import github_request_headers +from baldrick.github.github_auth import ( + github_request_headers, repo_to_installation_id) __all__ = ['GitHubHandler', 'RepoHandler', 'PullRequestHandler'] @@ -76,6 +77,9 @@ class GitHubHandler: A base class for things that represent things the github app can operate on. """ def __init__(self, repo, installation=None): + if installation is None: # pragma: no cover + installation = repo_to_installation_id(repo, raise_error=False) + self.repo = repo self.installation = installation self._cache = {} diff --git a/baldrick/github/github_auth.py b/baldrick/github/github_auth.py index 1472396..da121e2 100644 --- a/baldrick/github/github_auth.py +++ b/baldrick/github/github_auth.py @@ -136,15 +136,18 @@ def repo_to_installation_id_mapping(): return repos -def repo_to_installation_id(repository): +def repo_to_installation_id(repository, raise_error=True): """ Return the installation ID for a repository. """ mapping = repo_to_installation_id_mapping() if repository in mapping: return mapping[repository] + elif raise_error: + raise ValueError("Repository not recognized - should be one of:\n\n" + " - " + "\n - ".join(mapping)) else: - raise ValueError("Repository not recognized - should be one of:\n\n - " + "\n - ".join(mapping)) + return None def get_app_name(): diff --git a/baldrick/github/tests/test_github_auth.py b/baldrick/github/tests/test_github_auth.py index 6eb6765..f4a9c32 100644 --- a/baldrick/github/tests/test_github_auth.py +++ b/baldrick/github/tests/test_github_auth.py @@ -100,6 +100,7 @@ def test_repo_to_installation_id(app): with patch('requests.get', requests_patch): assert repo_to_installation_id('test1') == 3331 + assert repo_to_installation_id('test3', raise_error=False) is None with pytest.raises(ValueError) as exc: repo_to_installation_id('test3') diff --git a/baldrick/plugins/tests/test_github_milestones.py b/baldrick/plugins/tests/test_github_milestones.py index 275e913..51a51b8 100644 --- a/baldrick/plugins/tests/test_github_milestones.py +++ b/baldrick/plugins/tests/test_github_milestones.py @@ -28,6 +28,9 @@ class TestMilestonePlugin: def setup_method(self, method): + self.get_installation = patch('baldrick.github.github_auth.repo_to_installation_id_mapping') + i = self.get_installation.start() + i.return_value = {} self.get_file_contents_mock = patch('baldrick.github.github_api.PullRequestHandler.get_file_contents') self.get_base_branch_mock = patch('baldrick.github.github_api.PullRequestHandler.base_branch') a = self.get_base_branch_mock.start() @@ -45,6 +48,7 @@ def setup_method(self, method): cfg_cache.clear() def teardown_method(self, method): + self.get_installation.stop() self.get_file_contents_mock.stop() self.milestone_mock.stop() self.get_base_branch_mock.stop() diff --git a/baldrick/plugins/tests/test_github_towncrier_changelog.py b/baldrick/plugins/tests/test_github_towncrier_changelog.py index e8d5924..7aa210c 100644 --- a/baldrick/plugins/tests/test_github_towncrier_changelog.py +++ b/baldrick/plugins/tests/test_github_towncrier_changelog.py @@ -20,6 +20,9 @@ class TestTowncrierPlugin: def setup_method(self, method): + self.get_installation = patch('baldrick.github.github_auth.repo_to_installation_id_mapping') + i = self.get_installation.start() + i.return_value = {} self.get_file_contents_mock = patch('baldrick.github.github_api.PullRequestHandler.get_file_contents') self.get_base_branch_mock = patch('baldrick.github.github_api.PullRequestHandler.base_branch') a = self.get_base_branch_mock.start() @@ -35,6 +38,7 @@ def setup_method(self, method): cfg_cache.clear() def teardown_method(self, method): + self.get_installation.stop() self.get_file_contents_mock.stop() self.modified_files_mock.stop() self.get_base_branch_mock.stop()