Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion baldrick/github/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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 = {}
Expand Down
7 changes: 5 additions & 2 deletions baldrick/github/github_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions baldrick/github/tests/test_github_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions baldrick/plugins/tests/test_github_milestones.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions baldrick/plugins/tests/test_github_towncrier_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down