Skip to content

Commit 495ca5e

Browse files
authored
Skip expired workflow artifacts (#10)
Workflow artifacts expire after a default of 90 days. For some inscrutable reason, the GitHub workflow artifacts API still lists these artifacts, even though they are no longer available. Previously, if the repository contained an open PR with an expired artifact, it caused the action to fail when it tried to download the non-existent artifact and got an HTTP 500 error.
1 parent ac1c7fa commit 495ca5e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

reportsizedeltas/reportsizedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def get_artifact_download_url_for_run(self, run_id):
239239

240240
for artifact_data in artifacts_data["artifacts"]:
241241
# The artifact is identified by a specific name
242-
if artifact_data["name"] == self.sketches_reports_source:
242+
if not artifact_data["expired"] and artifact_data["name"] == self.sketches_reports_source:
243243
return artifact_data["archive_download_url"]
244244

245245
page_number += 1

reportsizedeltas/tests/test_reportsizedeltas.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ def test_get_artifact_download_url_for_sha():
411411
report_size_deltas.get_artifact_download_url_for_run.assert_called_once_with(run_id=run_id)
412412

413413

414-
def test_get_artifact_download_url_for_run():
414+
@pytest.mark.parametrize("expired", [True, False])
415+
@pytest.mark.parametrize("artifact_name", ["test_sketches_reports_source", "incorrect-artifact-name"])
416+
def test_get_artifact_download_url_for_run(expired, artifact_name):
415417
repository_name = "test_name/test_repo"
416418
sketches_reports_source = "test_sketches_reports_source"
417419
archive_download_url = "archive_download_url"
@@ -420,29 +422,35 @@ def test_get_artifact_download_url_for_run():
420422
report_size_deltas = get_reportsizedeltas_object(repository_name=repository_name,
421423
sketches_reports_source=sketches_reports_source)
422424

423-
json_data = {"artifacts": [{"name": sketches_reports_source, "archive_download_url": archive_download_url},
424-
{"name": "bar123", "archive_download_url": "wrong_artifact_url"}]}
425+
json_data = {
426+
"artifacts": [
427+
{
428+
"name": artifact_name,
429+
"archive_download_url": archive_download_url,
430+
"expired": expired
431+
},
432+
{
433+
"name": "bar123",
434+
"archive_download_url": "wrong_artifact_url",
435+
"expired": False
436+
}
437+
]
438+
}
425439
report_size_deltas.api_request = unittest.mock.MagicMock(return_value={"json_data": json_data,
426440
"additional_pages": False,
427441
"page_count": 1})
428442

429-
# Artifact name match
430-
assert archive_download_url == report_size_deltas.get_artifact_download_url_for_run(run_id=run_id)
443+
download_url = report_size_deltas.get_artifact_download_url_for_run(run_id=run_id)
444+
if not expired and artifact_name == sketches_reports_source:
445+
assert download_url == archive_download_url
446+
else:
447+
assert download_url is None
431448

432449
report_size_deltas.api_request.assert_called_once_with(
433450
request="repos/" + repository_name + "/actions/runs/" + str(run_id)
434451
+ "/artifacts",
435452
page_number=1)
436453

437-
json_data = {"artifacts": [{"name": "foo123", "archive_download_url": "test_artifact_url"},
438-
{"name": "bar123", "archive_download_url": "wrong_artifact_url"}]}
439-
report_size_deltas.api_request = unittest.mock.MagicMock(return_value={"json_data": json_data,
440-
"additional_pages": False,
441-
"page_count": 1})
442-
443-
# No artifact name match
444-
assert report_size_deltas.get_artifact_download_url_for_run(run_id=run_id) is None
445-
446454

447455
@pytest.mark.parametrize("test_artifact_name, expected_success",
448456
[("correct-artifact-name", True),

0 commit comments

Comments
 (0)