From e75c1ff35f1096247df0a07b8c8cb107630c197c Mon Sep 17 00:00:00 2001 From: Damien Coraboeuf Date: Sat, 7 Dec 2024 08:32:55 +0100 Subject: [PATCH] #1364 Auto-versioning problem when updating same file --- .../AutoVersioningProcessingServiceImpl.kt | 42 ++++++++++---- .../av/ACCAutoVersioningMultiplePaths.kt | 56 +++++++++++++++++++ 2 files changed, 86 insertions(+), 12 deletions(-) diff --git a/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/processing/AutoVersioningProcessingServiceImpl.kt b/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/processing/AutoVersioningProcessingServiceImpl.kt index 21a076a2e4..c58ca847e1 100644 --- a/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/processing/AutoVersioningProcessingServiceImpl.kt +++ b/ontrack-extension-auto-versioning/src/main/java/net/nemerosa/ontrack/extension/av/processing/AutoVersioningProcessingServiceImpl.kt @@ -96,6 +96,8 @@ class AutoVersioningProcessingServiceImpl( } // Map of current versions per path val currentVersions = mutableMapOf() + // Caching the updated content of each path + val updatedContent = mutableMapOf>() // For each target path val targetPathUpdated: List = try { order.allPaths.flatMap { configPath -> @@ -118,10 +120,12 @@ class AutoVersioningProcessingServiceImpl( } configPath.paths.map { targetPath -> // Gets the content of the target file - val lines = scm.download(scmBranch, targetPath, retryOnNotFound = true) - ?.toString(Charsets.UTF_8) - ?.lines() - ?: throw AutoVersioningNoContentException(scmBranch, targetPath) + val lines = updatedContent.getOrPut(targetPath) { + scm.download(scmBranch, targetPath, retryOnNotFound = true) + ?.toString(Charsets.UTF_8) + ?.lines() + ?: throw AutoVersioningNoContentException(scmBranch, targetPath) + } // Gets the current version in this file val currentVersion: String = autoVersioningTargetFileService.readVersion(configPath, lines) ?: throw AutoVersioningVersionNotFoundException(targetPath) @@ -133,14 +137,8 @@ class AutoVersioningProcessingServiceImpl( val updatedLines = configPath.replaceVersion(lines, targetVersion) // Audit autoVersioningAuditService.onProcessingUpdatingFile(order, upgradeBranch, targetPath) - // Uploads of the file content - scm.uploadLines( - upgradeBranch, - commitId, - targetPath, - updatedLines, - message = order.getCommitMessage() - ) + // Updating the cache + updatedContent[targetPath] = updatedLines // Changed true } else { @@ -160,6 +158,26 @@ class AutoVersioningProcessingServiceImpl( // At least one path was changed if (targetPathUpdated.any { it }) { + // Uploading the file contents + try { + updatedContent.forEach { (targetPath, updatedLines) -> + scm.uploadLines( + upgradeBranch, + commitId, + targetPath, + updatedLines, + message = order.getCommitMessage() + ) + } + } catch (e: Exception) { + autoVersioningEventService.sendError( + order, + e.message?.takeIf { it.isNotBlank() } ?: "Issue while uploading the change", + e + ) + throw e + } + // Templating renderer val avRenderer = autoVersioningTemplatingService.createAutoVersioningTemplateRenderer( order = order, diff --git a/ontrack-kdsl-acceptance/src/test/java/net/nemerosa/ontrack/kdsl/acceptance/tests/av/ACCAutoVersioningMultiplePaths.kt b/ontrack-kdsl-acceptance/src/test/java/net/nemerosa/ontrack/kdsl/acceptance/tests/av/ACCAutoVersioningMultiplePaths.kt index 82a7190f06..3183b02545 100644 --- a/ontrack-kdsl-acceptance/src/test/java/net/nemerosa/ontrack/kdsl/acceptance/tests/av/ACCAutoVersioningMultiplePaths.kt +++ b/ontrack-kdsl-acceptance/src/test/java/net/nemerosa/ontrack/kdsl/acceptance/tests/av/ACCAutoVersioningMultiplePaths.kt @@ -73,6 +73,62 @@ class ACCAutoVersioningMultiplePaths : AbstractACCAutoVersioningTestSupport() { } } + @Test + fun `Auto versioning with additional paths using the same path`() { + withMockScmRepository(ontrack) { + withAutoVersioning { + repositoryFile("gradle.properties") { + """ + one-version = 1.0.0 + another-version = 1.0.0 + """.trimIndent() + } + val dependency = branchWithPromotion(promotion = "IRON") + project { + branch { + configuredForMockRepository() + setAutoVersioningConfig( + listOf( + AutoVersioningSourceConfig( + sourceProject = dependency.project.name, + sourceBranch = dependency.name, + sourcePromotion = "IRON", + targetPath = "gradle.properties", + targetProperty = "one-version", + additionalPaths = listOf( + AutoVersioningSourceConfigPath( + path = "gradle.properties", + property = "another-version", + ) + ) + ) + ) + ) + + dependency.apply { + build(name = "2.0.0") { + setMetaInfoProperty("toml", "2.0.0-toml") + promote("IRON") + } + } + + waitForAutoVersioningCompletion() + + assertThatMockScmRepository { + fileContains("gradle.properties") { + """ + one-version = 2.0.0 + another-version = 2.0.0 + """.trimIndent() + } + } + + } + } + } + } + } + @Test fun `Auto versioning on multiple paths`() { withMockScmRepository(ontrack) {