fix: 修复检查版本更新时的问题

This commit is contained in:
2024-03-18 16:49:54 +08:00
parent aab082500a
commit fb75b4d95f
4 changed files with 60 additions and 57 deletions

View File

@@ -14,6 +14,9 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public final class UpdateCheckUtils {
private static final JsonParser JSON_PARSER = new JsonParser();
@@ -21,59 +24,59 @@ public final class UpdateCheckUtils {
private UpdateCheckUtils() {
}
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) {
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) throws IOException {
String version = config.getString("version");
if (version == null) {
return;
}
String checkType = config.getString("CHECK_TYPE", "");
try {
switch (checkType) {
case "GITEA_RELEASES": {
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;
}
String gitToken = config.getString("GIT_TOKEN");
String lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
if (lastRelease == null) {
break;
}
if (lastRelease.compareToIgnoreCase(version) <= 0) {
break;
}
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
break;
}
case "GITLAB_RELEASES": {
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;
}
String gitToken = config.getString("GIT_TOKEN");
int projectID = getGitlabProjectID(baseUrl, gitRepo, gitToken);
if (projectID < 0) {
break;
}
String lastRelease = getGitlabLastRelease(baseUrl, projectID, gitToken);
if (lastRelease == null) {
break;
}
if (lastRelease.compareToIgnoreCase(version) <= 0) {
break;
}
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
break;
}
}
} catch (Exception ignored) {
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;
}
String gitToken = config.getString("GIT_TOKEN");
String lastRelease = null;
switch (checkType) {
case "GITEA_RELEASES": {
lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
break;
}
case "GITLAB_RELEASES": {
int projectID = getGitlabProjectID(baseUrl, gitRepo, gitToken);
if (projectID < 0) {
break;
}
lastRelease = getGitlabLastRelease(baseUrl, projectID, gitToken);
break;
}
}
if (lastRelease == null) {
return;
}
if (compareVersion(version, lastRelease) <= 0) {
return;
}
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
}
public static int compareVersion(@NotNull String version1, String version2) {
List<Integer> collect1 = Arrays.stream(version1.split("[+-]")[0].split("\\.")).map(Integer::parseInt).collect(Collectors.toList());
List<Integer> collect2 = Arrays.stream(version2.split("[+-]")[0].split("\\.")).map(Integer::parseInt).collect(Collectors.toList());
int max = Math.max(collect1.size(), collect2.size());
for (int i = 0; i < max; i++) {
int v1 = i < collect1.size() ? collect1.get(i) : 0;
int v2 = i < collect2.size() ? collect2.get(i) : 0;
if (v1 > v2) {
return 1;
}
if (v1 < v2) {
return -1;
}
}
return 0;
}
@Nullable
@@ -103,12 +106,12 @@ public final class UpdateCheckUtils {
}
public static int getGitlabProjectID(@NotNull String baseUrl, @NotNull String repo, @Nullable String token) throws IOException {
URL url = new URL("https://" + baseUrl + "/api/v4/projects?search=" + repo);
URL url = new URL(baseUrl + "/api/v4/projects?search=" + repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
if (token != null) {
connection.setRequestProperty("PRIVATE-TOKEN", "token " + token);
connection.setRequestProperty("PRIVATE-TOKEN", token);
}
connection.connect();
try (InputStream stream = connection.getInputStream()) {
@@ -130,7 +133,7 @@ public final class UpdateCheckUtils {
connection.setDoInput(true);
connection.setRequestMethod("GET");
if (token != null) {
connection.setRequestProperty("Authorization", "token " + token);
connection.setRequestProperty("PRIVATE-TOKEN", token);
}
connection.connect();
try (InputStream stream = connection.getInputStream()) {