Use a hash to fingerprint GitLab CI output (#3456)

This commit is contained in:
Charlie Marsh 2023-03-12 00:22:39 -05:00 committed by GitHub
parent 0f78f27713
commit 7fb7268e8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,8 @@
use std::cmp::Reverse; use std::cmp::Reverse;
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fmt::Display; use std::fmt::Display;
use std::hash::{Hash, Hasher};
use std::io; use std::io;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::path::Path; use std::path::Path;
@ -350,7 +352,7 @@ impl Printer {
json!({ json!({
"description": format!("({}) {}", message.kind.rule().noqa_code(), message.kind.body), "description": format!("({}) {}", message.kind.rule().noqa_code(), message.kind.body),
"severity": "major", "severity": "major",
"fingerprint": message.kind.rule().noqa_code().to_string(), "fingerprint": fingerprint(message),
"location": { "location": {
"path": message.filename, "path": message.filename,
"lines": { "lines": {
@ -553,6 +555,18 @@ fn num_digits(n: usize) -> usize {
.max(1) .max(1)
} }
/// Generate a unique fingerprint to identify a violation.
fn fingerprint(message: &Message) -> String {
let mut hasher = DefaultHasher::new();
message.kind.rule().hash(&mut hasher);
message.location.row().hash(&mut hasher);
message.location.column().hash(&mut hasher);
message.end_location.row().hash(&mut hasher);
message.end_location.column().hash(&mut hasher);
message.filename.hash(&mut hasher);
format!("{:x}", hasher.finish())
}
struct CodeAndBody<'a>(&'a Message, fix::FixMode); struct CodeAndBody<'a>(&'a Message, fix::FixMode);
impl Display for CodeAndBody<'_> { impl Display for CodeAndBody<'_> {