Skip to content

Commit 834ff89

Browse files
authored
refactor(util): move the remaining Actions output onto the helper (#2047) (#2079)
1 parent 6a8fa68 commit 834ff89

21 files changed

Lines changed: 575 additions & 88 deletions

‎.github/workflows/create-release.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ jobs:
275275
released="$(mktemp -d)"
276276
git show "${from}:${tiers}" > "${released}/python-versions.toml"
277277
git show "${from}:util/python_versions.py" > "${released}/python_versions.py"
278+
# #2047: the reader imports util/_actions.py from beside it, from the
279+
# commit that has it.
280+
if git cat-file -e "${from}:util/_actions.py" 2> /dev/null; then
281+
git show "${from}:util/_actions.py" > "${released}/_actions.py"
282+
fi
278283
wheels="$(python3 "${released}/python_versions.py" --file "${released}/python-versions.toml" pypi-wheels)"
279284
builds="$(python3 "${released}/python_versions.py" --file "${released}/python-versions.toml" conda-builds)"
280285
fi

‎.github/workflows/deploy-pages.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ jobs:
199199
# `deploy-pages` ran or `head`'s gate failed, so never on a pull request's
200200
# run or a verdict for another commit,
201201
# which skip `deploy-pages` deliberately. It reads no artifact, and checks
202-
# out the script alone. The job and its step are `continue-on-error`;
202+
# out the script and util/_actions.py, which it imports (#2047), alone. The
203+
# job and its step are `continue-on-error`;
203204
# util/discussion_report.py writes a warning rather than failing.
204205
discussion:
205206
name: Post the Pages report
@@ -218,6 +219,7 @@ jobs:
218219
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
219220
with:
220221
sparse-checkout: |
222+
util/_actions.py
221223
util/discussion_report.py
222224
sparse-checkout-cone-mode: false
223225
persist-credentials: false

‎.github/workflows/experimental.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
with:
5555
sparse-checkout: |
5656
.github/python-versions.toml
57+
util/_actions.py
5758
util/python_versions.py
5859
util/superseded.py
5960
sparse-checkout-cone-mode: false
@@ -100,6 +101,7 @@ jobs:
100101
with:
101102
sparse-checkout: |
102103
.github/python-versions.toml
104+
util/_actions.py
103105
util/python_versions.py
104106
sparse-checkout-cone-mode: false
105107
persist-credentials: false

‎.github/workflows/project-status.yml‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ name: Project Status
1212
# the classic "pwn request". The job below:
1313
#
1414
# * never checks out the repository, at any ref, and has no `uses:` step at all;
15-
# * fetches util/project_status.py through the API at `github.sha`, which under
15+
# * fetches util/project_status.py, and util/_actions.py, which it imports
16+
# (#2047), through the API at `github.sha`, which under
1617
# `pull_request_target` is the tip of the *base* branch, never the PR head --
17-
# so editing the script in a pull request changes nothing about its own run;
18+
# so editing either in a pull request changes nothing about its own run;
1819
# * takes nothing from the event payload but the item number, passed through
1920
# `env:` rather than interpolated into the script, and re-reads labels and
2021
# state from the API.
@@ -84,10 +85,13 @@ jobs:
8485
exit 0
8586
fi
8687
87-
# The base branch's copy of the script -- see the security note above.
88-
gh api -H 'Accept: application/vnd.github.raw' \
89-
"repos/${GITHUB_REPOSITORY}/contents/util/project_status.py?ref=${SCRIPT_REF}" \
90-
> "${RUNNER_TEMP}/project_status.py"
88+
# The base branch's copy of the script, and of util/_actions.py, which
89+
# it imports from beside it (#2047) -- see the security note above.
90+
for script in project_status.py _actions.py; do
91+
gh api -H 'Accept: application/vnd.github.raw' \
92+
"repos/${GITHUB_REPOSITORY}/contents/util/${script}?ref=${SCRIPT_REF}" \
93+
> "${RUNNER_TEMP}/${script}"
94+
done
9195
9296
if [ -n "$ITEM" ]; then
9397
python3 "${RUNNER_TEMP}/project_status.py" --item "$ITEM"

‎.github/workflows/python-compatibility.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
with:
3434
sparse-checkout: |
3535
.github/python-versions.toml
36+
util/_actions.py
3637
util/python_versions.py
3738
sparse-checkout-cone-mode: false
3839
persist-credentials: false

‎.github/workflows/unit-tests.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
with:
6868
sparse-checkout: |
6969
.github/python-versions.toml
70+
util/_actions.py
7071
util/python_versions.py
7172
sparse-checkout-cone-mode: false
7273
persist-credentials: false

‎docs/source/contributing/workflows.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,10 @@ release waits for the next Vendor Update on ``main``.
503503
that runs with a secret on an event a fork can cause: it writes the project board's
504504
*Status* field with ``PROJECT_TOKEN``, which a fork's ``pull_request`` run would not
505505
receive. It therefore never checks out the repository and has no ``uses:`` step. It
506-
fetches :file:`util/project_status.py` through the API at ``github.sha`` -- the base
507-
branch's tip under that event -- and takes only the item number from the payload.
506+
fetches :file:`util/project_status.py`, and :file:`util/_actions.py`, which it imports
507+
(`#2047 <https://github.com/JarryShaw/PyPCAPKit/issues/2047>`__), through the API at
508+
``github.sha`` -- the base branch's tip under that event -- and takes only the item
509+
number from the payload.
508510
``tests/project/test_project_status.py`` asserts this over the file. Without the
509511
secret it skips with a ``::notice::``. The Status mapping it applies is documented in
510512
:doc:`conventions/process`.
@@ -1369,7 +1371,8 @@ does while its findings stand, and ``failure`` where actionlint failed the job.
13691371
``cron-vendor.yml``'s rules: ``push`` on its ``workflow_run``, ``scheduled`` on its
13701372
``schedule``, and never on a pull request or a verdict for another commit. Its
13711373
report is the results of ``head``, ``deploy-pages`` and ``publish-pages``, so it
1372-
uploads no artifact, and its ``discussion`` job checks out the script alone. The
1374+
uploads no artifact, and its ``discussion`` job checks out the script and
1375+
:file:`util/_actions.py`, which it imports, alone. The
13731376
``--status`` is the publish's result after a green build, the build's where the
13741377
build did not pass, and the gate's where the gate skipped the build.
13751378

‎tests/project/test_actions_helper_2047.py‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
function's exact output, GitHub's escaping included.
99
* :class:`TestScriptsImportIt` loads each script that imports the helper in the
1010
ways the steps and the tests do, and runs it as the steps do.
11+
* :class:`TestTheStepsCheckItOut` holds every sparse checkout of such a script
12+
to listing the helper too.
1113
1214
"""
1315

@@ -29,10 +31,11 @@
2931
from typing import TYPE_CHECKING
3032
from unittest import mock
3133

34+
from tests._workflows import WORKFLOWS, load
3235
from util import _actions
3336

3437
if TYPE_CHECKING:
35-
from typing import Iterator, Optional
38+
from typing import Any, Iterator, Optional
3639

3740
#: Test tier (#1958): reads and runs the tree's own scripts, a question about the tree.
3841
__tier__ = 'LATEST'
@@ -41,9 +44,10 @@
4144
HELPER = ROOT / 'util' / '_actions.py'
4245
#: The scripts that import the helper.
4346
SCRIPTS = ('apt_packages', 'check_suite_report', 'ci_timing', 'classify_changes', 'codeql_report',
44-
'coverage_report', 'leg_summary', 'pip_install', 'regenerate_samples', 'release_status', 'release_tags',
45-
'release_upload', 'release_vendor_update', 'update_python_versions', 'vendor_code_changed',
46-
'vendor_test_gate')
47+
'coverage_report', 'discussion_report', 'leg_summary', 'pip_install', 'project_status',
48+
'python_versions', 'regenerate_samples', 'release_status', 'release_tags', 'release_upload',
49+
'release_vendor_update', 'superseded', 'update_python_versions', 'vendor_code_changed',
50+
'vendor_test_gate', 'verdict_covers_checkout')
4751
#: The top-level modules the helper may import, each in Python 3.7's standard
4852
#: library: the running interpreter's :data:`sys.stdlib_module_names` would
4953
#: pass ``tomllib``, which 3.7 lacks.
@@ -56,16 +60,21 @@
5660
'classify_changes': ([], {'OWN_PR': 'false'}),
5761
'codeql_report': (['--help'], {}),
5862
'coverage_report': (['--help'], {}),
63+
'discussion_report': (['--help'], {}),
5964
'leg_summary': (['ktlo'], {'LEG': 'Python 3.8 (KTLO, non-blocking)', 'STEPS': '{}'}),
6065
'pip_install': (['--help'], {}),
66+
'project_status': (['--help'], {}),
67+
'python_versions': (['latest'], {}),
6168
'regenerate_samples': (['--help'], {}),
6269
'release_status': (['--help'], {}),
6370
'release_tags': (['--help'], {}),
6471
'release_upload': (['--help'], {}),
6572
'release_vendor_update': (['--help'], {}),
73+
'superseded': (['--help'], {}),
6674
'update_python_versions': (['--help'], {}),
6775
'vendor_code_changed': (['--help'], {}),
6876
'vendor_test_gate': (['--help'], {}),
77+
'verdict_covers_checkout': (['--help'], {}),
6978
}
7079
#: ``-X importtime``'s line for a module, by its name.
7180
IMPORT_TIME = re.compile(r'^import time:.*\|\s*(\S+)\s*$', re.M)
@@ -330,5 +339,42 @@ def test_run_as_the_steps_run_it(self) -> None:
330339
self.assertEqual(output.read_text(encoding='utf-8'), 'code=true\n')
331340

332341

342+
def sparse_checkouts_missing_the_helper(spec: 'Any') -> 'list[tuple[str, list[str]]]':
343+
"""Each job of the workflow ``spec`` whose sparse checkout lists a script of :data:`SCRIPTS` but not the helper."""
344+
found = []
345+
for job_id, job in (spec.get('jobs') or {}).items():
346+
for step in job.get('steps') or []:
347+
listed = str((step.get('with') or {}).get('sparse-checkout') or '').split()
348+
scripts = [path for path in listed if path.startswith('util/') and path.endswith('.py')
349+
and path[len('util/'):-len('.py')] in SCRIPTS]
350+
if scripts and 'util/_actions.py' not in listed:
351+
found.append((job_id, scripts))
352+
return found
353+
354+
355+
class TestTheStepsCheckItOut(unittest.TestCase):
356+
"""A sparse checkout of a script that imports the helper holds the helper too, or the import fails."""
357+
358+
def test_every_sparse_checkout_of_a_script_lists_the_helper(self) -> None:
359+
checked = 0
360+
for path in sorted(WORKFLOWS.glob('*.yml')):
361+
with self.subTest(workflow=path.name):
362+
spec = load(path.name)
363+
self.assertEqual(sparse_checkouts_missing_the_helper(spec), [])
364+
checked += sum('sparse-checkout' in (step.get('with') or {}) for job in spec['jobs'].values()
365+
for step in job.get('steps') or [])
366+
self.assertGreater(checked, 0, 'no sparse checkout was found to check')
367+
368+
def test_the_check_sees_a_missing_helper(self) -> None:
369+
def spec(*listed: 'str') -> 'Any':
370+
return {'jobs': {'j': {'steps': [{'with': {'sparse-checkout': ''.join(f'{p}\n' for p in listed)}}]}}}
371+
372+
self.assertEqual(sparse_checkouts_missing_the_helper(spec('.github/python-versions.toml',
373+
'util/python_versions.py')),
374+
[('j', ['util/python_versions.py'])])
375+
self.assertEqual(sparse_checkouts_missing_the_helper(spec('util/_actions.py', 'util/python_versions.py')), [])
376+
self.assertEqual(sparse_checkouts_missing_the_helper(spec('util/no_helper_here.py')), [])
377+
378+
333379
if __name__ == '__main__':
334380
unittest.main()

‎tests/project/test_discussion_report_2030.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,66 @@ def test_a_bad_command_line_exits_2(self) -> None:
438438
self.assertEqual(self.calls(), [])
439439

440440

441+
class TestTheWorkflowCommands(_FakeGitHub):
442+
"""#2047: the whole of what the script prints and appends to the summary, byte for byte.
443+
444+
The tests above read the warning with ``assertIn``; these pin each run's whole
445+
standard output and summary file, with text each escape changes, so moving the
446+
writes onto util/_actions.py can be shown to change nothing.
447+
448+
"""
449+
450+
def test_a_post_prints_two_lines_and_appends_one(self) -> None:
451+
title = 'ci-timing: 50% [odd], title'
452+
self.summary.write_bytes(b'before\n')
453+
result = self.script(title=title)
454+
url = f'https://github.com/{REPO}/discussions/100'
455+
self.assertEqual((result.returncode, result.stdout, result.stderr),
456+
(0, f"Opened the thread {title!r}: {url}\nPosted the report: {url}#discussioncomment-1\n", ''))
457+
self.assertEqual(self.summary.read_bytes(),
458+
f'before\nThe report was posted to [{title}]({url}#discussioncomment-1).\n'.encode())
459+
# A second post finds the thread, and appends a second line.
460+
result = self.script(title=title)
461+
self.assertEqual(result.stdout, f'Posted the report: {url}#discussioncomment-2\n')
462+
self.assertEqual(self.summary.read_bytes().count(b'\n'), 3)
463+
464+
def test_no_summary_file_or_one_that_will_not_open(self) -> None:
465+
"""Unset or empty, nothing is written; a directory, the post still succeeds, silently."""
466+
url = f'https://github.com/{REPO}/discussions/100'
467+
for name, value in (('unset', None), ('empty', ''), ('a directory', str(self.tmp))):
468+
with self.subTest(summary=name):
469+
self.update(discussions=[])
470+
env = self.env()
471+
if value is None:
472+
del env['GITHUB_STEP_SUMMARY']
473+
else:
474+
env['GITHUB_STEP_SUMMARY'] = value
475+
result = self.script(env=env)
476+
self.assertEqual((result.returncode, result.stdout, result.stderr),
477+
(0, f"Opened the thread {TITLE!r}: {url}\nPosted the report: {url}"
478+
'#discussioncomment-1\n', ''))
479+
self.assertFalse(self.summary.exists())
480+
481+
def test_each_warning_is_one_escaped_line(self) -> None:
482+
"""``%`` and LF in the message escape; the title, which holds neither ``:`` nor ``,``, is as written.
483+
484+
The stand-in's standard error reaches the script through universal
485+
newlines, so a CR arrives as LF; the helper's own tests pin CR.
486+
487+
"""
488+
self.update(fail={'op': 'discussionCategories', 'message': 'a 50%\nb: c, d'})
489+
self.summary.write_bytes(b'before\n')
490+
result = self.script()
491+
self.assertEqual((result.returncode, result.stdout),
492+
(0, '::warning title=CI report not posted::gh api graphql exited 1: gh: a 50%25%0Ab: '
493+
'c, d. The report was not posted.\n'))
494+
self.assertEqual(self.summary.read_bytes(), b'before\n')
495+
missing = self.tmp / '50%.md'
496+
result = self.script(report=missing)
497+
self.assertEqual(result.stdout, f"::warning title=CI report not posted::the report '{self.tmp}/50%25.md' "
498+
'could not be read (No such file or directory), so nothing was posted.\n')
499+
500+
441501
#: ``(workflow, reporting job, the thread's title, the artifact, the file in it)``.
442502
POSTERS = (
443503
('ci-timing.yml', 'report', 'ci-timing: weekly CI timing report', 'ci-timing-report', 'report.md'),

‎tests/project/test_lint_pages_reports_2030.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ def test_lint_hands_its_report_on(self) -> None:
159159
self.assertEqual(download['with'], {'name': 'lint-report', 'path': 'report'})
160160
self.assertIs(download['continue-on-error'], True)
161161

162-
def test_pages_reads_no_artifact_and_checks_out_the_script_alone(self) -> None:
162+
def test_pages_reads_no_artifact_and_checks_out_the_script_and_its_helper(self) -> None:
163163
job = load(PAGES)['jobs']['discussion']
164164
uses = [entry['uses'] for entry in job['steps'] if 'uses' in entry]
165165
self.assertEqual(len(uses), 1)
166166
self.assertTrue(pinned(uses[0], 'actions/checkout'))
167-
self.assertEqual(job['steps'][0]['with'], {'sparse-checkout': 'util/discussion_report.py\n',
167+
# The script imports util/_actions.py (#2047).
168+
self.assertEqual(job['steps'][0]['with'], {'sparse-checkout': 'util/_actions.py\nutil/discussion_report.py\n',
168169
'sparse-checkout-cone-mode': False,
169170
'persist-credentials': False})
170171

0 commit comments

Comments
 (0)