Skip to content

Commit

Permalink
Merge pull request #55 from uhafner/registry
Browse files Browse the repository at this point in the history
Use new parser registry to obtain analysis parsers
  • Loading branch information
uhafner authored Mar 2, 2021
2 parents f26f66e + 77c7718 commit a1e81cc
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 88 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build and deploy
run: mvn -ntp clean verify jib:dockerBuild
- name: Test setup
Expand Down
10 changes: 7 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>edu.hm.hafner</groupId>
<artifactId>codingstyle-pom</artifactId>
<version>1.6.0</version>
<version>2.0.3</version>
<relativePath/>
</parent>

Expand All @@ -21,13 +21,17 @@
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>incrementals.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/incrementals/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>edu.hm.hafner</groupId>
<artifactId>autograding-model</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<exclusions>
<exclusion>
<groupId>edu.hm.hafner</groupId>
Expand All @@ -39,7 +43,7 @@
<dependency>
<groupId>edu.hm.hafner</groupId>
<artifactId>analysis-model</artifactId>
<version>9.8.0</version>
<version>10.0.0-rc3980.fd5b09344558</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package edu.hm.hafner.grading;

import java.util.List;
import java.util.stream.Collectors;

import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.parser.violations.PitAdapter;

/**
* Provides static analysis scores by converting corresponding {@link Report} instances.
Expand Down
102 changes: 53 additions & 49 deletions src/main/java/edu/hm/hafner/grading/AutoGradingAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.FileReaderFactory;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Report.IssueFilterBuilder;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.parser.FindBugsParser;
import edu.hm.hafner.analysis.parser.FindBugsParser.PriorityProperty;
import edu.hm.hafner.analysis.parser.checkstyle.CheckStyleParser;
import edu.hm.hafner.analysis.parser.pmd.PmdParser;
import edu.hm.hafner.analysis.registry.ParserDescriptor;
import edu.hm.hafner.analysis.registry.ParserRegistry;
import edu.hm.hafner.grading.github.GitHubPullRequestWriter;

import de.tobiasmichael.me.Util.JacocoParser;
Expand All @@ -33,6 +31,9 @@
@SuppressWarnings("checkstyle:ClassDataAbstractionCoupling")
public class AutoGradingAction {
private static final String JACOCO_RESULTS = "target/site/jacoco/jacoco.xml";
private static final String CHECKSTYLE = "checkstyle";
private static final String PMD = "pmd";
private static final String SPOTBUGS = "spotbugs";

/**
* Public entry point, calls the action.
Expand All @@ -50,68 +51,78 @@ void run() {

JacksonFacade jackson = new JacksonFacade();

System.out.println("Test Configuration: " + jackson.toJson(score.getTestConfiguration()));
System.out.println("Code Coverage Configuration: " + jackson.toJson(score.getCoverageConfiguration()));
System.out.println("PIT Mutation Coverage Configuration: " + jackson.toJson(score.getPitConfiguration()));
AnalysisConfiguration analysisConfiguration = score.getAnalysisConfiguration();
System.out.println("Static Analysis Configuration: " + jackson.toJson(analysisConfiguration));
System.out.println("------------------------------------------------------------------");
System.out.println("------------------------ Configuration ---------------------------");
System.out.println("------------------------------------------------------------------");
System.out.println("-> Test Configuration: " + jackson.toJson(score.getTestConfiguration()));
System.out.println("-> Code Coverage Configuration: " + jackson.toJson(score.getCoverageConfiguration()));
System.out.println("-> PIT Mutation Coverage Configuration: " + jackson.toJson(score.getPitConfiguration()));
System.out.println("-> Static Analysis Configuration: " + jackson.toJson(score.getAnalysisConfiguration()));

GradingConfiguration configuration = new GradingConfiguration(jsonConfiguration);
List<Report> testReports = new TestReportFinder().find(configuration.getTestPattern());

System.out.println("==================================================================");
List<Report> testReports = new TestReportFinder().find(configuration.getTestPattern());
score.addTestScores(new TestReportSupplier(testReports));

System.out.println("==================================================================");
List<Report> pitReports = new PitReportFinder().find();
score.addPitScores(new PitReportSupplier(pitReports));

List<AnalysisScore> analysisReports = new ArrayList<>();

Report checkStyleReport = parse(configuration, new CheckStyleParser(), "target/checkstyle-result.xml");
analysisReports.add(createAnalysisScore(analysisConfiguration, "CheckStyle", "checkstyle",
checkStyleReport));

Report pmdReport = parse(configuration, new PmdParser(), "target/pmd.xml");
analysisReports.add(createAnalysisScore(analysisConfiguration, "PMD", "pmd",
pmdReport));

Report spotBugsReport = parse(configuration, new FindBugsParser(PriorityProperty.RANK),
"target/spotbugsXml.xml");
analysisReports.add(createAnalysisScore(analysisConfiguration, "SpotBugs", "spotbugs",
spotBugsReport));

score.addAnalysisScores(new AnalysisReportSupplier(analysisReports));

System.out.println("==================================================================");
if (Files.isReadable(Paths.get(JACOCO_RESULTS))) {
JacocoReport coverageReport = new JacocoParser().parse(read(JACOCO_RESULTS));
JacocoReport coverageReport = new JacocoParser().parse(new FileReaderFactory(Paths.get(JACOCO_RESULTS)));
score.addCoverageScores(new CoverageReportSupplier(coverageReport));
}
else {
System.out.println("No JaCoCo coverage result files found!");
}
System.out.println("==================================================================");
ReportFinder reportFinder = new ReportFinder();
ParserRegistry registry = new ParserRegistry();

String[] tools = {CHECKSTYLE, PMD, SPOTBUGS};
List<Report> analysisReports = new ArrayList<>();
List<AnalysisScore> analysisScores = new ArrayList<>();

for (String tool : tools) {
ParserDescriptor parser = registry.get(tool);
List<Path> files = reportFinder.find("target", "glob:" + parser.getPattern());
System.out.format("Searching for '%s' results matching file name pattern %s%n", parser.getName(), parser.getPattern());

if (files.size() == 0) {
System.out.println("No matching report result files found!");
}
else {
Collections.sort(files);

for (Path file : files) {
Report allIssues = parser.createParser().parse(new FileReaderFactory(file));
Report filteredIssues = filterAnalysisReport(allIssues, configuration.getAnalysisPattern());
System.out.format("- %s : %d warnings (from total %d)%n", file, filteredIssues.size(), allIssues.size());
analysisReports.add(filteredIssues);
analysisScores.add(createAnalysisScore(score.getAnalysisConfiguration(), parser.getName(),
parser.getId(), filteredIssues));
}
}
}
score.addAnalysisScores(new AnalysisReportSupplier(analysisScores));
System.out.println("==================================================================");

GradingReport results = new GradingReport();

GitHubPullRequestWriter pullRequestWriter = new GitHubPullRequestWriter();

String files = createAffectedFiles(configuration);

pullRequestWriter.addComment(getChecksName(), results.getHeader(), results.getSummary(score) + files,
results.getDetails(score, testReports),
Arrays.asList(pmdReport, checkStyleReport, spotBugsReport));
results.getDetails(score, testReports), analysisReports);
}

private String createAffectedFiles(final GradingConfiguration configuration) {
String analysisPattern = configuration.getAnalysisPattern();
if (StringUtils.isNotBlank(analysisPattern) && !StringUtils.equals(analysisPattern, GradingConfiguration.ALL_FILES)) {
if (StringUtils.isNotBlank(analysisPattern) && !StringUtils.equals(analysisPattern,
GradingConfiguration.INCLUDE_ALL_FILES)) {
return "\n" + new ReportFinder().renderLinks("./", "regex:" + analysisPattern);
}
return StringUtils.EMPTY;

}

private Report parse(final GradingConfiguration configuration,
final IssueParser parser, final String filePattern) {
return filterAnalysisReport(parser.parse(read(filePattern)), configuration.getAnalysisPattern());
}

private Report filterAnalysisReport(final Report checkStyleReport, final String analysisPattern) {
Expand All @@ -138,10 +149,6 @@ private String getChecksName() {
return StringUtils.defaultIfBlank(System.getenv("CHECKS_NAME"), "Autograding results");
}

private String getRepository() {
return StringUtils.defaultIfBlank(System.getenv("GITHUB_REPOSITORY"), "Autograding results");
}

private String getConfiguration() {
String configuration = System.getenv("CONFIG");
if (StringUtils.isBlank(configuration)) {
Expand All @@ -166,7 +173,4 @@ private String readDefaultConfiguration() {
}
}

private static FileReaderFactory read(final String s) {
return new FileReaderFactory(Paths.get(s));
}
}
22 changes: 11 additions & 11 deletions src/main/java/edu/hm/hafner/grading/GradingConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @author Ullrich Hafner
*/
public class GradingConfiguration {
static final String SUREFIRE_REPORT_PATTERN = "glob:./target/surefire-reports/*.xml";
static final String ALL_FILES = ".*";
static final String SUREFIRE_DEFAULT_PATTERN = "./target/surefire-reports/*.xml";
static final String INCLUDE_ALL_FILES = ".*";

private final String testPattern;
private final String analysisPattern;
Expand All @@ -22,20 +22,20 @@ public class GradingConfiguration {
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

testPattern = asString(configuration, mapper, "tests", SUREFIRE_REPORT_PATTERN);
System.out.println("Using test file pattern: " + testPattern);
testPattern = asString(configuration, mapper, "tests", "pattern", SUREFIRE_DEFAULT_PATTERN);
System.out.println("-> Using test files pattern: " + testPattern);

analysisPattern = asString(configuration, mapper, "analysis", ALL_FILES);
System.out.println("Using analysis file pattern: " + analysisPattern);
analysisPattern = asString(configuration, mapper, "analysis", "fileFilter", INCLUDE_ALL_FILES);
System.out.println("-> Using file name filter (include) for static analysis: " + analysisPattern);
}

private String asString(final String configuration, final ObjectMapper mapper, final String type,
final String defaultValue) {
final String propertyName, final String defaultValue) {
try {
ObjectNode node = mapper.readValue(configuration, ObjectNode.class);
JsonNode tests = node.get(type);
if (tests != null) {
JsonNode pattern = tests.get("pattern");
JsonNode typeNode = node.get(type);
if (typeNode != null) {
JsonNode pattern = typeNode.get(propertyName);
if (pattern != null) {
return pattern.asText(defaultValue);
}
Expand All @@ -48,7 +48,7 @@ private String asString(final String configuration, final ObjectMapper mapper, f
}

public String getTestPattern() {
return testPattern;
return "glob:" + testPattern;
}

public String getAnalysisPattern() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/hm/hafner/grading/PitReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public List<Report> find() {
}

System.out.println("Reading PIT results: ");
System.out.println(reportFiles);
Collections.sort(reportFiles);
reportFiles.forEach(file -> System.out.format("- %s%n", file));

PitAdapter parser = new PitAdapter();
return reportFiles.stream()
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/edu/hm/hafner/grading/ReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.util.VisibleForTesting;

/**
* Base class that finds files in the workspace and parses these files with a parser that returns a {@link Report}
Expand All @@ -25,6 +26,20 @@
* @author Ullrich Hafner
*/
class ReportFinder {
private final String repository;
private final String branch;

ReportFinder() {
this(StringUtils.defaultString(System.getenv("GITHUB_REPOSITORY")),
StringUtils.remove(StringUtils.defaultString(System.getenv("GITHUB_REF")), "refs/heads/"));
}

@VisibleForTesting
ReportFinder(final String repository, final String branch) {
this.repository = repository;
this.branch = branch;
}

/**
* Returns the paths that match the specified pattern.
*
Expand All @@ -51,13 +66,11 @@ protected List<Path> find(final String directory, final String pattern) {

public String renderLinks(final String directory, final String pattern) {
String result = "### Analyzed files\n\n";
String repo = StringUtils.defaultString(System.getenv("GITHUB_REPOSITORY"));
String ref = StringUtils.remove(StringUtils.defaultString(System.getenv("GITHUB_REF")), "refs/heads/");
return find(directory, pattern).stream()
.map(file -> String.format("- [%s](https://github.com/%s/blob/%s/%s)",
StringUtils.substringAfterLast(file.toString(), "/"),
repo,
ref,
repository,
branch,
file)).collect(Collectors.joining("\n", result, "\n"));
}

Expand All @@ -67,7 +80,7 @@ private static class PathMatcherFileVisitor extends SimpleFileVisitor<Path> {

PathMatcherFileVisitor(final String syntaxAndPattern) {
try {
this.pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
}
catch (IllegalArgumentException exception) {
throw new IllegalArgumentException("Pattern not valid for FileSystem.getPathMatcher: " + syntaxAndPattern);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/TestReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public List<Report> find(final String pattern) {

Collections.sort(reportFiles);
System.out.println("Reading test results: ");
System.out.println(reportFiles);
reportFiles.forEach(file -> System.out.format("- %s%n", file));

JUnitAdapter parser = new JUnitAdapter();
return reportFiles.stream()
Expand Down
Loading

0 comments on commit a1e81cc

Please sign in to comment.