deployment: Add discord webhook notification

This commit is contained in:
Oliver
2025-04-19 14:49:27 +02:00
parent 993cd17d4f
commit ad4b45004d
11 changed files with 82 additions and 1 deletions

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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";
}
}
}

View File

@@ -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.");
}
}
}

View File

@@ -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);

View File

@@ -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<String> 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<Embed> embeds) {
public record Embed(String title, String description, int color) {
}
}
}