Add method to retrieve issue to pagure module
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
This commit is contained in:
parent
3ff54fda40
commit
151820072a
2 changed files with 117 additions and 12 deletions
|
@ -78,6 +78,70 @@ class TestPagureGetAuthHeader:
|
|||
assert exp_header == header
|
||||
|
||||
|
||||
class TestPagureGetIssue:
|
||||
"""
|
||||
Test class for `toddlers.pagure.Pagure.get_issue` 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_issue(self):
|
||||
"""
|
||||
Assert that issue on pagure is retrieved correctly.
|
||||
"""
|
||||
response_mock = Mock()
|
||||
response_mock.status_code = 200
|
||||
data = {"id": 100}
|
||||
response_mock.json.return_value = data
|
||||
|
||||
self.pagure._requests_session.get.return_value = response_mock
|
||||
|
||||
issue_id = 100
|
||||
namespace = "test"
|
||||
|
||||
result = self.pagure.get_issue(issue_id, namespace)
|
||||
|
||||
self.pagure._requests_session.get.assert_called_with(
|
||||
"https://pagure.io/api/0/test/issue/100",
|
||||
headers=self.pagure.get_auth_header(),
|
||||
)
|
||||
|
||||
assert result == data
|
||||
|
||||
def test_get_issue_not_ok(self):
|
||||
"""
|
||||
Assert that failing to retrieve issue on pagure is handled correctly.
|
||||
"""
|
||||
response_mock = Mock()
|
||||
response_mock.status_code = 500
|
||||
|
||||
self.pagure._requests_session.get.return_value = response_mock
|
||||
|
||||
issue_id = 100
|
||||
namespace = "test"
|
||||
|
||||
expected_error = "Couldn't retrieve issue '{}' from project '{}'".format(
|
||||
issue_id, namespace
|
||||
)
|
||||
|
||||
with pytest.raises(PagureError, match=expected_error):
|
||||
self.pagure.get_issue(issue_id, namespace)
|
||||
|
||||
self.pagure._requests_session.get.assert_called_with(
|
||||
"https://pagure.io/api/0/test/issue/100",
|
||||
headers=self.pagure.get_auth_header(),
|
||||
)
|
||||
|
||||
|
||||
class TestPagureCloseIssue:
|
||||
"""
|
||||
Test class for `toddlers.pagure.Pagure.close_issue` function.
|
||||
|
|
|
@ -79,6 +79,47 @@ class Pagure:
|
|||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
def get_issue(self, issue_id: int, namespace: str) -> dict:
|
||||
"""
|
||||
Get specific issue from project in pagure.
|
||||
|
||||
Params:
|
||||
issue_id: Issue to retrieve
|
||||
namespace: Project namespace. For example: 'releng/fedora-scm-requests'
|
||||
|
||||
Returns:
|
||||
Dictionary containing pagure issue.
|
||||
|
||||
Raises:
|
||||
toddlers.utils.exceptions.PagureError when the issue couldn't be retrieved.
|
||||
"""
|
||||
api_url = "{0}/api/0/{1}/issue/{2}".format(
|
||||
self._pagure_url, namespace, issue_id
|
||||
)
|
||||
headers = self.get_auth_header()
|
||||
|
||||
log.debug(
|
||||
"Retrieving issue '{0}' from project '{1}'".format(issue_id, namespace)
|
||||
)
|
||||
response = self._requests_session.get(api_url, headers=headers)
|
||||
|
||||
result = {}
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
return result
|
||||
|
||||
log.error(
|
||||
"Error when retrieving issue '{0}' from project '{1}'. "
|
||||
"Got status_code '{2}'.".format(issue_id, namespace, response.status_code)
|
||||
)
|
||||
|
||||
raise PagureError(
|
||||
"Couldn't retrieve issue '{0}' from project '{1}'".format(
|
||||
issue_id, namespace
|
||||
)
|
||||
)
|
||||
|
||||
def close_issue(
|
||||
self, issue_id: int, namespace: str, message: str, reason: str = "Closed"
|
||||
):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue