|
| 1 | +package de.chkal.maven.gitlab.codequality.cpd; |
| 2 | + |
| 3 | +import de.chkal.maven.gitlab.codequality.Finding; |
| 4 | +import de.chkal.maven.gitlab.codequality.Finding.Severity; |
| 5 | +import de.chkal.maven.gitlab.codequality.FindingProvider; |
| 6 | +import jakarta.xml.bind.DatatypeConverter; |
| 7 | +import jakarta.xml.bind.JAXBContext; |
| 8 | +import jakarta.xml.bind.JAXBException; |
| 9 | +import jakarta.xml.bind.Unmarshaller; |
| 10 | +import java.io.File; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.security.MessageDigest; |
| 15 | +import java.security.NoSuchAlgorithmException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Locale; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +public class CpdFindingProvider implements FindingProvider { |
| 22 | + |
| 23 | + private final File repositoryRoot; |
| 24 | + |
| 25 | + public CpdFindingProvider(File repositoryRoot) { |
| 26 | + this.repositoryRoot = repositoryRoot; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String getName() { |
| 31 | + return "CPD"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<Finding> getFindings(InputStream stream) { |
| 36 | + |
| 37 | + try { |
| 38 | + |
| 39 | + JAXBContext jaxbContext = JAXBContext.newInstance(PmdCpd.class); |
| 40 | + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
| 41 | + PmdCpd cpdWarnings = (PmdCpd) unmarshaller.unmarshal(stream); |
| 42 | + |
| 43 | + return cpdWarnings.getDuplication().stream() |
| 44 | + .flatMap(this::transformDuplication) |
| 45 | + .collect(Collectors.toList()); |
| 46 | + |
| 47 | + } catch (JAXBException e) { |
| 48 | + throw new IllegalStateException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private Stream<Finding> transformDuplication(Duplication duplication) { |
| 53 | + return duplication.getFile().stream().map(fileLocation -> transformFileLocation(duplication, fileLocation)); |
| 54 | + } |
| 55 | + |
| 56 | + private Finding transformFileLocation(Duplication duplication, FileLocation fileLocation) { |
| 57 | + |
| 58 | + Finding finding = new Finding(); |
| 59 | + finding.setDescription(String.format("%s: Duplication (%d lines)", getName(), duplication.getLines().intValue())); |
| 60 | + finding.setFingerprint(createFingerprint(duplication, fileLocation)); |
| 61 | + finding.setSeverity(getSeverity(duplication)); |
| 62 | + finding.setPath(getRepositoryRelativePath(fileLocation)); |
| 63 | + finding.setLine(getLineNumber(fileLocation)); |
| 64 | + return finding; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private String getRepositoryRelativePath(FileLocation file) { |
| 69 | + Path absolutePath = Path.of(file.getPath()); |
| 70 | + return repositoryRoot.toPath().relativize(absolutePath).toString(); |
| 71 | + } |
| 72 | + |
| 73 | + private Severity getSeverity(Duplication duplication) { |
| 74 | + if (duplication.getLines().intValue() > 30) { |
| 75 | + return Severity.MAJOR; |
| 76 | + } |
| 77 | + if (duplication.getLines().intValue() > 10) { |
| 78 | + return Severity.MINOR; |
| 79 | + } |
| 80 | + return Severity.INFO; |
| 81 | + } |
| 82 | + |
| 83 | + private String createFingerprint(Duplication duplication, FileLocation fileLocation) { |
| 84 | + |
| 85 | + try { |
| 86 | + |
| 87 | + /* |
| 88 | + * The fingerprint is created from: |
| 89 | + * - file path |
| 90 | + * - number of lines |
| 91 | + * - number of tokens |
| 92 | + * - code fragment |
| 93 | + * - column index (which will most likely not change for a finding) |
| 94 | + * - end column index (which will most likely not change for a finding) |
| 95 | + * |
| 96 | + * We do NOT use: |
| 97 | + * - line number (will change if code is added/removed above or below the finding) |
| 98 | + */ |
| 99 | + String key = String.format("%s:%s:%s:%s:%s:%s", |
| 100 | + getRepositoryRelativePath(fileLocation), |
| 101 | + duplication.getLines().intValue(), |
| 102 | + duplication.getTokens().intValue(), |
| 103 | + duplication.getCodefragment().value, |
| 104 | + fileLocation.getColumn(), |
| 105 | + fileLocation.getEndcolumn() |
| 106 | + ); |
| 107 | + |
| 108 | + MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); |
| 109 | + messageDigest.update(key.getBytes(StandardCharsets.UTF_8)); |
| 110 | + byte[] digest = messageDigest.digest(); |
| 111 | + |
| 112 | + return DatatypeConverter.printHexBinary(digest).toLowerCase(Locale.ROOT); |
| 113 | + |
| 114 | + } catch (NoSuchAlgorithmException e) { |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private static Integer getLineNumber(FileLocation fileLocation) { |
| 121 | + return fileLocation.getLine().intValue(); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments