From a13870c94ef4746500674e821a7ad834f4b1d1e8 Mon Sep 17 00:00:00 2001 From: daz Date: Wed, 26 Apr 2023 10:47:38 -0600 Subject: [PATCH 1/2] Test for GE plugin applied in settingsEvaluated --- .../BaseInitScriptTest.groovy | 3 ++ .../TestBuildResultRecorder.groovy | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/BaseInitScriptTest.groovy b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/BaseInitScriptTest.groovy index 427b557..8212e0b 100644 --- a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/BaseInitScriptTest.groovy +++ b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/BaseInitScriptTest.groovy @@ -38,6 +38,9 @@ class BaseInitScriptTest extends Specification { static final List CONFIGURATION_CACHE_VERSIONS = [GRADLE_7_X, GRADLE_8_X] + static final List SETTINGS_PLUGIN_VERSIONS = + [GRADLE_6_X, GRADLE_7_X, GRADLE_8_X] + static final String PUBLIC_BUILD_SCAN_ID = 'i2wepy2gr7ovw' static final String DEFAULT_SCAN_UPLOAD_TOKEN = 'scan-upload-token' static final String ROOT_PROJECT_NAME = 'test-init-script' diff --git a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultRecorder.groovy b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultRecorder.groovy index d815202..4cfd4a2 100644 --- a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultRecorder.groovy +++ b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultRecorder.groovy @@ -148,6 +148,34 @@ class TestBuildResultRecorder extends BaseInitScriptTest { testGradleVersion << ALL_VERSIONS } + def "produces build results file with build scan when GE plugin is applied in settingsEvaluated"() { + assumeTrue testGradleVersion.compatibleWithCurrentJvm + + when: + settingsFile.text = """ + plugins { + id 'com.gradle.enterprise' version '3.13' apply(false) + } + gradle.settingsEvaluated { + apply plugin: 'com.gradle.enterprise' + gradleEnterprise { + server = '$mockScansServer.address' + buildScan { + publishAlways() + } + } + } + """ + settingsFile.text + + run(['help'], initScript, testGradleVersion.gradleVersion) + + then: + assertResults('help', testGradleVersion, false, true) + + where: + testGradleVersion << SETTINGS_PLUGIN_VERSIONS + } + void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan, boolean scanUploadFailed = false) { def results = new JsonSlurper().parse(buildResultFile) assert results['rootProjectName'] == ROOT_PROJECT_NAME From a580d9bd5715dcaf43ab8828ef8c720b4539ce86 Mon Sep 17 00:00:00 2001 From: daz Date: Tue, 25 Apr 2023 13:05:08 -0600 Subject: [PATCH 2/2] Detect GE plugin applied in settingsEvaluated The `PluginManager.hasPlugin` method was not detecting the GE plugin when it was applied during settingsEvaluated. Switching to `PluginManager.withPlugin` fixes this. Fixes #626 --- .../build-result-capture.init.gradle | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/resources/init-scripts/build-result-capture.init.gradle b/src/resources/init-scripts/build-result-capture.init.gradle index d44d2e8..d47e470 100644 --- a/src/resources/init-scripts/build-result-capture.init.gradle +++ b/src/resources/init-scripts/build-result-capture.init.gradle @@ -16,26 +16,29 @@ if (isTopLevelBuild) { if (atLeastGradle6) { def useBuildService = version >= GradleVersion.version("6.6") settingsEvaluated { settings -> - // The `buildScanPublished` hook is the only way to capture the build scan URI. - if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) { - captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject, invocationId) - } - // We also need to add hooks in case the plugin is applied but no build scan is published - // The `buildScanPublished` results will NOT be overwritten by these calls + // By default, use standard mechanisms to capture build results if (useBuildService) { captureUsingBuildService(settings, invocationId) } else { 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("com.gradle.enterprise") { + captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject, invocationId) + } } } else if (atLeastGradle3) { projectsEvaluated { gradle -> - if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) { + // By default, use 'buildFinished' to capture build results + captureUsingBuildFinished(gradle, invocationId) + + // The `buildScanPublished` hook allows the capture of the build scan URI. + // Results captured this way will overwrite any results from 'buildFinished'. + gradle.rootProject.pluginManager.withPlugin("com.gradle.build-scan") { captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject, invocationId) } - // Always attempt to capture in buildFinished in case the plugin is applied but no build scan is published - // The `buildScanPublished` results will NOT be overwritten by this call - captureUsingBuildFinished(gradle, invocationId) } } } @@ -70,6 +73,7 @@ def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId def captureUsingBuildFinished(gradle, invocationId) { gradle.buildFinished { result -> + println "Got buildFinished: ${result}" def buildResults = new BuildResults(invocationId, gradle, gradle.rootProject) buildResults.setBuildResult(result) buildResults.writeToResultsFile(false)