feat: 完成 gitea 更新检测功能
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class JenkinsUtils {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
private JenkinsUtils() {
|
||||
}
|
||||
|
||||
public static int getLastStableBuild(@NotNull String jobUrl, @Nullable String username, @Nullable String apiToken) throws IOException {
|
||||
URL url = new URL(jobUrl + "api/json");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (username != null && apiToken != null) {
|
||||
String token = username + ":" + apiToken;
|
||||
String base64 = Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8));
|
||||
connection.setRequestProperty("Authorization", "Basic " + base64);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
try (InputStreamReader reader = new InputStreamReader(stream)) {
|
||||
JsonObject object = JSON_PARSER.parse(reader).getAsJsonObject();
|
||||
JsonObject lastStableBuild = object.getAsJsonObject("lastStableBuild");
|
||||
return lastStableBuild.get("number").getAsInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class UpdateCheckUtils {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
private UpdateCheckUtils() {
|
||||
}
|
||||
|
||||
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) {
|
||||
String version = config.getString("version");
|
||||
if (version == null) {
|
||||
return;
|
||||
}
|
||||
String checkType = config.getString("CHECK_TYPE", "");
|
||||
String baseUrl = config.getString("GIT_BASE_URL");
|
||||
String gitRepo = config.getString("GIT_REPO");
|
||||
String downloadUrl = config.getString("DOWNLOAD_URL");
|
||||
if (baseUrl == null || gitRepo == null || downloadUrl == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
switch (checkType) {
|
||||
case "GITEA_RELEASES": {
|
||||
String gitToken = config.getString("GIT_TOKEN");
|
||||
String lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
|
||||
if (lastRelease == null) {
|
||||
break;
|
||||
}
|
||||
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
|
||||
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
|
||||
break;
|
||||
}
|
||||
case "": {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getGiteaLastRelease(@NotNull String baseUrl, @NotNull String repo, @Nullable String token) throws IOException {
|
||||
URL url = new URL(baseUrl + "/api/v1/repos/" + repo + "/releases?limit=10");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (token != null) {
|
||||
connection.setRequestProperty("Authorization", "token " + token);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
JsonArray array = JSON_PARSER.parse(reader).getAsJsonArray();
|
||||
if (array.size() >= 1) {
|
||||
JsonObject object = array.get(0).getAsJsonObject();
|
||||
return object.get("name").getAsString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public interface UpdateReceiver {
|
||||
void sendMessage(@NotNull String message);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user