Do not use code location for Gitlab fingerprints. (#7203)

This commit is contained in:
Greger 2023-09-08 08:25:26 +02:00 committed by GitHub
parent 45f9fca228
commit 9671922e40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::io::Write; use std::io::Write;
@ -6,8 +7,6 @@ use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use serde_json::json; use serde_json::json;
use ruff_source_file::SourceLocation;
use crate::fs::{relativize_path, relativize_path_to}; use crate::fs::{relativize_path, relativize_path_to};
use crate::message::{Emitter, EmitterContext, Message}; use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule; use crate::registry::AsRule;
@ -58,6 +57,7 @@ impl Serialize for SerializedMessages<'_> {
S: Serializer, S: Serializer,
{ {
let mut s = serializer.serialize_seq(Some(self.messages.len()))?; let mut s = serializer.serialize_seq(Some(self.messages.len()))?;
let mut fingerprints = HashSet::<u64>::with_capacity(self.messages.len());
for message in self.messages { for message in self.messages {
let start_location = message.compute_start_location(); let start_location = message.compute_start_location();
@ -82,10 +82,19 @@ impl Serialize for SerializedMessages<'_> {
|project_dir| relativize_path_to(message.filename(), project_dir), |project_dir| relativize_path_to(message.filename(), project_dir),
); );
let mut message_fingerprint = fingerprint(message, 0);
// Make sure that we do not get a fingerprint that is already in use
// by adding in the previously generated one.
while fingerprints.contains(&message_fingerprint) {
message_fingerprint = fingerprint(message, message_fingerprint);
}
fingerprints.insert(message_fingerprint);
let value = json!({ let value = 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": fingerprint(message, &start_location, &end_location), "fingerprint": format!("{:x}", message_fingerprint),
"location": { "location": {
"path": path, "path": path,
"lines": lines "lines": lines
@ -100,11 +109,7 @@ impl Serialize for SerializedMessages<'_> {
} }
/// Generate a unique fingerprint to identify a violation. /// Generate a unique fingerprint to identify a violation.
fn fingerprint( fn fingerprint(message: &Message, salt: u64) -> u64 {
message: &Message,
start_location: &SourceLocation,
end_location: &SourceLocation,
) -> String {
let Message { let Message {
kind, kind,
range: _, range: _,
@ -115,12 +120,11 @@ fn fingerprint(
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
kind.rule().hash(&mut hasher); salt.hash(&mut hasher);
start_location.hash(&mut hasher); kind.name.hash(&mut hasher);
end_location.hash(&mut hasher);
file.name().hash(&mut hasher); file.name().hash(&mut hasher);
format!("{:x}", hasher.finish()) hasher.finish()
} }
#[cfg(test)] #[cfg(test)]