Skip to content

Commit 08ade35

Browse files
committed
ci: post the CodeQL report to Discussions (#2030)
- codeql-analysis.yml: a `discussion` job after `analyze`, the only job holding `security-events: read` (beside `discussions: write` and `contents: read`); top-level `permissions: {}` stays. Scheduled runs post every run, push runs follow the push rule, pull requests never post. Title `codeql-analysis: CodeQL report`; status is `analyze`'s result; `REPORT_CATEGORY` env as the other workflows. - util/codeql_report.py: reads the open CodeQL code-scanning alerts (REST, paginated), writes the report by severity and the `count` output; a failed read warns and leaves `count` empty. - util/discussion_report.py: optional `--count N`, stored as a hidden `<!-- report-count: N -->` marker; a success whose count rose since the newest counted report posts as `warning`, and a push run whose count moved posts. Without `--count` nothing changes. - tests/project/test_codeql_report_2030.py pins the job and both paths; test_actions_helper_2047 lists the new script; workflows.rst documents it. Related project tests pass; actionlint 1.7.12 is clean.
1 parent 0ab1e95 commit 08ade35

6 files changed

Lines changed: 744 additions & 25 deletions

File tree

‎.github/workflows/codeql-analysis.yml‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
schedule:
99
- cron: '0 2 * * 6'
1010

11-
# #1968: no scopes by default. `analyze` declares the ones it needs.
11+
# #1968: no scopes by default. Each job declares the ones it needs.
1212
permissions: {}
1313

1414
# Only pull-request runs may be superseded (see unit-tests.yml's
@@ -18,6 +18,11 @@ concurrency:
1818
group: codeql-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
1919
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
2020

21+
# #2030: one Discussions category holds every report with no pull request to
22+
# comment on, one thread per job.
23+
env:
24+
REPORT_CATEGORY: CI reports
25+
2126
jobs:
2227
# Not gated on Unit Tests: CodeQL reads the source, so its findings do not
2328
# depend on the tests passing, and a failing test would suppress the scan.
@@ -41,3 +46,55 @@ jobs:
4146

4247
- name: Perform CodeQL Analysis
4348
uses: github/codeql-action/analyze@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
49+
50+
# #2030: the run's report, to its thread in Discussions. A pull request's run
51+
# posts nothing: its findings belong to the pull request, and the alerts the
52+
# report lists are the default branch's. The only job holding
53+
# `security-events: read` or `discussions: write`, and it runs no project
54+
# code but the two util/ scripts.
55+
discussion:
56+
name: Post the CodeQL report
57+
needs: [ analyze ]
58+
if: ${{ always() && github.event_name != 'pull_request' && needs.analyze.result != 'skipped' }}
59+
runs-on: ubuntu-latest
60+
timeout-minutes: 10
61+
continue-on-error: true
62+
permissions:
63+
contents: read
64+
discussions: write
65+
security-events: read
66+
67+
steps:
68+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
69+
with:
70+
persist-credentials: false
71+
72+
# The default branch's open alerts, by severity, after `Perform CodeQL
73+
# Analysis` waited for its upload to be processed; their number is `count`.
74+
- name: Write the report
75+
id: report
76+
continue-on-error: true
77+
env:
78+
GH_TOKEN: ${{ github.token }}
79+
ANALYZE_RESULT: ${{ needs.analyze.result }}
80+
run: python3 util/codeql_report.py --result "$ANALYZE_RESULT" --report report/codeql-report.md
81+
82+
# The status is `analyze`'s result; the script makes a `success` whose
83+
# alert count rose since the last counted report a `warning`. The schedule
84+
# posts every run; a push posts as a push run, and also whenever the count
85+
# moved, so the last report always holds the last count read.
86+
- name: Post the report to Discussions
87+
continue-on-error: true
88+
env:
89+
GH_TOKEN: ${{ github.token }}
90+
MODE: ${{ github.event_name == 'schedule' && 'scheduled' || 'push' }}
91+
ANALYZE_RESULT: ${{ needs.analyze.result }}
92+
ALERTS: ${{ steps.report.outputs.count }}
93+
run: |
94+
counted=()
95+
if [ -n "$ALERTS" ]; then
96+
counted=(--count "$ALERTS")
97+
fi
98+
python3 util/discussion_report.py --category "$REPORT_CATEGORY" \
99+
--title 'codeql-analysis: CodeQL report' --report report/codeql-report.md \
100+
--mode "$MODE" --status "$ANALYZE_RESULT" "${counted[@]}"

‎docs/source/contributing/workflows.rst‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,9 @@ does; each has tests under :file:`tests/project/` that run it.
12331233
- No step: it is imported by the scripts that share its annotations, step
12341234
outputs, job summary lines and logged runs (`#2047
12351235
<https://github.com/JarryShaw/PyPCAPKit/issues/2047>`__).
1236+
* - :file:`util/codeql_report.py`
1237+
- ``Write the report`` in ``codeql-analysis.yml``'s ``discussion`` (see `CI
1238+
Reports`_).
12361239

12371240
The summaries run on the image's ``/usr/bin/python3`` rather than the leg's
12381241
interpreter, which may have failed to set up or be too broken to run anything, so
@@ -1352,6 +1355,23 @@ run it links with ``GITHUB_WORKFLOW``, so the post step sets ``Unit Tests`` for
13521355
that one command. A push relay's concurrency group is its own run's, since a
13531356
group cancels a pending run when a newer one joins it.
13541357

1358+
``codeql-analysis.yml`` posts to ``codeql-analysis: CodeQL report``, as
1359+
``scheduled`` on its ``schedule`` and ``push`` on a push to ``main``, and never on
1360+
a pull request, whose findings belong to the pull request. Its ``discussion`` job
1361+
runs after ``analyze`` and alone holds ``security-events: read``, beside
1362+
``discussions: write`` and ``contents: read``. ``Write the report`` runs
1363+
:file:`util/codeql_report.py`, which lists the default branch's open CodeQL alerts
1364+
by severity, read through GitHub's `List code scanning alerts for a repository
1365+
<https://docs.github.com/en/rest/code-scanning/code-scanning#list-code-scanning-alerts-for-a-repository>`__
1366+
endpoint, and hands their number on as the step output ``count``. The ``--status``
1367+
is ``analyze``'s result, and ``Post the report to Discussions`` passes the count
1368+
as ``--count``. The comment then carries it in a hidden ``<!-- report-count: N
1369+
-->`` marker; a ``success`` whose count rose since the newest counted report posts
1370+
as ``warning``, and a push run whose count moved posts whatever its status, so the
1371+
newest counted report always holds the last count read and an unposted drop cannot
1372+
hide a later rise. Where the alerts cannot be read, the report says so and passes
1373+
no count, so nothing is compared.
1374+
13551375
.. _Action Pins:
13561376

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

‎tests/project/test_actions_helper_2047.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
ROOT = pathlib.Path(__file__).resolve().parents[2]
4141
HELPER = ROOT / 'util' / '_actions.py'
4242
#: The scripts that import the helper.
43-
SCRIPTS = ('apt_packages', 'classify_changes', 'coverage_report', 'leg_summary', 'pip_install',
43+
SCRIPTS = ('apt_packages', 'classify_changes', 'codeql_report', 'coverage_report', 'leg_summary', 'pip_install',
4444
'regenerate_samples', 'vendor_code_changed')
4545
#: The top-level modules the helper may import, each in Python 3.7's standard
4646
#: library: the running interpreter's :data:`sys.stdlib_module_names` would
@@ -50,6 +50,7 @@
5050
RUNS = {
5151
'apt_packages': (['--help'], {}),
5252
'classify_changes': ([], {'OWN_PR': 'false'}),
53+
'codeql_report': (['--help'], {}),
5354
'coverage_report': (['--help'], {}),
5455
'leg_summary': (['ktlo'], {'LEG': 'Python 3.8 (KTLO, non-blocking)', 'STEPS': '{}'}),
5556
'pip_install': (['--help'], {}),

0 commit comments

Comments
 (0)