From 6bd96f6433d4af549d82c26c4f47f2ba14fb2ec9 Mon Sep 17 00:00:00 2001 From: crschnick Date: Sat, 17 Feb 2024 17:43:49 +0000 Subject: [PATCH] Use improved changelog api --- .../io/xpipe/app/update/AppDownloads.java | 19 +++++++++++++------ .../io/xpipe/app/update/ChocoUpdater.java | 1 - .../io/xpipe/app/update/GitHubUpdater.java | 2 -- .../io/xpipe/app/update/HomebrewUpdater.java | 1 - .../io/xpipe/app/update/PortableUpdater.java | 1 - .../app/update/UpdateAvailableAlert.java | 13 +------------ .../app/update/UpdateChangelogAlert.java | 12 +----------- .../io/xpipe/app/update/UpdateHandler.java | 7 +------ .../java/io/xpipe/app/util/HttpHelper.java | 18 ++++++------------ 9 files changed, 22 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/update/AppDownloads.java b/app/src/main/java/io/xpipe/app/update/AppDownloads.java index 92f43e4a7..f42912221 100644 --- a/app/src/main/java/io/xpipe/app/update/AppDownloads.java +++ b/app/src/main/java/io/xpipe/app/update/AppDownloads.java @@ -4,6 +4,7 @@ import io.xpipe.app.core.AppProperties; import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.issue.TrackEvent; import io.xpipe.app.util.HttpHelper; +import io.xpipe.core.util.JacksonMapper; import org.apache.commons.io.FileUtils; import org.kohsuke.github.GHRelease; import org.kohsuke.github.GHRepository; @@ -78,6 +79,18 @@ public class AppDownloads { return Optional.empty(); } + try { + var url = URI.create("https://api.xpipe.io/changelog?from=" + AppProperties.get().getVersion() + + "&to=" + version + "&stage=" + AppProperties.get().isStaging()).toURL(); + var bytes = HttpHelper.executeGet(url, aFloat -> {}); + var string = new String(bytes, StandardCharsets.UTF_8); + var json = JacksonMapper.getDefault().readTree(string); + var changelog = json.required("changelog").asText(); + return Optional.of(changelog); + } catch (Throwable t) { + ErrorEvent.fromThrowable(t).omitted(omitErrors).handle(); + } + try { var asset = release.get().listAssets().toList().stream() .filter(ghAsset -> ghAsset.getName().equals("changelog.md")) @@ -96,12 +109,6 @@ public class AppDownloads { } } - public static String getLatestVersion() throws IOException { - return getLatestSuitableRelease() - .map(ghRelease -> ghRelease.getTagName()) - .orElse("?"); - } - public static Optional getLatestIncludingPreRelease() throws IOException { var repo = getRepository(); return Optional.ofNullable(repo.listReleases().iterator().next()); diff --git a/app/src/main/java/io/xpipe/app/update/ChocoUpdater.java b/app/src/main/java/io/xpipe/app/update/ChocoUpdater.java index 83c43b458..bf5ab4653 100644 --- a/app/src/main/java/io/xpipe/app/update/ChocoUpdater.java +++ b/app/src/main/java/io/xpipe/app/update/ChocoUpdater.java @@ -49,7 +49,6 @@ public class ChocoUpdater extends UpdateHandler { null, null, Instant.now(), - null, isUpdate); lastUpdateCheckResult.setValue(rel); return lastUpdateCheckResult.getValue(); diff --git a/app/src/main/java/io/xpipe/app/update/GitHubUpdater.java b/app/src/main/java/io/xpipe/app/update/GitHubUpdater.java index 2ce52a278..b11eeb289 100644 --- a/app/src/main/java/io/xpipe/app/update/GitHubUpdater.java +++ b/app/src/main/java/io/xpipe/app/update/GitHubUpdater.java @@ -35,7 +35,6 @@ public class GitHubUpdater extends UpdateHandler { XPipeDistributionType.get().getId(), lastUpdateCheckResult.getValue().getVersion(), lastUpdateCheckResult.getValue().getReleaseUrl(), - lastUpdateCheckResult.getValue().getReleaseDate(), downloadFile.get(), changelog, lastUpdateCheckResult.getValue().getAssetType()); @@ -79,7 +78,6 @@ public class GitHubUpdater extends UpdateHandler { ghAsset.get().getBrowserDownloadUrl(), assetType, Instant.now(), - rel.get().getCreatedAt() != null ? rel.get().getCreatedAt().toInstant() : null, isUpdate)); return lastUpdateCheckResult.getValue(); } diff --git a/app/src/main/java/io/xpipe/app/update/HomebrewUpdater.java b/app/src/main/java/io/xpipe/app/update/HomebrewUpdater.java index ce9a01fd2..8e091d57b 100644 --- a/app/src/main/java/io/xpipe/app/update/HomebrewUpdater.java +++ b/app/src/main/java/io/xpipe/app/update/HomebrewUpdater.java @@ -44,7 +44,6 @@ public class HomebrewUpdater extends GitHubUpdater { lastUpdateCheckResult.getValue().getVersion(), lastUpdateCheckResult.getValue().getReleaseUrl(), null, - null, changelog, lastUpdateCheckResult.getValue().getAssetType()); preparedUpdate.setValue(rel); diff --git a/app/src/main/java/io/xpipe/app/update/PortableUpdater.java b/app/src/main/java/io/xpipe/app/update/PortableUpdater.java index 8e5285083..ea2434355 100644 --- a/app/src/main/java/io/xpipe/app/update/PortableUpdater.java +++ b/app/src/main/java/io/xpipe/app/update/PortableUpdater.java @@ -50,7 +50,6 @@ public class PortableUpdater extends UpdateHandler { null, null, Instant.now(), - rel.get().getCreatedAt() != null ? rel.get().getCreatedAt().toInstant() : null, isUpdate)); return lastUpdateCheckResult.getValue(); } diff --git a/app/src/main/java/io/xpipe/app/update/UpdateAvailableAlert.java b/app/src/main/java/io/xpipe/app/update/UpdateAvailableAlert.java index f1383b058..f0b836f3d 100644 --- a/app/src/main/java/io/xpipe/app/update/UpdateAvailableAlert.java +++ b/app/src/main/java/io/xpipe/app/update/UpdateAvailableAlert.java @@ -10,10 +10,6 @@ import javafx.scene.control.ButtonType; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; - public class UpdateAvailableAlert { public static void showIfNeeded() { @@ -26,14 +22,7 @@ public class UpdateAvailableAlert { var update = AppWindowHelper.showBlockingAlert(alert -> { alert.setTitle(AppI18n.get("updateReadyAlertTitle")); alert.setAlertType(Alert.AlertType.NONE); - - var date = u.getReleaseDate() != null ? - DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) - .format(u.getReleaseDate().atZone(ZoneId.systemDefault())) : "Latest"; - var markdown = new MarkdownComp(u.getBody() != null ? u.getBody() : "", s -> { - var header = " 

" + AppI18n.get("whatsNew", u.getVersion(), date) + "

"; - return header + s; - }) + var markdown = new MarkdownComp(u.getBody() != null ? u.getBody() : "", s -> " " + s) .createRegion(); alert.getButtonTypes().clear(); var updaterContent = uh.createInterface(); diff --git a/app/src/main/java/io/xpipe/app/update/UpdateChangelogAlert.java b/app/src/main/java/io/xpipe/app/update/UpdateChangelogAlert.java index 045dc333b..995aba0c9 100644 --- a/app/src/main/java/io/xpipe/app/update/UpdateChangelogAlert.java +++ b/app/src/main/java/io/xpipe/app/update/UpdateChangelogAlert.java @@ -10,10 +10,6 @@ import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonType; import javafx.stage.Modality; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; - public class UpdateChangelogAlert { private static boolean shown = false; @@ -46,13 +42,7 @@ public class UpdateChangelogAlert { alert.setAlertType(Alert.AlertType.NONE); alert.initModality(Modality.NONE); - var date = update.getReleaseDate() != null ? - DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) - .format(update.getReleaseDate().atZone(ZoneId.systemDefault())) : "Latest"; - var markdown = new MarkdownComp(update.getRawDescription(), s -> { - var header = " 

" + AppI18n.get("whatsNew", update.getName(), date) + "

"; - return header + s; - }) + var markdown = new MarkdownComp(update.getRawDescription(), s -> " " + s) .createRegion(); alert.getDialogPane().setContent(markdown); diff --git a/app/src/main/java/io/xpipe/app/update/UpdateHandler.java b/app/src/main/java/io/xpipe/app/update/UpdateHandler.java index 67a85de34..6d7fcf0cb 100644 --- a/app/src/main/java/io/xpipe/app/update/UpdateHandler.java +++ b/app/src/main/java/io/xpipe/app/update/UpdateHandler.java @@ -184,7 +184,6 @@ public abstract class UpdateHandler { XPipeDistributionType.get().getId(), lastUpdateCheckResult.getValue().getVersion(), lastUpdateCheckResult.getValue().getReleaseUrl(), - lastUpdateCheckResult.getValue().getReleaseDate(), null, changelog, lastUpdateCheckResult.getValue().getAssetType()); @@ -223,8 +222,7 @@ public abstract class UpdateHandler { var performedUpdate = new PerformedUpdate( preparedUpdate.getValue().getVersion(), preparedUpdate.getValue().getBody(), - preparedUpdate.getValue().getVersion(), - preparedUpdate.getValue().getReleaseDate()); + preparedUpdate.getValue().getVersion()); AppCache.update("performedUpdate", performedUpdate); executeUpdateOnCloseImpl(); @@ -262,7 +260,6 @@ public abstract class UpdateHandler { String name; String rawDescription; String newVersion; - Instant releaseDate; } @Value @@ -277,7 +274,6 @@ public abstract class UpdateHandler { String downloadUrl; AppInstaller.InstallerAssetType assetType; Instant checkTime; - Instant releaseDate; boolean isUpdate; } @@ -289,7 +285,6 @@ public abstract class UpdateHandler { String sourceDist; String version; String releaseUrl; - Instant releaseDate; Path file; String body; AppInstaller.InstallerAssetType assetType; diff --git a/app/src/main/java/io/xpipe/app/util/HttpHelper.java b/app/src/main/java/io/xpipe/app/util/HttpHelper.java index c0172d75a..1a8fd5371 100644 --- a/app/src/main/java/io/xpipe/app/util/HttpHelper.java +++ b/app/src/main/java/io/xpipe/app/util/HttpHelper.java @@ -3,23 +3,12 @@ package io.xpipe.app.util; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import java.net.URI; import java.net.URL; import java.nio.ByteBuffer; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.function.Consumer; public class HttpHelper { - public static Path downloadFile(String urlS) throws Exception { - var url = URI.create(urlS).toURL(); - var bytes = HttpHelper.executeGet(url, aFloat -> {}); - var downloadFile = Files.createTempFile(null, null); - Files.write(downloadFile, bytes); - return downloadFile; - } - public static byte[] executeGet(URL targetURL, Consumer progress) throws Exception { HttpURLConnection connection = null; @@ -36,7 +25,12 @@ public class HttpHelper { } InputStream is = connection.getInputStream(); - int size = Integer.parseInt(connection.getHeaderField("Content-Length")); + var lengthField = connection.getHeaderField("Content-Length"); + if (lengthField == null) { + return is.readAllBytes(); + } + + int size = Integer.parseInt(lengthField); byte[] line; int bytes = 0;