Skip to content

Commit ac3108e

Browse files
committed
improvements on pagination after code review
1 parent 0951563 commit ac3108e

File tree

5 files changed

+91
-60
lines changed

5 files changed

+91
-60
lines changed

.gitignore

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

Makefile

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
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-
91
.PHONY: install
102
install: ## install dependencies
113
poetry install --with dev
12-
poetry run pre-commit install
4+
pre-commit install
135

146
.PHONY: lint
157
lint: ## lint code
16-
poetry run ruff check --fix coverage_comment tests
17-
poetry run ruff format coverage_comment tests
8+
pre-commit
189

1910
.PHONY: test
2011
test: ## run all tests
21-
poetry run pytest tests -vv --cov-report term-missing --cov=coverage_comment
12+
poetry run pytest tests

coverage_comment/github.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,15 @@ def download_artifact(
5454
filename: pathlib.Path,
5555
) -> str:
5656
repo_path = github.repos(repository)
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
57+
6558
try:
6659
artifact = next(
67-
iter(artifact for artifact in artifacts if artifact.name == artifact_name),
60+
artifact
61+
for artifact in _fetch_artifacts(repo_path, run_id)
62+
if artifact.name == artifact_name
6863
)
6964
except StopIteration:
70-
raise NoArtifact(
71-
f"Not artifact found with name {artifact_name} in run {run_id}"
72-
)
65+
raise NoArtifact(f"No artifact found with name {artifact_name} in run {run_id}")
7366

7467
zip_bytes = io.BytesIO(repo_path.actions.artifacts(artifact.id).zip.get(bytes=True))
7568
zipf = zipfile.ZipFile(zip_bytes)
@@ -80,6 +73,24 @@ def download_artifact(
8073
raise NoArtifact(f"File named {filename} not found in artifact {artifact_name}")
8174

8275

76+
def _fetch_artifacts(repo_path, run_id):
77+
page = 1
78+
total_fetched = 0
79+
80+
while True:
81+
result = repo_path.actions.runs(run_id).artifacts.get(page=str(page))
82+
if not result or not result.artifacts:
83+
break
84+
85+
yield from result.artifacts
86+
87+
total_fetched += len(result.artifacts)
88+
if total_fetched >= result.total_count:
89+
break
90+
91+
page += 1
92+
93+
8394
def get_branch_from_workflow_run(
8495
github: github_client.GitHub, repository: str, run_id: int
8596
) -> tuple[str, str]:

tests/integration/test_github.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ def test_download_artifact(gh, session, zip_bytes):
5252
{"name": "foo", "id": 789},
5353
]
5454
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
55-
json={"artifacts": artifacts}
55+
json={"artifacts": artifacts, "total_count": 2}
5656
)
57-
session.register(
58-
"GET",
59-
"/repos/foo/bar/actions/runs/123/artifacts",
60-
params={"page": "2"},
61-
)(json={})
6257

6358
session.register("GET", "/repos/foo/bar/actions/artifacts/789/zip")(
6459
content=zip_bytes(filename="foo.txt", content="bar")
@@ -84,18 +79,13 @@ def test_download_artifact_from_page_2(gh, session, zip_bytes):
8479
{"name": "foo", "id": 789},
8580
]
8681
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
87-
json={"artifacts": artifacts_page_1}
82+
json={"artifacts": artifacts_page_1, "total_count": 3}
8883
)
8984
session.register(
9085
"GET",
9186
"/repos/foo/bar/actions/runs/123/artifacts",
9287
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={})
88+
)(json={"artifacts": artifacts_page_2, "total_count": 3})
9989

10090
session.register("GET", "/repos/foo/bar/actions/artifacts/789/zip")(
10191
content=zip_bytes(filename="foo.txt", content="bar")
@@ -117,13 +107,8 @@ def test_download_artifact__no_artifact(gh, session):
117107
{"name": "bar", "id": 456},
118108
]
119109
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
120-
json={"artifacts": artifacts}
110+
json={"artifacts": artifacts, "total_count": 1}
121111
)
122-
session.register(
123-
"GET",
124-
"/repos/foo/bar/actions/runs/123/artifacts",
125-
params={"page": "2"},
126-
)(json={})
127112

128113
with pytest.raises(github.NoArtifact):
129114
github.download_artifact(
@@ -136,9 +121,7 @@ def test_download_artifact__no_artifact(gh, session):
136121

137122

138123
def test_download_artifact__no_file(gh, session, zip_bytes):
139-
artifacts = [
140-
{"name": "foo", "id": 789},
141-
]
124+
artifacts = [{"name": "foo", "id": 789}]
142125
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
143126
json={"artifacts": artifacts}
144127
)
@@ -161,6 +144,59 @@ def test_download_artifact__no_file(gh, session, zip_bytes):
161144
)
162145

163146

147+
def test_fetch_artifacts_empty_response(gh, session):
148+
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
149+
json={"artifacts": [], "total_count": 0}
150+
)
151+
152+
repo_path = gh.repos("foo/bar")
153+
154+
result = github._fetch_artifacts(
155+
repo_path=repo_path,
156+
run_id=123,
157+
)
158+
159+
assert not list(result)
160+
161+
162+
def test_fetch_artifacts_single_page(gh, session):
163+
artifacts = [{"name": "bar", "id": 456}]
164+
165+
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
166+
json={"artifacts": artifacts, "total_count": 1}
167+
)
168+
169+
repo_path = gh.repos("foo/bar")
170+
171+
result = github._fetch_artifacts(
172+
repo_path=repo_path,
173+
run_id=123,
174+
)
175+
176+
assert list(result) == artifacts
177+
178+
179+
def test_fetch_artifacts_multiple_pages(gh, session):
180+
artifacts_page_1 = [{"name": "bar", "id": 456}]
181+
artifacts_page_2 = [{"name": "bar", "id": 789}]
182+
183+
session.register("GET", "/repos/foo/bar/actions/runs/123/artifacts")(
184+
json={"artifacts": artifacts_page_1, "total_count": 2}
185+
)
186+
session.register(
187+
"GET", "/repos/foo/bar/actions/runs/123/artifacts", params={"page": "2"}
188+
)(json={"artifacts": artifacts_page_2, "total_count": 2})
189+
190+
repo_path = gh.repos("foo/bar")
191+
192+
result = github._fetch_artifacts(
193+
repo_path=repo_path,
194+
run_id=123,
195+
)
196+
197+
assert list(result) == artifacts_page_1 + artifacts_page_2
198+
199+
164200
def test_get_branch_from_workflow_run(gh, session):
165201
json = {
166202
"head_branch": "other",

tests/integration/test_main.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,7 @@ def test_action__workflow_run__no_artifact(
811811
session.register(
812812
"GET",
813813
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
814-
)(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={})
814+
)(json={"artifacts": [{"name": "wrong_name"}], "total_count": 1})
820815

821816
result = main.action(
822817
config=workflow_run_config(),
@@ -857,12 +852,12 @@ def test_action__workflow_run__post_comment(
857852
session.register(
858853
"GET",
859854
"/repos/py-cov-action/foobar/actions/runs/123/artifacts",
860-
)(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={})
855+
)(
856+
json={
857+
"artifacts": [{"name": "python-coverage-comment-action", "id": 789}],
858+
"total_count": 1,
859+
}
860+
)
866861

867862
session.register(
868863
"GET",

0 commit comments

Comments
 (0)