Skip to content

Commit

Permalink
Merge pull request #174 from slovensko-digital/fix-version-detection
Browse files Browse the repository at this point in the history
Use jpackage version detection, closes #173
  • Loading branch information
jsuchal authored May 5, 2023
2 parents ab47e24 + c6425f6 commit 3e7d166
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/digital/slovensko/autogram/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public static void main(String[] args) {
}

public static String getVersion() {
return requireNonNullElse(Main.class.getPackage().getImplementationVersion(), "dev");
return requireNonNullElse(System.getProperty("jpackage.app-version"), "dev");
}
}
19 changes: 11 additions & 8 deletions src/main/java/digital/slovensko/autogram/core/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@
import java.time.Duration;
import java.util.NoSuchElementException;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class Updater {
public static final String LATEST_RELEASE_URL = "https://github.com/slovensko-digital/autogram/releases/latest";
public static final String LATEST_RELEASE_API_URL = "https://api.github.com/repos/slovensko-digital/autogram/releases/latest";

public static boolean newVersionAvailable() {
if (Main.getVersion().equals("dev")) {
return false;
}

String latestVersion = "";
String latestVersionTag = "";
try {
var request = HttpRequest.newBuilder().uri(new URI(LATEST_RELEASE_URL))
var request = HttpRequest.newBuilder().uri(new URI(LATEST_RELEASE_API_URL))
.header("Accept", "application/vnd.github.v3+json").GET().build();

var client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 302)
return false;

var location = response.headers().firstValue("Location").orElseThrow();
latestVersion = location.substring(location.lastIndexOf('/') + 1);
var gson = new Gson();
var json = gson.fromJson(response.body(), JsonObject.class);
latestVersionTag = json.get("tag_name").getAsString();

} catch (IOException | InterruptedException | URISyntaxException | NoSuchElementException ignored) {
ignored.printStackTrace(); // TODO handle error
return false;
}

if (latestVersion.equals(""))
if (latestVersionTag.equals(""))
return false;

var latestVersion = latestVersionTag.replaceAll("^v", "");
return !Main.getVersion().equals(latestVersion);
}
}

0 comments on commit 3e7d166

Please sign in to comment.