Skip to content

Commit

Permalink
Do not use code location for Gitlab fingerprints.
Browse files Browse the repository at this point in the history
Using code location when creating hash for Gitlab fingerprints
makes the codequality reports in Gitlab very unstable.
Any change of position would trigger both a "fixed" message, and
a "new problem detected" message in Gitlab, making it very noisy.
  • Loading branch information
gregersn committed Sep 6, 2023
1 parent 171b66c commit f5589b9
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions crates/ruff/src/message/gitlab.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::io::Write;

use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
use serde_json::json;

use ruff_source_file::SourceLocation;

use crate::fs::{relativize_path, relativize_path_to};
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
Expand Down Expand Up @@ -58,6 +57,7 @@ impl Serialize for SerializedMessages<'_> {
S: Serializer,
{
let mut s = serializer.serialize_seq(Some(self.messages.len()))?;
let mut fingerprints = HashSet::<String>::new();

for message in self.messages {
let start_location = message.compute_start_location();
Expand All @@ -82,10 +82,16 @@ impl Serialize for SerializedMessages<'_> {
|project_dir| relativize_path_to(message.filename(), project_dir),
);

let mut message_fingerprint = fingerprint(message, "".to_string());
while fingerprints.contains(&message_fingerprint) {
message_fingerprint = fingerprint(message, message_fingerprint);
}
fingerprints.insert(message_fingerprint.to_string());

let value = json!({
"description": format!("({}) {}", message.kind.rule().noqa_code(), message.kind.body),
"severity": "major",
"fingerprint": fingerprint(message, &start_location, &end_location),
"fingerprint": message_fingerprint,
"location": {
"path": path,
"lines": lines
Expand All @@ -102,8 +108,7 @@ impl Serialize for SerializedMessages<'_> {
/// Generate a unique fingerprint to identify a violation.
fn fingerprint(
message: &Message,
start_location: &SourceLocation,
end_location: &SourceLocation,
salt: String,
) -> String {
let Message {
kind,
Expand All @@ -116,8 +121,7 @@ fn fingerprint(
let mut hasher = DefaultHasher::new();

kind.rule().hash(&mut hasher);
start_location.hash(&mut hasher);
end_location.hash(&mut hasher);
salt.hash(&mut hasher);
file.name().hash(&mut hasher);

format!("{:x}", hasher.finish())
Expand Down

0 comments on commit f5589b9

Please sign in to comment.