-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for PMD and CPD reports #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| target/ | ||
| *.iml | ||
| .idea | ||
| .classpath | ||
| .project | ||
| .settings/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/de/chkal/maven/gitlab/codequality/cpd/CpdFindingProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package de.chkal.maven.gitlab.codequality.cpd; | ||
|
|
||
| import de.chkal.maven.gitlab.codequality.Finding; | ||
| import de.chkal.maven.gitlab.codequality.Finding.Severity; | ||
| import de.chkal.maven.gitlab.codequality.FindingProvider; | ||
| import jakarta.xml.bind.DatatypeConverter; | ||
| import jakarta.xml.bind.JAXBContext; | ||
| import jakarta.xml.bind.JAXBException; | ||
| import jakarta.xml.bind.Unmarshaller; | ||
| import java.io.File; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class CpdFindingProvider implements FindingProvider { | ||
|
|
||
| private final File repositoryRoot; | ||
|
|
||
| public CpdFindingProvider(File repositoryRoot) { | ||
| this.repositoryRoot = repositoryRoot; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "CPD"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Finding> getFindings(InputStream stream) { | ||
|
|
||
| try { | ||
|
|
||
| JAXBContext jaxbContext = JAXBContext.newInstance(PmdCpd.class); | ||
| Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
| PmdCpd cpdWarnings = (PmdCpd) unmarshaller.unmarshal(stream); | ||
|
|
||
| return cpdWarnings.getDuplication().stream() | ||
| .flatMap(this::transformDuplication) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| } catch (JAXBException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
|
|
||
| private Stream<Finding> transformDuplication(Duplication duplication) { | ||
| return duplication.getFile().stream().map(fileLocation -> transformFileLocation(duplication, fileLocation)); | ||
| } | ||
|
|
||
| private Finding transformFileLocation(Duplication duplication, FileLocation fileLocation) { | ||
|
|
||
| Finding finding = new Finding(); | ||
| finding.setDescription(String.format("%s: Duplication (%d lines)", getName(), duplication.getLines().intValue())); | ||
| finding.setFingerprint(createFingerprint(duplication, fileLocation)); | ||
| finding.setSeverity(getSeverity(duplication)); | ||
| finding.setPath(getRepositoryRelativePath(fileLocation)); | ||
| finding.setLine(getLineNumber(fileLocation)); | ||
| return finding; | ||
|
|
||
| } | ||
|
|
||
| private String getRepositoryRelativePath(FileLocation file) { | ||
| Path absolutePath = Path.of(file.getPath()); | ||
| return repositoryRoot.toPath().relativize(absolutePath).toString(); | ||
| } | ||
|
|
||
| private Severity getSeverity(Duplication duplication) { | ||
| if (duplication.getLines().intValue() > 30) { | ||
| return Severity.MAJOR; | ||
| } | ||
| if (duplication.getLines().intValue() > 10) { | ||
| return Severity.MINOR; | ||
| } | ||
| return Severity.INFO; | ||
| } | ||
|
|
||
| private String createFingerprint(Duplication duplication, FileLocation fileLocation) { | ||
|
|
||
| try { | ||
|
|
||
| /* | ||
| * The fingerprint is created from: | ||
| * - file path | ||
| * - number of lines | ||
| * - number of tokens | ||
| * - code fragment | ||
| * - column index (which will most likely not change for a finding) | ||
| * - end column index (which will most likely not change for a finding) | ||
| * | ||
| * We do NOT use: | ||
| * - line number (will change if code is added/removed above or below the finding) | ||
| */ | ||
| String key = String.format("%s:%s:%s:%s:%s:%s", | ||
| getRepositoryRelativePath(fileLocation), | ||
| duplication.getLines().intValue(), | ||
| duplication.getTokens().intValue(), | ||
| duplication.getCodefragment().value, | ||
| fileLocation.getColumn(), | ||
| fileLocation.getEndcolumn() | ||
| ); | ||
|
|
||
| MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); | ||
| messageDigest.update(key.getBytes(StandardCharsets.UTF_8)); | ||
| byte[] digest = messageDigest.digest(); | ||
|
|
||
| return DatatypeConverter.printHexBinary(digest).toLowerCase(Locale.ROOT); | ||
|
|
||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static Integer getLineNumber(FileLocation fileLocation) { | ||
| return fileLocation.getLine().intValue(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.