Skip to content

Commit fa53c66

Browse files
authored
uploadInBackground can be configured even Develocity plugin is already applied (#36)
Allow uploadInBackground to be configured when Develocity plugin already applied. This is necessary for the Develocity Build Validation Scripts, but could also be useful for CI plugins. Where this will change behavior for CI users is if the project is currently configuring uploadInBackground = true on CI. The CI plugins disable this if configured to do so.
1 parent 56d9e3b commit fa53c66

3 files changed

Lines changed: 87 additions & 5 deletions

File tree

src/main/resources/develocity-injection.init.gradle

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ if (buildScanCaptureEnabled) {
110110
}
111111

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

116115
def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
@@ -196,8 +195,6 @@ void enableDevelocityInjection() {
196195
// Develocity plugin publishes scans by default
197196
buildScanExtension.publishAlways()
198197
}
199-
// uploadInBackground not available for build-scan-plugin 1.16
200-
if (buildScanExtension.metaClass.respondsTo(buildScanExtension, 'setUploadInBackground', Boolean)) buildScanExtension.uploadInBackground = buildScanUploadInBackground
201198
buildScanExtension.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
202199
if (isAtLeast(develocityPluginVersion, '2.1') && atLeastGradle5) {
203200
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
@@ -227,6 +224,9 @@ void enableDevelocityInjection() {
227224
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
228225
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
229226
}
227+
228+
logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
229+
develocity.buildScan.uploadInBackground = buildScanUploadInBackground
230230
},
231231
{ buildScan ->
232232
afterEvaluate {
@@ -247,6 +247,12 @@ void enableDevelocityInjection() {
247247
buildScan.licenseAgree = buildScanTermsOfUseAgree
248248
}
249249
}
250+
251+
// uploadInBackground available for build-scan-plugin 3.3.4 and later only
252+
if (buildScan.metaClass.respondsTo(buildScan, 'setUploadInBackground', Boolean)) {
253+
logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
254+
buildScan.uploadInBackground = buildScanUploadInBackground
255+
}
250256
}
251257
)
252258

@@ -282,7 +288,6 @@ void enableDevelocityInjection() {
282288
}
283289

284290
eachDevelocitySettingsExtension(settings) { ext ->
285-
ext.buildScan.uploadInBackground = buildScanUploadInBackground
286291
ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
287292
}
288293

@@ -319,6 +324,9 @@ void enableDevelocityInjection() {
319324
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
320325
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
321326
}
327+
328+
logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
329+
develocity.buildScan.uploadInBackground = buildScanUploadInBackground
322330
},
323331
{ gradleEnterprise ->
324332
if (develocityUrl && develocityEnforceUrl) {
@@ -338,6 +346,12 @@ void enableDevelocityInjection() {
338346
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
339347
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
340348
}
349+
350+
// uploadInBackground available for gradle-enterprise-plugin 3.3.4 and later only
351+
if (gradleEnterprise.buildScan.metaClass.respondsTo(gradleEnterprise.buildScan, 'setUploadInBackground', Boolean)) {
352+
logger.lifecycle("Setting uploadInBackground: $buildScanUploadInBackground")
353+
gradleEnterprise.buildScan.uploadInBackground = buildScanUploadInBackground
354+
}
341355
}
342356
)
343357

src/test/groovy/com/gradle/BaseInitScriptTest.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ abstract class BaseInitScriptTest extends Specification {
368368
return pluginId != BUILD_SCAN && pluginVersionAtLeast('3.16')
369369
}
370370

