Add response HTTP status code check to update checker to improve efficiency

This commit is contained in:
Focusvity 2023-03-09 17:16:14 +11:00
parent 37649f1fdc
commit a62bdef5b4
No known key found for this signature in database
GPG key ID: 85AD157561ABE94B

View file

@ -5,16 +5,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -28,6 +18,17 @@ import org.bukkit.command.CommandSender;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
public class UpdateChecker implements PlexBase public class UpdateChecker implements PlexBase
{ {
/* /*
@ -48,7 +49,7 @@ public class UpdateChecker implements PlexBase
{ {
try try
{ {
HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection();
connection.connect(); connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
{ {
@ -164,34 +165,48 @@ public class UpdateChecker implements PlexBase
try try
{ {
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); int statusCode = response.getStatusLine().getStatusCode();
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String jarFile = artifact.getString("fileName"); if (statusCode == HttpURLConnection.HTTP_OK)
sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest JAR file: " + jarFile));
File copyTo;
if (!module)
{ {
copyTo = new File(Bukkit.getUpdateFolderFile(), jarFile); JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String jarFile = artifact.getString("fileName");
sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest JAR file: " + jarFile));
File copyTo;
if (!module)
{
copyTo = new File(Bukkit.getUpdateFolderFile(), jarFile);
}
else
{
copyTo = new File(plugin.getModulesFolder().getPath(), jarFile);
}
CompletableFuture.runAsync(() ->
{
try
{
FileUtils.copyURLToFile(
new URL(url + "/lastSuccessfulBuild/artifact/build/libs/" + jarFile),
copyTo
);
sender.sendMessage(PlexUtils.mmDeserialize("<green>New JAR file downloaded successfully."));
}
catch (IOException e)
{
e.printStackTrace();
}
});
}
else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND)
{
sender.sendMessage(PlexUtils.mmDeserialize("<red>Could not update " + name + " as it can't be found on Jenkins."));
} }
else else
{ {
copyTo = new File(plugin.getModulesFolder().getPath(), jarFile); sender.sendMessage(PlexUtils.mmDeserialize("<red>Something went wrong while trying to update " + name + ". Please check the log for more information."));
PlexLog.error("Unable to update module {0} due to unexpected status code returned from Jenkins - Status Code: {1}", name, statusCode);
} }
CompletableFuture.runAsync(() ->
{
try
{
FileUtils.copyURLToFile(
new URL(url + "/lastSuccessfulBuild/artifact/build/libs/" + jarFile),
copyTo
);
sender.sendMessage(PlexUtils.mmDeserialize("<green>New JAR file downloaded successfully."));
}
catch (IOException e)
{
e.printStackTrace();
}
});
} }
catch (IOException e) catch (IOException e)
{ {
@ -199,7 +214,9 @@ public class UpdateChecker implements PlexBase
} }
catch (JSONException e) catch (JSONException e)
{ {
sender.sendMessage(PlexUtils.mmDeserialize("<red>The file could not be downloaded because Jenkins returned an invalid response. (Is it on Jenkins?)")); sender.sendMessage(PlexUtils.mmDeserialize("<red>Something went wrong while trying to gather information from Jenkins for " + name + ". Please check the log for more information"));
PlexLog.error("Unable to parse JSON information received from Jenkins - see below for more information...");
e.printStackTrace();
} }
} }
} }