Skip to content

Commit

Permalink
Add: Add GitHub API to get workflow runs information
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Sep 21, 2022
1 parent 3565e6a commit 795709d
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,30 @@ def create_workflow_dispatch(

response = self._request(api, data=data, request=httpx.post)
response.raise_for_status()

def get_workflow_runs(
self, repo: str, workflow: Optional[str] = None
) -> Iterable[JSON]:
"""
List all workflow runs of a repository or of a specific workflow.
Args:
repo: GitHub repository (owner/name) to use
workflow: ID of the workflow or workflow file name. For example
`main.yml`.
Raises:
HTTPStatusError: A httpx.HTTPStatusError is raised if the request
failed.
Returns:
Information about the workflow runs as an iterable of dicts
"""

api = (
f"/repos/{repo}/actions/workflows/{workflow}/runs"
if workflow
else f"/repos/{repo}/actions/runs"
)

return self._get_paged_items(api, "workflow_runs")
123 changes: 123 additions & 0 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,126 @@ def test_create_workflow_dispatch_failure(self, requests_mock: MagicMock):
follow_redirects=True,
json={"ref": "main"},
)

@patch("pontos.github.api.httpx.get")
def test_get_workflow_runs(self, requests_mock: MagicMock):
response = MagicMock()
response.links = None
response.json.return_value = {
"total_count": 1,
"workflow_runs": [
{
"id": 11,
"name": "Foo",
}
],
}
requests_mock.return_value = response
api = GitHubRESTApi("12345")
artifacts = api.get_workflow_runs("foo/bar")

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

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

@patch("pontos.github.api.httpx.get")
def test_get_workflow_runs_with_pagination(self, requests_mock: MagicMock):
response = MagicMock()
response.links = None
response.json.side_effect = [
{
"total_count": 120,
"workflow_runs": [
{
"id": id,
"name": f"Foo-{id}",
}
for id in range(0, 100)
],
},
{
"total_count": 120,
"workflow_runs": [
{
"id": id,
"name": f"Foo-{id}",
}
for id in range(100, 120)
],
},
]
requests_mock.return_value = response
api = GitHubRESTApi("12345")
artifacts = api.get_workflow_runs("foo/bar")

requests_mock.assert_has_calls(
[
call.__bool__(),
call(
"https://api.github.com/repos/foo/bar/actions/runs",
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
},
params={"per_page": 100, "page": 1},
follow_redirects=True,
),
call().raise_for_status(),
call().json(),
call.__bool__(),
call(
"https://api.github.com/repos/foo/bar/actions/runs",
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
},
params={"per_page": 100, "page": 2},
follow_redirects=True,
),
call().raise_for_status(),
call().json(),
]
)

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_runs_for_workflow(self, requests_mock: MagicMock):
response = MagicMock()
response.links = None
response.json.return_value = {
"total_count": 1,
"workflow_runs": [
{
"id": 11,
"name": "Foo",
}
],
}
requests_mock.return_value = response
api = GitHubRESTApi("12345")
artifacts = api.get_workflow_runs("foo/bar", "foo")

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

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

0 comments on commit 795709d

Please sign in to comment.