CLP-179 Add ruling-diff-comment GitHub Action for cross-analyzer use#31
Conversation
1d1b285 to
761a922
Compare
Changed ruling-diff-comment reference from @romain/ruling-diff-action to @romain/ruling-sync-PR to match the branch where both actions live. This keeps PR #31 focused on just ruling-diff-comment, while this branch includes both ruling-diff-comment and ruling-update-and-notify. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| def load_text_with_workspace_fallback(path: str, ref: str) -> str | None: | ||
| workspace_content = load_workspace_text(path) | ||
| if workspace_content is None: | ||
| logging.warning("Source file '%s' not found at %s", path, ref) | ||
| return None | ||
| logging.warning("Source file '%s' not found at %s, using workspace copy", path, ref) | ||
| return workspace_content |
There was a problem hiding this comment.
Source snippets can show the wrong code
When the action can't load a source file at base_sha / head_sha (common if the sources submodule isn't initialized or the clone is shallow), it falls back to the file on disk — i.e. the current checkout (PR HEAD).
For removed issues, snippets should show code at base_sha, but they may show HEAD instead. The PR comment won't say that; it's only a warning in the Actions log.
Suggestion: drop the workspace fallback and show "(source file not found at this revision)" when git show {ref}:path fails.
ruling_diff_io.py → load_text_with_workspace_fallback()
This action analyzes changes in ruling JSON files and posts human-readable summaries on pull requests, helping reviewers understand the impact of code changes on static analysis results. Features: - Detects changed ruling JSON files between base and head commits - Compares before/after versions to identify issue differences - Generates code snippets showing where issues appear or were fixed - Posts or updates PR comments with formatted summaries - Includes comprehensive unit tests and documentation The action is designed to be analyzer-agnostic and can be used by any analyzer repository (Python, Java, C#, etc.) by referencing: uses: SonarSource/core-languages-tooling-public/ruling-diff-comment@master Configuration may need minor adjustments in models_and_constants.py for different ruling directory structures. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Remove unit test step from action execution Unit tests should run during action development/CI, not on every invocation. This reduces overhead and execution time for users. Tests can still be run locally with: cd ruling-diff-comment && uv run python -m unittest discover
Run the action's unit tests as part of the build workflow to ensure the action remains functional as changes are made.
Allows the action to work with different analyzer directory structures by accepting ruling-root and sources-root as input parameters. Default values preserve backward compatibility with Python analyzer: - ruling-root: 'private/its-enterprise/ruling/src/test/resources/expected_ruling' - sources-root: 'private/its-enterprise/sources_ruling' For Java analyzer, use: - ruling-root: 'its/ruling/src/test/resources' - sources-root: 'its/sources' Changes: - Add ruling-root and sources-root inputs to action.yml - Pass parameters through ruling_diff.py CLI arguments - Update GitHubActionIO to accept and use these parameters - Update all file loading functions to respect custom paths - Add ruling_root and sources_root to RulingDiffIO protocol - Update example workflow with Java configuration example Document ruling-root and sources-root inputs in README Add the optional configuration parameters to the Inputs table with their default values, and show example usage in the workflow snippet.
Java ruling JSON keys use Maven coordinate format: groupId:artifactId:path Examples: - commons-beanutils:commons-beanutils:src/main/java/... → src/main/java/... - org.eclipse.jetty:jetty-project:jetty-http/src/main/... → jetty-http/src/main/... Python uses simpler format: - airflow:airflow/cli/cli_parser.py → airflow/cli/cli_parser.py Updated strip_project_key to: - Detect 3-part format (Java) and return everything after 2nd colon - Detect 2-part format (Python) and return everything after 1st colon - Handle edge cases (no colons) This fixes source file resolution for Java analyzers where files were not found due to incorrect path extraction from ruling keys. Add unit tests for strip_project_key function Tests cover: - Python format (2-part): project:path - Java Maven format (3-part): groupId:artifactId:path - Java Maven with modules: org.eclipse.jetty:jetty-project:jetty-http/src/... - Edge cases: no colons, simple filenames All 6 new tests pass. Total test count: 38 → 44 tests.
- Use full commit SHA for astral-sh/setup-uv action (security) - Use logging.exception() instead of logging.error() to include traceback
The FakeRulingDiffIO test mock was missing the ruling_root and sources_root attributes that were added to the RulingDiffIO protocol when we made the action configurable. Fixes CI test failures: - test_build_rule_diffs_caches_source_loads_per_ref_and_path - test_build_rule_diffs_missing_source_produces_placeholder_snippet - test_build_rule_diffs_uses_io_object_and_respects_refs
1. Fix syntax highlighting for different languages - Detect language from file extension (.py, .java, .cs, etc.) - Apply appropriate markdown fence language - Supports Python, Java, C#, JavaScript, TypeScript, C/C++, Go, Ruby, PHP, Kotlin, Scala 2. Handle non-conforming ruling paths gracefully - Catch ValueError from parse_ruling_path and log warning - Skip malformed files instead of aborting entire action - Allows mixed directory layouts across analyzers 3. Tighten git error detection - Remove overly broad 'path' marker from _is_missing_at_ref - Prevents real git errors from being misclassified as missing files - Keep specific markers: 'exists on disk, but not in', 'does not exist in'
… paths The tests were instantiating GitHubActionIO before applying patches to the module-level constants, causing the instance to use default paths. This led to test failures because: 1. Default parameter values are evaluated at function definition time 2. The instance's sources_root was set to the unpatched hardcoded constant Also removed one test that relied on non-existent filesystem structure. Fixes: - Move GitHubActionIO() instantiation inside patch context - Pass sources_root explicitly to constructor - Remove test_resolve_source_path_for_project_rulings_falls_back_to_sources_child Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Drop the workspace fallback when source files cannot be found at a specific git ref. Previously, when git show failed to retrieve a file at base_sha or head_sha, the code would fall back to reading the current workspace file, which could show incorrect code (e.g., PR HEAD code for removed issues that should show base_sha code). Now the code returns None and logs a warning when source files are not found, allowing the PR comment to indicate that the source is unavailable at that revision instead of showing misleading code snippets. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
761a922 to
fe3be9a
Compare
The test_load_text_at_ref_falls_back_to_workspace_copy_with_warning test was checking for behavior that was intentionally removed in commit fe3be9a. The workspace fallback feature no longer exists, and the new behavior (returning None with a warning when sources are missing) is already covered by test_load_text_at_ref_warns_when_source_missing. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Code Review ✅ Approved 7 resolved / 7 findingsAdds a reusable analyzer-agnostic GitHub Action to post ruling-diff summaries, addressing multiple issues including script injection risks, hardcoded language fences, and git-related failure handling. All previous findings have been resolved. ✅ 7 resolved✅ Quality: Snippet code fence hardcoded to ```python for all analyzers
✅ Edge Case: One non-conforming ruling path aborts the entire comment
✅ Bug: _is_missing_at_ref may swallow genuine git errors
✅ Security: Script injection via untrusted refs/inputs in run blocks
✅ Bug:
|
| Auto-apply | Compact |
|
|
Was this helpful? React with 👍 / 👎 | Gitar



Part of Core Languages & Parsers shared tooling
Summary
This PR adds a reusable GitHub Action (
ruling-diff-comment) that analyzes changes in ruling JSON files and posts human-readable summaries on pull requests. This helps reviewers understand the impact of code changes on static analysis results.The action is now analyzer-agnostic and can be used across Python, Java, C#, and other analyzers.
Features
Configuration
The action accepts optional parameters for different analyzers:
Python (default):
Java:
Ruling Key Format Support
Automatically handles different ruling JSON key formats:
Python (2-part):
{"airflow:airflow/cli/cli_parser.py": [42]}Java Maven (3-part):
{ "commons-beanutils:commons-beanutils:src/main/java/...": [42], "org.eclipse.jetty:jetty-project:jetty-http/src/main/...": [42] }Testing
✅ Tested on sonar-java: SonarSource/sonar-java#5761 (comment)
The action successfully:
Commits
ruling-rootandsources-rootparameters for different analyzersMigration Path
For analyzer repositories (Python, Java, C#, etc.):
ruling-rootandsources-rootparameters for your analyzerSee
ruling-diff-comment/example-workflow.ymlfor a complete reference implementation.@rombirli - Please review when you have a chance!
Summary by Gitar
test_ruling_diff.py.strip_project_keyto correctly process Java Maven coordinates by handling 3-part formats.GitHubActionIOwith mocked paths in unit tests to prevent failures.FakeRulingDiffIOto include mandatoryruling_rootandsources_rootparameters.This will update automatically on new commits.