-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance status page with detailed information about current git commi…
…t and add corresponding documentation into README Co-authored-by: Matthias Geißendörfer <[email protected]>
- Loading branch information
Showing
16 changed files
with
232 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
core/src/main/kotlin/de/otto/babbage/core/status/version/GitVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.otto.babbage.core.status.version | ||
|
||
data class GitVersion( | ||
val branch: String, | ||
val commitId: String, | ||
val time: Long, | ||
val user: String, | ||
val message: String, | ||
val repositoryUrl: String | ||
) |
46 changes: 46 additions & 0 deletions
46
core/src/main/kotlin/de/otto/babbage/core/status/version/GitVersionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package de.otto.babbage.core.status.version | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.boot.info.GitProperties | ||
import org.springframework.stereotype.Component | ||
|
||
private val LOG: Logger = LoggerFactory.getLogger(GitVersionProvider::class.java) | ||
|
||
@Component | ||
@ConditionalOnProperty(prefix = "babbage.status.git", name = ["enabled"], havingValue = "true", matchIfMissing = false) | ||
class GitVersionProvider( | ||
private val gitProperties: GitProperties | ||
) { | ||
|
||
@Suppress("ReturnCount") | ||
fun getGitVersion(): GitVersion? { | ||
return GitVersion( | ||
branch = gitProperties.branch ?: return handleNull("branch"), | ||
commitId = gitProperties.get("commit.id") ?: return handleNull("commit.id"), | ||
time = gitProperties.get("commit.time")?.toLong() ?: return handleNull("commit.time"), | ||
user = gitProperties.get("commit.user.name") ?: return handleNull("commit.user.name"), | ||
message = gitProperties.get("commit.message.short") ?: return handleNull("commit.message.short"), | ||
repositoryUrl = parseRemoteUrlToHttps() ?: return handleNull("remote.origin.url") | ||
) | ||
} | ||
|
||
private fun parseRemoteUrlToHttps(): String? { | ||
val remoteUrl = gitProperties.get("remote.origin.url") | ||
if (remoteUrl?.startsWith("git@") == true) { | ||
val urlParts = remoteUrl.split(":") | ||
val hostPath = urlParts[0].replace("git@", "https://") | ||
val repositoryPath = urlParts[1] | ||
return "$hostPath/$repositoryPath" | ||
} | ||
return remoteUrl | ||
} | ||
|
||
private fun handleNull(fieldName: String): GitVersion? { | ||
LOG.warn("Missing $fieldName in git.properties. Git Information are not show on the status page.") | ||
return null | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/test/kotlin/de/otto/babbage/core/version/GitVersionProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package de.otto.babbage.core.version | ||
|
||
import de.otto.babbage.core.status.version.GitVersion | ||
import de.otto.babbage.core.status.version.GitVersionProvider | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
import org.springframework.boot.info.GitProperties | ||
import java.util.* | ||
|
||
class GitVersionProviderTest { | ||
|
||
@Test | ||
fun `should convert git properties to git version`() { | ||
// given | ||
val properties = Properties() | ||
val branchName = "someBranch-${UUID.randomUUID()}" | ||
properties.setProperty("branch", branchName) | ||
val commitId = "someCommitId-${UUID.randomUUID()}" | ||
properties.setProperty("commit.id", commitId) | ||
val commitTime = "1714137126540" | ||
properties.setProperty("commit.time", commitTime) | ||
val user = "someUser-${UUID.randomUUID()}" | ||
properties.setProperty("commit.user.name", user) | ||
val messageShort = "someMessageShort-${UUID.randomUUID()}" | ||
properties.setProperty("commit.message.short", messageShort) | ||
val remoteUrl = "https://github.com/otto-de/babbage-microservice.git" | ||
properties.setProperty("remote.origin.url", remoteUrl) | ||
val gitProperties = GitProperties(properties) | ||
|
||
val versionProvider = GitVersionProvider(gitProperties) | ||
|
||
// when | ||
val gitVersion = versionProvider.getGitVersion() | ||
|
||
// then | ||
gitVersion shouldBe GitVersion( | ||
branch = branchName, | ||
commitId = commitId, | ||
time = commitTime.toLong() * 1000, | ||
user = user, | ||
message = messageShort, | ||
repositoryUrl = remoteUrl | ||
) | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"[email protected]:otto-de/babbage-microservice.git, https://github.com/otto-de/babbage-microservice.git", | ||
"[email protected]:otto-de/babbage-microservice.git, https://gitlab.com/otto-de/babbage-microservice.git", | ||
"https://github.com/otto-de/babbage-microservice.git, https://github.com/otto-de/babbage-microservice.git" | ||
) | ||
fun `should parse remote url to https url to link directly to web page of repository`( | ||
gitRemoteUrl: String, | ||
expectedRepositoryUrl: String | ||
) { | ||
// given | ||
val properties = Properties() | ||
properties.setProperty("branch", "someBranch-${UUID.randomUUID()}") | ||
properties.setProperty("commit.id", "someCommitId-${UUID.randomUUID()}") | ||
properties.setProperty("commit.time", "123456789") | ||
properties.setProperty("commit.user.name", "someUser-${UUID.randomUUID()}") | ||
properties.setProperty("commit.message.short", "someMessageShort-${UUID.randomUUID()}") | ||
properties.setProperty("remote.origin.url", gitRemoteUrl) | ||
val gitProperties = GitProperties(properties) | ||
|
||
val versionProvider = GitVersionProvider(gitProperties) | ||
|
||
// when | ||
val gitVersion = versionProvider.getGitVersion() | ||
|
||
// then | ||
gitVersion!!.repositoryUrl shouldBe expectedRepositoryUrl | ||
} | ||
|
||
@Test | ||
fun `should catch missing git properties and return null`() { | ||
// given | ||
val gitProperties = GitProperties(Properties()) | ||
val versionProvider = GitVersionProvider(gitProperties) | ||
|
||
// when | ||
val gitVersion = versionProvider.getGitVersion() | ||
|
||
// then | ||
gitVersion shouldBe null | ||
} | ||
} |
Oops, something went wrong.