Skip to content

Commit 2166b7c

Browse files
committed
ci: post CI reports to a Discussions thread per job (#2030)
- util/discussion_report.py: finds the category and the job's thread by name through `gh api graphql` and opens the thread on first use. `--mode scheduled` posts every run; `--mode push` posts a run that warned or failed, and a green run only after a non-green one, read from a hidden report-status marker. Every failure is one warning and exit 0. - ci-timing.yml, cron-vendor.yml: a `discussion` job, alone holding `discussions: write`, downloads the reporting job's artifact and posts it to the `CI reports` category (workflow-level REPORT_CATEGORY). ci-timing is `scheduled`; cron-vendor is `push` on `workflow_run`. vendor-update writes and uploads a run report and a `warning` status; other jobs keep their permissions. - tests/project/test_discussion_report_2030.py: the script against a stand-in gh, both modes, the workflow pins, and the vendor report step; test_ci_timing.py's permission and job pins follow. - workflows.rst: the script joins Workflow Scripts; CI Reports section.
1 parent 8d9b69e commit 2166b7c

6 files changed

Lines changed: 1190 additions & 8 deletions

File tree

‎.github/workflows/ci-timing.yml‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ name: CI Timing
33
# The periodic CI-duration review (#1538). util/ci_timing.py reads the last
44
# week of runs through the API, writes a report to the job summary and an
55
# artifact, and fails when a HARD threshold fires (status 1; 2 and 3 are an
6-
# unreadable API and a crash). GitHub emails a scheduled workflow's failures to
7-
# whoever last edited its cron line, so no `issues: write` is needed. The
8-
# thresholds are in docs/source/contributing/workflows.rst, the response to one
9-
# in conventions/process.rst.
6+
# unreadable API and a crash), which GitHub emails to whoever last edited the
7+
# cron line. The `discussion` job then posts the report to this job's thread in
8+
# the Discussions category REPORT_CATEGORY names (#2030); it alone holds a write
9+
# scope, and a report it cannot post is a warning. The thresholds are in
10+
# docs/source/contributing/workflows.rst, the response to one in
11+
# conventions/process.rst.
1012
#
1113
# Not a `workflow_run` on Unit Tests: that adds a job per run to a runner pool
1214
# already measured saturated.
@@ -36,6 +38,11 @@ concurrency:
3638
group: ci-timing
3739
cancel-in-progress: false
3840

41+
# #2030: one Discussions category holds every report with no pull request to
42+
# comment on, one thread per job.
43+
env:
44+
REPORT_CATEGORY: CI reports
45+
3946
jobs:
4047
report:
4148
name: CI timing report
@@ -117,3 +124,42 @@ jobs:
117124
report.json
118125
report.md
119126
retention-days: 90
127+
128+
# #2030: posts the report the `report` job uploaded, on `always()` like that
129+
# job's own upload, so a run failed by a HARD threshold or stopped by a cap
130+
# is posted too. util/discussion_report.py writes a warning rather than
131+
# failing; `continue-on-error` covers the rest.
132+
discussion:
133+
name: Post the CI timing report
134+
needs: [ report ]
135+
if: ${{ always() && needs.report.result != 'skipped' }}
136+
runs-on: ubuntu-latest
137+
timeout-minutes: 10
138+
continue-on-error: true
139+
permissions:
140+
contents: read
141+
discussions: write
142+
143+
steps:
144+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
145+
with:
146+
persist-credentials: false
147+
148+
# A run that never wrote a report uploaded none; the script then warns.
149+
- name: Download the report
150+
continue-on-error: true
151+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
152+
with:
153+
name: ci-timing-report
154+
path: report
155+
156+
- name: Post the report to Discussions
157+
continue-on-error: true
158+
env:
159+
GH_TOKEN: ${{ github.token }}
160+
STATUS: ${{ needs.report.result }}
161+
# `scheduled` on dispatch too: a manual run is a deliberate report.
162+
run: |
163+
python3 util/discussion_report.py --category "$REPORT_CATEGORY" \
164+
--title 'ci-timing: weekly CI timing report' --report report/report.md \
165+
--mode scheduled --status "$STATUS"

‎.github/workflows/cron-vendor.yml‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ concurrency:
3232
group: vendor-update-${{ github.event.workflow_run.head_sha || github.ref }}
3333
cancel-in-progress: false
3434

35+
# #2030: one Discussions category holds every report with no pull request to
36+
# comment on, one thread per job.
37+
env:
38+
REPORT_CATEGORY: CI reports
39+
3540
jobs:
3641
# #1967: the test gate on the schedule, `verdict` on a `workflow_run`;
3742
# shared with Conda Update and GitHub Pages.
@@ -61,6 +66,9 @@ jobs:
6166
github.event.workflow_run.head_branch == 'main' &&
6267
needs.head.outputs.verified == 'true'
6368
))
69+
# #2030: `warning` where the job passed over a failed target or test gate.
70+
outputs:
71+
status: ${{ steps.run-report.outputs.status }}
6472
concurrency:
6573
group: repository-maintenance
6674
cancel-in-progress: false
@@ -106,6 +114,7 @@ jobs:
106114
python -m pip install -e '.[all,vendor]'
107115
108116
- name: Update Vendor
117+
id: crawl
109118
run: |
110119
set -x
111120
@@ -119,6 +128,7 @@ jobs:
119128
pcapkit-vendor 2> "$RUNNER_TEMP/vendor-stderr.log"
120129
vendor_exit=$?
121130
set -e
131+
echo "status=$vendor_exit" >> "$GITHUB_OUTPUT"
122132
123133
# Replayed whatever the exit: each failing target's name and
124134
# exception, plus its VendorRuntimeWarning. `|| true` so a log that
@@ -306,3 +316,103 @@ jobs:
306316
git remote set-url origin git@github.com:JarryShaw/PyPCAPKit.git
307317
git pull --rebase origin "${GITHUB_REF#refs/heads/}"
308318
git push origin "HEAD:${GITHUB_REF}"
319+
320+
# #2030: the run's outcome for the `discussion` job to post, read from the
321+
# crawl's log and the steps above, and `status=warning` where a target or
322+
# the test gate failed without failing the job. `continue-on-error` on
323+
# both, since a failure here would fail the run Create Release reads.
324+
- name: Write the run report
325+
id: run-report
326+
if: ${{ !cancelled() }}
327+
continue-on-error: true
328+
env:
329+
CRAWL_STATUS: ${{ steps.crawl.outputs.status }}
330+
TESTS: ${{ steps.vendor-tests.outcome }}
331+
FILES_CHANGED: ${{ steps.verify-changed-files.outputs.files_changed }}
332+
COMMITTED: ${{ steps.commit.outcome == 'success' || steps.commit-docs.outputs.committed == 'true' }}
333+
run: |
334+
if [ "$CRAWL_STATUS" = 0 ] && [ "$TESTS" = success ]; then
335+
echo "status=success" >> "$GITHUB_OUTPUT"
336+
else
337+
echo "status=warning" >> "$GITHUB_OUTPUT"
338+
fi
339+
{
340+
echo '### Vendor Update'
341+
echo
342+
if [ -z "$CRAWL_STATUS" ]; then
343+
echo '- Crawl: did not finish'
344+
elif [ "$CRAWL_STATUS" = 0 ]; then
345+
echo '- Crawl: every target succeeded'
346+
else
347+
echo "- Crawl: pcapkit-vendor exited $CRAWL_STATUS; these targets failed and kept their modules:"
348+
echo
349+
echo '```'
350+
grep -E '^[A-Za-z_][A-Za-z0-9_.]* failed: ' "$RUNNER_TEMP/vendor-stderr.log" ||
351+
echo '(no target name could be read from stderr)'
352+
echo '```'
353+
echo
354+
fi
355+
echo "- Test gate: ${TESTS:-not run}"
356+
if [ "$COMMITTED" = true ]; then
357+
echo "- Committed: $(git log -1 --format=%s)"
358+
elif [ "$FILES_CHANGED" = true ]; then
359+
echo '- Committed: nothing, though the crawl changed files'
360+
elif [ "$FILES_CHANGED" = false ]; then
361+
echo '- Committed: nothing, as nothing changed'
362+
else
363+
echo '- Committed: nothing, as the job stopped first'
364+
fi
365+
} > "$RUNNER_TEMP/vendor-report.md"
366+
cat "$RUNNER_TEMP/vendor-report.md"
367+
368+
- name: Upload the run report
369+
if: ${{ !cancelled() }}
370+
continue-on-error: true
371+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
372+
with:
373+
name: vendor-report
374+
path: ${{ runner.temp }}/vendor-report.md
375+
retention-days: 14
376+
377+
# #2030: posts `vendor-update`'s report, and skips with it, so a run whose
378+
# every job skipped still concludes `skipped` for Create Release. Job-level
379+
# `continue-on-error` as well as the steps', so nothing here can fail the run
380+
# Create Release reads; util/discussion_report.py writes a warning instead.
381+
discussion:
382+
name: Post the vendor report
383+
needs: [ vendor-update ]
384+
if: ${{ !cancelled() && needs.vendor-update.result != 'skipped' }}
385+
runs-on: ubuntu-latest
386+
timeout-minutes: 10
387+
continue-on-error: true
388+
permissions:
389+
contents: read
390+
discussions: write
391+
steps:
392+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
393+
with:
394+
persist-credentials: false
395+
396+
# A run that never wrote a report uploaded none; the script then warns.
397+
- name: Download the report
398+
continue-on-error: true
399+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
400+
with:
401+
name: vendor-report
402+
path: report
403+
404+
# A `workflow_run` follows a push to main, so it posts as a push run: only
405+
# when it warned or failed, and once more on the next green run. The
406+
# schedule posts every run. A green job that warned reports `warning`.
407+
- name: Post the report to Discussions
408+
continue-on-error: true
409+
env:
410+
GH_TOKEN: ${{ github.token }}
411+
MODE: ${{ github.event_name == 'workflow_run' && 'push' || 'scheduled' }}
412+
STATUS: >-
413+
${{ needs.vendor-update.result == 'success' && needs.vendor-update.outputs.status ||
414+
needs.vendor-update.result }}
415+
run: |
416+
python3 util/discussion_report.py --category "$REPORT_CATEGORY" \
417+
--title 'cron-vendor: vendor constant update report' --report report/vendor-report.md \
418+
--mode "$MODE" --status "$STATUS"

