Skip to content

Commit

Permalink
Fix changelog crash (#2019)
Browse files Browse the repository at this point in the history
* Fix possible crash due to missing version in changelog

* Update display for changelog preview helper
  • Loading branch information
tokou authored Sep 5, 2024
1 parent 2d9e106 commit 356fe4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions maestro-cli/src/main/java/maestro/cli/util/ChangeLogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package maestro.cli.util
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import maestro.cli.util.EnvUtils.CLI_VERSION

typealias ChangeLog = List<String>?

object ChangeLogUtils {

fun formatBody(content: String?, version: String): ChangeLog = content
?.split("\n## ")?.map { it.lines() }
?.first { it.first().startsWith(version) }
?.firstOrNull { it.firstOrNull()?.startsWith(version) == true }
?.drop(1)
?.map { it.trim().replace("**", "") }
?.map { it.replace("\\[(.*?)]\\(.*?\\)".toRegex(), "$1") }
Expand All @@ -31,6 +32,11 @@ object ChangeLogUtils {
fun main() {
val changelogFile = File(System.getProperty("user.dir"), "CHANGELOG.md")
val content = changelogFile.readText()
val changelog = ChangeLogUtils.formatBody(content, "Unreleased")
val unreleased = ChangeLogUtils.formatBody(content, "Unreleased")
val current = ChangeLogUtils.formatBody(content, CLI_VERSION.toString())
val changelog = unreleased ?: current
println("## ${unreleased?.let { "Unreleased" } ?: CLI_VERSION.toString()}")
println("-".repeat(100))
println(ChangeLogUtils.print(changelog))
println("-".repeat(100))
}
19 changes: 19 additions & 0 deletions maestro-cli/src/test/kotlin/maestro/cli/util/ChangeLogUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package maestro.cli.util

import com.google.common.truth.Truth.assertThat
import java.io.File
import maestro.cli.util.EnvUtils.CLI_VERSION
import org.junit.jupiter.api.Test

class ChangeLogUtilsTest {
Expand All @@ -12,6 +13,24 @@ class ChangeLogUtilsTest {
fun `test format last version`() {
val content = changelogFile.readText()

val changelog = ChangeLogUtils.formatBody(content, CLI_VERSION.toString())

assertThat(changelog).isNotEmpty()
}

@Test
fun `test format unknown version`() {
val content = changelogFile.readText()

val changelog = ChangeLogUtils.formatBody(content, "x.yy.z")

assertThat(changelog).isNull()
}

@Test
fun `test format short version`() {
val content = changelogFile.readText()

val changelog = ChangeLogUtils.formatBody(content, "1.38.1")

assertThat(changelog).containsExactly(
Expand Down

0 comments on commit 356fe4a

Please sign in to comment.