]> git.frustrated-labs.net Git - gradle-changelog-plugin.git/commitdiff
feat: implement changelog markdown formatter + tests
authorAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 14:38:18 +0000 (09:38 -0500)
committerAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 14:38:18 +0000 (09:38 -0500)
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt
changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatterTests.kt [new file with mode: 0644]

index 9d509f10784088c111e0fbd694db4e1752e68dbd..d8a06f71084825ab07bb2899ebfd432a1e2512f2 100644 (file)
@@ -5,4 +5,15 @@ import io.github.aloussase.changelog.git.Commit
 data class ChangelogEntry(
     val release: String,
     val commits: List<Commit> = emptyList(),
-)
+) : Comparable<ChangelogEntry> {
+    override fun compareTo(other: ChangelogEntry): Int {
+        val myRelease = release.split(".").map { it.toInt() }
+        val otherRelease = other.release.split(".").map { it.toInt() }
+        return Comparator
+            .comparingInt<List<Int>> { it[0] }
+            .thenComparing { it[1] }
+            .thenComparing { it[2] }
+            .reversed()
+            .compare(myRelease, otherRelease)
+    }
+}
index b8d92240cb2fbf6090d8b355c7faac5f93ee6957..955e9845332c04955859f4e3338e242cd8f25b26 100644 (file)
@@ -25,6 +25,9 @@ class ChangelogPlugin : Plugin<Project> {
             }
             val document = changelogFile.readText()
             val changelog = parser.parse(document).getOrThrow()
+
+            // TODO: Add commits from current branch here.
+
             val formatter = ChangelogFormatterFactory.create(config.documentFormat)
             changelogFile.writeText(formatter.format(changelog))
         }
index 5f67ce6cadd6f45a185fa8686527b51225bc51d7..81c63442a72ccd2c1da00b9d28a09ce1fa997137 100644 (file)
@@ -4,6 +4,26 @@ import io.github.aloussase.changelog.Changelog
 
 class MarkdownFormatter : ChangelogFormatter {
     override fun format(changelog: Changelog): String {
-        return changelog.entries.first().release
+        return buildString {
+            append("# CHANGELOG\n\n")
+            for ((entryIndex, entry) in changelog.entries.sorted().withIndex()) {
+                append("## [${entry.release}]")
+                for ((index, commit) in entry.commits.withIndex()) {
+                    if (index == 0) {
+                        append("\n")
+                    }
+
+                    append("- ${commit.branchName}: ${commit.message} (${commit.author})")
+
+                    if (index < entry.commits.size - 1) {
+                        append("\n")
+                    }
+                }
+
+                if (entryIndex < changelog.entries.size - 1) {
+                    append("\n\n")
+                }
+            }
+        }
     }
 }
diff --git a/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatterTests.kt b/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatterTests.kt
new file mode 100644 (file)
index 0000000..d880ce4
--- /dev/null
@@ -0,0 +1,64 @@
+package io.github.aloussase.changelog.formatter
+
+import io.github.aloussase.changelog.Changelog
+import io.github.aloussase.changelog.ChangelogEntry
+import io.github.aloussase.changelog.git.Commit
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.jupiter.api.Test
+
+class MarkdownFormatterTests {
+
+    @Test
+    fun givenChangelogWhenFormatIsInvokedThenADocumentWithTheRightFormatIsGenerated() {
+        val changelog = Changelog(
+            listOf(
+                ChangelogEntry(
+                    "0.1.0",
+                    listOf(
+                        Commit(
+                            "[email protected]",
+                            "LOYMAR-123",
+                            "changes nice"
+                        ),
+                    ),
+                ),
+                ChangelogEntry(
+                    "0.1.1",
+                    listOf()
+                ),
+            ),
+        )
+
+        val expectedDocument =
+            "# CHANGELOG\n\n## [0.1.1]\n\n## [0.1.0]\n- LOYMAR-123: changes nice ([email protected])"
+        val formatter = MarkdownFormatter()
+
+        val result = formatter.format(changelog)
+
+        assertThat(result, equalTo(expectedDocument))
+    }
+
+    @Test
+    fun givenChangeloWithEntriesOutOfOrderWhenFormatIsCalledThenDocumentWithEntriesInRightOrderIsGenerated() {
+        val changelog = Changelog(
+            listOf(
+                ChangelogEntry("0.1.0", listOf()),
+                ChangelogEntry("0.1.1", listOf()),
+                ChangelogEntry("2.0.0", listOf()),
+                ChangelogEntry("2.1.0", listOf()),
+                ChangelogEntry("2.0.2", listOf()),
+                ChangelogEntry("2.0.1", listOf()),
+            ),
+        )
+
+        val expectedDocument =
+            "# CHANGELOG\n\n## [2.1.0]\n\n## [2.0.2]\n\n## [2.0.1]\n\n## [2.0.0]\n\n## [0.1.1]\n\n## [0.1.0]"
+        val formatter = MarkdownFormatter()
+
+        val result = formatter.format(changelog)
+
+        assertThat(result, equalTo(expectedDocument))
+    }
+
+}