Skip to content

Commit d2eeca9

Browse files
committed
build: include git-describe version as part of the bot in /ping command
It is uncertain on which version the bot is running in production, and those with a lack of access to the production infrastructure have almost no way to get such information. Expand the "/ping" slash command to actually mention which version it is running as well as the date on which it was built, more specfifically it now says: > Pong! Running on version v1.2.3-g1a2b3c (2026-02-28) That way we don't have to approach the lucky few folks with access and badger them with primitive questions. :) Signed-off-by: Chris Sdogkos <work@chris-sdogkos.com>
1 parent 1070f23 commit d2eeca9

5 files changed

Lines changed: 60 additions & 3 deletions

File tree

application/build.gradle

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,23 @@ application {
9393
mainClass = 'org.togetherjava.tjbot.Application'
9494
applicationDefaultJvmArgs = ["--enable-native-access=ALL-UNNAMED"]
9595
}
96+
97+
def generatedBuildPropertiesDir = file("build/resources/main/")
98+
tasks.register('generateBuildProperties') {
99+
def outputFile = new File(generatedBuildPropertiesDir, "build.properties")
100+
101+
doLast {
102+
if (!generatedBuildPropertiesDir.exists()) {
103+
generatedBuildPropertiesDir.mkdirs()
104+
}
105+
def props = new Properties()
106+
props['app.git-version'] = version
107+
props['app.build-date'] = new Date().format('yyyy-MM-dd HH:mm:ss:S z')
108+
109+
outputFile.withWriter { writer ->
110+
props.store(writer, null)
111+
}
112+
}
113+
}
114+
115+
processResources.dependsOn generateBuildProperties

application/src/main/java/org/togetherjava/tjbot/Application.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
import org.togetherjava.tjbot.logging.discord.DiscordLogging;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import java.sql.SQLException;
24+
import java.util.Properties;
2325

2426
/**
2527
* Main class of the application. Use {@link #main(String[])} to start an instance of it.
@@ -34,6 +36,8 @@ private Application() {
3436

3537
private static final Logger logger = LoggerFactory.getLogger(Application.class);
3638
private static final String DEFAULT_CONFIG_PATH = "config.json";
39+
private static final String DEFAULT_BUILD_PROPERTIES_PATH = "build.properties";
40+
private static final Properties BUILD_PROPERTIES = new Properties();
3741

3842
/**
3943
* Starts the application.
@@ -58,6 +62,20 @@ public static void main(final String[] args) {
5862
return;
5963
}
6064

65+
try (InputStream input = Application.class.getClassLoader()
66+
.getResourceAsStream(DEFAULT_BUILD_PROPERTIES_PATH)) {
67+
if (input == null) {
68+
logger.warn(
69+
"Unable to find {} file, you might not see properties like the git commit of this build",
70+
DEFAULT_BUILD_PROPERTIES_PATH);
71+
} else {
72+
BUILD_PROPERTIES.load(input);
73+
}
74+
} catch (IOException e) {
75+
logger.error("Exception while trying to load {} file", DEFAULT_BUILD_PROPERTIES_PATH,
76+
e);
77+
}
78+
6179
Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException);
6280
Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown));
6381
DiscordLogging.startDiscordLogging(config);
@@ -120,4 +138,7 @@ private static void onUncaughtException(Thread failingThread, Throwable failure)
120138
logger.error("Unknown error in thread {}.", failingThread.getName(), failure);
121139
}
122140

141+
public static Properties getBuildProperties() {
142+
return BUILD_PROPERTIES;
143+
}
123144
}

application/src/main/java/org/togetherjava/tjbot/features/basic/PingCommand.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
44

5+
import org.togetherjava.tjbot.Application;
56
import org.togetherjava.tjbot.features.CommandVisibility;
67
import org.togetherjava.tjbot.features.SlashCommandAdapter;
78

9+
import java.util.Properties;
10+
811
/**
912
* Implementation of an example command to illustrate how to respond to a user.
1013
* <p>
@@ -25,6 +28,11 @@ public PingCommand() {
2528
*/
2629
@Override
2730
public void onSlashCommand(SlashCommandInteractionEvent event) {
28-
event.reply("Pong!").queue();
31+
Properties buildProperties = Application.getBuildProperties();
32+
String response = "Pong! Running on version %s (%s)".formatted(
33+
buildProperties.getProperty("app.git-version"),
34+
buildProperties.getProperty("app.build-date"));
35+
36+
event.reply(response).queue();
2937
}
3038
}

application/src/test/java/org/togetherjava/tjbot/features/basic/PingCommandTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.DisplayName;
66
import org.junit.jupiter.api.Test;
7+
import org.mockito.ArgumentCaptor;
78

89
import org.togetherjava.tjbot.features.SlashCommand;
910
import org.togetherjava.tjbot.jda.JdaTester;
1011

12+
import static org.junit.jupiter.api.Assertions.assertTrue;
1113
import static org.mockito.Mockito.verify;
1214

1315
final class PingCommandTest {
@@ -35,6 +37,8 @@ void pingRespondsWithPong() {
3537
SlashCommandInteractionEvent event = triggerSlashCommand();
3638

3739
// THEN the bot replies with pong
38-
verify(event).reply("Pong!");
40+
ArgumentCaptor<String> replyMessageCaptor = ArgumentCaptor.forClass(String.class);
41+
verify(event).reply(replyMessageCaptor.capture());
42+
assertTrue(replyMessageCaptor.getValue().startsWith("Pong!"));
3943
}
4044
}

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ repositories {
88
mavenCentral()
99
}
1010

11+
def gitDescribeOutput = providers.exec {
12+
commandLine("git", "describe", "--tags")
13+
}.standardOutput.asText.get().trim()
14+
1115
group 'org.togetherjava'
12-
version '1.0-SNAPSHOT'
16+
version gitDescribeOutput
1317

1418
ext {
1519
jooqVersion = '3.20.5'

0 commit comments

Comments
 (0)