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)
+ }
+}
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")
+ }
+ }
+ }
}
}
--- /dev/null
+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(
+ "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))
+ }
+
+}