From ad4b45004da02d2edea27b0ef090ff0f294bbaf2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 19 Apr 2025 14:49:27 +0200 Subject: [PATCH] deployment: Add discord webhook notification --- .../release_deployment_config.json | 1 + .../snapshot_deployment_config.json | 1 + .../release_deployment_config.json | 1 + .../snapshot_deployment_config.json | 1 + .../fancynpcs/release_deployment_config.json | 1 + .../fancynpcs/snapshot_deployment_config.json | 1 + tools/deployment/example-config.json | 1 + .../de/oliver/deployment/Configuration.java | 14 ++++++++ .../main/java/de/oliver/deployment/Main.java | 25 +++++++++++++ .../deployment/modrinth/ModrinthService.java | 2 +- .../notification/DiscordWebhook.java | 35 +++++++++++++++++++ 11 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tools/deployment/src/main/java/de/oliver/deployment/notification/DiscordWebhook.java diff --git a/plugins/fancyholograms-v2/release_deployment_config.json b/plugins/fancyholograms-v2/release_deployment_config.json index 061bc10e..819a10af 100644 --- a/plugins/fancyholograms-v2/release_deployment_config.json +++ b/plugins/fancyholograms-v2/release_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyHolograms", "project_id": "5QNgOj66", "plugin_jar_path": "../../../../plugins/fancyholograms-v2/build/libs/FancyHolograms-%VERSION%.jar", "changelog_path": "../../../../plugins/fancyholograms-v2/CHANGELOG.md", diff --git a/plugins/fancyholograms-v2/snapshot_deployment_config.json b/plugins/fancyholograms-v2/snapshot_deployment_config.json index 2ed80ea1..eff54dd7 100644 --- a/plugins/fancyholograms-v2/snapshot_deployment_config.json +++ b/plugins/fancyholograms-v2/snapshot_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyHolograms", "project_id": "5QNgOj66", "plugin_jar_path": "../../../../plugins/fancyholograms-v2/build/libs/FancyHolograms-%VERSION%.jar", "changelog_path": "../../../../plugins/fancyholograms-v2/CHANGELOG-SNAPSHOT.md", diff --git a/plugins/fancyholograms/release_deployment_config.json b/plugins/fancyholograms/release_deployment_config.json index bb1fad4f..72a7210e 100644 --- a/plugins/fancyholograms/release_deployment_config.json +++ b/plugins/fancyholograms/release_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyHolograms", "project_id": "5QNgOj66", "plugin_jar_path": "../../../../plugins/fancyholograms/build/libs/FancyHolograms-%VERSION%.jar", "changelog_path": "../../../../plugins/fancyholograms/CHANGELOG.md", diff --git a/plugins/fancyholograms/snapshot_deployment_config.json b/plugins/fancyholograms/snapshot_deployment_config.json index d202f40d..09de21c6 100644 --- a/plugins/fancyholograms/snapshot_deployment_config.json +++ b/plugins/fancyholograms/snapshot_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyHolograms", "project_id": "5QNgOj66", "plugin_jar_path": "../../../../plugins/fancyholograms/build/libs/FancyHolograms-%VERSION%.jar", "changelog_path": "../../../../plugins/fancyholograms/CHANGELOG-SNAPSHOT.md", diff --git a/plugins/fancynpcs/release_deployment_config.json b/plugins/fancynpcs/release_deployment_config.json index 0445cc5c..91f44eab 100644 --- a/plugins/fancynpcs/release_deployment_config.json +++ b/plugins/fancynpcs/release_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyNpcs", "project_id": "EeyAn23L", "plugin_jar_path": "../../../../plugins/fancynpcs/build/libs/FancyNpcs-%VERSION%.jar", "changelog_path": "../../../../plugins/fancynpcs/CHANGELOG.md", diff --git a/plugins/fancynpcs/snapshot_deployment_config.json b/plugins/fancynpcs/snapshot_deployment_config.json index 499d2f83..10ef2c61 100644 --- a/plugins/fancynpcs/snapshot_deployment_config.json +++ b/plugins/fancynpcs/snapshot_deployment_config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyNpcs", "project_id": "EeyAn23L", "plugin_jar_path": "../../../../plugins/fancynpcs/build/libs/FancyNpcs-%VERSION%.jar", "changelog_path": "../../../../plugins/fancynpcs/CHANGELOG-SNAPSHOT.md", diff --git a/tools/deployment/example-config.json b/tools/deployment/example-config.json index c5a9a02b..0d2da379 100644 --- a/tools/deployment/example-config.json +++ b/tools/deployment/example-config.json @@ -1,4 +1,5 @@ { + "project_name": "FancyHolograms", "project_id": "fancyholograms", "plugin_jar_path": "../../../plugins/fancyholograms/build/libs/FancyHolograms-*.jar", "changelog_path": "../../../plugins/fancyholograms/CHANGELOG.md", diff --git a/tools/deployment/src/main/java/de/oliver/deployment/Configuration.java b/tools/deployment/src/main/java/de/oliver/deployment/Configuration.java index 63b07f56..9cf7672f 100644 --- a/tools/deployment/src/main/java/de/oliver/deployment/Configuration.java +++ b/tools/deployment/src/main/java/de/oliver/deployment/Configuration.java @@ -2,7 +2,12 @@ package de.oliver.deployment; import com.google.gson.annotations.SerializedName; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + public record Configuration( + @SerializedName("project_name") String projectName, @SerializedName("project_id") String projectID, @SerializedName("plugin_jar_path") String pluginJarPath, @SerializedName("changelog_path") String changeLogPath, @@ -12,4 +17,13 @@ public record Configuration( @SerializedName("loaders") String[] loaders, boolean featured ) { + + public String readVersion() { + try { + return Files.readString(Path.of(versionPath)); + } catch (IOException e) { + return "unknown"; + } + } + } diff --git a/tools/deployment/src/main/java/de/oliver/deployment/Main.java b/tools/deployment/src/main/java/de/oliver/deployment/Main.java index 680101da..e909c610 100644 --- a/tools/deployment/src/main/java/de/oliver/deployment/Main.java +++ b/tools/deployment/src/main/java/de/oliver/deployment/Main.java @@ -1,11 +1,14 @@ package de.oliver.deployment; import com.google.gson.Gson; +import de.oliver.deployment.git.GitService; import de.oliver.deployment.modrinth.ModrinthService; +import de.oliver.deployment.notification.DiscordWebhook; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; public class Main { @@ -24,6 +27,28 @@ public class Main { e.printStackTrace(); } + String discordWebhookUrl = System.getenv("DISCORD_WEBHOOK_URL"); + if (discordWebhookUrl != null) { + DiscordWebhook.Data data = new DiscordWebhook.Data("Deployment completed", List.of( + new DiscordWebhook.Data.Embed( + "New version of "+configuration.projectName(), + """ + **Version:** %s + **Commit:** %s + **Download:** %s + """.formatted( + configuration.readVersion(), + GitService.getCommitHash(), + "https://modrinth.com/plugin/"+configuration.projectName()+"/versions/"+configuration.readVersion()), + 0x00FF00 + ) + )); + DiscordWebhook discordWebhook = new DiscordWebhook(); + discordWebhook.sendWebhook(discordWebhookUrl, data); + } else { + System.out.println("Discord webhook URL not set. Skipping notification."); + } + } } diff --git a/tools/deployment/src/main/java/de/oliver/deployment/modrinth/ModrinthService.java b/tools/deployment/src/main/java/de/oliver/deployment/modrinth/ModrinthService.java index 1b1d10fb..d4b3f597 100644 --- a/tools/deployment/src/main/java/de/oliver/deployment/modrinth/ModrinthService.java +++ b/tools/deployment/src/main/java/de/oliver/deployment/modrinth/ModrinthService.java @@ -36,7 +36,7 @@ public class ModrinthService { changelog = changelog.replaceAll("%COMMIT_HASH%", GitService.getCommitHash()); changelog = changelog.replaceAll("%COMMIT_MESSAGE%", GitService.getCommitMessage()); - String version = Files.readString(Path.of(config.versionPath())); + String version = config.readVersion(); String pluginJarPath = config.pluginJarPath().replace("%VERSION%", version); File pluginFile = new File(pluginJarPath); diff --git a/tools/deployment/src/main/java/de/oliver/deployment/notification/DiscordWebhook.java b/tools/deployment/src/main/java/de/oliver/deployment/notification/DiscordWebhook.java new file mode 100644 index 00000000..4921505f --- /dev/null +++ b/tools/deployment/src/main/java/de/oliver/deployment/notification/DiscordWebhook.java @@ -0,0 +1,35 @@ +package de.oliver.deployment.notification; + +import de.oliver.fancyanalytics.sdk.utils.HttpRequest; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.http.HttpResponse; +import java.util.List; + +public class DiscordWebhook { + + public void sendWebhook(String webhookUrl, Data data) { + HttpRequest request = new HttpRequest(webhookUrl) + .withMethod("POST") + .withHeader("Content-Type", "application/json") + .withBody(data); + + try { + HttpResponse resp = request.send(); + if (resp.statusCode() != 204) { + System.out.println("Failed to send message with discord webhook: " + resp.body()); + } + } catch (URISyntaxException | IOException | InterruptedException e) { + System.out.println("Failed to send webhook"); + e.printStackTrace(); + } + } + + public record Data(String content, List embeds) { + + public record Embed(String title, String description, int color) { + } + } + +}