Add GitHub output format (#975)

This commit is contained in:
Edgar R. M 2022-12-01 09:22:11 -06:00 committed by GitHub
parent 9e1ba916f0
commit 6f48ac6c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 2 deletions

View File

@ -1465,7 +1465,7 @@ line-length = 120
#### [`format`](#format)
The style in which violation messages should be formatted: `"text"` (default), `"grouped"`
(group messages by file), `"json"` (machine-readable), or `"junit"` (machine-readable XML).
(group messages by file), `"json"` (machine-readable), `"junit"` (machine-readable XML), or `"github"` (GitHub Actions annotations).
**Default value**: `"text"`

View File

@ -64,6 +64,9 @@ pub fn explain(code: &CheckCode, format: SerializationFormat) -> Result<()> {
SerializationFormat::Junit => {
bail!("`--explain` does not support junit format")
}
SerializationFormat::Github => {
bail!("`--explain` does not support GitHub format")
}
};
Ok(())
}

View File

@ -192,6 +192,25 @@ impl<'a> Printer<'a> {
self.post_text(num_fixable);
}
SerializationFormat::Github => {
self.pre_text(diagnostics);
// Generate error workflow command in GitHub Actions format
// https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
diagnostics.messages.iter().for_each(|message| {
println!(
"::notice title=Ruff,file={},line={},col={},endLine={},endColumn={}::({}) \
{}",
relativize_path(Path::new(&message.filename)),
message.location.row(),
message.location.column(),
message.end_location.row(),
message.end_location.column(),
message.kind.code(),
message.kind.body(),
);
});
}
}
Ok(())

View File

@ -123,7 +123,7 @@ impl Configuration {
fix: options.fix.unwrap_or_default(),
fixable: options.fixable.unwrap_or_else(|| CATEGORIES.to_vec()),
unfixable: options.unfixable.unwrap_or_default(),
format: options.format.unwrap_or(SerializationFormat::Text),
format: options.format.unwrap_or_default(),
ignore: options.ignore.unwrap_or_default(),
line_length: options.line_length.unwrap_or(88),
per_file_ignores: options

View File

@ -1,4 +1,5 @@
use std::collections::BTreeSet;
use std::env;
use std::hash::Hash;
use std::path::{Path, PathBuf};
use std::str::FromStr;
@ -150,4 +151,16 @@ pub enum SerializationFormat {
Json,
Junit,
Grouped,
Github,
}
impl Default for SerializationFormat {
fn default() -> Self {
if let Ok(github_actions) = env::var("GITHUB_ACTIONS") {
if github_actions == "true" {
return Self::Github;
}
}
Self::Text
}
}