]> git.frustrated-labs.net Git - gradle-changelog-plugin.git/commitdiff
feat: add filter option to filter out commits from the CHANGELOG
authorAlexander Goussas <[email protected]>
Sun, 26 Oct 2025 03:53:47 +0000 (22:53 -0500)
committerAlexander Goussas <[email protected]>
Sun, 26 Oct 2025 04:01:51 +0000 (23:01 -0500)
app/build.gradle.kts
changelog-plugin/build.gradle.kts
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/GitInfo.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt
changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/git/GitServiceTests.kt [new file with mode: 0644]

index 9c302500eb61309a10f7c7cc886aad1b6d6120d0..4b4cae6d2aafda8ec6aa789f441ab3b5840d2555 100644 (file)
@@ -10,6 +10,9 @@ changelog {
 
     git {
         baseBranch = "main"
+        ignoreCommits = listOf(
+            "test",
+        )
     }
 }
 
index dead3ef57459ee700746b3d34365d927f23bf300..dd9a510771c7a0c45ada31ca0adb6a2155830d1a 100644 (file)
@@ -21,6 +21,7 @@ gradlePlugin {
 dependencies {
     testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
     testImplementation("org.hamcrest:java-hamcrest:2.0.0.0")
+    testImplementation("org.mockito:mockito-core:5.+")
 }
 
 tasks.withType<Test>().configureEach {
index 594d2686bd9e33cceb3e6b0340d7e2bab880eb0e..28921bd626e799d42129863f4f893be244068ada 100644 (file)
@@ -10,6 +10,7 @@ class ChangelogPlugin : Plugin<Project> {
         val extension = project.extensions.create("changelog", ChangelogPluginExtension::class.java)
 
         extension.gitInfo.baseBranch.convention("master")
+        extension.gitInfo.ignoreCommits.convention(emptyList())
         extension.format.convention("markdown")
         extension.fileName.convention("CHANGELOG.md")
 
index 0d27c82e18ad69a4a8d7de23e57a0212b6761a8a..2e596593d81bd40e440620dea876f5bb45bc615d 100644 (file)
@@ -22,6 +22,7 @@ abstract class ChangelogTask : DefaultTask() {
             GetCurrentBranchCommand(),
             GetCurrentBranchCommitsCommand(config.gitBranch),
             GetCurrentReleaseCommand(),
+            config.ignoreCommits
         )
     }
 
index ffd61da03c445a020c7dac032b204c707ca1e647..ec0fc7f53a7e7447191db6b70a3f5cbcc9f97a70 100644 (file)
@@ -7,6 +7,7 @@ class Config private constructor(
     val fileName: String,
     val gitBranch: String,
     val documentFormat: DocumentFormat,
+    val ignoreCommits: Set<String>,
 ) {
 
     companion object {
@@ -29,7 +30,8 @@ class Config private constructor(
             return Config(
                 outputFileName,
                 branchName,
-                format
+                format,
+                extension.gitInfo.ignoreCommits.get().toSet()
             )
         }
     }
index 4ad6bbae332e6354c410edb194f6fe94fb398cad..42b519c596793c455e5ee24d69460ce0c2545de7 100644 (file)
@@ -1,9 +1,18 @@
 package io.github.aloussase.changelog.config
 
+import org.gradle.api.provider.ListProperty
 import org.gradle.api.provider.Property
 
 interface GitInfo {
 
+    /**
+     * What is the branch we are comparing against.
+     */
     val baseBranch: Property<String>
 
+    /**
+     * Ignore the commits containing these strings.
+     */
+    val ignoreCommits: ListProperty<String>
+
 }
index 2b4bb078bc99345b1271051de8dea68be7ab4d3c..78341da3bb730fb6e932d1c664ad97f28b4d6ada 100644 (file)
@@ -6,7 +6,8 @@ import io.github.aloussase.changelog.git.commands.GitCommand
 class GitService(
     val currentBranchCommand: GitCommand<Branch>,
     val currentBranchCommitsCommand: GitCommand<List<RawCommit>>,
-    val currentReleaseCommand: GitCommand<Tag>
+    val currentReleaseCommand: GitCommand<Tag>,
+    val ignoreCommitsContaining: Set<String>
 ) {
 
     fun getCurrrentBranchChanges(): ChangelogEntry {
@@ -15,13 +16,19 @@ class GitService(
         val release = Tag("Unreleased")
         return ChangelogEntry(
             release.name,
-            commits.map {
-                Commit(
-                    it.author,
-                    currentBranch.name,
-                    it.message
-                )
-            }
+            commits
+                .filter { commit ->
+                    ignoreCommitsContaining.none {
+                        commit.message.contains(it)
+                    }
+                }
+                .map {
+                    Commit(
+                        it.author,
+                        currentBranch.name,
+                        it.message
+                    )
+                }
         )
     }
 
diff --git a/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/git/GitServiceTests.kt b/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/git/GitServiceTests.kt
new file mode 100644 (file)
index 0000000..50ce090
--- /dev/null
@@ -0,0 +1,92 @@
+package io.github.aloussase.changelog.git
+
+import io.github.aloussase.changelog.git.commands.GitCommand
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.hasSize
+import org.junit.jupiter.api.Test
+import org.mockito.Mockito.mock
+import org.mockito.Mockito.`when`
+
+class GitServiceTests {
+
+    val currentBranchCommand = mock(GitCommand::class.java) as GitCommand<Branch>
+    val currentBranchCommitsCommand = mock(GitCommand::class.java) as GitCommand<List<RawCommit>>
+    val currentReleaseCommand = mock(GitCommand::class.java) as GitCommand<Tag>
+
+    @Test
+    fun givenListOfCommitsToIgnoreWhenGettingCurrentBranchChangesThenCommitsContainingMessagesMatchingTheCriteriaAreNotIncludedInTheResults() {
+        val commitsToIgnore = setOf("test", "apple")
+
+        `when`(currentBranchCommand.execute())
+            .thenReturn(Result.success(Branch("master")))
+
+        `when`(currentBranchCommitsCommand.execute())
+            .thenReturn(
+                Result.success(
+                    listOf(
+                        RawCommit("[email protected]", "first commit"),
+                        RawCommit("[email protected]", "test: add test for thing"),
+                        RawCommit("[email protected]", "apples now cost more than bananas"),
+                        RawCommit("[email protected]", "upgrade the tests"),
+                    )
+                )
+            )
+
+        val gitService = GitService(
+            currentBranchCommand,
+            currentBranchCommitsCommand,
+            currentReleaseCommand,
+            commitsToIgnore
+        )
+
+        val result = gitService.getCurrrentBranchChanges()
+
+        assertThat(result.commits, hasSize(1))
+        assertThat(
+            result.commits.map { it.message },
+            contains("first commit")
+        )
+    }
+
+    @Test
+    fun givenEmptyListOfCommitsToIgnoreWhenCurrentBranchChangesThenAllCommitsAreReturned() {
+        val commitsToIgnore = setOf<String>()
+
+        `when`(currentBranchCommand.execute())
+            .thenReturn(Result.success(Branch("master")))
+
+        `when`(currentBranchCommitsCommand.execute())
+            .thenReturn(
+                Result.success(
+                    listOf(
+                        RawCommit("[email protected]", "first commit"),
+                        RawCommit("[email protected]", "test: add test for thing"),
+                        RawCommit("[email protected]", "apples now cost more than bananas"),
+                        RawCommit("[email protected]", "upgrade the tests"),
+                    )
+                )
+            )
+
+        val gitService = GitService(
+            currentBranchCommand,
+            currentBranchCommitsCommand,
+            currentReleaseCommand,
+            commitsToIgnore
+        )
+
+        val result = gitService.getCurrrentBranchChanges()
+
+        assertThat(result.commits, hasSize(4))
+        assertThat(
+            result.commits.map { it.message },
+            contains(
+                "first commit",
+                "test: add test for thing",
+                "apples now cost more than bananas",
+                "upgrade the tests"
+            )
+        )
+    }
+
+}