Qodana Scan

repository·main·Indexed 43 days ago

https://github.com/jetbrains/qodana-action

A code quality monitoring tool that identifies bugs, security vulnerabilities, and code imperfections. It integrates with GitHub Actions, Azure Pipelines, and GitLab CI to automate code analysis, enforce quality standards via pull request gates, and apply automatic quick-fixes.

Tokens
12.4K
Snippets
33
Records
39
Agent score
55%

What's inside Qodana

  1. Set up a Pull Request Quality Gate

    main

    You can block pull requests from being merged if they fail the Qodana quality gate.

    Workflow Setup:

    1. Configure the Qodana Scan action to run on pull_request events targeting your protected branch (e.g., main).
    2. Set a fail-threshold (an integer) in the Qodana action configuration to define the maximum number of allowed problems.

    GitHub Repository Settings:

    1. Go to Settings > Branches.
    2. Add or edit a Branch protection rule for your target branch.
    3. Enable Require status checks to pass before merging.
    4. Search for and select the Qodana status check.
    5. Save the rule.
  2. Install and configure Qodana Scan GitHub Action

    main

    To run Qodana on a GitHub repository, create a workflow file at .github/workflows/code_quality.yml.

    Key Requirements:

    • Permissions: The job must have contents: write, pull-requests: write, and checks: write permissions.
    • Checkout: Use actions/checkout@v3 with fetch-depth: 0 to ensure a full history is available for pull request analysis.
    • Authentication: You must provide a QODANA_TOKEN as an environment variable. This token should be stored as an encrypted secret in your GitHub repository settings.

    This configuration allows Qodana to run on the main branch, release branches, and pull requests.

    name: Qodana
    on:
      workflow_dispatch:
      pull_request:
      push:
        branches:
          - main
          - 'releases/*'
    
    jobs:
      qodana:
        runs-on: ubuntu-latest
        permissions:
          contents: write
          pull-requests: write
          checks: write
        steps:
          - uses: actions/checkout@v3
            with:
              ref: ${{ github.event.pull_request.head.sha }}
              fetch-depth: 0
          - name: 'Qodana Scan'
            uses: JetBrains/qodana-action@v2026.2
            env:
              QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
  3. Install the Qodana Maven plugin

    main

    To use Qodana with Maven, add the qodana-maven-plugin to your pom.xml.

    Prerequisites:

    • Maven 3.6 or newer.
    • docker installed and available in your environment (required to run Qodana in a container).
    • Docker requires at least 4GB of memory allocated in your Docker settings.
    <build>
        <plugins>
            <plugin>
                <groupId>io.github.qodana</groupId>
                <artifactId>qodana-maven-plugin</artifactId>
                <version>2026.2.1</version>
            </plugin>
        </plugins>
    </build>
  4. Install and use Qodana Scan in Azure Pipelines

    main

    Qodana Scan is an Azure Pipelines task provided by the Qodana Azure Pipelines extension. It allows you to scan your code for bugs, security vulnerabilities, and other imperfections directly within your pipeline.

    Requirements:

    • The agent must have Docker installed.
    • The docker daemon must be able to run Linux containers (as most Qodana Docker images are Linux-based).
    • The task can run on any OS and x86_64/arm64 CPUs.

    Basic Setup: To use the task, add the QodanaScan@2026 step to your azure-pipelines.yml. You can choose between 'native' mode or 'Docker' mode using the --within-docker argument.

    variables:
      nativeMode: 'true'  # set to 'false' for Docker mode
    
    steps:
      - task: QodanaScan@2026
        inputs:
          args: '--within-docker=$(nativeMode)'
  5. Manage technical debt with Quality Gates and Baselines

    main

    Use a baseline to ignore existing technical debt and a quality gate to prevent new issues from being introduced.

    How to establish a baseline:

    1. Run Qodana locally: qodana scan --show-report.
    2. Open the report at http://localhost:8080/, add existing problems to the baseline, and download the qodana.sarif.json file.
    3. Upload qodana.sarif.json to your project root on GitHub.
    4. Update your GitHub Action configuration to use the baseline via the args parameter:
    - name: Qodana Scan
      uses: JetBrains/qodana-action@main
      with:
        args: --baseline,qodana.sarif.json

    How to add a quality gate: Add the fail-threshold property to your workflow configuration. This defines the maximum number of new problems allowed before the workflow fails and blocks the pull request.

    - name: Qodana Scan
      uses: JetBrains/qodana-action@main
      with:
        args: --baseline,qodana.sarif.json
        fail-threshold: <number-of-accepted-problems>
  6. Install the Qodana Gradle plugin

    main

    To use Qodana with Gradle, apply the org.jetbrains.qodana plugin in your configuration file. This plugin requires Gradle 6.6 or newer. Ensure docker is installed and available in your environment, as Qodana runs in a container. Note that Docker requires at least 4GB of memory allocated.

    // Groovy – build.gradle
    plugins {
        id "org.jetbrains.qodana" version "2026.2.1"
    }
    // Kotlin DSL – build.gradle.kts
    plugins {
        id("org.jetbrains.qodana") version "2026.2.1"
    }
  7. Send Qodana Scan results to Qodana Cloud

    main

    To upload your scan results to Qodana Cloud, you must provide a QODANA_TOKEN as an environment variable to the QodanaScan@2026 task.

    1. Create a QODANA_TOKEN secret variable in the Azure Pipelines UI.
    2. Use your project token as the value.
    3. Map the variable in your YAML file under the env section of the task.
      - task: QodanaScan@2026
        env:
          QODANA_TOKEN: $(QODANA_TOKEN)
  8. Enable GitHub Code Scanning for Qodana

    main

    To integrate Qodana results into GitHub's native code scanning interface, upload the SARIF report generated by Qodana using the github/codeql-action/upload-sarif action.

    Add the following step to your workflow immediately after the Qodana Scan step:

    - uses: github/codeql-action/upload-sarif@v2
      with:
        sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json

    Important: When using GitHub code scanning, inspection results are not exported back to Qodana. You must configure your baseline and quality gates within Qodana itself before uploading to GitHub.

          - uses: github/codeql-action/upload-sarif@v2
            with:
              sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json
  9. Configure Pull Request mode and comments in Azure Pipelines

    main

    To optimize scans for Pull Requests in Azure Pipelines, use the following settings:

    1. prMode: Set to true to analyze ONLY changed files.
      • Note: Azure Pipelines checks out an ephemeral merge commit for PR builds. To ensure prMode works correctly and reports only problems introduced by the PR, you must check out the PR source commit.
    2. postPrComment: Set to true to post a summary comment to the PR.
      • Requirements: Requires Contribute to pull request permission and either SYSTEM_ACCESSTOKEN or persistCredentials: true during the checkout step.
  10. Enable Pull Request analysis in Azure Pipelines

    main

    To perform incremental analysis on Pull Requests (reporting only problems introduced by the PR), you must configure the pipeline to use prMode: true.

    Important: Azure Pipelines typically checks out an ephemeral merge commit for PR builds. To ensure Qodana correctly identifies the source-branch head (and avoids reporting unrelated changes from the target branch), you must manually check out the PR source commit using git checkout $(System.PullRequest.SourceCommitId) before running the scan.

    Requirements:

    • fetchDepth: 0 must be set in the checkout step.
    • persistCredentials: true should be set in the checkout step.
    • prMode: true must be set in the QodanaScan@2026 task inputs.
    pr:
      branches:
        include:
          - '*'
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
      - checkout: self
        fetchDepth: 0
        persistCredentials: true
      - script: git checkout $(System.PullRequest.SourceCommitId)
        condition: eq(variables['Build.Reason'], 'PullRequest')
        displayName: Check out PR source commit (not the merge commit)
      - task: QodanaScan@2026
        env:
          QODANA_TOKEN: $(QODANA_TOKEN)
        inputs:
          prMode: true
  11. Apply automatic quick-fixes with Qodana Scan

    main

    Qodana can automatically fix identified issues and push those changes back to your repository. To enable this, follow these steps:

    1. Choose a strategy:
      • Use the args property in the task with --apply-fixes or --cleanup.
      • Alternatively, specify fixesStrategy in your repository's qodana.yaml file.
    2. Set the push mode: Set the pushFixes input to either:
      • pull-request: Creates a new branch with fixes and opens a PR.
      • branch: Pushes fixes directly to the original branch.
    3. Configure Permissions: In Azure DevOps, go to Repositories → Manage repositories → Security. For the Qodana for Azure Pipelines Build Service user, allow:
      • Contribute
      • Bypass policies when pushing (to ensure fixes aren't blocked by branch policies)
      • Create branch (if using pull-request mode)
    4. Enable Credentials: Set persistCredentials: true in your checkout step to allow the task to push changes.
    steps:
      - checkout: self
        fetchDepth: 0
        persistCredentials: true
      - task: QodanaScan@2026
        env:
          QODANA_TOKEN: $(QODANA_TOKEN)
        inputs:
          pushFixes: "branch"
          args: "--apply-fixes"