Skip to content

feat: Add support for --sarif flag to qlty smells command #2037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qlty-cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl Check {
formatter.write_to(&mut std::io::stdout())?;
Ok(false)
} else if self.sarif {
let formatter = SarifFormatter::boxed(report.clone());
let formatter = SarifFormatter::boxed(report.messages.clone(), report.issues.clone());
formatter.write_to(&mut std::io::stdout())?;
Ok(false)
} else {
Expand Down
19 changes: 14 additions & 5 deletions qlty-cli/src/commands/smells.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::format::SarifFormatter;
use crate::ui::Highlighter;
use crate::ui::Steps;
use crate::{Arguments, CommandError, CommandSuccess};
Expand Down Expand Up @@ -46,9 +47,13 @@ pub struct Smells {
pub quiet: bool,

/// JSON output
#[arg(long, hide = true)]
#[arg(long, hide = true, conflicts_with = "sarif")]
json: bool,

/// SARIF output
#[arg(long, conflicts_with = "json")]
sarif: bool,

/// Files to analyze
pub paths: Vec<PathBuf>,
}
Expand Down Expand Up @@ -101,7 +106,7 @@ impl Smells {

steps.start(SPARKLES, "Reporting... ");
println!();
self.write_stdout(&workspace, &report.issues)?;
self.write_stdout(&workspace, &report)?;

CommandSuccess::ok()
}
Expand Down Expand Up @@ -204,11 +209,15 @@ impl Smells {
Ok(executor.report())
}

fn write_stdout(&self, workspace: &Workspace, issues: &[Issue]) -> Result<()> {
fn write_stdout(&self, workspace: &Workspace, report: &Report) -> Result<()> {
if self.json {
self.write_stdout_json(issues)
self.write_stdout_json(&report.issues)
} else if self.sarif {
let formatter = SarifFormatter::boxed(report.messages.clone(), report.issues.clone());
formatter.write_to(&mut std::io::stdout())?;
Ok(())
} else {
self.write_stdout_text(workspace, issues)
self.write_stdout_text(workspace, &report.issues)
}
}

Expand Down
19 changes: 9 additions & 10 deletions qlty-cli/src/format/sarif.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use qlty_check::Report;
use qlty_config::version::{BUILD_DATE, LONG_VERSION, QLTY_VERSION};
use qlty_formats::Formatter;
use qlty_types::analysis::v1::{Category, Issue, Language, Level, Location, Message, MessageLevel};
Expand All @@ -9,16 +8,17 @@ use std::io::Write;

#[derive(Debug)]
pub struct SarifFormatter {
report: Report,
pub messages: Vec<Message>,
pub issues: Vec<Issue>,
}

impl SarifFormatter {
pub fn new(report: Report) -> Self {
Self { report }
pub fn new(messages: Vec<Message>, issues: Vec<Issue>) -> Self {
Self { messages, issues }
}

pub fn boxed(report: Report) -> Box<dyn Formatter> {
Box::new(Self::new(report))
pub fn boxed(messages: Vec<Message>, issues: Vec<Issue>) -> Box<dyn Formatter> {
Box::new(Self::new(messages, issues))
}

fn convert_level(&self, level: Level) -> &'static str {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl SarifFormatter {
let mut rules = vec![];
let mut rule_ids = std::collections::HashSet::new();

for issue in &self.report.issues {
for issue in &self.issues {
if !rule_ids.contains(&issue.rule_key) {
rule_ids.insert(issue.rule_key.clone());

Expand All @@ -284,14 +284,12 @@ impl SarifFormatter {
}

let results = self
.report
.issues
.iter()
.map(|issue| self.serialize_issue(issue))
.collect::<Vec<_>>();

let notifications = self
.report
.messages
.iter()
.map(|message| self.serialize_notification(message))
Expand Down Expand Up @@ -339,6 +337,7 @@ impl Formatter for SarifFormatter {
mod test {
use super::*;
use qlty_analysis::{workspace_entries::TargetMode, IssueCount};
use qlty_check::Report;
use qlty_types::analysis::v1::{
Category, ExecutionVerb, Mode, Range, Replacement, Suggestion, SuggestionSource,
};
Expand Down Expand Up @@ -494,7 +493,7 @@ mod test {
counts: IssueCount::default(),
};

let formatter = SarifFormatter::boxed(report);
let formatter = SarifFormatter::boxed(report.messages.clone(), report.issues.clone());
let output = formatter.read().unwrap();
let output_str = String::from_utf8_lossy(&output);

Expand Down
Loading