‎docs/source/contributing/workflows.rst‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ cancelled at 95% of its cap or later timed out and counts, even in a run that re
557557
``cancelled`` for that reason; one cancelled earlier was superseded and does not. A
558558
step cap written as an expression differs by leg and reads as none.
559559
The report goes to the job summary and to the ``ci-timing-report`` artifact (90
560-
days), and the newest earlier report is the next run's baseline. It is not a
560+
days), and the newest earlier report is the next run's baseline. The
561+
``discussion`` job then posts it to the ``ci-timing: weekly CI timing report``
562+
thread (see `CI Reports`_). It is not a
561563
``workflow_run`` on Unit Tests, which would add a job per run to a runner pool
562564
measured saturated at 20 concurrent jobs.
563565

@@ -1204,6 +1206,9 @@ does; each has tests under :file:`tests/project/` that run it.
12041206
(`#1963 <https://github.com/JarryShaw/PyPCAPKit/issues/1963>`__), and in
12051207
``unit-tests.yml``'s ``test`` and ``test-ktlo``, on PyPy's legs (`#2005
12061208
<https://github.com/JarryShaw/PyPCAPKit/issues/2005>`__).
1209+
* - :file:`util/discussion_report.py`
1210+
- ``Post the report to Discussions`` in the ``discussion`` job of
1211+
``ci-timing.yml`` and of ``cron-vendor.yml`` (see `CI Reports`_).
12071212

