class MarkdownChangelogParser : ChangelogParser {
companion object {
- private val BRANCH_NAME_REGEX = Regex("## [\\w-]+")
- private val COMMIT_REGRX = Regex("- [\\w ]+")
+ private val RELEASE_VERSION_REGEX = Regex("## \\[(\\d+\\.\\d+\\.\\d+)]")
+ private val COMMIT_REGEX = Regex("- ([\\w-]+): ([\\w ]+) \\(([\\w.]+@[\\w.]+)\\)")
private val DOC_TITLE_REGEX = Regex("# (Changelog|CHANGELOG)")
}
val lines = blk.split("\n")
if (lines.isEmpty()) continue
- val branchName = lines[0]
- if (!BRANCH_NAME_REGEX.matches(branchName)) {
+ val releaseMatch = RELEASE_VERSION_REGEX.matchEntire(lines[0])
+ if (releaseMatch == null) {
return Result.failure(
- GradleException("Expected a valid branch name, but got $branchName")
+ GradleException("Expected a valid release version, but got ${lines[0]}")
)
}
+ val releaseVersion = releaseMatch.groupValues[1]
val commits = arrayListOf<Commit>()
for (line in lines.subList(1, lines.size)) {
- if (COMMIT_REGRX.matches(line)) {
- commits.add(Commit(line.dropWhile { it == '-' || it == ' ' }))
+ val commitMatch = COMMIT_REGEX.matchEntire(line)
+ if (commitMatch != null) {
+ val (branchName, message, author) = commitMatch.destructured
+ commits.add(
+ Commit(
+ author,
+ branchName,
+ message,
+ )
+ )
} else {
return Result.failure(
- GradleException("Expected a valid commit message, but got $line")
+ GradleException("Expected a valid commit line, but got $line")
)
}
}
entries.add(
ChangelogEntry(
- branchName.dropWhile { it == '#' || it == ' ' },
+ releaseVersion,
commits
)
)
package io.github.aloussase.changelog.parser
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.emptyIterable
-import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.*
import org.hamcrest.collection.IsCollectionWithSize.hasSize
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
@Test
fun givenDocumentContainingSingleEntryWithNoCommitsWhenParseisInvokedThenReturnChangelogWithThatEntryAndNoCommits() {
- val doc = "# Changelog\n\n## LOYMAR-123"
+ val doc = "# Changelog\n\n## [0.1.1]"
val parser = MarkdownChangelogParser()
val result = parser.parse(doc)
assertThat(result.isSuccess, equalTo(true))
assertThat(result.getOrThrow().entries, hasSize(1))
- assertThat(result.getOrThrow().entries.first().branchName, equalTo("LOYMAR-123"))
+ assertThat(result.getOrThrow().entries.first().release, equalTo("0.1.1"))
assertThat(result.getOrThrow().entries.first().commits, emptyIterable())
}
@Test
fun givenDocumentContainingSingleEntryWithCommitsWhenParseIsInvokedThenReturnChangelogWithSingleEntryAndCommits() {
- val doc = "# Changelog\n\n## LOYMAR-123\n- first commit\n- second commit"
+ val doc =
val parser = MarkdownChangelogParser()
val result = parser.parse(doc)
@Test
fun givenDocumentWithTwoEntriesWhenParseIsInvokedThenReturnChangelogWithTwoEntries() {
- val doc = "# Changelog\n\n## LOYMAR-123\n- first commit\n- second commit\n\n## LOYMAR-456\n- third commit"
+ val doc =
val parser = MarkdownChangelogParser()
val result = parser.parse(doc)
assertThat(result.isSuccess, equalTo(true))
assertThat(result.getOrThrow().entries, hasSize(2))
- assertThat(result.getOrThrow().entries[0].commits, hasSize(2))
+ assertThat(result.getOrThrow().entries[0].commits, hasSize(1))
assertThat(result.getOrThrow().entries[1].commits, hasSize(1))
assertThat(result.getOrThrow().entries[1].commits[0].message, equalTo("third commit"))
}
+ @ParameterizedTest
+ @ValueSource(
+ strings = [
+ "LOYMAR-123",
+ "0.1.x",
+ ""
+ ]
+ )
+ fun givenDocumentWithInvalidReleaseVersionWhenParseIsInvokedThenExceptionShouldBeThrown(releaseVersion: String) {
+ val doc =
+ val parser = MarkdownChangelogParser()
+
+ val result = parser.parse(doc)
+
+ assertThat(result.isFailure, equalTo(true))
+ assertThat(result.exceptionOrNull()?.message, containsString("Expected a valid release version"))
+ assertThat(result.exceptionOrNull()?.message, containsString(releaseVersion))
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = ["LOYMAR 123", "", "@@@"])
+ fun givenDocumentWithInvalidCommitBranchWhenParseIsInvokedThenFailureShouldBeReturned(branchName: String) {
+ val doc = "# Changelog\n\n## [1.1.1]\n- $branchName: first commit (
[email protected])"
+ val parser = MarkdownChangelogParser()
+
+ val result = parser.parse(doc)
+
+ assertThat(result.isFailure, equalTo(true))
+ assertThat(result.exceptionOrNull()?.message, containsString(branchName))
+ assertThat(result.exceptionOrNull()?.message, containsString("Expected a valid commit"))
+ }
+
}