Fix Develocity deprecation warnings triggered by init-scripts

This commit is contained in:
daz 2024-04-08 10:12:04 -06:00
parent 0ac212a9d2
commit c93523a078
No known key found for this signature in database
5 changed files with 107 additions and 56 deletions

View file

@ -3,6 +3,13 @@
*/ */
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
def BUILD_SCAN_PLUGIN_ID = "com.gradle.build-scan"
def BUILD_SCAN_EXTENSION = "buildScan"
def DEVELOCITY_PLUGIN_ID = "com.gradle.develocity"
def DEVELOCITY_EXTENSION = "develocity"
def GE_PLUGIN_ID = "com.gradle.enterprise"
def GE_EXTENSION = "gradleEnterprise"
// Only run against root build. Do not run against included builds. // Only run against root build. Do not run against included builds.
def isTopLevelBuild = gradle.getParent() == null def isTopLevelBuild = gradle.getParent() == null
if (isTopLevelBuild) { if (isTopLevelBuild) {
@ -23,13 +30,16 @@ if (isTopLevelBuild) {
captureUsingBuildFinished(gradle, invocationId) captureUsingBuildFinished(gradle, invocationId)
} }
// The `buildScanPublished` hook allows the capture of the Build Scan URI.
// Results captured this way will overwrite any results from the other mechanism. settings.pluginManager.withPlugin(GE_PLUGIN_ID) {
settings.pluginManager.withPlugin("com.gradle.enterprise") { // Only execute if develocity plugin isn't applied.
captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject, invocationId) if (!settings.extensions.findByName(DEVELOCITY_EXTENSION)) {
captureUsingBuildScanPublished(settings.extensions[GE_EXTENSION].buildScan, settings.rootProject, invocationId)
} }
settings.pluginManager.withPlugin("com.gradle.develocity") { }
captureUsingBuildScanPublished(settings.extensions["develocity"].buildScan, settings.rootProject, invocationId)
settings.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
captureUsingBuildScanPublished(settings.extensions[DEVELOCITY_EXTENSION].buildScan, settings.rootProject, invocationId)
} }
} }
} else if (atLeastGradle3) { } else if (atLeastGradle3) {
@ -37,15 +47,22 @@ if (isTopLevelBuild) {
// By default, use 'buildFinished' to capture build results // By default, use 'buildFinished' to capture build results
captureUsingBuildFinished(gradle, invocationId) captureUsingBuildFinished(gradle, invocationId)
// The `buildScanPublished` hook allows the capture of the Build Scan URI. gradle.rootProject.pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
// Results captured this way will overwrite any results from 'buildFinished'. // Only execute if develocity plugin isn't applied.
gradle.rootProject.pluginManager.withPlugin("com.gradle.build-scan") { if (!gradle.rootProject.extensions.findByName(DEVELOCITY_EXTENSION)) {
captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject, invocationId) captureUsingBuildScanPublished(gradle.rootProject.extensions[BUILD_SCAN_EXTENSION], gradle.rootProject, invocationId)
}
}
gradle.rootProject.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
captureUsingBuildScanPublished(gradle.rootProject.extensions[DEVELOCITY_EXTENSION].buildScan, gradle.rootProject, invocationId)
} }
} }
} }
} }
// The `buildScanPublished` hook allows the capture of the Build Scan URI.
// Results captured this way will overwrite any results from 'buildFinished'.
def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) { def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) {
buildScanExtension.with { buildScanExtension.with {
def buildResults = new BuildResults(invocationId, gradle, rootProject) def buildResults = new BuildResults(invocationId, gradle, rootProject)

View file

@ -80,13 +80,9 @@ def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'
def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise' def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin' def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin'
def GRADLE_ENTERPRISE_EXTENSION_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension'
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity' def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
def DEVELOCITY_PLUGIN_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityPlugin' def DEVELOCITY_PLUGIN_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityPlugin'
def DEVELOCITY_CONFIGURATION_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityConfiguration'
def SETTINGS_EXTENSION_CLASSES = [GRADLE_ENTERPRISE_EXTENSION_CLASS, DEVELOCITY_CONFIGURATION_CLASS]
def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection' def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection'
def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin' def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
@ -194,6 +190,8 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
} }
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) { pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
// Only execute if develocity plugin isn't applied.
if (gradle.rootProject.extensions.findByName("develocity")) return
afterEvaluate { afterEvaluate {
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
buildScan.server = develocityUrl buildScan.server = develocityUrl
@ -242,61 +240,64 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
applyPluginExternally(settings.pluginManager, pluginClass) applyPluginExternally(settings.pluginManager, pluginClass)
if (develocityUrl) { if (develocityUrl) {
logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints") logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
eachDevelocitySettingsExtension(settings, SETTINGS_EXTENSION_CLASSES) { ext -> eachDevelocitySettingsExtension(settings) { ext ->
ext.server = develocityUrl ext.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer ext.allowUntrustedServer = develocityAllowUntrustedServer
} }
} }
eachDevelocitySettingsExtension(settings, SETTINGS_EXTENSION_CLASSES) { ext -> eachDevelocitySettingsExtension(settings) { ext ->
ext.buildScan.uploadInBackground = buildScanUploadInBackground ext.buildScan.uploadInBackground = buildScanUploadInBackground
ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
} }
eachDevelocitySettingsExtension(settings, [GRADLE_ENTERPRISE_EXTENSION_CLASS]) { ext -> eachDevelocitySettingsExtension(settings,
ext.buildScan.publishAlways() { develocity ->
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
develocity.buildScan.capture.fileFingerprints = develocityCaptureFileFingerprints
},
{ gradleEnterprise ->
gradleEnterprise.buildScan.publishAlways()
if (isAtLeast(develocityPluginVersion, '2.1')) { if (isAtLeast(develocityPluginVersion, '2.1')) {
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints") logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
if (isAtLeast(develocityPluginVersion, '3.7')) { if (isAtLeast(develocityPluginVersion, '3.7')) {
ext.buildScan.capture.taskInputFiles = develocityCaptureFileFingerprints gradleEnterprise.buildScan.capture.taskInputFiles = develocityCaptureFileFingerprints
} else { } else {
ext.buildScan.captureTaskInputFiles = develocityCaptureFileFingerprints gradleEnterprise.buildScan.captureTaskInputFiles = develocityCaptureFileFingerprints
} }
} }
} }
)
eachDevelocitySettingsExtension(settings, [DEVELOCITY_CONFIGURATION_CLASS]) { ext ->
ext.buildScan.capture.fileFingerprints = develocityCaptureFileFingerprints
}
} }
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints") logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
} }
eachDevelocitySettingsExtension(settings, [GRADLE_ENTERPRISE_EXTENSION_CLASS]) { ext -> eachDevelocitySettingsExtension(settings,
{ develocity ->
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
ext.server = develocityUrl develocity.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer develocity.allowUntrustedServer = develocityAllowUntrustedServer
} }
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) { if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
ext.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
ext.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
} }
} },
{ gradleEnterprise ->
eachDevelocitySettingsExtension(settings, [DEVELOCITY_CONFIGURATION_CLASS]) { ext ->
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
ext.server = develocityUrl gradleEnterprise.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer gradleEnterprise.allowUntrustedServer = develocityAllowUntrustedServer
} }
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) { if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
ext.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
ext.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
} }
} }
)
} }
if (ccudPluginVersion) { if (ccudPluginVersion) {
@ -331,10 +332,26 @@ void applyPluginExternally(def pluginManager, String pluginClassName) {
} }
} }
static def eachDevelocitySettingsExtension(def settings, List<String> publicTypes, def action) { /**
settings.extensions.extensionsSchema.elements.findAll { publicTypes.contains(it.publicType.concreteClass.name) } * Apply the `dvAction` to all 'develocity' extensions.
* If no 'develocity' extensions are found, apply the `geAction` to all 'gradleEnterprise' extensions.
* (The develocity plugin creates both extensions, and we want to prefer configuring 'develocity').
*/
static def eachDevelocitySettingsExtension(def settings, def dvAction, def geAction = dvAction) {
def GRADLE_ENTERPRISE_EXTENSION_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension'
def DEVELOCITY_CONFIGURATION_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityConfiguration'
def dvExtensions = settings.extensions.extensionsSchema.elements
.findAll { it.publicType.concreteClass.name == DEVELOCITY_CONFIGURATION_CLASS }
.collect { settings[it.name] } .collect { settings[it.name] }
.each(action) if (!dvExtensions.empty) {
dvExtensions.each(dvAction)
} else {
def geExtensions = settings.extensions.extensionsSchema.elements
.findAll { it.publicType.concreteClass.name == GRADLE_ENTERPRISE_EXTENSION_CLASS }
.collect { settings[it.name] }
geExtensions.each(geAction)
}
} }
static boolean isAtLeast(String versionUnderTest, String referenceVersion) { static boolean isAtLeast(String versionUnderTest, String referenceVersion) {

View file

@ -54,6 +54,8 @@ class BaseInitScriptTest extends Specification {
File settingsFile File settingsFile
File buildFile File buildFile
boolean allowDevelocityDeprecationWarning = false
@TempDir @TempDir
File testProjectDir File testProjectDir
@ -200,14 +202,16 @@ task expectFailure {
} }
BuildResult run(List<String> args, String initScript, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) { BuildResult run(List<String> args, String initScript, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) {
createRunner(initScript, args, gradleVersion, jvmArgs, envVars).build() def result = createRunner(args, initScript, gradleVersion, jvmArgs, envVars).build()
assertNoDeprecationWarning(result)
} }
BuildResult runAndFail(List<String> args, String initScript, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) { BuildResult runAndFail(List<String> args, String initScript, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) {
createRunner(initScript, args, gradleVersion, jvmArgs, envVars).buildAndFail() def result = createRunner(args, initScript, gradleVersion, jvmArgs, envVars).buildAndFail()
assertNoDeprecationWarning(result)
} }
GradleRunner createRunner(String initScript, List<String> args, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) { GradleRunner createRunner(List<String> args, String initScript, GradleVersion gradleVersion = GradleVersion.current(), List<String> jvmArgs = [], Map<String, String> envVars = [:]) {
File initScriptsDir = new File(testProjectDir, "initScripts") File initScriptsDir = new File(testProjectDir, "initScripts")
args << '-I' << new File(initScriptsDir, initScript).absolutePath args << '-I' << new File(initScriptsDir, initScript).absolutePath
@ -236,6 +240,13 @@ task expectFailure {
} }
} }
BuildResult assertNoDeprecationWarning(BuildResult result) {
if (!allowDevelocityDeprecationWarning) {
assert !result.output.contains("WARNING: The following functionality has been deprecated")
}
return result
}
static final class TestGradleVersion { static final class TestGradleVersion {
final GradleVersion gradleVersion final GradleVersion gradleVersion

View file

@ -1,6 +1,7 @@
package com.gradle.gradlebuildaction package com.gradle.gradlebuildaction
import groovy.json.JsonSlurper import groovy.json.JsonSlurper
import org.gradle.util.GradleVersion
import static org.junit.Assume.assumeTrue import static org.junit.Assume.assumeTrue
@ -199,6 +200,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
} }
""" + settingsFile.text """ + settingsFile.text
allowDevelocityDeprecationWarning = true
run(testGradleVersion.gradleVersion) run(testGradleVersion.gradleVersion)
then: then:

View file

@ -134,6 +134,10 @@ class TestDevelocityInjection extends BaseInitScriptTest {
declareDevelocityPluginApplication(testGradleVersion.gradleVersion) declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
when: when:
// Init-script emits deprecation warnings when CCUD plugin is applied on Gradle 5.6.4
if (testGradleVersion.gradleVersion.version == "5.6.4") {
allowDevelocityDeprecationWarning = true
}
def result = run(testGradleVersion, testConfig().withCCUDPlugin()) def result = run(testGradleVersion, testConfig().withCCUDPlugin())
then: then: