Skip to content

Commit fb464e5

Browse files
committed
Rename size-deltas-reports-artifact-name input to sketches-reports-source-name
Preparation to use the input for specifying the source of sketches reports either in a workflow artifact or a local folder. The old input name is still supported but warning will be displayed in the build log to explain the name change.
1 parent a1aa1bc commit fb464e5

File tree

4 files changed

+88
-28
lines changed

4 files changed

+88
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This action comments on the pull request with a report on the resulting change i
88

99
## Inputs
1010

11-
### `size-deltas-reports-artifact-name`
11+
### `sketches-reports-source-name`
1212

1313
Name of the [workflow artifact](https://docs.github.com/en/actions/configuring-and-managing-workflows/persisting-workflow-data-using-artifacts) that contains the memory usage data, as specified to the [`actions/upload-artifact`](https://github.com/actions/upload-artifact) action via its `name` input.
1414

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: 'Report Arduino Sketch Size Deltas'
22
description: 'Comments on the pull request with a report on the resulting change in memory usage of Arduino sketches'
33
inputs:
4-
size-deltas-reports-artifact-name:
5-
description: 'Name of the workflow artifact that contains the memory usage data, as specified to the actions/upload-artifact action via its name input'
4+
sketches-reports-source-name:
5+
description: 'When run from scheduled workflow, name of the workflow artifact that contains sketches reports. When run from a pull request triggered workflow, path to the folder containing sketches reports.'
66
default: 'size-deltas-reports'
77
github-token:
88
description: 'GitHub access token used to comment the memory usage comparison results to the PR thread'

reportsizedeltas/reportsizedeltas.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
def main():
2020
set_verbosity(enable_verbosity=False)
2121

22+
if "INPUT_SIZE-DELTAS-REPORTS-ARTIFACT-NAME" in os.environ:
23+
print("::warning::The size-deltas-report-artifact-name input is deprecated. Use the equivalent input: "
24+
"sketches-reports-source-name instead.")
25+
os.environ["INPUT_SKETCHES-REPORTS-SOURCE-NAME"] = os.environ["INPUT_SIZE-DELTAS-REPORTS-ARTIFACT-NAME"]
26+
2227
report_size_deltas = ReportSizeDeltas(repository_name=os.environ["GITHUB_REPOSITORY"],
23-
artifact_name=os.environ["INPUT_SIZE-DELTAS-REPORTS-ARTIFACT-NAME"],
28+
sketches_reports_source_name=os.environ["INPUT_SKETCHES-REPORTS-SOURCE-NAME"],
2429
token=os.environ["INPUT_GITHUB-TOKEN"])
2530

2631
report_size_deltas.report_size_deltas()
@@ -73,9 +78,9 @@ class ReportKeys:
7378
sketches = "sketches"
7479
compilation_success = "compilation_success"
7580

76-
def __init__(self, repository_name, artifact_name, token):
81+
def __init__(self, repository_name, sketches_reports_source_name, token):
7782
self.repository_name = repository_name
78-
self.artifact_name = artifact_name
83+
self.sketches_reports_source_name = sketches_reports_source_name
7984
self.token = token
8085

8186
def report_size_deltas(self):
@@ -209,7 +214,7 @@ def get_artifact_download_url_for_run(self, run_id):
209214

210215
for artifact_data in artifacts_data["artifacts"]:
211216
# The artifact is identified by a specific name
212-
if artifact_data["name"] == self.artifact_name:
217+
if artifact_data["name"] == self.sketches_reports_source_name:
213218
return artifact_data["archive_download_url"]
214219

215220
page_number += 1
@@ -228,12 +233,13 @@ def get_artifact(self, artifact_download_url):
228233
artifact_folder_object = tempfile.TemporaryDirectory(prefix="reportsizedeltas-")
229234
try:
230235
# Download artifact
231-
with open(file=artifact_folder_object.name + "/" + self.artifact_name + ".zip", mode="wb") as out_file:
236+
with open(file=artifact_folder_object.name + "/" + self.sketches_reports_source_name + ".zip",
237+
mode="wb") as out_file:
232238
with self.raw_http_request(url=artifact_download_url) as fp:
233239
out_file.write(fp.read())
234240

235241
# Unzip artifact
236-
artifact_zip_file = artifact_folder_object.name + "/" + self.artifact_name + ".zip"
242+
artifact_zip_file = artifact_folder_object.name + "/" + self.sketches_reports_source_name + ".zip"
237243
with zipfile.ZipFile(file=artifact_zip_file, mode="r") as zip_ref:
238244
zip_ref.extractall(path=artifact_folder_object.name)
239245
os.remove(artifact_zip_file)

reportsizedeltas/tests/test_reportsizedeltas.py

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import distutils.dir_util
22
import filecmp
33
import json
4+
import os
45
import pathlib
56
import tempfile
67
import unittest.mock
@@ -18,16 +19,17 @@
1819

1920

2021
def get_reportsizedeltas_object(repository_name="FooOwner/BarRepository",
21-
artifact_name="foo-artifact-name",
22+
sketches_reports_source_name="foo-artifact-name",
2223
token="foo token"):
2324
"""Return a reportsizedeltas.ReportSizeDeltas object to use in tests.
2425
2526
Keyword arguments:
2627
repository_name -- repository owner and name e.g., octocat/Hello-World
27-
artifact_name -- name of the workflow artifact that contains the memory usage data
28+
sketches_reports_source_name -- name of the workflow artifact that contains the memory usage data
2829
token -- GitHub access token
2930
"""
30-
return reportsizedeltas.ReportSizeDeltas(repository_name=repository_name, artifact_name=artifact_name, token=token)
31+
return reportsizedeltas.ReportSizeDeltas(repository_name=repository_name,
32+
sketches_reports_source_name=sketches_reports_source_name, token=token)
3133

3234

3335
def directories_are_same(left_directory, right_directory):
@@ -89,33 +91,85 @@ def test_directories_are_same(tmp_path):
8991
assert directories_are_same(left_directory=left_directory, right_directory=right_directory) is True
9092

9193

92-
def test_main(monkeypatch, mocker):
93-
repository_name = "FooOwner/BarRepository"
94-
artifact_name = "foo-artifact-name"
95-
token = "foo GitHub token"
96-
monkeypatch.setenv("GITHUB_REPOSITORY", repository_name)
97-
monkeypatch.setenv("INPUT_SIZE-DELTAS-REPORTS-ARTIFACT-NAME", artifact_name)
98-
monkeypatch.setenv("INPUT_GITHUB-TOKEN", token)
94+
@pytest.fixture
95+
def setup_environment_variables(monkeypatch):
96+
"""Test fixture that sets up the environment variables required by reportsizedeltas.main() and returns an object
97+
containing the values"""
9998

99+
class ActionInputs:
100+
"""A container for the values of the environment variables"""
101+
repository_name = "GoldenOwner/GoldenRepository"
102+
sketches_reports_source_name = "golden-artifact-name"
103+
token = "golden-github-token"
104+
105+
monkeypatch.setenv("GITHUB_REPOSITORY", ActionInputs.repository_name)
106+
monkeypatch.setenv("INPUT_SKETCHES-REPORTS-SOURCE-NAME", ActionInputs.sketches_reports_source_name)
107+
monkeypatch.setenv("INPUT_GITHUB-TOKEN", ActionInputs.token)
108+
109+
return ActionInputs()
110+
111+
112+
def test_main(monkeypatch, mocker, setup_environment_variables):
100113
class ReportSizeDeltas:
101114
"""Stub"""
102115

103116
def report_size_deltas(self):
104117
"""Stub"""
105-
pass # pragma: no cover
118+
pass # pragma: no cover
106119

107120
mocker.patch("reportsizedeltas.set_verbosity", autospec=True)
108121
mocker.patch("reportsizedeltas.ReportSizeDeltas", autospec=True, return_value=ReportSizeDeltas())
109122
mocker.patch.object(ReportSizeDeltas, "report_size_deltas")
110123
reportsizedeltas.main()
111124

112125
reportsizedeltas.set_verbosity.assert_called_once_with(enable_verbosity=False)
113-
reportsizedeltas.ReportSizeDeltas.assert_called_once_with(repository_name=repository_name,
114-
artifact_name=artifact_name,
115-
token=token)
126+
reportsizedeltas.ReportSizeDeltas.assert_called_once_with(
127+
repository_name=setup_environment_variables.repository_name,
128+
sketches_reports_source_name=setup_environment_variables.sketches_reports_source_name,
129+
token=setup_environment_variables.token
130+
)
116131
ReportSizeDeltas.report_size_deltas.assert_called_once()
117132

118133

134+
@pytest.mark.parametrize("use_size_deltas_report_artifact_name", [True, False])
135+
def test_main_size_deltas_report_artifact_name_deprecation_warning(capsys,
136+
mocker,
137+
monkeypatch, setup_environment_variables,
138+
use_size_deltas_report_artifact_name):
139+
size_deltas_report_artifact_name = "golden-size-deltas-report-artifact-name-value"
140+
141+
if use_size_deltas_report_artifact_name:
142+
monkeypatch.setenv("INPUT_SIZE-DELTAS-REPORTS-ARTIFACT-NAME", size_deltas_report_artifact_name)
143+
expected_sketches_reports_source_name = size_deltas_report_artifact_name
144+
else:
145+
expected_sketches_reports_source_name = setup_environment_variables.sketches_reports_source_name
146+
147+
class ReportSizeDeltas:
148+
"""Stub"""
149+
150+
def report_size_deltas(self):
151+
"""Stub"""
152+
pass # pragma: no cover
153+
154+
mocker.patch("reportsizedeltas.set_verbosity", autospec=True)
155+
mocker.patch("reportsizedeltas.ReportSizeDeltas", autospec=True, return_value=ReportSizeDeltas())
156+
mocker.patch.object(ReportSizeDeltas, "report_size_deltas")
157+
158+
reportsizedeltas.main()
159+
160+
expected_output = ""
161+
if use_size_deltas_report_artifact_name:
162+
expected_output = (
163+
expected_output
164+
+ "::warning::The size-deltas-report-artifact-name input is deprecated. Use the equivalent input: "
165+
"sketches-reports-source-name instead."
166+
)
167+
168+
assert capsys.readouterr().out.strip() == expected_output
169+
170+
assert os.environ["INPUT_SKETCHES-REPORTS-SOURCE-NAME"] == expected_sketches_reports_source_name
171+
172+
119173
def test_set_verbosity():
120174
with pytest.raises(TypeError):
121175
reportsizedeltas.set_verbosity(enable_verbosity=2)
@@ -303,14 +357,14 @@ def test_get_artifact_download_url_for_sha():
303357

304358
def test_get_artifact_download_url_for_run():
305359
repository_name = "test_name/test_repo"
306-
artifact_name = "test_artifact_name"
360+
sketches_reports_source_name = "test_sketches_reports_source_name"
307361
archive_download_url = "archive_download_url"
308362
run_id = "1234"
309363

310364
report_size_deltas = get_reportsizedeltas_object(repository_name=repository_name,
311-
artifact_name=artifact_name)
365+
sketches_reports_source_name=sketches_reports_source_name)
312366

