Skip to content

Conversation

@greenc-FNAL
Copy link
Contributor

  • Useful utility scripts for dealing with CodeQL alerts and report files
  • PR comments now actually report something useful

@greenc-FNAL greenc-FNAL requested a review from knoepfel November 14, 2025 17:26
@github-actions
Copy link
Contributor

github-actions bot commented Nov 14, 2025

Review the full CodeQL report for details.

Copy link
Member

@knoepfel knoepfel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greenc-FNAL, thanks for the PR. I have several questions/comments. I'm not sure that the resolution of any of them should hold up this PR.

It's unclear to me what level of rigor we should apply to code that has been generated by AI. I think we should at least be able to vet what the code is doing, but that can be difficult, and this PR is not an exception to that.

@greenc-FNAL
Copy link
Contributor Author

@jules please resolve the following error with the CodeQL CI check:

Run set -euo pipefail
  set -euo pipefail
  ARGS=(
    --sarif "$GITHUB_WORKSPACE/sarif"
    --min-level "${CODEQL_MIN_LEVEL}"
    --log-path "/home/runner/work/_temp/codeql-alerts.log"
  )
  if [ "pull_request" = "pull_request" ]; then
    ARGS+=(--ref "refs/pull/117/merge")
  fi
  python3 scripts/check_codeql_alerts.py "${ARGS[@]}"
  shell: /usr/bin/bash -e {0}
  env:
    BUILD_TYPE: RelWithDebInfo
    CPP_COMPILER: g++
    CODEQL_EXTRACTOR_CPP_COMPILATION_DATABASE: /home/runner/work/phlex/phlex/phlex-build/compile_commands.json
    CODEQL_MIN_LEVEL: warning
    GITHUB_TOKEN: ***
Traceback (most recent call last):
  File "/home/runner/work/phlex/phlex/scripts/check_codeql_alerts.py", line 19, in <module>
    class APIAlertComparison:
  File "/home/runner/work/phlex/phlex/scripts/check_codeql_alerts.py", line 22, in APIAlertComparison
    new_alerts: list[Alert]
                     ^^^^^
NameError: name 'Alert' is not defined
Error: Process completed with exit code 1.

The relevant agent task is https://jules.google.com/session/2491371005401655318/code/scripts/check_codeql_alerts.py

greenc-FNAL and others added 10 commits November 19, 2025 11:50
- Provide token where needed

- Fix CI workflow errors

- Fix AI thinko

- Work around comment permissions issue

- Getting into the weeds

- Refactor CodeQL workflow to fix PR commenting

  This change addresses the failure of the CodeQL workflow to comment on pull requests. The previous implementation was overly complex, with two separate workflows attempting to handle analysis and commenting, leading to permission errors.

  - Deleted `.github/workflows/codeql-comment.yml`, a redundant workflow that was created as a workaround.
  - Updated `.github/workflows/codeql-analysis.yaml` to grant it `pull-requests: write` permissions, allowing it to post comments directly on PRs.
  - Removed the unnecessary artifact upload steps from the main workflow, as the comment is now posted directly.

  This consolidates all CodeQL logic into a single, streamlined workflow that now has the correct permissions to function as intended.

- Refactor CodeQL debug log handling for clarity and reliability

  This change streamlines the handling of the debug log in the CodeQL workflow.

  - The `check_codeql_alerts.py` script now accepts a `--log-path` argument, removing the hardcoded log file path.
  - The `codeql-analysis.yaml` workflow now defines the log path in a single environment variable and passes it to the script.
  - The redundant "fallback" log upload step has been removed, and the primary upload step has been renamed for clarity.

  This makes the debug log handling more robust, maintainable, and easier to understand.

- Refine CodeQL script logic and PR comment content

  - Refactor CodeQL script for clarity and maintainability:

    - Consolidated duplicated summary-printing logic into a single `_print_summary` function.
    - This improves maintainability and ensures consistent output.

- Refine PR comment to focus on PR-specific changes:

  - Removed the comparison against the repository's 'main' branch from the PR comment.
  - The comment now only shows alerts that are new or fixed relative to the PR's branch point and its previous commit.
  - This makes the comment more concise and directly relevant to the changes in the pull request.

- Fix CodeQL workflow validation error

  This change corrects a workflow syntax error caused by referencing the `runner` context in a job-level `env` block.

  - The invalid `CODEQL_ALERTS_LOG_PATH` environment variable has been removed from the `codeql-report` job.
  - A new initial step, "Set log file path," has been added to the `codeql-report` job. This step correctly defines the log file path using `$RUNNER_TEMP` and makes it available to subsequent steps via a step output.
  - The script invocation and artifact upload steps have been updated to reference this new step output.

  This resolves the "Unrecognized named-value: 'runner'" error and ensures the workflow is valid.
This change adjusts the format of the CodeQL PR comment to be more concise and directly relevant to the pull request's lifecycle.

- The "Alert Matching Summary," which compared the PR to the `main` branch, has been replaced with a more focused summary.
- The new summary highlights the number of new and fixed alerts relative to the PR's **branch point** and its **previous commit**.
- This provides developers with a clearer, more actionable summary of how the PR has impacted the CodeQL alerts within its own context.
- The detailed lists of new and fixed alerts for these comparisons are preserved.
Co-authored-by: Kyle Knoepfel <knoepfel@fnal.gov>
- Replace the `in locals()` check for variable existence with a more explicit pattern of initializing variables to `None` and then checking `is not None`.
Refactor the `check_codeql_alerts.py` script to improve its structure and remove all uses of `locals()`.

- The large `main()` function has been broken down into smaller, more focused helper functions (`_compare_alerts_via_api` and `_build_multi_section_comment`).
- A new `APIAlertComparison` dataclass is used to provide a clear data structure for API comparison results.
- This refactoring eliminates the need for `locals()` by ensuring variables are properly initialized and passed between functions.
Refactor the `check_codeql_alerts.py` script to improve its structure and remove all uses of `locals()`.

- The large `main()` function has been broken down into smaller, more focused helper functions (`_compare_alerts_via_api` and `_build_multi_section_comment`).
- A new `APIAlertComparison` dataclass is used to provide a clear data structure for API comparison results.
- This refactoring eliminates the need for `locals()` by ensuring variables are properly initialized and passed between functions.

Fixes the following bugs introduced during the refactoring:
- A `NameError` caused by incorrect class definition order.
- A regression where matched alerts were no longer reported in the summary output.
- A regression in the logic for when to post a PR comment.
@greenc-FNAL greenc-FNAL force-pushed the maintenance/codeql-fix-pr-comment branch from e46e6ca to 0bc5cad Compare November 19, 2025 17:50
@knoepfel knoepfel self-requested a review November 19, 2025 18:17
@knoepfel knoepfel merged commit 810faf6 into main Nov 19, 2025
20 checks passed
@greenc-FNAL greenc-FNAL deleted the maintenance/codeql-fix-pr-comment branch November 19, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants