From cf860b3916f55454728c451ad6d7424630982ace Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 27 Oct 2022 15:38:59 +0200 Subject: [PATCH] Improve software updater --- .../openrocket/communication/ReleaseInfo.java | 19 ++++++++++--------- .../communication/UpdateInfoRetriever.java | 12 +++++++----- .../gui/dialogs/UpdateInfoDialog.java | 2 -- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/core/src/net/sf/openrocket/communication/ReleaseInfo.java b/core/src/net/sf/openrocket/communication/ReleaseInfo.java index 8d4134a8c..9f9233512 100644 --- a/core/src/net/sf/openrocket/communication/ReleaseInfo.java +++ b/core/src/net/sf/openrocket/communication/ReleaseInfo.java @@ -1,5 +1,6 @@ package net.sf.openrocket.communication; +import com.sun.istack.NotNull; import net.sf.openrocket.util.ArrayList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -7,6 +8,7 @@ import org.slf4j.LoggerFactory; import javax.json.JsonArray; import javax.json.JsonObject; import java.util.List; +import java.util.Objects; /** * Class containing info about a GitHub release. All the info is stored in a JSON objects, retrieved using the GitHub @@ -21,8 +23,8 @@ public class ReleaseInfo { private static final Logger log = LoggerFactory.getLogger(ReleaseInfo.class); - public ReleaseInfo(JsonObject obj) { - this.obj = obj; + public ReleaseInfo(@NotNull JsonObject obj) { + this.obj = Objects.requireNonNull(obj, "JsonObject cannot be null"); } /** @@ -30,8 +32,6 @@ public class ReleaseInfo { * @return release name (e.g. "15.0.3") */ public String getReleaseName() { - if (this.obj == null) return null; - String name = this.obj.get("tag_name").toString(); // Release label is encapsulated in the 'tag_name'-tag name = name.replaceAll("^\"+|\"+$", ""); // Remove double quotations in the beginning and end @@ -50,8 +50,9 @@ public class ReleaseInfo { * @return release notes (this is the text that explains a certain GitHub release) */ public String getReleaseNotes() { - if (this.obj == null) return null; - return this.obj.get("body").toString(); + String releaseNotes = this.obj.get("body").toString(); + releaseNotes = releaseNotes.replaceAll("^\"+|\"+$", ""); // Remove double quotations in the beginning and end + return releaseNotes; } /** @@ -59,8 +60,9 @@ public class ReleaseInfo { * @return release URL (e.g. 'https://github.com/openrocket/openrocket/releases/tag/release-15.03') */ public String getReleaseURL() { - if (this.obj == null) return null; - return this.obj.get("html_url").toString(); + String releaseURL = this.obj.get("html_url").toString(); + releaseURL = releaseURL.replaceAll("^\"+|\"+$", ""); // Remove double quotations in the beginning and end + return releaseURL; } /** @@ -68,7 +70,6 @@ public class ReleaseInfo { * @return list of asset download URLs (e.g. 'https://github.com/openrocket/openrocket/releases/download/release-15.03/OpenRocket-15.03-installer.exe') */ public List getAssetURLs() { - if (this.obj == null) return null; List assetURLs = new ArrayList<>(); JsonArray assets = this.obj.getJsonArray("assets"); diff --git a/core/src/net/sf/openrocket/communication/UpdateInfoRetriever.java b/core/src/net/sf/openrocket/communication/UpdateInfoRetriever.java index 8edd7c481..692df1834 100644 --- a/core/src/net/sf/openrocket/communication/UpdateInfoRetriever.java +++ b/core/src/net/sf/openrocket/communication/UpdateInfoRetriever.java @@ -282,10 +282,12 @@ public class UpdateInfoRetriever { * This function extracts all the release names that start with the specified preTag. * If preTag is null, the default release names without a pre-tag, starting with a number, are returned (e.g. '15.03'). * @param names list of release names to filter - * @param preTag pre-tag to filter the names on. If null, no special preTag filtering is applied + * @param preTag pre-tag to filter the names on. If null, return all tags that start with a number * @return list of names starting with the preTag */ - public List filterReleasePreTag(List names, String preTag) { + public static List filterReleasePreTag(List names, String preTag) { + if (names == null) return null; + List filteredTags = new LinkedList<>(); // Filter out the names that are not related to the preTag @@ -317,7 +319,7 @@ public class UpdateInfoRetriever { * @param tags filter tags * @return list of release names containing the filter tag */ - public List filterReleaseTags(List names, String[] tags) { + public static List filterReleaseTags(List names, String[] tags) { if (names == null) return null; if (tags == null) return names; return names.stream().filter(c -> Arrays.stream(tags) @@ -330,7 +332,7 @@ public class UpdateInfoRetriever { * @param names list of release names to filter * @return list of release names that do not contain a devTag */ - public List filterOfficialRelease(List names) { + public static List filterOfficialRelease(List names) { if (names == null) return null; return names.stream().filter(c -> Arrays.stream(devTags.keySet().toArray(new String[0])) .noneMatch(c::contains)).collect(Collectors.toList()); @@ -346,7 +348,7 @@ public class UpdateInfoRetriever { * @param onlyOfficial bool to check whether to only include official (non-test) releases * @return latest JSON GitHub release object */ - public JsonObject getLatestReleaseJSON(JsonArray jsonArr, String preTag, String[] tags, boolean onlyOfficial) throws UpdateCheckerException { + public static JsonObject getLatestReleaseJSON(JsonArray jsonArr, String preTag, String[] tags, boolean onlyOfficial) throws UpdateCheckerException { if (jsonArr == null) return null; JsonObject latestObj = null; diff --git a/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java index 3a8cc9bcf..e00f96152 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java @@ -84,12 +84,10 @@ public class UpdateInfoDialog extends JDialog { // Release notes String releaseNotes = release.getReleaseNotes(); - releaseNotes = releaseNotes.replaceAll("^\"|\"$", ""); // Remove leading and trailing quotations sb.append(MarkdownUtil.toHtml(releaseNotes)).append("

"); // GitHub link String releaseURL = release.getReleaseURL(); - releaseURL = releaseURL.replaceAll("^\"|\"$", ""); // Remove leading and trailing quotations sb.append(String.format("%s", releaseURL, trans.get("update.dlg.updateAvailable.txtPane.readMore"))); sb.append(""); textPane.addHyperlinkListener(new HyperlinkListener() {