Skip to content

Commit

Permalink
feat(reporting): add gitlab code quality format (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvvinceocam authored Feb 1, 2025
1 parent d1851d8 commit e32e7d1
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ self_update = { version = "0.42.0", features = ["archive-tar", "archive-zip", "c
openssl = { version = "0.10", features = ["vendored"] }
tempfile = "3.15.0"
colored = "3.0.0"
blake3 = "1.5.5"

[lints]
workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/reporting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ serde_json = { workspace = true }
codespan-reporting = { workspace = true }
termcolor = { workspace = true }
strum = { workspace = true }
blake3 = { workspace = true }
89 changes: 89 additions & 0 deletions crates/reporting/src/internal/emitter/gitlab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use mago_interner::ThreadedInterner;
use mago_source::HasSource;
use mago_source::SourceManager;

use serde::Serialize;
use termcolor::WriteColor;

use crate::error::ReportingError;
use crate::IssueCollection;
use crate::Level;

use super::utils::long_message;

#[derive(Serialize)]
struct CodeQualityIssue<'a> {
description: String,
check_name: &'a str,
fingerprint: String,
severity: &'a str,
location: Location,
}

#[derive(Serialize)]
struct Location {
path: String,
lines: Lines,
}

#[derive(Serialize)]
struct Lines {
begin: usize,
}

pub fn gitlab_format(
writer: &mut dyn WriteColor,
sources: &SourceManager,
interner: &ThreadedInterner,
issues: IssueCollection,
) -> Result<Option<Level>, ReportingError> {
let highest_level = issues.get_highest_level();

let code_quality_issues = issues
.iter()
.map(|issue| {
let severity = match &issue.level {
Level::Note | Level::Help => "info",
Level::Warning => "minor",
Level::Error => "major",
};

let (path, line) = match issue.annotations.iter().find(|annotation| annotation.is_primary()) {
Some(annotation) => {
let source = sources.load(&annotation.span.source()).unwrap();

let file_path = interner.lookup(&source.identifier.0).to_string();
let line = source.line_number(annotation.span.start.offset) + 1;

(file_path, line)
}
None => ("<unknown>".to_string(), 0),
};

let description = long_message(issue);

let check_name = issue.code.as_deref().unwrap_or("other");

let fingerprint = {
let mut hasher = blake3::Hasher::new();
hasher.update(check_name.as_bytes());
hasher.update(path.as_bytes());
hasher.update(line.to_le_bytes().as_slice());
hasher.update(description.as_bytes());
hasher.finalize().to_hex()[..32].to_string()
};

CodeQualityIssue {
description,
check_name,
fingerprint,
severity,
location: Location { path, lines: Lines { begin: line } },
}
})
.collect::<Vec<_>>();

serde_json::to_writer_pretty(writer, &code_quality_issues)?;

Ok(highest_level)
}
2 changes: 2 additions & 0 deletions crates/reporting/src/internal/emitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod codespan;
pub mod count;
pub mod emacs;
pub mod github;
pub mod gitlab;
pub mod json;

pub trait Emitter {
Expand Down Expand Up @@ -60,6 +61,7 @@ impl Emitter for ReportingFormat {
ReportingFormat::Medium => codespan::medium_format.emit(writer, sources, interner, issues),
ReportingFormat::Short => codespan::short_format.emit(writer, sources, interner, issues),
ReportingFormat::Github => github::github_format.emit(writer, sources, interner, issues),
ReportingFormat::Gitlab => gitlab::gitlab_format.emit(writer, sources, interner, issues),
ReportingFormat::Json => json::json_format.emit(writer, sources, interner, issues),
ReportingFormat::Count => count::count_format.emit(writer, sources, interner, issues),
ReportingFormat::Checkstyle => checkstyle::checkstyle_format.emit(writer, sources, interner, issues),
Expand Down
2 changes: 2 additions & 0 deletions crates/reporting/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum ReportingFormat {
Medium,
Short,
Github,
Gitlab,
Json,
Count,
Checkstyle,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl FromStr for ReportingFormat {
"medium" => Ok(Self::Medium),
"short" => Ok(Self::Short),
"github" => Ok(Self::Github),
"gitlab" => Ok(Self::Gitlab),
"json" => Ok(Self::Json),
"count" => Ok(Self::Count),
"checkstyle" => Ok(Self::Checkstyle),
Expand Down

0 comments on commit e32e7d1

Please sign in to comment.