Skip to content

Commit

Permalink
Catch exceptions and show error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 13, 2023
1 parent ebe28e9 commit ac49a36
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 63 deletions.
26 changes: 25 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<java.version>17</java.version>

<jib-maven-plugin.version>3.4.0</jib-maven-plugin.version>
<testcontainers.version>1.19.1</testcontainers.version>

</properties>

<dependencyManagement>
Expand Down Expand Up @@ -128,6 +130,20 @@
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand All @@ -137,12 +153,20 @@
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
<configuration>
<to>
<image>docker.io/uhafner/autograding-github-action</image>
</to>
<from>
<image>maven:3.6.3-openjdk-8</image>
<image>maven:3.9.5-eclipse-temurin-17</image>
</from>
</configuration>
</plugin>
Expand Down
48 changes: 35 additions & 13 deletions src/main/java/edu/hm/hafner/grading/AutoGradingAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Report.IssueFilterBuilder;
import edu.hm.hafner.grading.AnalysisScore.AnalysisScoreBuilder;
import edu.hm.hafner.grading.github.GitHubPullRequestWriter;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SecureXmlParserFactory;

/**
* GitHub action entrypoint for the autograding action.
Expand All @@ -35,36 +38,55 @@ public static void main(final String... args) {
void run() {
String jsonConfiguration = getConfiguration();

FilteredLog log = new FilteredLog("Autograding Action Errors:");
FilteredLog log = new FilteredLog("Autograding GitHub Action Errors:");
var logHandler = new LogHandler(System.out, log);

AggregatedScore score = new AggregatedScore(jsonConfiguration, log);

System.out.println("------------------------------------------------------------------");
System.out.println("------------------------ Configuration ---------------------------");
System.out.println("------------------------ Start Grading ---------------------------");
System.out.println("------------------------------------------------------------------");

AggregatedScore score = new AggregatedScore(jsonConfiguration, log);
logHandler.print();

System.out.println("Reading configuration: " + jsonConfiguration);

GradingConfiguration configuration = new GradingConfiguration(jsonConfiguration);

System.out.println("==================================================================");

score.gradeTests(new ConsoleTestReportFactory());
GradingReport results = new GradingReport();

System.out.println("==================================================================");
try {
score.gradeTests(new ConsoleTestReportFactory());
logHandler.print();

score.gradeCoverage(new ConsoleCoverageReportFactory());
System.out.println("==================================================================");

System.out.println("==================================================================");
score.gradeCoverage(new ConsoleCoverageReportFactory());
logHandler.print();

score.gradeAnalysis(new ConsoleAnalysisReportFactory());
System.out.println("==================================================================");

System.out.println("==================================================================");
score.gradeAnalysis(new ConsoleAnalysisReportFactory());
logHandler.print();

log.getInfoMessages().forEach(System.out::println);
System.out.println("==================================================================");

System.out.println("==================================================================");
System.out.println(results.getHeader());
System.out.println(results.getSummary(score));
System.out.println(results.getDetails(score, List.of()));
}
catch (NoSuchElementException | ParsingException | SecureXmlParserFactory.ParsingException exception) {
System.out.println("==================================================================");
System.out.println(results.getHeader());
System.out.println(results.getSummary(score));
System.out.println(results.getErrors(score, exception));
}

System.out.println("------------------------------------------------------------------");
System.out.println("------------------------- End Grading ----------------------------");
System.out.println("------------------------------------------------------------------");

GradingReport results = new GradingReport();
GitHubPullRequestWriter pullRequestWriter = new GitHubPullRequestWriter();

String files = createAffectedFiles(configuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package edu.hm.hafner.grading;

import java.nio.file.Path;
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.Report;
Expand All @@ -18,32 +14,21 @@
*
* @author Ullrich Hafner
*/
public class ConsoleAnalysisReportFactory implements AnalysisReportFactory {
public class ConsoleAnalysisReportFactory extends ReportFactory implements AnalysisReportFactory {
@Override
public Report create(final ToolConfiguration tool, final FilteredLog log) {
ParserDescriptor parser = new ParserRegistry().get(tool.getId());

var displayName = StringUtils.defaultIfBlank(tool.getName(), parser.getName());
var total = new Report(tool.getId(), displayName);

log.logInfo("Searching for %s results matching file name pattern %s",
parser.getName(), tool.getPattern());
List<Path> files = new ReportFinder().find("./", "glob:" + tool.getPattern());
var total = new Report(tool.getId(), tool.getDisplayName());

if (files.isEmpty()) {
log.logError("No matching report files found! Configuration error?");
var analysisParser = parser.createParser();
for (Path file : findFiles(tool, log)) {
Report allIssues = analysisParser.parse(new FileReaderFactory(file));
log.logInfo("- %s: %d warnings", file, allIssues.size());
total.addAll(allIssues);
}
else {
Collections.sort(files);

var analysisParser = parser.createParser();
for (Path file : files) {
Report allIssues = analysisParser.parse(new FileReaderFactory(file));
log.logInfo("- %s: %d warnings", file, allIssues.size());
total.addAll(allIssues);
}
}
log.logInfo("-> %s Total: %d warnings", displayName, total.size());
log.logInfo("-> %s Total: %d warnings", tool.getDisplayName(), total.size());
return total;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.FileReaderFactory;
import edu.hm.hafner.coverage.CoverageParser.ProcessingMode;
import edu.hm.hafner.coverage.Metric;
import edu.hm.hafner.coverage.ModuleNode;
import edu.hm.hafner.coverage.Node;
import edu.hm.hafner.coverage.Value;
import edu.hm.hafner.coverage.registry.ParserRegistry;
Expand All @@ -22,36 +19,24 @@
*
* @author Ullrich Hafner
*/
public class ConsoleCoverageReportFactory implements CoverageReportFactory {
public class ConsoleCoverageReportFactory extends ReportFactory implements CoverageReportFactory {
@Override
public Node create(final ToolConfiguration tool, final FilteredLog log) {
var parser = new ParserRegistry().getParser(StringUtils.upperCase(tool.getId()), ProcessingMode.FAIL_FAST);

var name = StringUtils.defaultIfEmpty(tool.getName(), tool.getId());
log.logInfo("Searching for %s results matching file name pattern %s",
name, tool.getPattern());
List<Path> files = new ReportFinder().find("./", "glob:" + tool.getPattern());

if (files.isEmpty()) {
log.logError("No matching report files found! Configuration error?");
return new ModuleNode("empty");
}

Collections.sort(files);

var nodes = new ArrayList<Node>();
for (Path file : files) {
var node = parser.parse(new FileReaderFactory(files.get(0)).create(), log);
for (Path file : findFiles(tool, log)) {
var node = parser.parse(new FileReaderFactory(file).create(), log);
log.logInfo("- %s: %s", file, extractMetric(tool, node));
nodes.add(node);
}

var aggregation = Node.merge(nodes);
log.logInfo("-> %s Total: %s", name, extractMetric(tool, aggregation));
log.logInfo("-> %s Total: %s", tool.getDisplayName(), extractMetric(tool, aggregation));
return aggregation;
}

private String extractMetric(final ToolConfiguration tool, final Node node) {

return node.getValue(Metric.fromTag(tool.getMetric())).map(Value::toString).orElse("<none>");
}
}
29 changes: 29 additions & 0 deletions src/main/java/edu/hm/hafner/grading/ReportFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.hm.hafner.grading;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

import edu.hm.hafner.util.FilteredLog;

/**
* A base class for all report factories. Provides common functionality to find report files.
*
* @author Ullrich Hafner
*/
abstract class ReportFactory {
protected List<Path> findFiles(final ToolConfiguration tool, final FilteredLog log) {
log.logInfo("Searching for %s results matching file name pattern %s",
tool.getDisplayName(), tool.getPattern());
List<Path> files = new ReportFinder().find("glob:" + tool.getPattern());

if (files.isEmpty()) {
throw new NoSuchElementException(String.format("No matching report files found when using pattern '%s'! "
+ "Configuration error for '%s'?", tool.getPattern(), tool.getDisplayName()));
}

Collections.sort(files);
return files;
}
}
12 changes: 6 additions & 6 deletions src/main/java/edu/hm/hafner/grading/ReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
* @author Ullrich Hafner
*/
class ReportFinder {
private static String getEnv(final String name) {
return StringUtils.defaultString(System.getenv(name));
}

private final String repository;
private final String branch;

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

private static String getEnv(final String name) {
return StringUtils.defaultString(System.getenv(name));
}

@VisibleForTesting
ReportFinder(final String repository, final String branch) {
this.repository = repository;
Expand All @@ -52,7 +52,7 @@ private static String getEnv(final String name) {
* @return the matching paths
* @see FileSystem#getPathMatcher(String)
*/
protected List<Path> find(final String directory, final String pattern) {
public List<Path> find(final String directory, final String pattern) {
try {
PathMatcherFileVisitor visitor = new PathMatcherFileVisitor(pattern);
Files.walkFileTree(Paths.get(directory), visitor);
Expand All @@ -74,7 +74,7 @@ protected List<Path> find(final String directory, final String pattern) {
* @return the matching paths
* @see FileSystem#getPathMatcher(String)
*/
protected List<Path> find(final String pattern) {
public List<Path> find(final String pattern) {
return find(".", pattern);
}

Expand Down
Loading

0 comments on commit ac49a36

Please sign in to comment.