Skip to content

Commit

Permalink
Merge pull request #335 from uhafner/version-info
Browse files Browse the repository at this point in the history
Add methods to read Maven version and Git hash
  • Loading branch information
uhafner committed Apr 1, 2024
2 parents c6931fd + 841a759 commit 5ff90da
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.StringJoiner;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -56,6 +57,10 @@ protected String getDisplayName() {
return "Autograding";
}

private String getFullDisplayName(final FilteredLog log) {
return String.format("%s %s (#%s)", getDisplayName(), readVersion(log), readSha(log));
}

/**
* Runs the autograding.
*
Expand All @@ -67,7 +72,7 @@ public AggregatedScore run() {
var logHandler = new LogHandler(outputStream, log);

log.logInfo(SINGLE_LINE);
log.logInfo(center("Start"));
log.logInfo(center("Start", log));
log.logInfo(SINGLE_LINE);

var configuration = getConfiguration(log);
Expand Down Expand Up @@ -109,15 +114,64 @@ public AggregatedScore run() {
}

log.logInfo(SINGLE_LINE);
log.logInfo(center("End"));
log.logInfo(center("End", log));
log.logInfo(SINGLE_LINE);
logHandler.print();

return score;
}

private String center(final String message) {
return StringUtils.center(message + " " + getDisplayName(), 80);
/**
* Reads the Maven version information from the git.properties file.
*
* @param log
* the logger
*
* @return the version information
*/
protected String readVersion(final FilteredLog log) {
return readGitProperty("git.build.version", log);
}

/**
* Reads the Git SHA from the git.properties file.
*
* @param log
* the logger
*
* @return the Git SHA
*/
protected String readSha(final FilteredLog log) {
return readGitProperty("git.commit.id.abbrev", log);
}

protected String readGitProperty(final String key, final FilteredLog log) {
try (var propertiesFile = getClass().getResourceAsStream("/git.properties")) {

Check warning on line 149 in src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

SpotBugs: UI_INHERITANCE_UNSAFE_GETRESOURCE

Usage of GetResource in edu.hm.hafner.grading.AutoGradingRunner.readGitProperty(String, FilteredLog) may be unsafe if class is extended
if (propertiesFile == null) {
log.logError("Version information file '/git.properties' not found in class path");

return StringUtils.EMPTY;
}

try {
var gitProperties = new Properties();

gitProperties.load(propertiesFile);

return gitProperties.getProperty(key);
}
catch (IOException exception) {
log.logError("Can't read version information in '/git.properties'.");
}
return StringUtils.EMPTY;
}
catch (IOException exception) {
return StringUtils.EMPTY; // ignore exception on close
}
}

private String center(final String message, final FilteredLog log) {
return StringUtils.center(message + " " + getFullDisplayName(log), 80);
}

/**
Expand Down

0 comments on commit 5ff90da

Please sign in to comment.