Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,23 @@ application {
mainClass = 'org.togetherjava.tjbot.Application'
applicationDefaultJvmArgs = ["--enable-native-access=ALL-UNNAMED"]
}

def generatedBuildPropertiesDir = file("build/resources/main/")
tasks.register('generateBuildProperties') {
def outputFile = new File(generatedBuildPropertiesDir, "build.properties")

doLast {
if (!generatedBuildPropertiesDir.exists()) {
generatedBuildPropertiesDir.mkdirs()
}
def props = new Properties()
props['app.git-version'] = version
props['app.build-date'] = new Date().format('yyyy-MM-dd HH:mm:ss:S z')

outputFile.withWriter { writer ->
props.store(writer, null)
}
}
}

processResources.dependsOn generateBuildProperties
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.togetherjava.tjbot.logging.discord.DiscordLogging;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.Properties;

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

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

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

try (InputStream input = Application.class.getClassLoader()
.getResourceAsStream(DEFAULT_BUILD_PROPERTIES_PATH)) {
if (input == null) {
logger.warn(
"Unable to find {} file, you might not see properties like the git commit of this build",
DEFAULT_BUILD_PROPERTIES_PATH);
} else {
BUILD_PROPERTIES.load(input);
}
} catch (IOException e) {
logger.error("Exception while trying to load {} file", DEFAULT_BUILD_PROPERTIES_PATH,
e);
}

Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException);
Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown));
DiscordLogging.startDiscordLogging(config);
Expand Down Expand Up @@ -120,4 +138,7 @@ private static void onUncaughtException(Thread failingThread, Throwable failure)
logger.error("Unknown error in thread {}.", failingThread.getName(), failure);
}

public static Properties getBuildProperties() {
return BUILD_PROPERTIES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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

import org.togetherjava.tjbot.Application;
import org.togetherjava.tjbot.features.CommandVisibility;
import org.togetherjava.tjbot.features.SlashCommandAdapter;

import java.util.Properties;

/**
* Implementation of an example command to illustrate how to respond to a user.
* <p>
Expand All @@ -25,6 +28,11 @@ public PingCommand() {
*/
@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
event.reply("Pong!").queue();
Properties buildProperties = Application.getBuildProperties();
String response = "Pong! Running on version %s (%s)".formatted(
buildProperties.getProperty("app.git-version"),
buildProperties.getProperty("app.build-date"));

event.reply(response).queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import org.togetherjava.tjbot.features.SlashCommand;
import org.togetherjava.tjbot.jda.JdaTester;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;

final class PingCommandTest {
Expand Down Expand Up @@ -35,6 +37,8 @@ void pingRespondsWithPong() {
SlashCommandInteractionEvent event = triggerSlashCommand();

// THEN the bot replies with pong
verify(event).reply("Pong!");
ArgumentCaptor<String> replyMessageCaptor = ArgumentCaptor.forClass(String.class);
verify(event).reply(replyMessageCaptor.capture());
assertTrue(replyMessageCaptor.getValue().startsWith("Pong!"));
}
}
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ repositories {
mavenCentral()
}

def gitDescribeOutput = providers.exec {
commandLine("git", "describe", "--tags")
}.standardOutput.asText.get().trim()

group 'org.togetherjava'
version '1.0-SNAPSHOT'
version gitDescribeOutput

ext {
jooqVersion = '3.20.5'
Expand Down
Loading