Skip to content

Conversation

@phuongfi91
Copy link

@phuongfi91 phuongfi91 commented Mar 7, 2025

PR Type

Enhancement, Tests


Description

  • Added support for Ruff's SARIF output format.

  • Introduced a custom parser to handle ruleId null cases.

  • Updated plugin.yaml to include .ruff.toml configuration support.

  • Added comprehensive test snapshots for Ruff linter functionality.


Changes walkthrough 📝

Relevant files
Enhancement
ruff_sarif_to_trunk_sarif.py
Add custom parser for Ruff SARIF output                                   

linters/ruff/ruff_sarif_to_trunk_sarif.py

  • Added a script to convert Ruff SARIF output to Trunk SARIF.
  • Handles cases where ruleId is null by assigning "E999".
  • Outputs a formatted SARIF JSON structure.
  • +20/-0   
    plugin.yaml
    Update plugin configuration for Ruff SARIF and `.ruff.toml`

    linters/ruff/plugin.yaml

  • Updated to use Ruff's SARIF output format for linting.
  • Added .ruff.toml as a direct configuration file.
  • Adjusted commands and parsers to align with Ruff v0.9.9.
  • Enhanced support for Jupyter notebooks and Python interfaces.
  • +19/-5   
    Tests
    ruff_v0.9.9_basic.check.shot
    Add test snapshot for basic Ruff linting                                 

    linters/ruff/test_data/ruff_v0.9.9_basic.check.shot

  • Added test snapshot for basic Ruff linting functionality.
  • Includes issues like unused imports and incorrect module imports.
  • +88/-0   
    ruff_v0.9.9_basic_nb.check.shot
    Add test snapshot for Ruff on Jupyter notebooks                   

    linters/ruff/test_data/ruff_v0.9.9_basic_nb.check.shot

  • Added test snapshot for Ruff linting on Jupyter notebooks.
  • Captures unused imports in notebook files.
  • +43/-0   
    ruff_v0.9.9_interface.check.shot
    Add test snapshot for Ruff on Python interfaces                   

    linters/ruff/test_data/ruff_v0.9.9_interface.check.shot

  • Added test snapshot for Ruff linting on Python interfaces.
  • Includes issues like undefined names and redefinitions.
  • +183/-0 
    ruff_v0.9.9_syntax.check.shot
    Add test snapshot for Ruff syntax error handling                 

    linters/ruff/test_data/ruff_v0.9.9_syntax.check.shot

  • Added test snapshot for Ruff syntax error handling.
  • Captures unexpected EOF errors with E999 code.
  • +43/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.

  • This change is Reviewable

    @phuongfi91 phuongfi91 requested review from Copilot and jhassine March 7, 2025 10:44
    @bito-code-review
    Copy link

    Code Review Agent Run Status

    • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at jukka.hassinen@gmail.com.

    @qodo-code-review
    Copy link

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The script handles the case where ruleId is null by assigning "E999", but there's no error handling for other potential issues like malformed JSON input or missing expected fields in the SARIF structure.

    results = []
    for run in json.load(sys.stdin).get("runs", []):
        for result in run.get("results", []):
            # Ruff will set code to null for syntax errors
            if result["ruleId"] is None:
                result["ruleId"] = "E999"
            results.append(result)

    Copy link

    Copilot AI left a comment

    Choose a reason for hiding this comment

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

    PR Overview

    This PR adds support for ruff's SARIF output and introduces a new configuration file format (.ruff.toml) for improved compatibility.

    • Introduces a new script (ruff_sarif_to_trunk_sarif.py) to convert ruff’s SARIF output
    • Updates the plugin configuration to reference the new SARIF parser and adds ".ruff.toml" as a direct configuration file

    Reviewed Changes

    File Description
    linters/ruff/ruff_sarif_to_trunk_sarif.py Adds a script to post-process ruff’s SARIF output by standardizing ruleIds
    linters/ruff/plugin.yaml Updates the linter configuration to use the new parser and include .ruff.toml

    Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

    Comments suppressed due to low confidence (2)

    linters/ruff/ruff_sarif_to_trunk_sarif.py:10

    • [nitpick] Consider defining a constant for the fallback ruleId (e.g., E999) instead of using the magic literal directly.
    if result["ruleId"] is None:
    

    linters/ruff/plugin.yaml:67

    • Ensure that the updated reference to ruff_sarif_to_trunk_sarif.py is consistent throughout the plugin configuration and that outdated references have been removed.
    run: python3 ${cwd}/ruff_sarif_to_trunk_sarif.py
    

    @qodo-code-review
    Copy link

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add JSON error handling

    Add error handling for malformed JSON input to prevent the script from crashing
    when processing invalid SARIF data.

    linters/ruff/ruff_sarif_to_trunk_sarif.py [7-12]

    -for run in json.load(sys.stdin).get("runs", []):
    -    for result in run.get("results", []):
    -        # Ruff will set code to null for syntax errors
    -        if result["ruleId"] is None:
    -            result["ruleId"] = "E999"
    -        results.append(result)
    +try:
    +    input_data = json.load(sys.stdin)
    +    for run in input_data.get("runs", []):
    +        for result in run.get("results", []):
    +            # Ruff will set code to null for syntax errors
    +            if result["ruleId"] is None:
    +                result["ruleId"] = "E999"
    +            results.append(result)
    +except json.JSONDecodeError:
    +    print("Error: Invalid JSON input", file=sys.stderr)
    +    sys.exit(1)
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion adds robust error handling for JSON parsing, which is critical for preventing script crashes when processing invalid input. This is an important improvement for production code that handles external data.

    Medium
    • More

    @phuongfi91 phuongfi91 merged commit fbdc103 into main-ng Mar 18, 2025
    8 of 10 checks passed
    @phuongfi91 phuongfi91 deleted the feat/phuongfi91/linter-ruff-improvements branch March 18, 2025 15:51
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants