feat: 完成 gitea 更新检测功能

This commit is contained in:
2024-03-18 15:03:35 +08:00
parent 57f06c3ce2
commit bcfbaab30c
19 changed files with 203 additions and 218 deletions

View File

@@ -3,14 +3,12 @@ package cn.hamster3.mc.plugin.core.bungee;
import cn.hamster3.mc.plugin.core.bungee.api.CoreBungeeAPI;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
import cn.hamster3.mc.plugin.core.common.util.JenkinsUtils;
import cn.hamster3.mc.plugin.core.common.util.UpdateCheckUtils;
import lombok.Getter;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
@@ -41,11 +39,11 @@ public class HamsterCorePlugin extends Plugin {
}
File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
Files.copy(
getResourceAsStream("config.yml"),
configFile.toPath(),
StandardCopyOption.REPLACE_EXISTING
);
try (InputStream stream = getResourceAsStream("config.yml")) {
if (stream != null) {
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
CoreBungeeAPI.init(configFile);
logger.info("已初始化 CoreAPI");
@@ -66,7 +64,24 @@ public class HamsterCorePlugin extends Plugin {
logger.info("已创建 AudienceProvider");
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
showUpdate(ProxyServer.getInstance().getConsole());
CoreAPI.getInstance().getExecutorService().submit(() -> {
for (Plugin plugin : ProxyServer.getInstance().getPluginManager().getPlugins()) {
try (InputStream stream = plugin.getResourceAsStream("update.yml")) {
if (stream == null) {
continue;
}
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
YamlConfig config = YamlConfig.load(reader);
UpdateCheckUtils.showUpdate(
plugin.getDescription().getName(), config,
(s) -> ProxyServer.getInstance().getConsole().sendMessage(TextComponent.fromLegacyText(s))
);
}
} catch (IOException ignored) {
}
}
});
}
@Override
@@ -84,37 +99,4 @@ public class HamsterCorePlugin extends Plugin {
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms");
}
private void showUpdate(@NotNull CommandSender sender) {
ProxyServer.getInstance().getScheduler().runAsync(HamsterCorePlugin.getInstance(), () -> {
for (Plugin plugin : ProxyServer.getInstance().getPluginManager().getPlugins()) {
InputStream resource = plugin.getResourceAsStream("jenkins.yml");
if (resource == null) {
continue;
}
try (InputStreamReader reader = new InputStreamReader(resource, StandardCharsets.UTF_8)) {
YamlConfig jenkinsConfig = YamlConfig.load(reader);
String jobUrl = jenkinsConfig.getString("JOB_URL");
if (jobUrl == null || jobUrl.equalsIgnoreCase("DEV")) {
continue;
}
String buildNumberString = jenkinsConfig.getString("BUILD_NUMBER");
if (buildNumberString == null || buildNumberString.equalsIgnoreCase("DEV")) {
continue;
}
int lastStableBuild = JenkinsUtils.getLastStableBuild(jobUrl, null, null);
int buildNumber = Integer.parseInt(buildNumberString);
int version = lastStableBuild - buildNumber;
if (version <= 0) {
continue;
}
String pluginName = plugin.getDescription().getName();
sender.sendMessage(TextComponent.fromLegacyText(String.format(
"§a检测到插件 %s 落后 %d 个版本更新, 下载链接: §n§l%s", pluginName, version, jobUrl
)));
} catch (IOException ignored) {
}
}
});
}
}

View File

@@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@@ -66,11 +67,11 @@ public final class CoreBungeeCordUtils {
}
File configFile = new File(plugin.getDataFolder(), filename);
try {
Files.copy(
plugin.getResourceAsStream(filename),
configFile.toPath(),
StandardCopyOption.REPLACE_EXISTING
);
try (InputStream stream = plugin.getResourceAsStream(filename)) {
if (stream != null) {
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
} catch (Exception e) {
throw new RuntimeException(e);

View File

@@ -1,6 +0,0 @@
BUILD_ID: ${BUILD_ID}
BUILD_NUMBER: ${BUILD_NUMBER}
BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}
JOB_URL: ${JOB_URL}
BUILD_URL: ${BUILD_URL}
GIT_COMMIT: ${GIT_COMMIT}

View File

@@ -0,0 +1,6 @@
version: ${version}
CHECK_TYPE: GITEA_RELEASES
GIT_BASE_URL: https://git.airgame.net
GIT_REPO: MiniDay/hamster-core
GIT_TOKEN: a44a69a4d1b8601bf6091403247759cd28764d5e
DOWNLOAD_URL: https://jenkins.airgame.net/job/opensource/job/hamster-core/