Skip to content

Commit 0951563

Browse files
committed
add pagination for artifacts retrieval
1 parent bb6740a commit 0951563

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
.coverage
44
dev-env-vars
55
.coverage*
6+
.python-version
7+
__pycache__
8+
.vscode

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.DEFAULT_GOAL := help
2+
SHELL = bash
3+
4+
5+
.PHONY: help
6+
help: ## show this help
7+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
8+
9+
.PHONY: install
10+
install: ## install dependencies
11+
poetry install --with dev
12+
poetry run pre-commit install
13+
14+
.PHONY: lint
15+
lint: ## lint code
16+
poetry run ruff check --fix coverage_comment tests
17+
poetry run ruff format coverage_comment tests
18+
19+
.PHONY: test
20+
test: ## run all tests
21+
poetry run pytest tests -vv --cov-report term-missing --cov=coverage_comment

coverage_comment/github.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ def download_artifact(
5454
filename: pathlib.Path,
5555
) -> str:
5656
repo_path = github.repos(repository)
57-
artifacts = repo_path.actions.runs(run_id).artifacts.get().artifacts
57+
page = 1
58+
artifacts = []
59+
while True:
60+
result = repo_path.actions.runs(run_id).artifacts.get(page=str(page))
61+
if not result:
62+
break
63+
artifacts.extend(result.artifacts)
64+
page += 1
5865
try:
5966
artifact = next(
6067
iter(artifact for artifact in artifacts if artifact.name == artifact_name),

tests/integration/test_github.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,48 @@ def test_download_artifact(gh, session, zip_bytes):
5454
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
5555
json={"artifacts": artifacts}
5656
)
57+
session.register(
58+
"GET",
59+
"/repos/foo/bar/actions/runs/123/artifacts",
60+
params={"page": "2"},
61+
)(json={})
62+
63+
session.register("GET", "/repos/foo/bar/actions/artifacts/789/zip")(
64+
content=zip_bytes(filename="foo.txt", content="bar")
65+
)
66+
67+
result = github.download_artifact(
68+
github=gh,
69+
repository="foo/bar",
70+
artifact_name="foo",
71+
run_id=123,
72+
filename=pathlib.Path("foo.txt"),
73+
)
74+
75+
assert result == "bar"
76+
77+
78+
def test_download_artifact_from_page_2(gh, session, zip_bytes):
79+
artifacts_page_1 = [
80+
{"name": "test", "id": 000},
81+
]
82+
artifacts_page_2 = [
83+
{"name": "bar", "id": 456},
84+
{"name": "foo", "id": 789},
85+
]
86+
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
87+
json={"artifacts": artifacts_page_1}
88+
)
89+
session.register(
90+
"GET",
91+
"/repos/foo/bar/actions/runs/123/artifacts",
92+
params={"page": "2"},
93+
)(json={"artifacts": artifacts_page_2})
94+
session.register(
95+
"GET",
96+
"/repos/foo/bar/actions/runs/123/artifacts",
97+
params={"page": "3"},
98+
)(json={})
5799

58100
session.register("GET", "/repos/foo/bar/actions/artifacts/789/zip")(
59101
content=zip_bytes(filename="foo.txt", content="bar")
@@ -77,6 +119,11 @@ def test_download_artifact__no_artifact(gh, session):
77119
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
78120
json={"artifacts": artifacts}
79121
)
122+
session.register(
123+
"GET",
124+
"/repos/foo/bar/actions/runs/123/artifacts",
125+
params={"page": "2"},
126+
)(json={})
80127

81128
with pytest.raises(github.NoArtifact):
82129
github.download_artifact(
@@ -95,6 +142,11 @@ def test_download_artifact__no_file(gh, session, zip_bytes):
95142
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
96143
json={"artifacts": artifacts}
97144
)
145+
session.register(
146+
"GET",
147+
"/repos/foo/bar/actions/runs/123/artifacts",
148+
params={"page": "2"},
149+
)(json={})
98150

99151
session.register("GET", "/repos/foo/bar/actions/artifacts/789/zip")(
100152
content=zip_bytes(filename="foo.txt", content="bar")

tests/integration/test_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ def test_action__workflow_run__no_artifact(
812812
"GET",
813813
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
814814
)(json={"artifacts": [{"name": "wrong_name"}]})
815+
session.register(
816+
"GET",
817+
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
818+
params={"page": "2"},
819+
)(json={})
815820

816821
result = main.action(
817822
config=workflow_run_config(),
@@ -853,6 +858,11 @@ def test_action__workflow_run__post_comment(
853858
"GET",
854859
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
855860
)(json={"artifacts": [{"name": "python-coverage-comment-action", "id": 789}]})
861+
session.register(
862+
"GET",
863+
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
864+
params={"page": "2"},
865+
)(json={})
856866

857867
session.register(
858868
"GET",

0 commit comments

Comments
 (0)