313-
json_data = {"artifacts": [{"name": artifact_name, "archive_download_url": archive_download_url},
367+
json_data = {"artifacts": [{"name": sketches_reports_source_name, "archive_download_url": archive_download_url},
314368
{"name": "bar123", "archive_download_url": "wrong_artifact_url"}]}
315369
report_size_deltas.api_request = unittest.mock.MagicMock(return_value={"json_data": json_data,
316370
"additional_pages": False,
@@ -621,7 +675,7 @@ def test_get_sketches_reports(sketches_reports_path, expected_sketches_reports):
621675
try:
622676
distutils.dir_util.copy_tree(src=str(sketches_reports_path),
623677
dst=artifact_folder_object.name)
624-
except Exception: # pragma: no cover
678+
except Exception: # pragma: no cover
625679
artifact_folder_object.cleanup()
626680
raise
627681
sketches_reports = report_size_deltas.get_sketches_reports(artifact_folder_object=artifact_folder_object)
@@ -662,7 +716,7 @@ def test_generate_report():
662716
try:
663717
distutils.dir_util.copy_tree(src=str(sketches_report_path),
664718
dst=artifact_folder_object.name)
665-
except Exception: # pragma: no cover
719+
except Exception: # pragma: no cover
666720
artifact_folder_object.cleanup()
667721
raise
668722
sketches_reports = report_size_deltas.get_sketches_reports(artifact_folder_object=artifact_folder_object)

0 commit comments

Comments
 (0)