371+
boolean isCompatibleWithUploadInBackground() {
372+
if (pluginId == BUILD_SCAN || pluginId == GRADLE_ENTERPRISE) {
373+
// Only BS & GE plugins 3.3.4+ support uploadInBackground
374+
return pluginVersionAtLeast('3.3.4')
375+
}
376+
return true
377+
}
378+
371379
String getConfigBlock(URI serverUri) {
372380
switch (pluginId) {
373381
case DEVELOCITY:

src/test/groovy/com/gradle/TestDevelocityInjection.groovy

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,48 @@ class TestDevelocityInjection extends BaseInitScriptTest {
260260
[testGradle, testDvPlugin] << getVersionsToTestForExistingDvPlugin()
261261
}
262262

263+
@Requires({data.testGradle.compatibleWithCurrentJvm})
264+
def "can configure uploadInBackground when Develocity plugin is applied by the init script"() {
265+
when:
266+
def result = run(testGradle, testConfig(testDvPlugin.version).withUploadInBackground(true))
267+
268+
then:
269+
if (testGradle.gradleVersion < GRADLE_5) {
270+
// Gradle 4.x and earlier will always inject build-scan-plugin 1.16 which doesn't have uploadInBackground
271+
outputMissesUploadInBackground(result)
272+
} else {
273+
outputContainsUploadInBackground(result, true)
274+
}
275+
276+
and:
277+
outputContainsBuildScanUrl(result)
278+
279+
where:
280+
[testGradle, testDvPlugin] << getVersionsToTestForPluginInjection()
281+
}
282+
283+
@Requires({data.testGradle.compatibleWithCurrentJvm})
284+
def "can configure uploadInBackground when Develocity plugin already applied"() {
285+
given:
286+
declareDvPluginApplication(testGradle, testDvPlugin, null, mockScansServer.address)
287+
288+
when:
289+
def result = run(testGradle, testConfig().withoutDevelocityPluginVersion())
290+
291+
then:
292+
if (testDvPlugin.compatibleWithUploadInBackground) {
293+
outputContainsUploadInBackground(result, true)
294+
} else {
295+
outputMissesUploadInBackground(result)
296+
}
297+
298+
and:
299+
outputContainsBuildScanUrl(result)
300+
301+
where:
302+
[testGradle, testDvPlugin] << getVersionsToTestForExistingDvPlugin()
303+
}
304+
263305
@Requires({data.testGradle.compatibleWithCurrentJvm})
264306
def "can configure alternative repository for plugins when Develocity plugin is applied by the init script"() {
265307
when:
@@ -485,6 +527,18 @@ class TestDevelocityInjection extends BaseInitScriptTest {
485527
assert 1 == result.output.count(message)
486528
}
487529

530+
void outputContainsUploadInBackground(BuildResult result, boolean uploadInBackground) {
531+
def message = "Setting uploadInBackground: $uploadInBackground"
532+
assert result.output.contains(message)
533+
assert 1 == result.output.count(message)
534+
}
535+
536+
void outputMissesUploadInBackground(BuildResult result) {
537+
def message = "Setting uploadInBackground:"
538+
assert !result.output.contains(message)
539+
assert 0 == result.output.count(message)
540+
}
541+
488542
private BuildResult run(TestGradleVersion testGradle, DvInjectionTestConfig config, List<String> args = ["help"]) {
489543
return run(args, testGradle, config.envVars)
490544
}
@@ -508,6 +562,7 @@ class TestDevelocityInjection extends BaseInitScriptTest {
508562
boolean captureFileFingerprints = false
509563
String termsOfUseUrl = null
510564
String termsOfUseAgree = null
565+
boolean uploadInBackground = true // Need to upload in background since our Mock server doesn't cope with foreground upload
511566

512567
DvInjectionTestConfig(URI serverAddress, String develocityPluginVersion) {
513568
this.serverUrl = serverAddress.toString()
@@ -552,13 +607,18 @@ class TestDevelocityInjection extends BaseInitScriptTest {
552607
return this
553608
}
554609

610+
DvInjectionTestConfig withUploadInBackground(boolean uploadInBackground) {
611+
this.uploadInBackground = uploadInBackground
612+
return this
613+
}
614+
555615
Map<String, String> getEnvVars() {
556616
Map<String, String> envVars = [
557617
DEVELOCITY_INJECTION_INIT_SCRIPT_NAME : "develocity-injection.init.gradle",
558618
DEVELOCITY_INJECTION_ENABLED : "true",
559619
DEVELOCITY_URL : serverUrl,
560620
DEVELOCITY_ALLOW_UNTRUSTED_SERVER : "true",
561-
DEVELOCITY_BUILD_SCAN_UPLOAD_IN_BACKGROUND: "true", // Need to upload in background since our Mock server doesn't cope with foreground upload
621+
DEVELOCITY_BUILD_SCAN_UPLOAD_IN_BACKGROUND: String.valueOf(uploadInBackground),
562622
DEVELOCITY_AUTO_INJECTION_CUSTOM_VALUE : 'gradle-actions'
563623
]
564624
if (enforceUrl) envVars.put("DEVELOCITY_ENFORCE_URL", "true")

0 commit comments

Comments
 (0)