Add method to obtain default branch to pagure module
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
This commit is contained in:
parent
0ec16b0195
commit
fc7eb34205
3 changed files with 133 additions and 1 deletions
|
@ -857,3 +857,94 @@ class TestPagureGetProjectContributors:
|
|||
"https://pagure.io/api/0/{0}/{1}/contributors".format(namespace, repo),
|
||||
headers=self.pagure.get_auth_header()
|
||||
)
|
||||
|
||||
|
||||
class TestPagureGetDefaultBranch:
|
||||
"""
|
||||
Test class for `toddlers.pagure.Pagure.get_default_branch` method.
|
||||
"""
|
||||
def setup(self):
|
||||
"""
|
||||
Setup method for the test class.
|
||||
"""
|
||||
config = {
|
||||
"pagure_url": "https://pagure.io",
|
||||
"pagure_api_key": "Very secret key"
|
||||
}
|
||||
self.pagure = pagure.set_pagure(config)
|
||||
self.pagure._requests_session = Mock()
|
||||
|
||||
def test_get_default_branch(self):
|
||||
"""
|
||||
Assert that getting default branch works correctly.
|
||||
"""
|
||||
response_mock = Mock()
|
||||
response_mock.status_code = 200
|
||||
|
||||
branch = "branch"
|
||||
data = {"default": branch}
|
||||
|
||||
response_mock.json.return_value = data
|
||||
|
||||
self.pagure._requests_session.get.return_value = response_mock
|
||||
|
||||
namespace = "namespace"
|
||||
repo = "repo"
|
||||
|
||||
result = self.pagure.get_default_branch(namespace, repo)
|
||||
|
||||
self.pagure._requests_session.get.assert_called_with(
|
||||
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
|
||||
headers=self.pagure.get_auth_header()
|
||||
)
|
||||
|
||||
assert result == branch
|
||||
|
||||
def test_get_default_branch_not_ser(self):
|
||||
"""
|
||||
Assert that getting default branch returns None if not set.
|
||||
"""
|
||||
response_mock = Mock()
|
||||
response_mock.status_code = 200
|
||||
|
||||
data = {}
|
||||
|
||||
response_mock.json.return_value = data
|
||||
|
||||
self.pagure._requests_session.get.return_value = response_mock
|
||||
|
||||
namespace = "namespace"
|
||||
repo = "repo"
|
||||
|
||||
result = self.pagure.get_default_branch(namespace, repo)
|
||||
|
||||
self.pagure._requests_session.get.assert_called_with(
|
||||
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
|
||||
headers=self.pagure.get_auth_header()
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_get_default_branch_failure(self):
|
||||
"""
|
||||
Assert that failing to get default branch is handled correctly.
|
||||
"""
|
||||
response_mock = Mock()
|
||||
response_mock.status_code = 500
|
||||
|
||||
self.pagure._requests_session.get.side_effect = response_mock
|
||||
|
||||
namespace = "namespace"
|
||||
repo = "repo"
|
||||
|
||||
expected_error = "Couldn't get default branch for project '{0}/{1}'".format(namespace, repo)
|
||||
|
||||
with pytest.raises(PagureError, match=expected_error):
|
||||
self.pagure.get_default_branch(
|
||||
namespace, repo
|
||||
)
|
||||
|
||||
self.pagure._requests_session.get.assert_called_with(
|
||||
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
|
||||
headers=self.pagure.get_auth_header()
|
||||
)
|
||||
|
|
|
@ -608,6 +608,14 @@ class SCMRequestProcessor(ToddlerBase):
|
|||
with TemporaryDirectory(dir=self.temp_dir) as tmp_dir:
|
||||
repo = git.clone_repo(dist_git_url, tmp_dir)
|
||||
default_branch = self.dist_git.get_default_branch(namespace, repo)
|
||||
if not default_branch:
|
||||
self.pagure_io.close_issue(
|
||||
issue["id"],
|
||||
namespace=PROJECT_NAMESPACE,
|
||||
message='There is no default branch set for {0}/{1}'.format(namespace, repo),
|
||||
reason="Invalid"
|
||||
)
|
||||
return
|
||||
commit = repo.first_commit(default_branch)
|
||||
self.dist_git.new_branch(namespace, repo, branch_name, from_commit=commit)
|
||||
new_branch_comment = ('The branch was created in PDC and git. It '
|
||||
|
|
|
@ -389,7 +389,7 @@ class Pagure:
|
|||
Dictionary containing list of contributors.
|
||||
|
||||
Raises:
|
||||
`toddlers.utils.exceptions.PagureError``: When setting new admin fails.
|
||||
`toddlers.utils.exceptions.PagureError``: When getting contributors fails.
|
||||
"""
|
||||
contributors_api_url = '{0}/api/0/{1}/{2}/contributors'.format(self._pagure_url, namespace, repo)
|
||||
headers = self.get_auth_header()
|
||||
|
@ -409,3 +409,36 @@ class Pagure:
|
|||
raise PagureError("Couldn't get contributors for project '{0}/{1}'".format(namespace, repo))
|
||||
|
||||
return response.json()
|
||||
|
||||
def get_default_branch(self, namespace: str, repo: str) -> str:
|
||||
"""
|
||||
Return the default branch for the specified repository.
|
||||
|
||||
Params:
|
||||
namespace: Namespace of the project
|
||||
repo: Name of the project
|
||||
|
||||
Returns:
|
||||
Name of the default branch or None.
|
||||
|
||||
Raises:
|
||||
`toddlers.utils.exceptions.PagureError``: When getting default branch fails.
|
||||
"""
|
||||
branches_api_url = '{0}/api/0/{1}/{2}/git/branches'.format(self._pagure_url, namespace, repo)
|
||||
headers = self.get_auth_header()
|
||||
|
||||
log.debug("Getting default branch for project '{0}/{1}'".format(
|
||||
namespace, repo))
|
||||
response = self._requests_session.get(
|
||||
branches_api_url, headers=headers
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
log.error(
|
||||
"Error when getting default branch for project '{0}/{1}'. Got status_code '{2}'.".format(
|
||||
namespace, repo, response.status_code
|
||||
)
|
||||
)
|
||||
raise PagureError("Couldn't get default branch for project '{0}/{1}'".format(namespace, repo))
|
||||
|
||||
return response.json().get("default", None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue