fix(diagnostics): need to escape strings for --format github

closes #3212
This commit is contained in:
Boshen 2024-05-12 13:01:07 +08:00
parent d8173e1000
commit b86ef7d2dd
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1

View file

@ -38,5 +38,41 @@ fn format_github(diagnostic: &Error) -> String {
Severity::Warning | miette::Severity::Advice => "warning",
};
let title = rule_id.map_or(Cow::Borrowed("oxlint"), Cow::Owned);
let filename = escape_property(&filename);
let message = escape_data(&message);
format!("::{severity} file={filename},line={line},endLine={line},col={column},endColumn={column},title={title}::{message}\n")
}
fn escape_data(value: &str) -> String {
// Refs:
// - https://github.com/actions/runner/blob/a4c57f27477077e57545af79851551ff7f5632bd/src/Runner.Common/ActionCommand.cs#L18-L22
// - https://github.com/actions/toolkit/blob/fe3e7ce9a7f995d29d1fcfd226a32bca407f9dc8/packages/core/src/command.ts#L80-L94
let mut result = String::with_capacity(value.len());
for c in value.chars() {
match c {
'\r' => result.push_str("%0D"),
'\n' => result.push_str("%0A"),
'%' => result.push_str("%25"),
_ => result.push(c),
}
}
result
}
fn escape_property(value: &str) -> String {
// Refs:
// - https://github.com/actions/runner/blob/a4c57f27477077e57545af79851551ff7f5632bd/src/Runner.Common/ActionCommand.cs#L25-L32
// - https://github.com/actions/toolkit/blob/fe3e7ce9a7f995d29d1fcfd226a32bca407f9dc8/packages/core/src/command.ts#L80-L94
let mut result = String::with_capacity(value.len());
for c in value.chars() {
match c {
'\r' => result.push_str("%0D"),
'\n' => result.push_str("%0A"),
':' => result.push_str("%3A"),
',' => result.push_str("%2C"),
'%' => result.push_str("%25"),
_ => result.push(c),
}
}
result
}