Skip to content

Commit

Permalink
Add: Add GitHub API for requesting workflow artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Sep 20, 2022
1 parent c14b798 commit 870155b
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 9 deletions.
41 changes: 32 additions & 9 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,11 @@ def set_labels(self, repo: str, issue: int, labels: List[str]):
response = self._request(api, data=data, request=httpx.post)
response.raise_for_status()

def get_repository_artifacts(self, repo: str) -> Iterable[JSON]:
def _get_artifacts(self, api: str) -> Iterable[JSON]:
"""
List all artifacts of a repository
Args:
repo: GitHub repository (owner/name) to use
Returns:
Information about the artifacts in the repository as a dict
Internal method to get the artifacts information from different REST
URLs.
"""
api = f"/repos/{repo}/actions/artifacts"
page = 1
per_page = 100
params = {"per_page": per_page, "page": page}
Expand All @@ -573,6 +567,19 @@ def get_repository_artifacts(self, repo: str) -> Iterable[JSON]:

return artifacts

def get_repository_artifacts(self, repo: str) -> Iterable[JSON]:
"""
List all artifacts of a repository
Args:
repo: GitHub repository (owner/name) to use
Returns:
Information about the artifacts in the repository as a dict
"""
api = f"/repos/{repo}/actions/artifacts"
return self._get_artifacts(api)

def get_repository_artifact(
self, repo: str, artifact: str
) -> Iterable[JSON]:
Expand Down Expand Up @@ -623,3 +630,19 @@ def download_repository_artifact(
"""
api = f"{self.url}/repos/{repo}/actions/artifacts/{artifact}/zip"
return download(api, destination, headers=self._request_headers())

def get_workflow_artifacts(
self, repo: str, workflow: str
) -> Iterable[JSON]:
"""
List all artifacts for a workflow run
Args:
repo: GitHub repository (owner/name) to use
workflow: The unique identifier of the workflow run
Returns:
Information about the artifacts in the workflow as a dict
"""
api = f"/repos/{repo}/actions/runs/{workflow}/artifacts"
return self._get_artifacts(api)
98 changes: 98 additions & 0 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=too-many-lines

import json
import unittest
from pathlib import Path
Expand Down Expand Up @@ -957,3 +959,99 @@ def test_download_repository_artifact(

with self.assertRaises(StopIteration):
next(it)

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

requests_mock.assert_called_once_with(
"https://api.github.com/repos/foo/bar/actions/runs/123/artifacts",
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_artifacts_with_pagination(
self, requests_mock: MagicMock
):
response = MagicMock()
response.links = None
response.json.side_effect = [
{
"total_count": 120,
"artifacts": [
{
"id": id,
"name": f"Foo-{id}",
}
for id in range(0, 100)
],
},
{
"total_count": 120,
"artifacts": [
{
"id": id,
"name": f"Foo-{id}",
}
for id in range(100, 120)
],
},
]
requests_mock.return_value = response
api = GitHubRESTApi("12345")
artifacts = api.get_workflow_artifacts("foo/bar", "123")

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

self.assertEqual(len(artifacts), 120)
self.assertEqual(artifacts[0]["name"], "Foo-0")
self.assertEqual(artifacts[119]["name"], "Foo-119")

0 comments on commit 870155b

Please sign in to comment.