Finish update system

This commit is contained in:
libraryaddict
2020-04-27 16:56:48 +12:00
parent b247a49a23
commit 7090ea51cc
6 changed files with 165 additions and 63 deletions

View File

@@ -50,6 +50,7 @@ public class LDGithub {
public DisguiseUpdate getLatestRelease() {
try {
DisguiseUtilities.getLogger().info("Now looking for update on Github..");
// We're connecting to md_5's jenkins REST api
URL url = new URL("https://api.github.com/repos/libraryaddict/LibsDisguises/releases/latest");
// Creating a connection

View File

@@ -3,9 +3,8 @@ package me.libraryaddict.disguise.utilities.updates;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.plugin.PluginInformation;
import org.bukkit.ChatColor;
import java.io.BufferedReader;
import java.io.InputStream;
@@ -14,8 +13,8 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -48,8 +47,9 @@ public class LDJenkins {
*/
private Map<String, Object> fetchLastSnapshotBuild() {
try {
DisguiseUtilities.getLogger().info("Now looking for update on Jenkins..");
// We're connecting to md_5's jenkins REST api
URL url = new URL("https://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/api/json");
URL url = new URL("https://ci.md-5.net/job/LibsDisguises/api/json?tree=builds[changeSet[items[msg]],id,result]");
// Creating a connection
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDefaultUseCaches(false);
@@ -77,28 +77,50 @@ public class LDJenkins {
public DisguiseUpdate getLatestSnapshot() {
Map<String, Object> lastBuild = fetchLastSnapshotBuild();
if (lastBuild == null || !lastBuild.containsKey("id") || !lastBuild.containsKey("timestamp")) {
if (lastBuild == null || !lastBuild.containsKey("builds")) {
return null;
}
ArrayList<String> changelog = new ArrayList<>();
String version = null;
if (lastBuild.get("changeSet") instanceof Map) {
Object items = ((Map) lastBuild.get("changeSet")).get("items");
for (Map map : (List<Map>) lastBuild.get("builds")) {
String result = (String) map.get("result");
if (items instanceof Map[]) {
for (Map item : (Map[]) items) {
if (!"SUCCESS".equalsIgnoreCase(result)) {
continue;
}
if (changelog.isEmpty()) {
version = (String) map.get("id");
}
Object items = ((Map) map.get("changeSet")).get("items");
boolean release = false;
if (items instanceof List) {
for (Map item : (List<Map>) items) {
String msg = (String) item.get("msg");
if (msg == null) {
continue;
}
changelog.add(msg);
changelog.add("#" + map.get("id") + ": " + ChatColor.YELLOW + msg);
release = release || msg.toLowerCase().matches("release.? .*");
}
}
if (release) {
break;
}
}
return new JenkinsUpdate((String) lastBuild.get("id"), changelog.toArray(new String[0]));
if (changelog.isEmpty()) {
return null;
}
return new JenkinsUpdate(version, changelog.toArray(new String[0]));
}
}

View File

@@ -11,7 +11,6 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.io.BufferedReader;
import java.io.File;
@@ -42,10 +41,51 @@ public class UpdateChecker {
this.resourceID = resourceID;
}
public boolean isServerLatestVersion() {
return isOnLatestUpdate(false);
}
public boolean isOnLatestUpdate(boolean includeDownloaded) {
if (getUpdate() == null) {
return false;
}
boolean isRelease =
includeDownloaded && getLastDownload() != null ? !getLastDownload().getVersion().contains("-SNAPSHOT") :
LibsDisguises.getInstance().isReleaseBuild();
if (getUpdate().isReleaseBuild() != isRelease) {
return false;
}
String version;
if (getUpdate().isReleaseBuild()) {
if (lastDownload != null && includeDownloaded) {
version = lastDownload.getVersion();
} else {
version = LibsDisguises.getInstance().getDescription().getVersion();
}
} else {
if (lastDownload != null && includeDownloaded) {
version = lastDownload.getBuildNumber();
} else {
version = LibsDisguises.getInstance().getBuildNo();
}
}
return getUpdate() != null && getUpdate().getVersion().equals(version);
}
public boolean isDownloading() {
return downloading.get();
}
public boolean isOldUpdate() {
return getUpdate() == null ||
getUpdate().getFetched().before(new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1)));
}
public boolean isUsingReleaseBuilds() {
DisguiseConfig.UpdatesBranch builds = DisguiseConfig.getUpdatesBranch();
@@ -71,30 +111,6 @@ public class UpdateChecker {
}
}
public boolean isUpdateReady() {
if (getUpdate() == null) {
return false;
}
String version;
if (getUpdate().isReleaseBuild()) {
if (lastDownload != null) {
version = lastDownload.getVersion();
} else {
version = LibsDisguises.getInstance().getDescription().getVersion();
}
} else {
if (lastDownload != null) {
version = lastDownload.getBuildNumber();
} else {
version = LibsDisguises.getInstance().getBuildNo();
}
}
return getUpdate() != null && !getUpdate().getVersion().equals(version);
}
public void doAutoUpdateCheck() {
try {
DisguiseUpdate oldUpdate = getUpdate();
@@ -103,7 +119,8 @@ public class UpdateChecker {
doUpdateCheck();
if (!isUpdateReady() || (oldUpdate != null && oldUpdate.getVersion().equals(getUpdate().getVersion()))) {
if (isOnLatestUpdate(true) ||
(oldUpdate != null && oldUpdate.getVersion().equals(getUpdate().getVersion()))) {
return;
}
@@ -164,11 +181,13 @@ public class UpdateChecker {
if (getUpdate().isReleaseBuild()) {
String currentVersion = LibsDisguises.getInstance().getDescription().getVersion();
if (!isNewerVersion(currentVersion, getUpdate().getVersion())) {
if (LibsDisguises.getInstance().isReleaseBuild() &&
!isNewerVersion(currentVersion, getUpdate().getVersion())) {
return LibsMsg.UPDATE_ON_LATEST;
}
updateMessage = new String[]{LibsMsg.UPDATE_READY.get(currentVersion, getUpdate().getVersion())};
updateMessage = new String[]{LibsMsg.UPDATE_READY.get(currentVersion, getUpdate().getVersion()),
LibsMsg.UPDATE_HOW.get()};
} else {
if (!getUpdate().getVersion().matches("[0-9]+")) {
return LibsMsg.UPDATE_FAILED;
@@ -180,8 +199,11 @@ public class UpdateChecker {
return LibsMsg.UPDATE_ON_LATEST;
}
String build = LibsDisguises.getInstance().getBuildNo();
updateMessage = new String[]{
LibsMsg.UPDATE_READY_SNAPSHOT.get(LibsDisguises.getInstance().getBuildNo(), newBuild)};
LibsMsg.UPDATE_READY_SNAPSHOT.get((build.matches("[0-9]+") ? "#" : "") + build, newBuild),
LibsMsg.UPDATE_HOW.get()};
}
return null;