|
| 1 | +package io.github.fvarrui.javapackager.utils.updater; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import org.apache.commons.io.IOUtils; |
| 6 | + |
| 7 | +import java.net.URL; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.function.Predicate; |
| 13 | + |
| 14 | +public class Github { |
| 15 | + |
| 16 | + /** |
| 17 | + * Searches the latest GitHub release and returns a {@link SearchResult} object with all the relevant information. |
| 18 | + * @param repoName GitHub repository name. |
| 19 | + * @param currentVersion current version of the installed software. |
| 20 | + * @param assetNamePredicate predicate that contains the asset name and ist used to determine the asset to download. |
| 21 | + */ |
| 22 | + public static SearchResult searchUpdate(String repoName, String currentVersion, Predicate<String> assetNamePredicate) { |
| 23 | + Exception exception = null; |
| 24 | + boolean updateAvailable = false; |
| 25 | + String downloadUrl = null; |
| 26 | + String latestVersion = null; |
| 27 | + String downloadFile = null; |
| 28 | + String sha256 = null; |
| 29 | + try { |
| 30 | + JsonObject latestRelease = Json.fromUrlAsObject("https://api.github.com/repos/" + repoName + "/releases/latest"); |
| 31 | + latestVersion = latestRelease.get("tag_name").getAsString(); |
| 32 | + if (latestVersion != null) |
| 33 | + latestVersion = latestVersion.replaceAll("[^0-9.]", ""); // Before passing over remove everything except numbers and dots |
| 34 | + if (new UtilsVersion().isLatestBigger(currentVersion, latestVersion)) { |
| 35 | + updateAvailable = true; |
| 36 | + // Contains JsonObjects sorted by their asset-names lengths, from smallest to longest. |
| 37 | + // The following does that sorting. |
| 38 | + List<JsonObject> sortedArtifactObjects = new ArrayList<>(); |
| 39 | + for (JsonElement e : |
| 40 | + latestRelease.getAsJsonArray("assets")) { |
| 41 | + JsonObject obj = e.getAsJsonObject(); |
| 42 | + String name = obj.get("name").getAsString(); |
| 43 | + if (sortedArtifactObjects.size() == 0) sortedArtifactObjects.add(obj); |
| 44 | + else { |
| 45 | + int finalIndex = 0; |
| 46 | + boolean isSmaller = false; |
| 47 | + for (int i = 0; i < sortedArtifactObjects.size(); i++) { |
| 48 | + String n = sortedArtifactObjects.get(i).get("name").getAsString(); |
| 49 | + if (name.length() < n.length()) { |
| 50 | + isSmaller = true; |
| 51 | + finalIndex = i; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + if (!isSmaller) sortedArtifactObjects.add(obj); |
| 56 | + else sortedArtifactObjects.add(finalIndex, obj); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Find asset-name containing our provided asset-name |
| 61 | + for (JsonObject obj : sortedArtifactObjects) { |
| 62 | + String name = obj.get("name").getAsString(); |
| 63 | + if (assetNamePredicate.test(name)) { |
| 64 | + downloadFile = name; |
| 65 | + downloadUrl = obj.get("browser_download_url").getAsString(); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (downloadUrl == null) { |
| 71 | + List<String> names = new ArrayList<>(); |
| 72 | + for (JsonObject obj : |
| 73 | + sortedArtifactObjects) { |
| 74 | + String n = obj.get("name").getAsString(); |
| 75 | + names.add(n); |
| 76 | + } |
| 77 | + throw new Exception("Failed to find an asset-name matching the assetNamePredicate inside of " + Arrays.toString(names.toArray())); |
| 78 | + } |
| 79 | + |
| 80 | + // Determine sha256 |
| 81 | + String expectedShaAssetName = downloadFile + ".sha256"; |
| 82 | + for (JsonObject obj : sortedArtifactObjects) { |
| 83 | + String name = obj.get("name").getAsString(); |
| 84 | + if (name.equals(expectedShaAssetName)) { |
| 85 | + sha256 = IOUtils.toString(new URL(obj.get("browser_download_url").getAsString()), StandardCharsets.UTF_8); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + } catch (Exception e) { |
| 92 | + exception = e; |
| 93 | + } |
| 94 | + |
| 95 | + return new SearchResult(updateAvailable, exception, latestVersion, downloadUrl, downloadFile, sha256); |
| 96 | + } |
| 97 | +} |
0 commit comments