Skip to content

Introduce --format flag to conditionally generate SARIF report#70

Merged
gayaldassanayake merged 11 commits intoballerina-platform:mainfrom
nureka-rodrigo:sarif
Aug 6, 2025
Merged

Introduce --format flag to conditionally generate SARIF report#70
gayaldassanayake merged 11 commits intoballerina-platform:mainfrom
nureka-rodrigo:sarif

Conversation

@nureka-rodrigo
Copy link
Copy Markdown
Contributor

@nureka-rodrigo nureka-rodrigo commented Jun 27, 2025

Purpose

Fixes: #68

Approach

This PR adds support for the Static Analysis Results Interchange Format (SARIF) in addition to the existing JSON output. The implementation preserves backward compatibility and introduces SARIF as an alternative reporting option for improved integration with external development tools.

A new convertIssuesToSarifString method has been implemented to generate SARIF-compliant output by mapping internal Issue objects to the SARIF schema, including sections such as runs, driver, rules, and results. Severity levels defined by RuleKind are translated to their SARIF equivalents using a helper method, and detailed source location data—including line and column positions—is included in the output.

The Gson library continues to be used for pretty-printing in both JSON and SARIF formats. Output format selection is handled via a new --format flag in the CLI, with input validation to restrict accepted values. Both console and file output workflows support the new format, with JSON remaining the default when no format is specified.

Check List

@nureka-rodrigo nureka-rodrigo changed the title Introduce --sarif flag to conditionally generate SARIF report Introduce --format flag to conditionally generate SARIF report Jul 3, 2025
5. Run analysis and specify the output format (json or sarif).

```bash
bal scan --format=sarif
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
bal scan --format=sarif
$ bal scan --format=sarif

Comment thread scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestOptions.java Outdated
@keizer619 keizer619 requested a review from Copilot August 5, 2025 07:41
Copy link
Copy Markdown

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.

Pull Request Overview

This PR introduces SARIF (Static Analysis Results Interchange Format) support as an alternative to the existing JSON output format. The implementation adds a new --format flag to the CLI that accepts either "json" or "sarif" values, with JSON remaining the default format to preserve backward compatibility.

Key changes include:

  • Added ReportFormat enum to handle format validation and conversion
  • Implemented SARIF output generation with proper schema compliance and severity mapping
  • Updated CLI help documentation and test coverage for the new format option

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
scan-command/src/main/java/io/ballerina/scan/ReportFormat.java New enum defining supported report formats with validation
scan-command/src/main/java/io/ballerina/scan/internal/ScanCmd.java Updated command class to support format option and SARIF output generation
scan-command/src/main/java/io/ballerina/scan/utils/ScanUtils.java Enhanced utility methods to support SARIF format generation and output
scan-command/src/main/java/io/ballerina/scan/utils/Constants.java Added SARIF-related constants for schema and tool metadata
scan-command/src/test/java/io/ballerina/scan/internal/ScanCmdTest.java Comprehensive test coverage for format validation and output generation
scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestOptions.java Updated test utilities to support format testing
Documentation files Updated help text and README with format option examples

Comment thread scan-command/src/main/java/io/ballerina/scan/utils/Constants.java Outdated
Comment thread scan-command/src/main/java/io/ballerina/scan/utils/ScanUtils.java Outdated
Comment thread scan-command/src/main/java/io/ballerina/scan/utils/ScanUtils.java Outdated
Comment thread scan-command/src/main/java/io/ballerina/scan/utils/Constants.java Outdated
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Aug 5, 2025

@gayaldassanayake gayaldassanayake merged commit 5e55f28 into ballerina-platform:main Aug 6, 2025
4 checks passed
@nureka-rodrigo nureka-rodrigo deleted the sarif branch September 4, 2025 03:24
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.

SARIF support for the scan tool

4 participants