From 349ab639da8dc0291d93197beaad4182ba338563 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 26 Jul 2023 22:18:20 +0200 Subject: [PATCH 1/2] Add a report to discord gradle task and added it to ci --- .gitea/workflows/test.yml | 4 +- build.gradle | 14 +++++ buildSrc/build.gradle | 11 ++++ buildSrc/gradle.properties | 1 + buildSrc/settings.gradle | 7 +++ .../main/java/fr/altarik/ReportDiscord.java | 51 +++++++++++++++++++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 buildSrc/build.gradle create mode 100644 buildSrc/gradle.properties create mode 100644 buildSrc/settings.gradle create mode 100644 buildSrc/src/main/java/fr/altarik/ReportDiscord.java diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 1e4fa74..61e0ee7 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -64,5 +64,7 @@ jobs: chmod +x ./gradlew touch local.properties - name: deploy - run: ./gradlew publish + run: | + ./gradlew publish + ./gradlew reportToDiscord diff --git a/build.gradle b/build.gradle index 51d2d7b..2278137 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import fr.altarik.ReportDiscord + plugins { id 'fabric-loom' version '1.2-SNAPSHOT' apply false } @@ -7,6 +9,14 @@ try { local.load(new FileInputStream(rootProject.file("local.properties"))) } catch (IOException ignored) {} +group = project.maven_group +version = project.maven_version + +String webhookId = getEnv("DISCORD_PUB_ID", local.getProperty("discord_pub_id")) +String webhookToken = getEnv("DISCORD_PUB_TOKEN", local.getProperty("discord_pub_token")) +String repoUrl = "https://repo.altarik.fr/#/" + (project.version.endsWith('SNAPSHOT') ? 'snapshots/' : 'releases/') + project.group.replace(".", "/") + "/" + project.rootProject.name + "/" + project.version + +var reportConfig = new ReportDiscord.ReportData("https://discord.com/api/", webhookId, webhookToken, "Update " + project.version + " has been published", repoUrl); allprojects { apply plugin: 'maven-publish' @@ -110,6 +120,10 @@ static def getEnv(String envName, String defaultValue) { } } +tasks.register("reportToDiscord", ReportDiscord) { + config.set(reportConfig) +} + /*jar { dependsOn subprojects.jar subprojects.each { project -> diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 0000000..3a31fad --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,11 @@ +plugins { + id 'java' +} + +dependencies { + implementation "com.squareup.okhttp3:okhttp:${project.okhttp_version}" +} + +repositories { + mavenCentral() +} \ No newline at end of file diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties new file mode 100644 index 0000000..1b7390b --- /dev/null +++ b/buildSrc/gradle.properties @@ -0,0 +1 @@ +okhttp_version=4.10.0 \ No newline at end of file diff --git a/buildSrc/settings.gradle b/buildSrc/settings.gradle new file mode 100644 index 0000000..bc2d8d1 --- /dev/null +++ b/buildSrc/settings.gradle @@ -0,0 +1,7 @@ +pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + } +} +rootProject.name = "buildSrc" \ No newline at end of file diff --git a/buildSrc/src/main/java/fr/altarik/ReportDiscord.java b/buildSrc/src/main/java/fr/altarik/ReportDiscord.java new file mode 100644 index 0000000..c61d69d --- /dev/null +++ b/buildSrc/src/main/java/fr/altarik/ReportDiscord.java @@ -0,0 +1,51 @@ +package fr.altarik; + +import okhttp3.*; +import org.gradle.api.DefaultTask; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.TaskAction; + +import java.io.IOException; + +public abstract class ReportDiscord extends DefaultTask { + + public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + + @Input + public abstract Property getConfig(); + + private final OkHttpClient client = new OkHttpClient(); + + @TaskAction + public void report() throws IOException { + ReportData data = getConfig().get(); + String message = data.message(); + RequestBody body = RequestBody.create(""" + { + "embeds": [ + { + "title": "A new update for Toolbox is available", + "description":\s""" + "\"" + message + "\"" + """ + , + "url":\s""" + "\"" + data.url() + "\"" + """ + } + ] + } + """, JSON); + + String url = data.baseUrl() + "/webhooks/" + data.webhookId + "/" + data.webhookToken; + Request request = new Request.Builder() + .url(url) + .post(body) + .build(); + try(Response response = client.newCall(request).execute()) { + getLogger().info("report sent"); + } + } + + public record ReportData(String baseUrl, String webhookId, String webhookToken, String message, String url) { + + } + +} -- 2.39.5 From 23236ac2dceda794758d434f76415d6099cd3345 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 26 Jul 2023 22:19:37 +0200 Subject: [PATCH 2/2] Dump to 4.3.2-SNAPSHOT --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 72cb25d..eafac93 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,4 +8,4 @@ loader_version=0.14.14 fabric_version=0.75.1+1.19.3 maven_group=fr.altarik.toolbox -maven_version=4.3.1-SNAPSHOT +maven_version=4.3.2-SNAPSHOT -- 2.39.5