From 548b6758adb1806043691bc78dac9648dd83ad9b Mon Sep 17 00:00:00 2001 From: Alexander Goussas Date: Sat, 25 Oct 2025 22:53:47 -0500 Subject: [PATCH] feat: add filter option to filter out commits from the CHANGELOG --- app/build.gradle.kts | 3 + changelog-plugin/build.gradle.kts | 1 + .../aloussase/changelog/ChangelogPlugin.kt | 1 + .../aloussase/changelog/ChangelogTask.kt | 1 + .../aloussase/changelog/config/Config.kt | 4 +- .../aloussase/changelog/config/GitInfo.kt | 9 ++ .../aloussase/changelog/git/GitService.kt | 23 +++-- .../changelog/git/GitServiceTests.kt | 92 +++++++++++++++++++ 8 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/git/GitServiceTests.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9c30250..4b4cae6 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -10,6 +10,9 @@ changelog { git { baseBranch = "main" + ignoreCommits = listOf( + "test", + ) } } diff --git a/changelog-plugin/build.gradle.kts b/changelog-plugin/build.gradle.kts index dead3ef..dd9a510 100644 --- a/changelog-plugin/build.gradle.kts +++ b/changelog-plugin/build.gradle.kts @@ -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().configureEach { diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt index 594d268..28921bd 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt @@ -10,6 +10,7 @@ class ChangelogPlugin : Plugin { 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") diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt index 0d27c82..2e59659 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt @@ -22,6 +22,7 @@ abstract class ChangelogTask : DefaultTask() { GetCurrentBranchCommand(), GetCurrentBranchCommitsCommand(config.gitBranch), GetCurrentReleaseCommand(), + config.ignoreCommits ) } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt index ffd61da..ec0fc7f 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt @@ -7,6 +7,7 @@ class Config private constructor( val fileName: String, val gitBranch: String, val documentFormat: DocumentFormat, + val ignoreCommits: Set, ) { companion object { @@ -29,7 +30,8 @@ class Config private constructor( return Config( outputFileName, branchName, - format + format, + extension.gitInfo.ignoreCommits.get().toSet() ) } } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/GitInfo.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/GitInfo.kt index 4ad6bba..42b519c 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/GitInfo.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/GitInfo.kt @@ -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 + /** + * Ignore the commits containing these strings. + */ + val ignoreCommits: ListProperty + } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt index 2b4bb07..78341da 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt @@ -6,7 +6,8 @@ import io.github.aloussase.changelog.git.commands.GitCommand class GitService( val currentBranchCommand: GitCommand, val currentBranchCommitsCommand: GitCommand>, - val currentReleaseCommand: GitCommand + val currentReleaseCommand: GitCommand, + val ignoreCommitsContaining: Set ) { 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 index 0000000..50ce090 --- /dev/null +++ b/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/git/GitServiceTests.kt @@ -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 + val currentBranchCommitsCommand = mock(GitCommand::class.java) as GitCommand> + val currentReleaseCommand = mock(GitCommand::class.java) as GitCommand + + @Test + fun givenListOfCommitsToIgnoreWhenGettingCurrentBranchChangesThenCommitsContainingMessagesMatchingTheCriteriaAreNotIncludedInTheResults() { + val commitsToIgnore = setOf("test", "apple") + + `when`(currentBranchCommand.execute()) + .thenReturn(Result.success(Branch("master"))) + + `when`(currentBranchCommitsCommand.execute()) + .thenReturn( + Result.success( + listOf( + RawCommit("johndoe@email.com", "first commit"), + RawCommit("johndoe@email.com", "test: add test for thing"), + RawCommit("johndoe@email.com", "apples now cost more than bananas"), + RawCommit("johndoe@email.com", "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() + + `when`(currentBranchCommand.execute()) + .thenReturn(Result.success(Branch("master"))) + + `when`(currentBranchCommitsCommand.execute()) + .thenReturn( + Result.success( + listOf( + RawCommit("johndoe@email.com", "first commit"), + RawCommit("johndoe@email.com", "test: add test for thing"), + RawCommit("johndoe@email.com", "apples now cost more than bananas"), + RawCommit("johndoe@email.com", "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" + ) + ) + } + +} -- 2.43.0