diff --git a/README.md b/README.md index 5d245fd..0cf2a4e 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ affectedModuleDetector { buildAllWhenNoProjectsChanged = true // default is true includeUncommitted = true top = "HEAD" + specifiedBranch = "main" + specifiedRawCommitSha = "abc123" + parentBranch = "main" // Optional: specify parent branch for ForkCommit comparison customTasks = [ new AffectedModuleConfiguration.CustomTask( "runDetektByImpact", @@ -114,6 +117,7 @@ affectedModuleDetector { - `logFolder`: A folder to output the log file in - `specifiedBranch`: A branch to specify changes against. Must be used in combination with configuration `compareFrom = "SpecifiedBranchCommit"` - `specifiedRawCommitSha`: A raw commit SHA to specify changes against. Must be used in combination with configuration `compareFrom = "SpecifiedRawCommitSha"` + - `parentBranch`: A branch to specify as the parent branch for ForkCommit comparison. If not provided, ForkCommit will try to detect it automatically. - `ignoredFiles`: A set of files that will be filtered out of the list of changed files retrieved by git. - `buildAllWhenNoProjectsChanged`: If true, the plugin will build all projects when no projects are considered affected. - `compareFrom`: A commit to compare the branch changes against. Can be either: @@ -225,7 +229,6 @@ which is implementing the [AffectedModuleTaskType](https://github.com/dropbox/Af ```groovy // ... - affectedModuleDetector { // ... customTasks = [ @@ -263,3 +266,4 @@ Special thanks to the AndroidX team for originally developing this project at ht WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + \ No newline at end of file diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt index 7150166..9496830 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt @@ -84,6 +84,8 @@ class AffectedModuleConfiguration : Serializable { var specifiedRawCommitSha: String? = null + var parentBranch: String? = null + var compareFrom: String = "PreviousCommit" set(value) { val commitShaProviders = listOf( diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt index c00ca92..bc681f8 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt @@ -207,7 +207,8 @@ abstract class AffectedModuleDetector(protected val logger: Logger?) { specifiedBranch = config.specifiedBranch, specifiedSha = config.specifiedRawCommitSha, top = config.top, - includeUncommitted = config.includeUncommitted + includeUncommitted = config.includeUncommitted, + parentBranch = config.parentBranch ), ignoredFiles = config.ignoredFiles ) diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt index 5b3e836..c549bbf 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt @@ -232,7 +232,7 @@ internal abstract class GitChangedFilesSource : val specifiedSha = parameters.commitShaProvider.specifiedSha val type = when (parameters.commitShaProvider.type) { "PreviousCommit" -> PreviousCommit() - "ForkCommit" -> ForkCommit() + "ForkCommit" -> ForkCommit(parameters.commitShaProvider.parentBranch) "SpecifiedBranchCommit" -> { requireNotNull(specifiedBranch) { "Specified branch must be defined" diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt index 99e0ff9..242a762 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt @@ -13,5 +13,6 @@ data class CommitShaProviderConfiguration( val specifiedBranch: String? = null, val specifiedSha: String? = null, val top: Sha, - val includeUncommitted: Boolean + val includeUncommitted: Boolean, + val parentBranch: String? = null ) : Serializable diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommit.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommit.kt index 8e632c0..89c805f 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommit.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommit.kt @@ -3,11 +3,11 @@ package com.dropbox.affectedmoduledetector.commitshaproviders import com.dropbox.affectedmoduledetector.GitClient import com.dropbox.affectedmoduledetector.Sha -class ForkCommit : CommitShaProvider { +class ForkCommit(private val providedParentBranch: String? = null) : CommitShaProvider { override fun get(commandRunner: GitClient.CommandRunner): Sha { val currentBranch = commandRunner.executeAndParseFirst(CURRENT_BRANCH_CMD) - val parentBranch = commandRunner.executeAndParse(SHOW_ALL_BRANCHES_CMD) + val parentBranch = providedParentBranch ?: commandRunner.executeAndParse(SHOW_ALL_BRANCHES_CMD) .firstOrNull { !it.contains(currentBranch) && it.contains("*") } ?.substringAfter("[") ?.substringBefore("]") diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfigurationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfigurationTest.kt index ad006a7..a115f8f 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfigurationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfigurationTest.kt @@ -376,4 +376,20 @@ class AffectedModuleConfigurationTest { // THEN assertFalse(actual) } + + @Test + fun `GIVEN AffectedModuleConfiguration WHEN parentBranch is set THEN value is returned`() { + val parentBranch = "main" + + config.parentBranch = parentBranch + + val actual = config.parentBranch + assertThat(actual).isEqualTo(parentBranch) + } + + @Test + fun `GIVEN AffectedModuleConfiguration WHEN parentBranch is not set THEN null is returned`() { + val actual = config.parentBranch + assertThat(actual).isNull() + } } diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommitTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommitTest.kt index cd4ab38..865f3c2 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommitTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/ForkCommitTest.kt @@ -75,4 +75,17 @@ class ForkCommitTest { assertThat(actual).isEqualTo("commit-sha") } + + @Test + fun givenProvidedParentBranch_whenGetCommitSha_thenVerifyExactCommand() { + val providedParentBranch = "abc" + val forkCommitWithParent = ForkCommit(providedParentBranch) + + commandRunner.addReply(ForkCommit.CURRENT_BRANCH_CMD, "feature") + commandRunner.addReply("git merge-base feature abc", "commit-sha") + + forkCommitWithParent.get(commandRunner) + + assertThat(commandRunner.executedCommands).contains("git merge-base feature abc") + } } diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/mocks/MockCommandRunner.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/mocks/MockCommandRunner.kt index 49d5f41..c8dcfe5 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/mocks/MockCommandRunner.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/mocks/MockCommandRunner.kt @@ -5,6 +5,8 @@ import com.dropbox.affectedmoduledetector.GitClient internal class MockCommandRunner(private val logger: FileLogger) : GitClient.CommandRunner { private val replies = mutableMapOf>() + private val _executedCommands = mutableListOf() + val executedCommands: List get() = _executedCommands fun addReply(command: String, response: String) { logger.info("add reply. cmd: $command response: $response") @@ -12,6 +14,7 @@ internal class MockCommandRunner(private val logger: FileLogger) : GitClient.Com } override fun execute(command: String): String { + _executedCommands.add(command) return replies.getOrDefault(command, emptyList()) .joinToString(System.lineSeparator()).also { logger.info("cmd: $command response: $it") @@ -19,12 +22,14 @@ internal class MockCommandRunner(private val logger: FileLogger) : GitClient.Com } override fun executeAndParse(command: String): List { + _executedCommands.add(command) return replies.getOrDefault(command, emptyList()).also { logger.info("cmd: $command response: $it") } } override fun executeAndParseFirst(command: String): String { + _executedCommands.add(command) return replies.getOrDefault(command, emptyList()).first().also { logger.info("cmd: $command response: $it") }