|
| 1 | +use oxc_diagnostics::{ |
| 2 | + Error, Severity, |
| 3 | + reporter::{DiagnosticReporter, DiagnosticResult, Info}, |
| 4 | +}; |
| 5 | + |
| 6 | +use std::hash::{DefaultHasher, Hash, Hasher}; |
| 7 | + |
| 8 | +use crate::output_formatter::InternalFormatter; |
| 9 | + |
| 10 | +#[derive(Debug, Default)] |
| 11 | +pub struct GitlabOutputFormatter; |
| 12 | + |
| 13 | +#[derive(Debug, serde::Serialize)] |
| 14 | +struct GitlabErrorLocationLinesJson { |
| 15 | + begin: usize, |
| 16 | + end: usize, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, serde::Serialize)] |
| 20 | +struct GitlabErrorLocationJson { |
| 21 | + path: String, |
| 22 | + lines: GitlabErrorLocationLinesJson, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, serde::Serialize)] |
| 26 | +struct GitlabErrorJson { |
| 27 | + description: String, |
| 28 | + check_name: String, |
| 29 | + fingerprint: String, |
| 30 | + severity: String, |
| 31 | + location: GitlabErrorLocationJson, |
| 32 | +} |
| 33 | + |
| 34 | +impl InternalFormatter for GitlabOutputFormatter { |
| 35 | + fn get_diagnostic_reporter(&self) -> Box<dyn DiagnosticReporter> { |
| 36 | + Box::new(GitlabReporter::default()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Renders reports as a Gitlab Code Quality Report |
| 41 | +/// |
| 42 | +/// <https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format> |
| 43 | +/// |
| 44 | +/// Note that, due to syntactic restrictions of JSON arrays, this reporter waits until all |
| 45 | +/// diagnostics have been reported before writing them to the output stream. |
| 46 | +#[derive(Default)] |
| 47 | +struct GitlabReporter { |
| 48 | + diagnostics: Vec<Error>, |
| 49 | +} |
| 50 | + |
| 51 | +impl DiagnosticReporter for GitlabReporter { |
| 52 | + fn finish(&mut self, _: &DiagnosticResult) -> Option<String> { |
| 53 | + Some(format_gitlab(&mut self.diagnostics)) |
| 54 | + } |
| 55 | + |
| 56 | + fn render_error(&mut self, error: Error) -> Option<String> { |
| 57 | + self.diagnostics.push(error); |
| 58 | + None |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn format_gitlab(diagnostics: &mut Vec<Error>) -> String { |
| 63 | + let errors = diagnostics.drain(..).map(|error| { |
| 64 | + let Info { start, end, filename, message, severity, rule_id } = Info::new(&error); |
| 65 | + let severity = match severity { |
| 66 | + Severity::Error => "critical".to_string(), |
| 67 | + Severity::Warning => "major".to_string(), |
| 68 | + Severity::Advice => "minor".to_string(), |
| 69 | + }; |
| 70 | + |
| 71 | + let fingerprint = { |
| 72 | + let mut hasher = DefaultHasher::new(); |
| 73 | + start.line.hash(&mut hasher); |
| 74 | + end.line.hash(&mut hasher); |
| 75 | + filename.hash(&mut hasher); |
| 76 | + message.hash(&mut hasher); |
| 77 | + severity.hash(&mut hasher); |
| 78 | + |
| 79 | + format!("{:x}", hasher.finish()) |
| 80 | + }; |
| 81 | + |
| 82 | + GitlabErrorJson { |
| 83 | + description: message, |
| 84 | + check_name: rule_id.unwrap_or_default(), |
| 85 | + location: GitlabErrorLocationJson { |
| 86 | + path: filename, |
| 87 | + lines: GitlabErrorLocationLinesJson { begin: start.line, end: end.line }, |
| 88 | + }, |
| 89 | + fingerprint, |
| 90 | + severity, |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + serde_json::to_string_pretty(&errors.collect::<Vec<_>>()).expect("Failed to serialize") |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod test { |
| 99 | + use oxc_diagnostics::{ |
| 100 | + NamedSource, OxcDiagnostic, |
| 101 | + reporter::{DiagnosticReporter, DiagnosticResult}, |
| 102 | + }; |
| 103 | + use oxc_span::Span; |
| 104 | + |
| 105 | + use super::GitlabReporter; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn reporter() { |
| 109 | + let mut reporter = GitlabReporter::default(); |
| 110 | + |
| 111 | + let error = OxcDiagnostic::warn("error message") |
| 112 | + .with_label(Span::new(0, 8)) |
| 113 | + .with_source_code(NamedSource::new("file://test.ts", "debugger;")); |
| 114 | + |
| 115 | + let first_result = reporter.render_error(error); |
| 116 | + |
| 117 | + // reporter keeps it in memory |
| 118 | + assert!(first_result.is_none()); |
| 119 | + |
| 120 | + // reporter gives results when finishing |
| 121 | + let second_result = reporter.finish(&DiagnosticResult::default()); |
| 122 | + |
| 123 | + assert!(second_result.is_some()); |
| 124 | + assert_eq!( |
| 125 | + second_result.unwrap(), |
| 126 | + "[\n {\n \"description\": \"error message\",\n \"check_name\": \"\",\n \"fingerprint\": \"8b23bd85b148d3\",\n \"severity\": \"major\",\n \"location\": {\n \"path\": \"file://test.ts\",\n \"lines\": {\n \"begin\": 1,\n \"end\": 1\n }\n }\n }\n]" |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments