Skip to content

Commit

Permalink
Allow uploadInBackground to be configured when Develocity plugin alre…
Browse files Browse the repository at this point in the history
…ady applied
  • Loading branch information
erichaagdev committed Feb 5, 2025
1 parent 471cf19 commit 09f6696
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/main/resources/develocity-injection.init.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ if (buildScanCaptureEnabled) {
}

void enableDevelocityInjection() {
def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'

def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
Expand Down Expand Up @@ -190,8 +189,6 @@ void enableDevelocityInjection() {
// Develocity plugin publishes scans by default
buildScanExtension.publishAlways()
}
// uploadInBackground not available for build-scan-plugin 1.16
if (buildScanExtension.metaClass.respondsTo(buildScanExtension, 'setUploadInBackground', Boolean)) buildScanExtension.uploadInBackground = buildScanUploadInBackground
buildScanExtension.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
if (isAtLeast(develocityPluginVersion, '2.1') && atLeastGradle5) {
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
Expand Down Expand Up @@ -221,6 +218,9 @@ void enableDevelocityInjection() {
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
}

logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
develocity.buildScan.uploadInBackground = buildScanUploadInBackground
},
{ buildScan ->
afterEvaluate {
Expand All @@ -241,6 +241,12 @@ void enableDevelocityInjection() {
buildScan.licenseAgree = buildScanTermsOfUseAgree
}
}

// uploadInBackground available for build-scan-plugin 3.3.4 and later only
if (buildScan.metaClass.respondsTo(buildScan, 'setUploadInBackground', Boolean)) {
logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
buildScan.uploadInBackground = buildScanUploadInBackground
}
}
)

Expand Down Expand Up @@ -276,7 +282,6 @@ void enableDevelocityInjection() {
}

eachDevelocitySettingsExtension(settings) { ext ->
ext.buildScan.uploadInBackground = buildScanUploadInBackground
ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
}

Expand Down Expand Up @@ -313,6 +318,9 @@ void enableDevelocityInjection() {
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
}

logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
develocity.buildScan.uploadInBackground = buildScanUploadInBackground
},
{ gradleEnterprise ->
if (develocityUrl && develocityEnforceUrl) {
Expand All @@ -332,6 +340,12 @@ void enableDevelocityInjection() {
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
}

// 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")
gradleEnterprise.buildScan.uploadInBackground = buildScanUploadInBackground
}
}
)

Expand Down
6 changes: 6 additions & 0 deletions src/test/groovy/com/gradle/BaseInitScriptTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ abstract class BaseInitScriptTest extends Specification {
return pluginId != BUILD_SCAN && pluginVersionAtLeast('3.16')
}

boolean isCompatibleWithUploadInBackground() {
// Only BS & GE plugins 3.3.4+ & DV plugin support uploadInBackground
return (pluginId == BUILD_SCAN || pluginId == GRADLE_ENTERPRISE) && pluginVersionAtLeast('3.3.4')
|| pluginId == DEVELOCITY
}

String getConfigBlock(URI serverUri) {
switch (pluginId) {
case DEVELOCITY:
Expand Down
65 changes: 64 additions & 1 deletion src/test/groovy/com/gradle/TestDevelocityInjection.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.util.GradleVersion
import spock.lang.Requires

import static com.gradle.BaseInitScriptTest.DvPluginId.BUILD_SCAN
import static com.gradle.BaseInitScriptTest.DvPluginId.GRADLE_ENTERPRISE

class TestDevelocityInjection extends BaseInitScriptTest {
static final List<TestGradleVersion> CCUD_COMPATIBLE_GRADLE_VERSIONS = ALL_GRADLE_VERSIONS - [GRADLE_3_X]

Expand Down Expand Up @@ -260,6 +263,48 @@ class TestDevelocityInjection extends BaseInitScriptTest {
[testGradle, testDvPlugin] << getVersionsToTestForExistingDvPlugin()
}

@Requires({data.testGradle.compatibleWithCurrentJvm})
def "can configure uploadInBackground when Develocity plugin is applied by the init script"() {
when:
def result = run(testGradle, testConfig(testDvPlugin.version).withUploadInBackground(true))

then:
if (testGradle.gradleVersion < GRADLE_5) {
// Gradle 4.x and earlier will always inject build-scan-plugin 1.16 which doesn't have uploadInBackground
outputMissesUploadInBackground(result)
} else {
outputContainsUploadInBackground(result, true)
}

and:
outputContainsBuildScanUrl(result)

where:
[testGradle, testDvPlugin] << getVersionsToTestForPluginInjection()
}

@Requires({data.testGradle.compatibleWithCurrentJvm})
def "can configure uploadInBackground when Develocity plugin already applied"() {
given:
declareDvPluginApplication(testGradle, testDvPlugin, null, mockScansServer.address)

when:
def result = run(testGradle, testConfig().withoutDevelocityPluginVersion())

then:
if (testDvPlugin.compatibleWithUploadInBackground) {
outputContainsUploadInBackground(result, true)
} else {
outputMissesUploadInBackground(result)
}

and:
outputContainsBuildScanUrl(result)

where:
[testGradle, testDvPlugin] << getVersionsToTestForExistingDvPlugin()
}

@Requires({data.testGradle.compatibleWithCurrentJvm})
def "can configure alternative repository for plugins when Develocity plugin is applied by the init script"() {
when:
Expand Down Expand Up @@ -470,6 +515,18 @@ class TestDevelocityInjection extends BaseInitScriptTest {
assert 1 == result.output.count(message)
}

void outputContainsUploadInBackground(BuildResult result, boolean uploadInBackground) {
def message = "Setting uploadInBackground: $uploadInBackground"
assert result.output.contains(message)
assert 1 == result.output.count(message)
}

void outputMissesUploadInBackground(BuildResult result) {
def message = "Setting uploadInBackground:"
assert !result.output.contains(message)
assert 0 == result.output.count(message)
}

private BuildResult run(TestGradleVersion testGradle, DvInjectionTestConfig config, List<String> args = ["help"]) {
return run(args, testGradle, config.envVars)
}
Expand All @@ -493,6 +550,7 @@ class TestDevelocityInjection extends BaseInitScriptTest {
boolean captureFileFingerprints = false
String termsOfUseUrl = null
String termsOfUseAgree = null
boolean uploadInBackground = true // Need to upload in background since our Mock server doesn't cope with foreground upload

DvInjectionTestConfig(URI serverAddress, String develocityPluginVersion) {
this.serverUrl = serverAddress.toString()
Expand Down Expand Up @@ -537,13 +595,18 @@ class TestDevelocityInjection extends BaseInitScriptTest {
return this
}

DvInjectionTestConfig withUploadInBackground(boolean uploadInBackground) {
this.uploadInBackground = uploadInBackground
return this
}

Map<String, String> getEnvVars() {
Map<String, String> envVars = [
DEVELOCITY_INJECTION_INIT_SCRIPT_NAME : "develocity-injection.init.gradle",
DEVELOCITY_INJECTION_ENABLED : "true",
DEVELOCITY_URL : serverUrl,
DEVELOCITY_ALLOW_UNTRUSTED_SERVER : "true",
DEVELOCITY_BUILD_SCAN_UPLOAD_IN_BACKGROUND: "true", // Need to upload in background since our Mock server doesn't cope with foreground upload
DEVELOCITY_BUILD_SCAN_UPLOAD_IN_BACKGROUND: String.valueOf(uploadInBackground),
DEVELOCITY_AUTO_INJECTION_CUSTOM_VALUE : 'gradle-actions'
]
if (enforceUrl) envVars.put("DEVELOCITY_ENFORCE_URL", "true")
Expand Down

0 comments on commit 09f6696

Please # to comment.