12081213
The summaries run on the image's ``/usr/bin/python3`` rather than the leg's
12091214
interpreter, which may have failed to set up or be too broken to run anything, so
@@ -1216,6 +1221,43 @@ tree. ``create-release.yml``'s ``pypi`` job keeps its own copy of
12161221
the KTLO summary's shell until its shell moves to scripts too; a project pin runs
12171222
that copy and the script on the same steps and requires the same output.
12181223

1224+
.. _CI Reports:
1225+
1226+
CI Reports (`#2030 <https://github.com/JarryShaw/PyPCAPKit/issues/2030>`__)
1227+
---------------------------------------------------------------------------
1228+
1229+
A report with no pull request to comment on goes to GitHub Discussions. One
1230+
category, ``CI reports``, holds every report, and each job has one thread in it,
1231+
titled ``<workflow file stem>: <job or report name>``. A run adds a comment to its
1232+
job's thread rather than opening a new one; the comment links the run, names its
1233+
commit and its status, and ends in a hidden ``<!-- report-status: ... -->``
1234+
marker. How often depends on the script's ``--mode``:
1235+
1236+
* ``scheduled``: every run posts, on ``schedule`` and on ``workflow_dispatch``,
1237+
since a manual run is a deliberate report.
1238+
* ``push``, for a run on a push or a ``workflow_run`` relay of one: a run that
1239+
warned or failed posts every time, and a green run posts only where the
1240+
thread's newest marker is not ``success``, so one "back to green" comment
1241+
follows a bad run and later green runs stay silent. A thread with no marker
1242+
counts as green.
1243+
1244+
``ci-timing.yml`` posts as ``scheduled`` to ``ci-timing: weekly CI timing
1245+
report``. ``cron-vendor.yml`` posts to ``cron-vendor: vendor constant update
1246+
report``, as ``push`` on its ``workflow_run`` and ``scheduled`` otherwise, and
1247+
reports ``warning`` where ``vendor-update`` passed over a failed target or test
1248+
gate. The category is the workflow-level ``REPORT_CATEGORY``. The API cannot
1249+
create one, so it is made by hand, in Settings, Discussions.
1250+
1251+
The reporting job uploads its report as an artifact, and a ``discussion`` job of
1252+
its own downloads it and runs :file:`util/discussion_report.py`, which finds the
1253+
category and the thread by name and opens the thread on first use. That job alone
1254+
holds ``discussions: write``, beside ``contents: read`` for its checkout, so every
1255+
other job keeps its permissions. It uses ``GITHUB_TOKEN`` and first-party actions
1256+
only. Reporting never fails a run: a missing category, a missing report or a
1257+
failed API call is one warning, and the job is ``continue-on-error``, since a
1258+
failed Vendor Update run is one Create Release reads as not updated. A CI timing
1259+
run failed by a HARD threshold is posted all the same, on ``always()``.
1260+
12191261
.. _Action Pins:
12201262

