Skip to content

Add parentBranch configuration to support CI environments with detached HEAD #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -225,7 +229,6 @@ which is implementing the [AffectedModuleTaskType](https://github.com/dropbox/Af

```groovy
// ...

affectedModuleDetector {
// ...
customTasks = [
Expand Down Expand Up @@ -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.

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class AffectedModuleConfiguration : Serializable {

var specifiedRawCommitSha: String? = null

var parentBranch: String? = null

var compareFrom: String = "PreviousCommit"
set(value) {
val commitShaProviders = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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("]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import com.dropbox.affectedmoduledetector.GitClient

internal class MockCommandRunner(private val logger: FileLogger) : GitClient.CommandRunner {
private val replies = mutableMapOf<String, List<String>>()
private val _executedCommands = mutableListOf<String>()
val executedCommands: List<String> get() = _executedCommands

fun addReply(command: String, response: String) {
logger.info("add reply. cmd: $command response: $response")
replies[command] = response.split(System.lineSeparator())
}

override fun execute(command: String): String {
_executedCommands.add(command)
return replies.getOrDefault(command, emptyList())
.joinToString(System.lineSeparator()).also {
logger.info("cmd: $command response: $it")
}
}

override fun executeAndParse(command: String): List<String> {
_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")
}
Expand Down