feat: 自动检测 Jenkins 更新
This commit is contained in:
@@ -7,7 +7,7 @@ import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "VulnerableCodeUsages"})
|
||||
public class YamlConfig extends ConfigSection {
|
||||
public static final Yaml YAML_LOADER = new Yaml();
|
||||
|
||||
@@ -22,12 +22,16 @@ public class YamlConfig extends ConfigSection {
|
||||
public static YamlConfig load(@NotNull File file) throws IOException {
|
||||
try (FileInputStream stream = new FileInputStream(file)) {
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
Map<String, Object> load = YAML_LOADER.load(reader);
|
||||
return new YamlConfig(load);
|
||||
return load(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static YamlConfig load(@NotNull Reader reader) {
|
||||
Map<String, Object> load = YAML_LOADER.load(reader);
|
||||
return new YamlConfig(load);
|
||||
}
|
||||
|
||||
public void save(@NotNull File file) throws IOException {
|
||||
try (FileOutputStream stream = new FileOutputStream(file)) {
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
|
||||
|
@@ -0,0 +1,41 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user