12211263
Action Pins (`#1968 <https://github.com/JarryShaw/PyPCAPKit/issues/1968>`__)

‎tests/project/test_ci_timing.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ def test_the_reusable_workflow_asks_for_no_write_permission(self) -> 'None':
11631163

11641164

11651165
class TestReviewWorkflow(unittest.TestCase):
1166-
""":file:`ci-timing.yml` stays read-only, weekly, and off Unit Tests' runs."""
1166+
""":file:`ci-timing.yml` stays read-only but for posting its report (#2030), weekly, and off Unit Tests' runs."""
11671167

11681168
def setUp(self) -> 'None':
11691169
self.text = REVIEW.read_text(encoding='utf-8')
@@ -1177,13 +1177,15 @@ def test_triggers(self) -> 'None':
11771177
self.assertNotRegex(self.code, rf'(?m)^ {trigger}:')
11781178

11791179
def test_permissions_are_read_only(self) -> 'None':
1180+
"""But for the ``discussion`` job's ``discussions: write``, pinned in test_discussion_report_2030."""
11801181
block = re.search(r'(?ms)^permissions:\n(.*?)(?=^\S)', self.code).group(1)
11811182
self.assertEqual(sorted(block.split()), ['actions:', 'contents:', 'read', 'read'])
1182-
self.assertNotIn('write', self.code)
1183+
self.assertEqual(re.findall(r'(?m)^\s+([\w-]+):\s*write\s*$', self.code), ['discussions'])
11831184

11841185
def test_the_job(self) -> 'None':
11851186
specs = ci_timing.parse_workflow(self.text)
1186-
self.assertEqual([(spec['name'], spec['timeout']) for spec in specs], [('CI timing report', 40)])
1187+
self.assertEqual([(spec['name'], spec['timeout']) for spec in specs],
1188+
[('CI timing report', 40), ('Post the CI timing report', 10)])
11871189
self.assertIn('python util/ci_timing.py', self.code)
11881190
# The upload and the baseline download name the same artifact.
11891191
self.assertIn('name: ci-timing-report', self.code)

0 commit comments

Comments
 (0)