From 132237ba057d83a8ae0fcde1007ed74d51576e29 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Wed, 15 Jun 2022 08:08:41 -0600 Subject: [PATCH] Capture failure to publish build scan in results --- ...build-result-capture-service.plugin.groovy | 3 +- .../build-result-capture.init.gradle | 30 +++++++++++++++++-- .../BaseInitScriptTest.groovy | 13 ++++++++ .../TestBuildResultRecorder.groovy | 19 +++++++++++- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/resources/init-scripts/build-result-capture-service.plugin.groovy b/src/resources/init-scripts/build-result-capture-service.plugin.groovy index 2d6482f..e8ea40c 100644 --- a/src/resources/init-scripts/build-result-capture-service.plugin.groovy +++ b/src/resources/init-scripts/build-result-capture-service.plugin.groovy @@ -41,7 +41,8 @@ abstract class BuildResultsRecorder implements BuildService + def buildResults = [ + rootProjectName: rootProjectName, + rootProjectDir: rootProjectDir, + requestedTasks: requestedTasks, + gradleVersion: gradleVersion, + gradleHomeDir: gradleHomeDir, + buildFailed: buildFailed, + buildScanUri: null, + buildScanFailed: true + ] + + def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") + buildResultsDir.mkdirs() + def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") + + // Overwrite any contents written by buildFinished or build service, since this result is a superset. + if (buildResultsFile.exists()) { + buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults) + } else { + buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) + } + } } } @@ -89,7 +114,8 @@ def captureUsingBuildFinished(gradle, invocationId) { gradleVersion: GradleVersion.current().version, gradleHomeDir: gradle.gradleHomeDir.absolutePath, buildFailed: result.failure != null, - buildScanUri: null + buildScanUri: null, + buildScanFailed: false ] def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") 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 f5a9d06..4f62235 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 @@ -45,6 +45,7 @@ class BaseInitScriptTest extends Specification { 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' + boolean failScanUpload = false File settingsFile File buildFile @@ -59,6 +60,10 @@ class BaseInitScriptTest extends Specification { handlers { post('in/:gradleVersion/:pluginVersion') { + if (failScanUpload) { + context.response.status(401).send() + return + } def scanUrlString = "${mockScansServer.address}s/$PUBLIC_BUILD_SCAN_ID" def body = [ id : PUBLIC_BUILD_SCAN_ID, @@ -72,6 +77,10 @@ class BaseInitScriptTest extends Specification { } prefix('scans/publish') { post('gradle/:pluginVersion/token') { + if (failScanUpload) { + context.response.status(401).send() + return + } def pluginVersion = context.pathTokens.pluginVersion def scanUrlString = "${mockScansServer.address}s/$PUBLIC_BUILD_SCAN_ID" def body = [ @@ -85,6 +94,10 @@ class BaseInitScriptTest extends Specification { .send(jsonWriter.writeValueAsBytes(body)) } post('gradle/:pluginVersion/upload') { + if (failScanUpload) { + context.response.status(401).send() + return + } context.request.getBody(1024 * 1024 * 10).then { context.response .contentType('application/vnd.gradle.scan-upload-ack+json') 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 813fbba..48baa2d 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 @@ -118,7 +118,23 @@ class TestBuildResultRecorder extends BaseInitScriptTest { testGradleVersion << CONFIGURATION_CACHE_VERSIONS } - void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan) { + def "produces build results file for failing build on #testGradleVersion when build scan publish fails"() { + assumeTrue testGradleVersion.compatibleWithCurrentJvm + + when: + declareGePluginApplication(testGradleVersion.gradleVersion) + addFailingTaskToBuild() + failScanUpload = true + runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion) + + then: + assertResults('expectFailure', testGradleVersion, true, false, true) + + where: + testGradleVersion << ALL_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 assert results['rootProjectDir'] == testProjectDir.canonicalPath @@ -127,6 +143,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest { assert results['gradleHomeDir'] != null assert results['buildFailed'] == hasFailure assert results['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null) + assert results['buildScanFailed'] == scanUploadFailed } private File getBuildResultFile() {