Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/better-lan…
Browse files Browse the repository at this point in the history
…guage-names

# Conflicts:
#	report-viewer/src/model/Language.ts
#	report-viewer/tests/unit/model/factories/OverviewFactory.test.ts
#	report-viewer/tests/unit/model/factories/ValidOptions.json
#	report-viewer/tests/unit/model/factories/ValidOverview.json
  • Loading branch information
Kr0nox committed Aug 26, 2024
2 parents 630e27b + 7860d71 commit 7eeee1f
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 391 deletions.
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
<executions>
<execution>
<id>npm install</id>
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public CLI(String[] args) {
*/
public void executeCli() throws ExitException, IOException {
logger.debug("Your version of JPlag is {}", JPlag.JPLAG_VERSION);
JPlagVersionChecker.printVersionNotification();

if (!this.inputHandler.parse()) {
CollectedLogger.setLogLevel(this.inputHandler.getCliOptions().advanced.logLevel);
Expand Down Expand Up @@ -110,6 +111,7 @@ public File runJPlag() throws ExitException, FileNotFoundException {
* @throws IOException If something went wrong with the internal server
*/
public void runViewer(File zipFile) throws IOException {
finalizeLogger(); // Prints the errors. The later finalizeLogger will print any errors logged after this point.
JPlagRunner.runInternalServer(zipFile, this.inputHandler.getCliOptions().advanced.port);
}

Expand Down
91 changes: 91 additions & 0 deletions cli/src/main/java/de/jplag/cli/JPlagVersionChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package de.jplag.cli;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Optional;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.JPlag;
import de.jplag.reporting.reportobject.model.Version;

/**
* Handles the check for newer versions.
*/
public class JPlagVersionChecker {
private static final String API_URL = "https://api.github.com/repos/jplag/JPlag/releases";
private static final Logger logger = LoggerFactory.getLogger(JPlagVersionChecker.class);
private static final String EXPECTED_VERSION_FORMAT = "v\\d\\.\\d\\.\\d+";
private static final String WARNING_UNABLE_TO_FETCH = "Unable to fetch version information. New version notification will not work.";
private static final String NEWER_VERSION_AVAILABLE = "There is a newer version ({}) available. You can download the newest version here: https://github.com/jplag/JPlag/releases";
private static final String UNEXPECTED_ERROR = "There was an unexpected error, when checking for new versions. Please report this on: https://github.com/jplag/JPlag/issues";

private JPlagVersionChecker() {

}

/**
* Prints a warning if a newer version is available on GitHub.
*/
public static void printVersionNotification() {
Optional<Version> newerVersion = checkForNewVersion();
newerVersion.ifPresent(version -> logger.warn(NEWER_VERSION_AVAILABLE, version));
}

private static Optional<Version> checkForNewVersion() {
try {
JsonArray array = fetchApi();
Version newest = getNewestVersion(array);
Version current = JPlag.JPLAG_VERSION;

if (newest.compareTo(current) > 0) {
return Optional.of(newest);
}
} catch (IOException | URISyntaxException e) {
logger.info(WARNING_UNABLE_TO_FETCH);
} catch (Exception e) {
logger.warn(UNEXPECTED_ERROR, e);
}

return Optional.empty();
}

private static JsonArray fetchApi() throws IOException, URISyntaxException {
URL url = new URI(API_URL).toURL();
URLConnection connection = url.openConnection();

try (JsonReader reader = Json.createReader(connection.getInputStream())) {
return reader.readArray();
}
}

private static Version getNewestVersion(JsonArray apiResult) {
return apiResult.stream().map(JsonObject.class::cast).map(version -> version.getString("name"))
.filter(versionName -> versionName.matches(EXPECTED_VERSION_FORMAT)).limit(1).map(JPlagVersionChecker::parseVersion).findFirst()
.orElse(JPlag.JPLAG_VERSION);
}

/**
* Parses the version name.
* @param versionName The version name. The expected format is: v[major].[minor].[patch]
* @return The parsed version
*/
private static Version parseVersion(String versionName) {
String withoutPrefix = versionName.substring(1);
String[] parts = withoutPrefix.split("\\.");
return parseVersionParts(parts);
}

private static Version parseVersionParts(String[] parts) {
return new Version(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void writeOverview(JPlagResult result) {
missingComparisons);
OverviewReport overviewReport = new OverviewReport(REPORT_VIEWER_VERSION, folders.stream().map(File::getPath).toList(), // submissionFolderPath
baseCodePath, // baseCodeFolderPath
result.getOptions().language().getName(), // language
result.getOptions().language().getIdentifier(), // language
result.getOptions().fileSuffixes(), // fileExtensions
submissionNameToIdMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)), // submissionIds
submissionNameToNameToComparisonFileName, // result.getOptions().getMinimumTokenMatch(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.jplag.reporting.reportobject.model;

import java.util.Comparator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -12,7 +14,8 @@
* @param minor MINOR version when you add functionality in a backwards compatible manner
* @param patch PATCH version when you make backwards compatible bug fixes
*/
public record Version(@JsonProperty("major") int major, @JsonProperty("minor") int minor, @JsonProperty("patch") int patch) {
public record Version(@JsonProperty("major") int major, @JsonProperty("minor") int minor, @JsonProperty("patch") int patch)
implements Comparable<Version> {

/**
* The default version for development (0.0.0).
Expand Down Expand Up @@ -42,4 +45,9 @@ public static Version parseVersion(String version) {
public String toString() {
return String.format("%d.%d.%d", major, minor, patch);
}

@Override
public int compareTo(Version other) {
return Comparator.comparing(Version::major).thenComparing(Version::minor).thenComparing(Version::patch).compare(this, other);
}
}
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<spotless.version>2.43.0</spotless.version>
<slf4j.version>2.0.13</slf4j.version>
<junit.version>5.10.3</junit.version>
<slf4j.version>2.0.16</slf4j.version>
<junit.version>5.11.0</junit.version>

<antlr2.version>2.7.7</antlr2.version>
<antlr4.version>4.13.1</antlr4.version>
<antlr4.version>4.13.2</antlr4.version>
<emf.version>2.36.0</emf.version>
<emf.ecore.version>2.30.0</emf.ecore.version>
<emf.ecore.xmi.version>2.37.0</emf.ecore.xmi.version>
Expand Down Expand Up @@ -240,7 +240,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -269,7 +269,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.4</version>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Loading

0 comments on commit 7eeee1f

Please sign in to comment.