Skip to content

Commit

Permalink
Download link in "+botinfo extended" is wrong if the build was compil…
Browse files Browse the repository at this point in the history
…ed via GitHub (#2223)

Fixes #2130

* Build URL generation extracted to method

* Build URL generation extracted

* Download link in "+botinfo extended" is wrong if the build was compiled via GitHub #2130
  • Loading branch information
bit-man committed Oct 12, 2020
1 parent 8d816bc commit 05085ca
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/loritta-pr.yml
Expand Up @@ -26,7 +26,7 @@ jobs:

# Build Loritta
- name: Build with Gradle
run: "./gradlew build -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"
run: "./gradlew build -Dgithub.build.id=$GITHUB_RUN_ID -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"

# Upload Artifacts to GitHub
# Loritta
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/loritta.yml
Expand Up @@ -34,7 +34,7 @@ jobs:

# Runs a single command using the runners shell
- name: Build with Gradle
run: "./gradlew build -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"
run: "./gradlew build -Dgithub.build.id=$GITHUB_RUN_ID -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"

# Upload Artifacts to GitHub
# Loritta
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Expand Up @@ -46,6 +46,7 @@ allprojects {
addIfAvailable("commit.hash", "Commit-Hash")
addIfAvailable("git.branch", "Git-Branch")
addIfAvailable("compiled.at", "Compiled-At")
addIfAvailable("github.build.id", "Github-Build-Id")
attributes["Main-Class"] = mainClass
attributes["Kotlin-Version"] = kotlinVersion
attributes["Class-Path"] = configurations.runtimeClasspath.get()
Expand Down
Expand Up @@ -20,7 +20,6 @@ import com.mrpowergamerbr.loritta.commands.vanilla.undertale.UndertaleBoxCommand
import com.mrpowergamerbr.loritta.commands.vanilla.utils.*
import com.mrpowergamerbr.loritta.dao.ServerConfig
import com.mrpowergamerbr.loritta.events.LorittaMessageEvent
import com.mrpowergamerbr.loritta.network.Databases
import com.mrpowergamerbr.loritta.utils.*
import com.mrpowergamerbr.loritta.utils.config.EnvironmentType
import com.mrpowergamerbr.loritta.utils.extensions.await
Expand All @@ -43,9 +42,9 @@ import net.perfectdreams.loritta.utils.UserPremiumPlans
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*
import java.util.concurrent.CancellationException
import java.util.jar.JarFile

class CommandManager(loritta: Loritta) {
companion object {
Expand Down Expand Up @@ -151,7 +150,7 @@ class CommandManager(loritta: Loritta) {
commandMap.add(LyricsCommand())

// =======[ DISCORD ]=======
commandMap.add(BotInfoCommand())
commandMap.add(createBotinfoCommand())
commandMap.add(AvatarCommand())
commandMap.add(ServerIconCommand())
commandMap.add(EmojiCommand())
Expand Down Expand Up @@ -229,6 +228,14 @@ class CommandManager(loritta: Loritta) {
commandMap.add(ExchangeCommand())
}

private fun createBotinfoCommand(): BotInfoCommand {
val path = this::class.java.protectionDomain.codeSource.location.path
val jar = JarFile(path)
val manifest = jar.manifest
val mainAttributes = manifest.mainAttributes
return BotInfoCommand(BuildInfo(mainAttributes))
}

suspend fun matches(ev: LorittaMessageEvent, rawArguments: List<String>, serverConfig: ServerConfig, locale: BaseLocale, legacyLocale: LegacyBaseLocale, lorittaUser: LorittaUser): Boolean {
// Primeiro os comandos vanilla da Loritta(tm)
for (command in commandMap) {
Expand Down
Expand Up @@ -3,7 +3,6 @@ package com.mrpowergamerbr.loritta.commands.vanilla.discord
import com.mrpowergamerbr.loritta.LorittaLauncher
import com.mrpowergamerbr.loritta.commands.AbstractCommand
import com.mrpowergamerbr.loritta.commands.CommandContext
import com.mrpowergamerbr.loritta.network.Databases
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.extensions.isEmote
import com.mrpowergamerbr.loritta.utils.locale.LegacyBaseLocale
Expand All @@ -17,14 +16,13 @@ import net.perfectdreams.loritta.tables.ExecutedCommandsLog
import net.perfectdreams.loritta.tables.Payments
import net.perfectdreams.loritta.utils.Emotes
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import java.awt.Color
import java.lang.management.ManagementFactory
import java.util.concurrent.TimeUnit
import java.util.jar.Attributes
import java.util.jar.JarFile

class BotInfoCommand : AbstractCommand("botinfo", category = CommandCategory.DISCORD) {
class BotInfoCommand(private val buildInfo: BuildInfo) : AbstractCommand("botinfo", category = CommandCategory.DISCORD) {
override fun getDescription(locale: LegacyBaseLocale): String {
return locale.toNewLocale()["commands.discord.botinfo.description"]
}
Expand Down Expand Up @@ -149,6 +147,8 @@ class BotInfoCommand : AbstractCommand("botinfo", category = CommandCategory.DIS
val maxMemory = runtime.maxMemory() / mb
val totalMemory = runtime.totalMemory() / mb

val buildURL = getBuildURL()

context.reply(
LorittaReply(
forceMention = true,
Expand All @@ -160,7 +160,7 @@ class BotInfoCommand : AbstractCommand("botinfo", category = CommandCategory.DIS
mentionUser = false
),
LorittaReply(
"**${locale["BOTINFO_BuildNumber"]}:** #$buildNumber <https://jenkins.perfectdreams.net/job/Loritta/$buildNumber/>",
"**${locale["BOTINFO_BuildNumber"]}:** #$buildNumber <$buildURL>",
"\uD83C\uDFD7",
mentionUser = false
),
Expand Down Expand Up @@ -231,4 +231,6 @@ class BotInfoCommand : AbstractCommand("botinfo", category = CommandCategory.DIS
)
)
}

private fun getBuildURL() = buildInfo.buildUrl()
}
@@ -0,0 +1,28 @@
package com.mrpowergamerbr.loritta.commands.vanilla.discord

import java.util.jar.Attributes

class BuildInfo(val attribute: Attributes) {
val githubBuildId = attribute[GITHUB_BUILD_ID] as String
fun buildUrl(): String {
return if (isJenkinsBuild())
createJenkinsBuildURL()
else
createGithubBuildURL()
}

private fun createGithubBuildURL() = "$GITHUB_BUILD_BASE_URL$githubBuildId"
private fun createJenkinsBuildURL(): String {
val buildNumber = attribute[BUILD_NUMBER] as String
return "$JENKINS_BUILD_BASE_URL$buildNumber/"
}

private fun isJenkinsBuild() = (githubBuildId as String) == ""

private companion object {
val BUILD_NUMBER = Attributes.Name("Build-Number")
val GITHUB_BUILD_ID = Attributes.Name("Github-Build-Id")
const val GITHUB_BUILD_BASE_URL = "https://github.com/LorittaBot/Loritta/actions/runs/"
const val JENKINS_BUILD_BASE_URL = "https://jenkins.perfectdreams.net/job/Loritta/"
}
}
@@ -0,0 +1,33 @@
package com.mrpowergamerbr.loritta.commands.vanilla.discord

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.jar.Attributes

class BuildInfoTest {
@Test
fun `generate Jenkins build URL`() {
val attribute = Attributes()
attribute[BUILD_NUMBER] = "123"
attribute[GITHUB_BUILD_ID] = ""

val buildInfo = BuildInfo(attribute)

assertThat( buildInfo.buildUrl()).isEqualTo("https://jenkins.perfectdreams.net/job/Loritta/123/")
}
@Test
fun `generate Github build URL`() {
val attribute = Attributes()
attribute[BUILD_NUMBER] = "123"
attribute[GITHUB_BUILD_ID] = "456"

val buildInfo = BuildInfo(attribute)

assertThat( buildInfo.buildUrl()).isEqualTo("https://github.com/LorittaBot/Loritta/actions/runs/456")
}

private companion object {
val BUILD_NUMBER = Attributes.Name("Build-Number")
val GITHUB_BUILD_ID= Attributes.Name("Github-Build-Id")
}
}

0 comments on commit 05085ca

Please sign in to comment.