Skip to content

Commit 810faf6

Browse files
greenc-FNALgoogle-labs-jules[bot]knoepfel
authored
Make CodeQL PR comments actually useful (#117)
* Useful utility scripts for dealing with CodeQL alerts and report files * Refactor CodeQL workflow to fix PR commenting * Refactor CodeQL debug log handling for clarity and reliability * Refine CodeQL script logic and PR comment content --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Kyle Knoepfel <knoepfel@fnal.gov>
1 parent 17a6721 commit 810faf6

File tree

4 files changed

+940
-53
lines changed

4 files changed

+940
-53
lines changed

.github/workflows/codeql-analysis.yaml

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ jobs:
8989
permissions:
9090
contents: read
9191
issues: write
92+
pull-requests: write
93+
security-events: read
9294
env:
9395
CODEQL_MIN_LEVEL: warning
96+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9497
steps:
98+
- name: Set log file path
99+
id: set_log_path
100+
run: echo "path=$RUNNER_TEMP/codeql-alerts.log" >> "$GITHUB_OUTPUT"
95101
- name: Checkout repository
96102
uses: actions/checkout@v4
97103
with:
@@ -108,13 +114,49 @@ jobs:
108114
id: check_codeql
109115
run: |
110116
set -euo pipefail
111-
python3 scripts/check_codeql_alerts.py \
112-
--sarif "$GITHUB_WORKSPACE/sarif" \
117+
ARGS=(
118+
--sarif "$GITHUB_WORKSPACE/sarif"
113119
--min-level "${CODEQL_MIN_LEVEL}"
120+
--log-path "${{ steps.set_log_path.outputs.path }}"
121+
)
122+
if [ "${{ github.event_name }}" = "pull_request" ]; then
123+
ARGS+=(--ref "refs/pull/${{ github.event.pull_request.number }}/merge")
124+
fi
125+
python3 scripts/check_codeql_alerts.py "${ARGS[@]}"
126+
127+
- name: Upload CodeQL alerts debug log
128+
if: always()
129+
uses: actions/upload-artifact@v4
130+
with:
131+
name: codeql-alerts-debug-log
132+
path: ${{ steps.set_log_path.outputs.path }}
133+
retention-days: 3
134+
135+
- name: "Debug: PR head and repo info (no-op for non-PR runs)"
136+
if: github.event_name == 'pull_request'
137+
run: |
138+
echo "github.repository: ${{ github.repository }}"
139+
echo "pr.head.repo.full_name: ${{ github.event.pull_request.head.repo.full_name }}"
140+
echo "pr.head.repo.owner.login: ${{ github.event.pull_request.head.repo.owner.login }}"
141+
echo "pr.base.repo.full_name: ${{ github.event.pull_request.base.repo.full_name }}"
142+
echo "actor: ${{ github.actor }}"
143+
echo "Available step outputs from check_codeql:"
144+
echo " new_alerts=${{ steps.check_codeql.outputs.new_alerts }}"
145+
echo " fixed_alerts=${{ steps.check_codeql.outputs.fixed_alerts }}"
146+
echo "Event payload head/type (first 200 chars):"
147+
if [ -n "$GITHUB_EVENT_PATH" ]; then
148+
jq -c . < "$GITHUB_EVENT_PATH" | cut -c-200 || true
149+
fi
114150
115151
- name: Comment on PR with CodeQL alert changes
152+
# Only attempt to post comments on pull requests that originate from
153+
# the same repository. GitHub's `GITHUB_TOKEN` cannot create comments
154+
# on pull requests originating from forks (Resource not accessible
155+
# by integration). For forked PRs, maintainers can inspect the
156+
# uploaded log/artifact instead.
116157
if: >-
117158
github.event_name == 'pull_request' &&
159+
github.event.pull_request.head.repo.full_name == github.repository &&
118160
(steps.check_codeql.outputs.new_alerts == 'true' ||
119161
steps.check_codeql.outputs.fixed_alerts == 'true')
120162
uses: actions/github-script@v7
@@ -159,7 +201,13 @@ jobs:
159201
}
160202
161203
- name: Fail workflow due to new CodeQL alerts
162-
if: github.event_name == 'pull_request' && steps.check_codeql.outputs.new_alerts == 'true'
204+
# Only fail the job for PRs from the same repository where the
205+
# action has permission to comment / act. Forked PR runs cannot
206+
# reliably perform repo-write actions with `GITHUB_TOKEN`.
207+
if: >-
208+
github.event_name == 'pull_request' &&
209+
github.event.pull_request.head.repo.full_name == github.repository &&
210+
steps.check_codeql.outputs.new_alerts == 'true'
163211
run: |
164212
echo "New CodeQL alerts detected; failing job."
165213
exit 1

0 commit comments

Comments
 (0)