From 202a30aae67196fc797600b4f57544b0265b87e4 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Sat, 27 May 2023 13:40:58 +0200 Subject: [PATCH] Fixed GitHub Action run numbers and updated download url --- build-logic/src/main/kotlin/extensions.kt | 10 ++++-- .../command/main/VersionSubcommand.java | 36 ++++++++++++------- .../geysermc/floodgate/news/NewsChecker.java | 4 +-- .../geysermc/floodgate/util/Constants.java | 8 +++-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/build-logic/src/main/kotlin/extensions.kt b/build-logic/src/main/kotlin/extensions.kt index cbb5e8a8..bbf7531c 100644 --- a/build-logic/src/main/kotlin/extensions.kt +++ b/build-logic/src/main/kotlin/extensions.kt @@ -40,7 +40,7 @@ fun Project.lastCommitHash(): String? = the().commit()?.name?.substring(0, 7) fun Project.branchName(): String = - the().branchName() ?: System.getenv("BRANCH_NAME") ?: "local/dev" + the().branchName() ?: jenkinsBranchName() ?: "local/dev" fun Project.shouldAddBranchName(): Boolean = System.getenv("IGNORE_BRANCH")?.toBoolean() ?: (branchName() !in arrayOf("master", "local/dev")) @@ -49,7 +49,7 @@ fun Project.versionWithBranchName(): String = branchName().replace(Regex("[^0-9A-Za-z-_]"), "-") + '-' + version fun buildNumber(): Int = - System.getenv("BUILD_NUMBER")?.let { Integer.parseInt(it) } ?: -1 + (System.getenv("GITHUB_RUN_NUMBER") ?: jenkinsBuildNumber())?.let { Integer.parseInt(it) } ?: -1 fun buildNumberAsString(): String = buildNumber().takeIf { it != -1 }?.toString() ?: "??" @@ -78,4 +78,8 @@ fun Project.relocate(pattern: String) = .add(pattern) private fun calcExclusion(section: String, bit: Int, excludedOn: Int): String = - if (excludedOn and bit > 0) section else "" \ No newline at end of file + if (excludedOn and bit > 0) section else "" + +// todo remove these when we're not using Jenkins anymore +private fun jenkinsBranchName(): String? = System.getenv("BRANCH_NAME") +private fun jenkinsBuildNumber(): String? = System.getenv("BUILD_NUMBER") \ No newline at end of file diff --git a/core/src/main/java/org/geysermc/floodgate/command/main/VersionSubcommand.java b/core/src/main/java/org/geysermc/floodgate/command/main/VersionSubcommand.java index ea985778..985c47c1 100644 --- a/core/src/main/java/org/geysermc/floodgate/command/main/VersionSubcommand.java +++ b/core/src/main/java/org/geysermc/floodgate/command/main/VersionSubcommand.java @@ -28,8 +28,9 @@ package org.geysermc.floodgate.command.main; import static org.geysermc.floodgate.util.Constants.COLOR_CHAR; import cloud.commandframework.context.CommandContext; -import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.inject.Inject; +import com.google.inject.name.Named; import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.command.WhitelistCommand.Message; import org.geysermc.floodgate.command.util.Permission; @@ -45,6 +46,10 @@ public class VersionSubcommand extends FloodgateSubCommand { @Inject private FloodgateLogger logger; + @Inject + @Named("implementationName") + private String implementationName; + @Override public String name() { return "version"; @@ -70,14 +75,19 @@ public class VersionSubcommand extends FloodgateSubCommand { Constants.VERSION, Constants.GIT_BRANCH )); - String baseUrl = String.format( - "https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/%s/lastSuccessfulBuild/", - Constants.GIT_BRANCH - ); + //noinspection ConstantValue + if (!Constants.GIT_MAIN_BRANCH.equals(Constants.GIT_BRANCH)) { + sender.sendMessage(String.format( + COLOR_CHAR + "7Detected that you aren't on the %s branch, " + + "so we can't fetch the latest version.", + Constants.GIT_MAIN_BRANCH + )); + return; + } httpClient.asyncGet( - baseUrl + "buildNumber", - JsonElement.class + String.format(Constants.LATEST_VERSION_URL, Constants.PROJECT_NAME), + JsonObject.class ).whenComplete((result, error) -> { if (error != null) { sender.sendMessage(COLOR_CHAR + "cCould not retrieve latest version info!"); @@ -85,10 +95,9 @@ public class VersionSubcommand extends FloodgateSubCommand { return; } - JsonElement response = result.getResponse(); + JsonObject response = result.getResponse(); - logger.info(String.valueOf(response)); - logger.info("{}", result.getHttpCode()); + logger.debug("code: {}, response: ", result.getHttpCode(), String.valueOf(response)); if (result.getHttpCode() == 404) { sender.sendMessage( @@ -102,20 +111,21 @@ public class VersionSubcommand extends FloodgateSubCommand { //todo make it more generic instead of using a Whitelist command strings logger.error( "Got an error from requesting the latest Floodgate version: {}", - response.toString() + String.valueOf(response) ); sender.sendMessage(Message.UNEXPECTED_ERROR); return; } - int buildNumber = response.getAsInt(); + int buildNumber = response.get("build").getAsInt(); if (buildNumber > Constants.BUILD_NUMBER) { sender.sendMessage(String.format( COLOR_CHAR + "7There is a newer version of Floodgate available!\n" + COLOR_CHAR + "7You are " + COLOR_CHAR + "e%s " + COLOR_CHAR + "7builds behind.\n" + COLOR_CHAR + "7Download the latest Floodgate version here: " + COLOR_CHAR + "b%s", - buildNumber - Constants.BUILD_NUMBER, baseUrl + buildNumber - Constants.BUILD_NUMBER, + String.format(Constants.LATEST_DOWNLOAD_URL, implementationName) )); return; } diff --git a/core/src/main/java/org/geysermc/floodgate/news/NewsChecker.java b/core/src/main/java/org/geysermc/floodgate/news/NewsChecker.java index 40358247..71f9d667 100644 --- a/core/src/main/java/org/geysermc/floodgate/news/NewsChecker.java +++ b/core/src/main/java/org/geysermc/floodgate/news/NewsChecker.java @@ -81,7 +81,7 @@ public class NewsChecker { private void checkNews() { HttpResponse response = httpClient.getSilent( - Constants.NEWS_OVERVIEW_URL + Constants.NEWS_PROJECT_NAME, + Constants.NEWS_OVERVIEW_URL + Constants.PROJECT_NAME, JsonArray.class ); @@ -173,7 +173,7 @@ public class NewsChecker { switch (item.getType()) { case ANNOUNCEMENT: - if (!item.getDataAs(AnnouncementData.class).isAffected(Constants.NEWS_PROJECT_NAME)) { + if (!item.getDataAs(AnnouncementData.class).isAffected(Constants.PROJECT_NAME)) { return; } break; diff --git a/core/src/main/templates/org/geysermc/floodgate/util/Constants.java b/core/src/main/templates/org/geysermc/floodgate/util/Constants.java index b79a610f..62cec467 100644 --- a/core/src/main/templates/org/geysermc/floodgate/util/Constants.java +++ b/core/src/main/templates/org/geysermc/floodgate/util/Constants.java @@ -29,6 +29,7 @@ public final class Constants { public static final String VERSION = "@floodgateVersion@"; public static final int BUILD_NUMBER = Integer.parseInt("@buildNumber@"); public static final String GIT_BRANCH = "@branch@"; + public static final String GIT_MAIN_BRANCH = "master"; public static final int METRICS_ID = 14649; public static final char COLOR_CHAR = '\u00A7'; @@ -48,9 +49,12 @@ public final class Constants { public static final String NEWS_OVERVIEW_URL = "http" + API_BASE_URL + "/v2/news/"; public static final String GET_BEDROCK_LINK = "http" + API_BASE_URL + "/v2/link/bedrock/"; + public static final String PROJECT_NAME = "floodgate"; public static final String LINK_INFO_URL = "https://link.geysermc.org/"; - - public static final String NEWS_PROJECT_NAME = "floodgate"; + public static final String LATEST_DOWNLOAD_URL = + "https://geysermc.org/download#%s"; + public static final String LATEST_VERSION_URL = + "https://download.geysermc.org/v2/projects/%s/versions/latest/builds/latest"; public static final String NTP_SERVER = "time.cloudflare.com";