From df11ab8fb2bb931c05d908bd1d69abbf3b2cea01 Mon Sep 17 00:00:00 2001
From: Bot Githubaction <98587996+bot-githubaction@users.noreply.github.com>
Date: Fri, 4 Apr 2025 19:20:19 +0200
Subject: [PATCH] Update develocity-injection init script to v1.3 (#592)

Updates the develocity-injection init script to the latest reference script content
from https://github.com/gradle/develocity-ci-injection.

---------

Co-authored-by: daz <daz@gradle.com>
---
 ...adle-actions.inject-develocity.init.gradle | 53 +++++++++++--------
 .../TestDevelocityInjection.groovy            |  6 ++-
 2 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle b/sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle
index d656506..7abbc92 100644
--- a/sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle
+++ b/sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle
@@ -1,6 +1,6 @@
 /*
  * Initscript for injection of Develocity into Gradle builds.
- * Version: 1.2
+ * Version: 1.3
  */
 
 import org.gradle.util.GradleVersion
@@ -35,19 +35,20 @@ initscript {
     def pluginRepositoryPassword = getInputParam(gradle, 'gradle.plugin-repository.password')
     def develocityPluginVersion = getInputParam(gradle, 'develocity.plugin.version')
     def ccudPluginVersion = getInputParam(gradle, 'develocity.ccud-plugin.version')
+    def logLevel = Boolean.parseBoolean(getInputParam(gradle, 'develocity.injection.debug')) ? LogLevel.LIFECYCLE : LogLevel.INFO
 
     def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
     def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
 
     if (develocityPluginVersion || ccudPluginVersion && atLeastGradle4) {
         pluginRepositoryUrl = pluginRepositoryUrl ?: 'https://plugins.gradle.org/m2'
-        logger.lifecycle("Develocity plugins resolution: $pluginRepositoryUrl")
+        logger.log(logLevel, "Develocity plugins resolution: $pluginRepositoryUrl")
 
         repositories {
             maven {
                 url = pluginRepositoryUrl
                 if (pluginRepositoryUsername && pluginRepositoryPassword) {
-                    logger.lifecycle("Using credentials for plugin repository")
+                    logger.log(logLevel, "Using credentials for plugin repository")
                     credentials {
                         username = pluginRepositoryUsername
                         password = pluginRepositoryPassword
@@ -92,13 +93,13 @@ if (!isTopLevelBuild) {
 
 def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
 def initScriptName = buildscript.sourceFile.name
-if (requestedInitScriptName != initScriptName) {
-    logger.quiet("Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match")
-    return
-}
 
 def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam(gradle, "develocity.injection-enabled"))
 if (develocityInjectionEnabled) {
+    if (requestedInitScriptName != initScriptName) {
+        logger.log(LogLevel.WARN, "Develocity injection not enabled because requested init script name was '${requestedInitScriptName}', but '${initScriptName}' was expected")
+        return
+    }
     enableDevelocityInjection()
 }
 
@@ -106,6 +107,10 @@ if (develocityInjectionEnabled) {
 def buildScanCollector = new BuildScanCollector()
 def buildScanCaptureEnabled = buildScanCollector.metaClass.respondsTo(buildScanCollector, 'captureBuildScanLink', String)
 if (buildScanCaptureEnabled) {
+    if (requestedInitScriptName != initScriptName) {
+        logger.log(LogLevel.WARN, "Build Scan capture not enabled because requested init script name was '${requestedInitScriptName}', but '${initScriptName}' was expected")
+        return
+    }
     enableBuildScanLinkCapture(buildScanCollector)
 }
 
@@ -132,6 +137,7 @@ void enableDevelocityInjection() {
     def buildScanTermsOfUseUrl = getInputParam(gradle, 'develocity.terms-of-use.url')
     def buildScanTermsOfUseAgree = getInputParam(gradle, 'develocity.terms-of-use.agree')
     def ciAutoInjectionCustomValueValue = getInputParam(gradle, 'develocity.auto-injection.custom-value')
+    def logLevel = Boolean.parseBoolean(getInputParam(gradle, 'develocity.injection.debug')) ? LogLevel.LIFECYCLE : LogLevel.INFO
 
     def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
     def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
@@ -145,22 +151,22 @@ void enableDevelocityInjection() {
     }
 
     def printEnforcingDevelocityUrl = {
-        logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
+        logger.log(logLevel, "Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
     }
 
     def printAcceptingGradleTermsOfUse = {
-        logger.lifecycle("Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
+        logger.log(logLevel, "Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
     }
 
     // finish early if DV plugin version is unsupported (v3.6.4 is the minimum version tested and supports back to DV 2021.1)
     if (develocityPluginVersion && isNotAtLeast(develocityPluginVersion, '3.6.4')) {
-        logger.warn("Develocity Gradle plugin must be at least 3.6.4. Configured version is $develocityPluginVersion.")
+        logger.log(LogLevel.WARN, "Develocity Gradle plugin must be at least 3.6.4. Configured version is $develocityPluginVersion.")
         return
     }
 
     // finish early if configuration parameters passed in via system properties are not valid/supported
     if (ccudPluginVersion && isNotAtLeast(ccudPluginVersion, '1.7')) {
-        logger.warn("Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
+        logger.log(LogLevel.WARN, "Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
         return
     }
 
@@ -187,7 +193,7 @@ void enableDevelocityInjection() {
                             { rootExtension }
                         )
                         if (develocityUrl) {
-                            logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
+                            logger.log(logLevel, "Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
                             rootExtension.server = develocityUrl
                             rootExtension.allowUntrustedServer = develocityAllowUntrustedServer
                         }
@@ -197,7 +203,7 @@ void enableDevelocityInjection() {
                         }
                         buildScanExtension.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
                         if (isAtLeast(develocityPluginVersion, '2.1') && atLeastGradle5) {
-                            logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
+                            logger.log(logLevel, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
                             if (isAtLeast(develocityPluginVersion, '3.17')) {
                                 buildScanExtension.capture.fileFingerprints.set(develocityCaptureFileFingerprints)
                             } else if (isAtLeast(develocityPluginVersion, '3.7')) {
@@ -225,7 +231,7 @@ void enableDevelocityInjection() {
                             develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
                         }
 
-                        logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
+                        logger.log(logLevel, "Setting uploadInBackground: $buildScanUploadInBackground")
                         develocity.buildScan.uploadInBackground = buildScanUploadInBackground
                     },
                     { buildScan ->
@@ -250,7 +256,7 @@ void enableDevelocityInjection() {
 
                         // uploadInBackground available for build-scan-plugin 3.3.4 and later only
                         if (buildScan.metaClass.respondsTo(buildScan, 'setUploadInBackground', Boolean)) {
-                            logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
+                            logger.log(logLevel, "Setting uploadInBackground: $buildScanUploadInBackground")
                             buildScan.uploadInBackground = buildScanUploadInBackground
                         }
                     }
@@ -261,7 +267,7 @@ void enableDevelocityInjection() {
                         it.moduleVersion.with { group == "com.gradle" && name == "common-custom-user-data-gradle-plugin" }
                     }
                     if (!ccudPluginComponent) {
-                        logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
+                        logger.log(logLevel, "Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
                         pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
                     }
                 }
@@ -274,7 +280,7 @@ void enableDevelocityInjection() {
                     def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS)
                     applyPluginExternally(settings.pluginManager, pluginClass, develocityPluginVersion)
                     if (develocityUrl) {
-                        logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
+                        logger.log(logLevel, "Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
                         eachDevelocitySettingsExtension(settings) { ext ->
                             // server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
                             if (ext.metaClass.respondsTo(ext, 'getServer')) {
@@ -293,13 +299,13 @@ void enableDevelocityInjection() {
 
                     eachDevelocitySettingsExtension(settings,
                         { develocity ->
-                            logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
+                            logger.log(logLevel, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
                             develocity.buildScan.capture.fileFingerprints = develocityCaptureFileFingerprints
                         },
                         { gradleEnterprise ->
                             gradleEnterprise.buildScan.publishAlways()
                             if (isAtLeast(develocityPluginVersion, '2.1')) {
-                                logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
+                                logger.log(logLevel, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
                                 if (isAtLeast(develocityPluginVersion, '3.7')) {
                                     gradleEnterprise.buildScan.capture.taskInputFiles = develocityCaptureFileFingerprints
                                 } else {
@@ -325,7 +331,7 @@ void enableDevelocityInjection() {
                         develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
                     }
 
-                    logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
+                    logger.log(logLevel, "Setting uploadInBackground: $buildScanUploadInBackground")
                     develocity.buildScan.uploadInBackground = buildScanUploadInBackground
                 },
                 { gradleEnterprise ->
@@ -349,7 +355,7 @@ void enableDevelocityInjection() {
 
                     // uploadInBackground available for gradle-enterprise-plugin 3.3.4 and later only
                     if (gradleEnterprise.buildScan.metaClass.respondsTo(gradleEnterprise.buildScan, 'setUploadInBackground', Boolean)) {
-                        logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
+                        logger.log(logLevel, "Setting uploadInBackground: $buildScanUploadInBackground")
                         gradleEnterprise.buildScan.uploadInBackground = buildScanUploadInBackground
                     }
                 }
@@ -357,7 +363,7 @@ void enableDevelocityInjection() {
 
             if (ccudPluginVersion) {
                 if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) {
-                    logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
+                    logger.log(logLevel, "Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
                     settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
                 }
             }
@@ -366,7 +372,8 @@ void enableDevelocityInjection() {
 }
 
 void applyPluginExternally(def pluginManager, String pluginClassName, String pluginVersion) {
-    logger.lifecycle("Applying $pluginClassName with version $pluginVersion via init script")
+    def logLevel = Boolean.parseBoolean(getInputParam(gradle, 'develocity.injection.debug')) ? LogLevel.LIFECYCLE : LogLevel.INFO
+    logger.log(logLevel, "Applying $pluginClassName with version $pluginVersion via init script")
 
     def externallyApplied = 'develocity.externally-applied'
     def externallyAppliedDeprecated = 'gradle.enterprise.externally-applied'
diff --git a/sources/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDevelocityInjection.groovy b/sources/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDevelocityInjection.groovy
index f5d7e49..b7c54e5 100644
--- a/sources/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDevelocityInjection.groovy
+++ b/sources/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDevelocityInjection.groovy
@@ -496,7 +496,8 @@ class TestDevelocityInjection extends BaseInitScriptTest {
                 DEVELOCITY_ALLOW_UNTRUSTED_SERVER         : "true",
                 DEVELOCITY_PLUGIN_VERSION                 : develocityPluginVersion,
                 DEVELOCITY_BUILD_SCAN_UPLOAD_IN_BACKGROUND: "true", // Need to upload in background since our Mock server doesn't cope with foreground upload
-                DEVELOCITY_AUTO_INJECTION_CUSTOM_VALUE    : 'gradle-actions'
+                DEVELOCITY_AUTO_INJECTION_CUSTOM_VALUE    : 'gradle-actions',
+                DEVELOCITY_INJECTION_DEBUG                : "true"
             ]
             if (enforceUrl) envVars.put("DEVELOCITY_ENFORCE_URL", "true")
             if (ccudPluginVersion != null) envVars.put("DEVELOCITY_CCUD_PLUGIN_VERSION", ccudPluginVersion)
@@ -516,7 +517,8 @@ class TestDevelocityInjection extends BaseInitScriptTest {
                 "-Ddevelocity.allow-untrusted-server=true",
                 "-Ddevelocity.plugin.version=$develocityPluginVersion",
                 "-Ddevelocity.build-scan.upload-in-background=true",
-                "-Ddevelocity.auto-injection.custom-value=gradle-actions"
+                "-Ddevelocity.auto-injection.custom-value=gradle-actions",
+                "-Ddevelocity.injection.debug=true"
             ]
 
             if (enforceUrl) jvmArgs.add("-Ddevelocity.enforce-url=true")