Skip to content

Commit

Permalink
Add: Add GitHub API for getting information about a single workflow run
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Sep 21, 2022
1 parent 795709d commit 355f65a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,22 @@ def get_workflow_runs(
)

return self._get_paged_items(api, "workflow_runs")

def get_workflow_run(self, repo: str, run: str) -> JSON:
"""
Get information about a single workflow run
Args:
repo: GitHub repository (owner/name) to use
Raises:
HTTPStatusError: A httpx.HTTPStatusError is raised if the request
failed.
Returns:
Information about the workflow runs as a dict
"""
api = f"/repos/{repo}/actions/runs/{run}"
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 @@ -1406,3 +1406,52 @@ def test_get_workflow_runs_for_workflow(self, requests_mock: MagicMock):

self.assertEqual(len(artifacts), 1)
self.assertEqual(artifacts[0]["name"], "Foo")

@patch("pontos.github.api.httpx.get")
def test_get_workflow_run(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_run("foo/bar", "123")

requests_mock.assert_called_once_with(
"https://api.github.com/repos/foo/bar/actions/runs/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_run_invalid(self, requests_mock: MagicMock):
response = MagicMock(autospec=httpx.Response)
response.is_success = False
response.raise_for_status.side_effect = httpx.HTTPStatusError(
"Workflow Run Not Found", request=None, response=response
)

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

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

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

0 comments on commit 355f65a

Please sign in to comment.