Skip to content

Commit

Permalink
Add: Add GitHub API to get information of a single workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Sep 21, 2022
1 parent d4729a8 commit e72bb34
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,28 @@ def get_workflows(self, repo: str) -> Iterable[JSON]:
Args:
repo: GitHub repository (owner/name) to use
Returns:
Information about the workflows as an iterable of dicts
"""
api = f"/repos/{repo}/actions/workflows"
return self._get_paged_items(api, "workflows")

def get_workflow(self, repo: str, workflow: str) -> JSON:
"""
List all workflows of a repository
Args:
repo: GitHub repository (owner/name) to use
workflow: ID of the workflow
Raises:
HTTPStatusError: A httpx.HTTPStatusError is raised if the request
failed.
Returns:
Information about the as a dict
Information about the workflows as a dict
"""
api = f"/repos/{repo}/actions/workflows"
return self._get_paged_items(api, "workflows")
api = f"/repos/{repo}/actions/workflows/{workflow}"
response = self._request(api, request=httpx.get)
response.raise_for_status()
return response.json()
49 changes: 49 additions & 0 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,52 @@ def test_get_workflows_with_pagination(self, requests_mock: MagicMock):
self.assertEqual(len(artifacts), 120)
self.assertEqual(artifacts[0]["name"], "Foo-0")
self.assertEqual(artifacts[119]["name"], "Foo-119")

@patch("pontos.github.api.httpx.get")
def test_get_workflow(self, requests_mock: MagicMock):
response = MagicMock(autospec=httpx.Response)
response.json.return_value = {
"id": 123,
"name": "Foo",
}

requests_mock.return_value = response
api = GitHubRESTApi("12345")
artifacts = api.get_workflow("foo/bar", "123")

requests_mock.assert_called_once_with(
"https://api.github.com/repos/foo/bar/actions/workflows/123",
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
},
params=None,
follow_redirects=True,
)

self.assertEqual(artifacts["id"], 123)
self.assertEqual(artifacts["name"], "Foo")

@patch("pontos.github.api.httpx.get")
def test_get_workflow_invalid(self, requests_mock: MagicMock):
response = MagicMock(autospec=httpx.Response)
response.is_success = False
response.raise_for_status.side_effect = httpx.HTTPStatusError(
"Testing Status Message", request=None, response=response
)

requests_mock.return_value = response
api = GitHubRESTApi("12345")

with self.assertRaises(httpx.HTTPStatusError):
api.get_workflow("foo/bar", "123")

requests_mock.assert_called_once_with(
"https://api.github.com/repos/foo/bar/actions/workflows/123",
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
},
params=None,
follow_redirects=True,
)

0 comments on commit e72bb34

Please sign in to comment.