diff --git a/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceImpl.kt b/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceImpl.kt index ed93e07ab2..50d65e65d1 100644 --- a/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceImpl.kt +++ b/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceImpl.kt @@ -40,6 +40,10 @@ class AutoVersioningPromotionListenerServiceImpl( .flatMap { branch -> autoVersioningConfigurationService.getAutoVersioning(branch) ?.configurations + ?.filter { config -> + config.sourceProject == promotionLevel.project.name && + config.sourcePromotion == promotionLevel.name + } ?.map { config -> AutoVersioningConfiguredBranch( branch = branch, @@ -126,6 +130,8 @@ class AutoVersioningPromotionListenerServiceImpl( } return match } else { + // This is not supposed to happen since configurations are already filtered on + // source project & source promotion level name tracking.reject(branchTrail, "Project & promotion names not matching") return false } @@ -140,11 +146,13 @@ class AutoVersioningPromotionListenerServiceImpl( val cacheKey = LatestSourceBranchCacheKey(branchTrail.branch, sourceBranch.project, branchTrail.configuration) val latestSourceBranch = cache.getOrPut(cacheKey) { LatestSourceBranchCacheValue( - autoVersioningConfigurationService.getLatestBranch( - branchTrail.branch, - sourceBranch.project, - branchTrail.configuration - ) + logger.logTime("AV Source Branch get [target=${branchTrail.branch.project.name}/${branchTrail.branch.name}][source=${sourceBranch.project.name}][config=${branchTrail.configuration.targetPath}]") { + autoVersioningConfigurationService.getLatestBranch( + branchTrail.branch, + sourceBranch.project, + branchTrail.configuration + ) + } ) }.branch // We want the promoted build to be on the latest source branch diff --git a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/AbstractAutoVersioningTestSupport.kt b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/AbstractAutoVersioningTestSupport.kt index f901c910ad..80066052f5 100644 --- a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/AbstractAutoVersioningTestSupport.kt +++ b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/AbstractAutoVersioningTestSupport.kt @@ -22,6 +22,66 @@ abstract class AbstractAutoVersioningTestSupport : AbstractQLKTITSupport() { autoVersioningConfigurationService.setAutoVersioning(this, init) } + protected fun withThreeDependencies( + code: ( + target: Branch, + dep1: Branch, + dep2: Branch, + dep3: Branch, + ) -> Unit, + ) { + asAdmin { + val dep1 = project { + branch { + promotionLevel("GOLD") + } + } + val dep2 = project { + branch { + promotionLevel("GOLD") + } + } + val dep3 = project { + branch { + promotionLevel("GOLD") + } + } + mockSCMTester.withMockSCMRepository { + project { + branch { + configureMockSCMBranch() + autoVersioningConfigurationService.setupAutoVersioning( + this, + AutoVersioningConfig( + configurations = listOf( + AutoVersioningTestFixtures.sourceConfig( + sourceProject = dep1.project.name, + sourceBranch = dep1.name, + sourcePromotion = "GOLD", + targetPath = "dep1.properties", + ), + AutoVersioningTestFixtures.sourceConfig( + sourceProject = dep2.project.name, + sourceBranch = dep2.name, + sourcePromotion = "GOLD", + targetPath = "dep2.properties", + ), + AutoVersioningTestFixtures.sourceConfig( + sourceProject = dep3.project.name, + sourceBranch = dep3.name, + sourcePromotion = "GOLD", + targetPath = "dep3.properties", + ), + ) + ) + ) + code(this, dep1, dep2, dep3) + } + } + } + } + } + protected fun withPromotionLevelTargets( code: ( pl: PromotionLevel, diff --git a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/config/AutoVersioningConfigurationServiceIT.kt b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/config/AutoVersioningConfigurationServiceIT.kt index 72cc7ae658..4b71b7b648 100644 --- a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/config/AutoVersioningConfigurationServiceIT.kt +++ b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/config/AutoVersioningConfigurationServiceIT.kt @@ -29,6 +29,17 @@ internal class AutoVersioningConfigurationServiceIT : AbstractAutoVersioningTest @Autowired protected lateinit var eventSubscriptionService: EventSubscriptionService + @Test + fun `Getting the branches configured to a promotion`() { + withThreeDependencies { target, dep1, _, _ -> + val targets = autoVersioningConfigurationService.getBranchesConfiguredFor(dep1.project.name, "GOLD") + assertEquals( + listOf(target), + targets + ) + } + } + @Test fun `Using &same expression in source branch returns the same branch as the promoted build`() { asAdmin { diff --git a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceIT.kt b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceIT.kt index db5f2bf97e..0587ad71aa 100644 --- a/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceIT.kt +++ b/ontrack-extension-auto-versioning/src/test/java/net/nemerosa/ontrack/extension/av/listener/AutoVersioningPromotionListenerServiceIT.kt @@ -4,6 +4,7 @@ import net.nemerosa.ontrack.extension.av.AbstractAutoVersioningTestSupport import net.nemerosa.ontrack.extension.av.tracking.AutoVersioningTrackingService import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired +import kotlin.jvm.optionals.getOrNull import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull @@ -13,6 +14,36 @@ class AutoVersioningPromotionListenerServiceIT : AbstractAutoVersioningTestSuppo @Autowired private lateinit var autoVersioningTrackingService: AutoVersioningTrackingService + @Autowired + private lateinit var autoVersioningPromotionListenerService: AutoVersioningPromotionListenerService + + @Test + fun `Checking that AV targets only eligible project and promotion`() { + withThreeDependencies { target, dep1, _, _ -> + val pl1 = structureService.findPromotionLevelByName( + dep1.project.name, dep1.name, "GOLD" + ).getOrNull() ?: error("Promotion level not found") + val tracking = autoVersioningTrackingService.startInMemoryTrail() + autoVersioningPromotionListenerService.getConfiguredBranches(pl1, tracking) + assertNotNull(tracking.trail, "Trail created") { trail -> + assertEquals(1, trail.branches.size) + val branchTrail = trail.branches.first() + assertEquals( + target.id, + branchTrail.branch.id + ) + assertEquals( + true, + branchTrail.isEligible() + ) + assertEquals( + dep1.project.name, + branchTrail.configuration.sourceProject + ) + } + } + } + @Test fun `Getting the trail of a promotion run`() { withPromotionLevelTargets { pl, app1, app2 ->