Inceptum/common/src/main/java/io/gitlab/jfronny/inceptum/util/UpdateChecker.java

86 lines
4.4 KiB
Java

package io.gitlab.jfronny.inceptum.util;
import io.gitlab.jfronny.commons.ComparableVersion;
import io.gitlab.jfronny.commons.HttpUtils;
import io.gitlab.jfronny.inceptum.model.gitlab.*;
import io.gitlab.jfronny.inceptum.model.inceptum.InceptumVersion;
import io.gitlab.jfronny.inceptum.model.inceptum.UpdateChannel;
import io.gitlab.jfronny.inceptum.model.inceptum.UpdateInfo;
import io.gitlab.jfronny.inceptum.util.api.GitlabApi;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.function.Consumer;
public class UpdateChecker {
private static final long PROJECT_ID = 30862253L;
public static UpdateInfo check(UpdateChannel channel, ComparableVersion current, String flavor, Consumer<UpdateChannel> channelInvalid) {
try {
int jvm = Runtime.version().feature();
if (flavor.equals("custom")) {
Utils.LOGGER.error("Custom build, skipping update check");
return null;
}
GitlabProject project = GitlabApi.getProject(PROJECT_ID);
GitlabPackage experimental = null;
ComparableVersion experimentalVersion = null;
GitlabPackage stable = null;
ComparableVersion stableVersion = null;
packageLoop: for (GitlabPackage info : GitlabApi.getPackages(project)) {
if (info.status.equals("default") && info.name.equals("io/gitlab/jfronny/inceptum/Inceptum")) {
for (GitlabPipeline pipeline : info.pipelines) {
for (GitlabJob job : GitlabApi.getJobs(project, pipeline.id)) {
if (!job.name.equals("build_test")) continue;
try {
InceptumVersion iv = HttpUtils.get(GitlabApi.PROJECTS + project.id + "/jobs/" + job.id + "/artifacts/version.json").sendSerialized(InceptumVersion.class);
if (iv.jvm > jvm) {
Utils.LOGGER.error("A newer JVM is required to use the latest inceptum version. Please update!");
continue packageLoop;
}
}
catch (IOException | URISyntaxException e) {
continue packageLoop;
}
}
if (pipeline.ref.equals("master") && pipeline.status.equals("success")) {
ComparableVersion cvNew = new ComparableVersion(info.version);
if (experimentalVersion == null || experimentalVersion.compareTo(cvNew) < 0) {
experimental = info;
experimentalVersion = cvNew;
}
if (!info.version.contains("-") && (stableVersion == null || stableVersion.compareTo(cvNew) < 0)) {
stable = info;
stableVersion = cvNew;
}
}
}
}
}
if (experimental == null) {
throw new IOException("No version could be found");
}
else if (stable == null && channel == UpdateChannel.Stable) {
channel = UpdateChannel.CI;
channelInvalid.accept(channel);
}
GitlabPackage info = switch (channel) {
case CI -> experimental;
case Stable -> stable;
};
Utils.LOGGER.info("Latest version is " + info.version + ", current is " + current);
if (current.compareTo(new ComparableVersion(info.version)) >= 0) {
Utils.LOGGER.info("Up-to-date");
return null;
}
GitlabPackageFile file = GitlabApi.getFile(project, info, f -> f.file_name.endsWith('-' + flavor + ".jar"));
if (file == null)
Utils.LOGGER.error("No valid package was discovered");
else return new UpdateInfo("https://gitlab.com/" + project.path_with_namespace + "/-/package_files/" + file.id + "/download", file.file_sha1, new ComparableVersion(info.version));
} catch (IOException | URISyntaxException e) {
Utils.LOGGER.error("Could not check for updates", e);
}
return null;
}
}