Skip to content

Commit b826d47

Browse files
authored
Add support for providing a raw commit SHA to compare against as a new comparison strategy (#279)
* Add support for providing the raw String of a commit SHA as a comparison strategy * Add documentation for new comparison strategy to README * Fix ‘Not enough information to infer type variable T’ * Add unit tests for the new comparison strategy * Undo reformatting * Fix formatting * Add unit tests for configuration * Revert "Fix ‘Not enough information to infer type variable T’" This reverts commit 2586a3d.
1 parent 78f344e commit b826d47

File tree

7 files changed

+83
-5
lines changed

7 files changed

+83
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,16 @@ affectedModuleDetector {
113113
- `logFilename`: A filename for the output detector to use
114114
- `logFolder`: A folder to output the log file in
115115
- `specifiedBranch`: A branch to specify changes against. Must be used in combination with configuration `compareFrom = "SpecifiedBranchCommit"`
116+
- `specifiedRawCommitSha`: A raw commit SHA to specify changes against. Must be used in combination with configuration `compareFrom = "SpecifiedRawCommitSha"`
116117
- `ignoredFiles`: A set of files that will be filtered out of the list of changed files retrieved by git.
117118
- `buildAllWhenNoProjectsChanged`: If true, the plugin will build all projects when no projects are considered affected.
118119
- `compareFrom`: A commit to compare the branch changes against. Can be either:
119120
- PreviousCommit: compare against the previous commit
120121
- ForkCommit: compare against the commit the branch was forked from
121122
- SpecifiedBranchCommit: compare against the last commit of `$specifiedBranch` using `git rev-parse` approach.
122123
- SpecifiedBranchCommitMergeBase: compare against the nearest ancestors with `$specifiedBranch` using `git merge base` approach.
123-
124+
- SpecifiedRawCommitSha: compare against the provided raw commit SHA.
125+
124126
**Note:** specify the branch to compare changes against using the `specifiedBranch` configuration before the `compareFrom` configuration
125127
- `excludedModules`: A list of modules that will be excluded from the build process, can be the name of a module, or a regex against the module gradle path
126128
- `includeUncommitted`: If uncommitted files should be considered affected
@@ -162,6 +164,12 @@ Suppose we have changed 6 files in our "feature" branch.
162164

163165
Hence, depending on your CI settings you have to configure AMD appropriately.
164166

167+
## SpecifiedRawCommitSha
168+
169+
If you want AMD plugin to skip performing the git operations under-the-hood (like `git rev-parse` and `git merge base`), you can provide the raw commit SHA you wish to compare against.
170+
One of the main reasons you might want to follow this approach is, maybe your environment leverages [Git mirroring](https://docs.github.com/en/repositories/creating-and-managing-repositories/duplicating-a-repository) for speed optimizations, for example CI/CD environments.
171+
Mirroring _can_ lead to inaccurate common ancestor commits as the duplicated repository _may be_ out of sync with the true remote repository.
172+
165173
## Sample Usage
166174

167175
Running the plugin generated tasks is quite simple. By default, if `affected_module_detector.enable` is not set,

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ class AffectedModuleConfiguration {
8181

8282
var specifiedBranch: String? = null
8383

84+
var specifiedRawCommitSha: String? = null
85+
8486
var compareFrom: String = "PreviousCommit"
8587
set(value) {
8688
val commitShaProviders = listOf(
8789
"PreviousCommit",
8890
"ForkCommit",
8991
"SpecifiedBranchCommit",
90-
"SpecifiedBranchCommitMergeBase"
92+
"SpecifiedBranchCommitMergeBase",
93+
"SpecifiedRawCommitSha"
9194
)
9295
require(commitShaProviders.contains(value)) {
9396
"The property configuration compareFrom must be one of the following: ${commitShaProviders.joinToString(", ")}"
@@ -97,6 +100,11 @@ class AffectedModuleConfiguration {
97100
"Specify a branch using the configuration specifiedBranch"
98101
}
99102
}
103+
if (value == "SpecifiedRawCommitSha") {
104+
requireNotNull(specifiedRawCommitSha) {
105+
"Provide a Commit SHA for the specifiedRawCommitSha property when using SpecifiedRawCommitSha comparison strategy."
106+
}
107+
}
100108
field = value
101109
}
102110

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class AffectedModuleDetectorImpl constructor(
340340
injectedGitClient ?: GitClientImpl(
341341
rootProject.projectDir,
342342
logger,
343-
commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch),
343+
commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch, config.specifiedRawCommitSha),
344344
ignoredFiles = config.ignoredFiles
345345
)
346346
}

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ interface CommitShaProvider {
88
fun get(commandRunner: GitClient.CommandRunner): Sha
99

1010
companion object {
11-
fun fromString(string: String, specifiedBranch: String? = null): CommitShaProvider {
11+
fun fromString(
12+
string: String,
13+
specifiedBranch: String? = null,
14+
specifiedRawCommitSha: String? = null
15+
): CommitShaProvider {
1216
return when (string) {
1317
"PreviousCommit" -> PreviousCommit()
1418
"ForkCommit" -> ForkCommit()
@@ -24,6 +28,12 @@ interface CommitShaProvider {
2428
}
2529
SpecifiedBranchCommitMergeBase(specifiedBranch)
2630
}
31+
"SpecifiedRawCommitSha" -> {
32+
requireNotNull(specifiedRawCommitSha) {
33+
"Specified raw commit sha must be defined"
34+
}
35+
SpecifiedRawCommitSha(specifiedRawCommitSha)
36+
}
2737
else -> throw IllegalArgumentException("Unsupported compareFrom type")
2838
}
2939
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.dropbox.affectedmoduledetector.commitshaproviders
2+
3+
import com.dropbox.affectedmoduledetector.GitClient
4+
import com.dropbox.affectedmoduledetector.Sha
5+
6+
class SpecifiedRawCommitSha(private val commitSha: String) : CommitShaProvider {
7+
override fun get(commandRunner: GitClient.CommandRunner): Sha {
8+
return commitSha
9+
}
10+
}

affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfigurationTest.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,39 @@ class AffectedModuleConfigurationTest {
233233
fail("Expected to catch an exception")
234234
}
235235

236+
@Test
237+
fun `GIVEN AffectedModuleConfiguration WHEN compareFrom is set to SpecifiedRawCommitSha THEN is SpecifiedRawCommitSha`() {
238+
val specifiedRawCommitSha = "SpecifiedRawCommitSha"
239+
val commitSha = "12345"
240+
241+
config.specifiedRawCommitSha = commitSha
242+
config.compareFrom = specifiedRawCommitSha
243+
244+
val actual = config.compareFrom
245+
assertThat(actual).isEqualTo(specifiedRawCommitSha)
246+
}
247+
248+
@Test
249+
fun `GIVEN AffectedModuleConfiguration WHEN compareFrom is set to SpecifiedRawCommitSha AND specifiedRawCommitSha not defined THEN error thrown`() {
250+
val specifiedRawCommitSha = "SpecifiedRawCommitSha"
251+
252+
try {
253+
config.compareFrom = specifiedRawCommitSha
254+
} catch (e: IllegalArgumentException) {
255+
// THEN
256+
assertThat(e.message).isEqualTo("Provide a Commit SHA for the specifiedRawCommitSha property when using SpecifiedRawCommitSha comparison strategy.")
257+
return
258+
}
259+
}
260+
236261
@Test
237262
fun `GIVEN AffectedModuleConfiguration WHEN compareFrom is set to invalid sha provider THEN exception thrown and value not set`() {
238263
try {
239264
config.compareFrom = "InvalidInput"
240265
fail()
241266
} catch (e: Exception) {
242267
assertThat(e::class).isEqualTo(IllegalArgumentException::class)
243-
assertThat(e.message).isEqualTo("The property configuration compareFrom must be one of the following: PreviousCommit, ForkCommit, SpecifiedBranchCommit, SpecifiedBranchCommitMergeBase")
268+
assertThat(e.message).isEqualTo("The property configuration compareFrom must be one of the following: PreviousCommit, ForkCommit, SpecifiedBranchCommit, SpecifiedBranchCommitMergeBase, SpecifiedRawCommitSha")
244269
assertThat(config.compareFrom).isEqualTo("PreviousCommit")
245270
}
246271
}

affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProviderTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ class CommitShaProviderTest {
5858
assertThat(actual::class).isEqualTo(SpecifiedBranchCommitMergeBase::class)
5959
}
6060

61+
@Test
62+
fun givenSpecifiedRawCommitSha_whenFromString_thenReturnSpecifiedRawCommitSha() {
63+
val actual = CommitShaProvider.fromString("SpecifiedRawCommitSha", specifiedRawCommitSha = "sha")
64+
65+
assertThat(actual::class).isEqualTo(SpecifiedRawCommitSha::class)
66+
}
67+
68+
@Test
69+
fun givenSpecifiedRawCommitShaAndSpecifiedRawCommitShaNull_whenFromString_thenThrowException() {
70+
try {
71+
CommitShaProvider.fromString("SpecifiedRawCommitSha")
72+
} catch (e: Exception) {
73+
assertThat(e::class).isEqualTo(IllegalArgumentException::class)
74+
assertThat(e.message).isEqualTo("Specified raw commit sha must be defined")
75+
}
76+
}
77+
6178
@Test
6279
fun givenInvalidCommitString_whenFromString_thenExceptionThrown() {
6380
try {

0 commit comments

Comments
 (0)