Skip to content

Commit

Permalink
Add: Add API for getting all artifacts of a GitHub repo
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Sep 20, 2022
1 parent 1d29701 commit 08c7731
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,34 @@ def set_labels(self, repo: str, issue: int, labels: List[str]):
data = {"labels": labels}
response = self._request(api, data=data, request=httpx.post)
response.raise_for_status()

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"
page = 1
per_page = 100
params = {"per_page": per_page, "page": page}
response = self._request(api, request=httpx.get, params=params)
json = response.json()
total = json.get("total_count", 0)
artifacts = list(json.get("artifacts", []))
downloaded = len(artifacts)

# downloading all the artifacts uses a different pagination :-/
while total - downloaded > 0:
page += 1
params = {"per_page": per_page, "page": page}
response = self._request(api, request=httpx.get, params=params)
json = response.json()
artifacts.extend(list(json.get("artifacts", [])))
downloaded = len(artifacts)

return artifacts
94 changes: 94 additions & 0 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,97 @@ def test_upload_release_assets_upload_fails(
},
params=None,
)

@patch("pontos.github.api.httpx.get")
def test_get_repository_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_repository_artifacts("foo/bar")

requests_mock.assert_called_once_with(
"https://api.github.com/repos/foo/bar/actions/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_repository_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_repository_artifacts("foo/bar")

requests_mock.assert_has_calls(
[
call.__bool__(),
call(
"https://api.github.com/repos/foo/bar/actions/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/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 08c7731

Please sign in to comment.