From 8ad55045273c79b3dad2e7cd88f46af8806fb93b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 22 Dec 2016 16:41:50 +0100 Subject: [PATCH] Improved UpdatePlugin - Added branch checking - Added IsTestServer() - Restricted production updating to the master branch --- .../java/buttondevteam/core/MainPlugin.java | 3 + .../java/buttondevteam/lib/TBMCCoreAPI.java | 115 +++++++++++------- 2 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index 2b09a02..e3f5f06 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -16,6 +16,7 @@ import net.milkbowl.vault.permission.Permission; public class MainPlugin extends JavaPlugin { public static MainPlugin Instance; public static Permission permission; + public static boolean Test; private PluginDescriptionFile pdfFile; private Logger logger; @@ -27,6 +28,8 @@ public class MainPlugin extends JavaPlugin { pdfFile = getDescription(); logger = getLogger(); setupPermissions(); + Test = getConfig().getBoolean("test", true); + saveConfig(); TBMCChatAPI.AddCommand(this, UpdatePluginCommand.class); TBMCChatAPI.AddCommand(this, ScheduledRestartCommand.class); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index c3a1b05..2f7f73f 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -1,17 +1,10 @@ package buttondevteam.lib; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; +import java.io.*; +import java.net.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; import java.util.Map.Entry; import org.apache.commons.io.FileUtils; @@ -22,21 +15,22 @@ import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import com.google.gson.*; + +import buttondevteam.core.MainPlugin; public final class TBMCCoreAPI { static List coders = new ArrayList() { private static final long serialVersionUID = -4462159250738367334L; - { - add("Alisolarflare"); - add("NorbiPeti"); - add("iie"); - add("thewindmillman"); - add("mayskam1995"); - }}; + { + add("Alisolarflare"); + add("NorbiPeti"); + add("iie"); + add("thewindmillman"); + add("mayskam1995"); + } + }; + /** * Updates or installs the specified plugin. The plugin must use Maven. * @@ -73,32 +67,43 @@ public final class TBMCCoreAPI { error(sender, "Can't find plugin: " + name); return; } - info(sender, "Updating TBMC plugin: " + correctname + " from " + branch); + info(sender, "Checking branch name..."); + if (TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) { + error(sender, "The server is in production mode, updating only allowed from master!"); + return; + } + Optional correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch)) + .findAny(); + if (!correctbranch.isPresent()) { + error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\""); + return; + } + info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get()); URL url; File result = new File("plugins/" + correctname + ".jar_tmp"); File finalresult = new File("plugins/" + correctname + ".jar"); try { url = new URL("https://jitpack.io/com/github/TBMCPlugins/" - + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/" + branch - + "-SNAPSHOT/" + correctname + "-" + branch + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well + + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/" + + correctbranch.get() + "-SNAPSHOT/" + correctname + "-" + correctbranch.get() + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well FileUtils.copyURLToFile(url, result); if (!result.exists() || result.length() < 25) { result.delete(); - error(sender, "The downloaded JAR for " + correctname + " from " + branch + error(sender, "The downloaded JAR for " + correctname + " from " + correctbranch.get() + " is too small (smnaller than 25 bytes). Am I downloading from the right place?"); return; } else { finalresult.delete(); // Attempt to support Windows Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING); - info(sender, "Updating plugin " + correctname + " from " + branch + " done!"); + info(sender, "Updating plugin " + correctname + " from " + correctbranch.get() + " done!"); } } catch (FileNotFoundException e) { error(sender, - "Can't find JAR for " + correctname + " from " + branch - + ", the build probably failed. Build log (scroll to bottom):" + "\n" - + "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + branch - + "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/" - + correctname + "\nAnd delete the newest build."); + "Can't find JAR for " + correctname + " from " + correctbranch.get() + + ", the build probably failed. Build log (scroll to bottom):" + "\n" + + "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + correctbranch.get() + + "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/" + + correctname + "\nAnd delete the newest build."); } catch (IOException e) { error(sender, "IO error while updating " + correctname + "\n" + e.getMessage()); } catch (Exception e) { @@ -139,6 +144,26 @@ public final class TBMCCoreAPI { return ret; } + /** + * Retrieves all the branches from the plugin repository. + * + * @return A list of names + */ + public static List GetPluginBranches(String plugin) { + List ret = new ArrayList<>(); + try { + String resp = DownloadString("https://api.github.com/repos/TBMCPlugins/" + plugin + "/branches"); + JsonArray arr = new JsonParser().parse(resp).getAsJsonArray(); + for (JsonElement obj : arr) { + JsonObject jobj = obj.getAsJsonObject(); + ret.add(jobj.get("name").getAsString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return ret; + } + public static String DownloadString(String urlstr) throws MalformedURLException, IOException { URL url = new URL(urlstr); URLConnection con = url.openConnection(); @@ -154,7 +179,6 @@ public final class TBMCCoreAPI { private static HashMap exceptionsToSend = new HashMap<>(); private static List debugMessagesToSend = new ArrayList<>(); - /** * Send exception to the {@link TBMCExceptionEvent}. * @@ -163,9 +187,10 @@ public final class TBMCCoreAPI { * @param e * The exception to send */ - public static void SendException(String sourcemsg, Throwable e){ + public static void SendException(String sourcemsg, Throwable e) { SendException(sourcemsg, e, false); } + public static void SendException(String sourcemsg, Throwable e, boolean debugPotato) { SendUnsentExceptions(); TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e); @@ -174,25 +199,25 @@ public final class TBMCCoreAPI { exceptionsToSend.put(sourcemsg, e); Bukkit.getLogger().warning(sourcemsg); e.printStackTrace(); - if (debugPotato){ + if (debugPotato) { List devsOnline = new ArrayList(); - for (Player player : Bukkit.getOnlinePlayers()){ - if (coders.contains(player.getName())){ + for (Player player : Bukkit.getOnlinePlayers()) { + if (coders.contains(player.getName())) { devsOnline.add(player); } - }; + } + ; if (!devsOnline.isEmpty()) { DebugPotato potato = new DebugPotato() .setMessage(new String[] { // "§b§o" + e.getClass().getSimpleName(), // "§c§o" + sourcemsg, // - "§a§oFind a dev to fix this issue" }) + "§a§oFind a dev to fix this issue" }) .setType(e instanceof IOException ? "Throwable Potato" - : e instanceof ClassCastException ? "Squished Potato" - : e instanceof NullPointerException ? "Plain Potato" - : e instanceof StackOverflowError ? "Chips" - : "Error Potato"); - for (Player dev : devsOnline){ + : e instanceof ClassCastException ? "Squished Potato" + : e instanceof NullPointerException ? "Plain Potato" + : e instanceof StackOverflowError ? "Chips" : "Error Potato"); + for (Player dev : devsOnline) { potato.Send(dev); } } @@ -247,4 +272,8 @@ public final class TBMCCoreAPI { debugMessagesToSend.remove(message); } } + + public static boolean IsTestServer() { + return MainPlugin.Test; + } } \ No newline at end of file