Skip to content

Commit 8ac32de

Browse files
committed
Improved fingerprint handling for Checkstyle
1 parent cb74556 commit 8ac32de

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/main/java/de/chkal/maven/gitlab/codequality/checkstyle/CheckstyleFindingProvider.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,10 @@ private Finding transformErrorType(FileType fileType, ErrorType errorType) {
6565

6666
Finding finding = new Finding();
6767
finding.setDescription(String.format("%s: %s", getName(), errorType.getMessage()));
68+
finding.setFingerprint(createFingerprint(fileType, errorType));
6869
finding.setSeverity(getSeverity(errorType.getSeverity()));
6970
finding.setPath(getRepositoryRelativePath(fileType));
7071
finding.setLine(getLineNumber(errorType));
71-
72-
// Checkstyle doesn't provide any fingerprints, so we create our own one
73-
autoGenerateFingerprint(finding);
74-
7572
return finding;
7673

7774
}
@@ -94,18 +91,32 @@ private Severity getSeverity(String severity) {
9491
}
9592
}
9693

97-
private void autoGenerateFingerprint(Finding finding) {
94+
private String createFingerprint(FileType fileType, ErrorType errorType) {
9895

9996
try {
10097

101-
String key = String.format("%s:%s:%d",
102-
finding.getDescription(), finding.getPath(), finding.getLine());
98+
/*
99+
* The fingerprint is created from:
100+
* - file path
101+
* - severity
102+
* - message text
103+
* - column index (which will most likely not change for a finding)
104+
*
105+
* We do NOT use:
106+
* - line number (will change if code is added/removed above or below the finding)
107+
*/
108+
String key = String.format("%s:%s:%s:%s",
109+
getRepositoryRelativePath(fileType),
110+
errorType.getSeverity(),
111+
errorType.getMessage(),
112+
errorType.getColumn()
113+
);
103114

104115
MessageDigest sha1Digest = MessageDigest.getInstance("SHA256");
105116
sha1Digest.update(key.getBytes(StandardCharsets.UTF_8));
106117
byte[] digest = sha1Digest.digest();
107118

108-
finding.setFingerprint(DatatypeConverter.printHexBinary(digest).toLowerCase(Locale.ROOT));
119+
return DatatypeConverter.printHexBinary(digest).toLowerCase(Locale.ROOT);
109120

110121
} catch (NoSuchAlgorithmException e) {
111122
throw new RuntimeException(e);

src/test/java/de/chkal/maven/gitlab/codequality/checkstyle/CheckstyleFindingProviderTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ void shouldParseCheckstyleFileWithoutNamespace() throws IOException {
2525

2626
Finding first = findings.get(0);
2727
assertThat(first.getDescription()).startsWith("Checkstyle: 'static' modifier out of order");
28-
assertThat(first.getFingerprint()).isNotBlank();
28+
assertThat(first.getFingerprint())
29+
.isEqualTo("ab6037e7ca18412687356d3460d9eb6e73777d004168730864daaabe993df2bc");
2930
assertThat(first.getSeverity()).isEqualTo(Severity.MAJOR);
3031
assertThat(first.getPath()).isEqualTo("src/main/java/com/example/myapp/Bar.java");
3132
assertThat(first.getLine()).isEqualTo(25);
3233

3334
Finding second = findings.get(1);
3435
assertThat(second.getDescription()).startsWith("Checkstyle: 'if' child has incorrect");
35-
assertThat(second.getFingerprint()).isNotBlank();
36+
assertThat(second.getFingerprint())
37+
.isEqualTo("cf3472bc41cca24f7a5bfc1278cc873effc31053f729c4c0c111aa95bdb5a267");
3638
assertThat(second.getSeverity()).isEqualTo(Severity.MAJOR);
3739
assertThat(second.getPath()).isEqualTo("src/main/java/com/example/myapp/Bar.java");
3840
assertThat(second.getLine()).isEqualTo(90);

0 commit comments

Comments
 (0)