Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

ci: add error message if Error signal #508

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions local/configs/jenkins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ jobs:
- file: "/var/pipeline-library/src/test/resources/jobs/downstream.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/abort.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/connectionRefused.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/error.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/success.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/unstable.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/git.dsl"
Expand Down
34 changes: 31 additions & 3 deletions resources/scripts/generate-build-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ DEFAULT_HASH="{ }"
DEFAULT_LIST="[ ]"
DEFAULT_STRING='" "'

### To manipulate the steps
BASE_URL="${JENKINS_URL}"
if [ -z "${JENKINS_URL}" ] ; then
BASE_URL=${BO_JOB_URL//\/blue\/rest\/*/}
fi

### Prepare the utils for the context if possible
if [ -e "${UTILS_LIB}" ] ; then
# shellcheck disable=SC1091,SC1090
Expand Down Expand Up @@ -132,7 +138,7 @@ function fetchAndPrepareTestsInfo() {
## Tests json response differs when there were tests executed in
## the pipeline, otherwise it returns:
## { message: "no tests", code: 404, errors: [] }
if jq -e 'select(.code==404)' "${file}" > /dev/null ; then
if jq -e 'select(.code==404)' "${file}" > /dev/null 2>&1 ; then
echo "${default}" > "${file}"
else
normaliseTests "${file}"
Expand Down Expand Up @@ -205,14 +211,36 @@ function fetchAndDefaultStepsInfo() {
default=$3

fetchAndDefault "${file}" "${url}" "${default}"
normaliseSteps "${file}"

### Prepare steps errors report
output="${STEPS_ERRORS}"
jq 'map(select(.result=="FAILURE"))' "${file}" > "${output}"
if ! grep -q 'result' "${output}" ; then
echo "${default}" > "${output}"
else
### Update the displayDescription for those steps with a failure and an empty displayDescription.
### For instance, when using the pipeline step `error('foo')`
### then the 'foo' message is not shown in the BlueOcean restAPI.
###
if jq -e 'map(select(.type=="STEP" and .result=="FAILURE" and .displayDescription==null))' "${output}" > /dev/null ; then
tmp="$(mktemp -d)/step.log"
for href in $(jq -r 'map(select(.type=="STEP" and .result=="FAILURE" and .displayDescription==null) | ._links.self.href) | .[]' "${output}"); do
id=$(basename "${href}")
new=$(curl -s "${BASE_URL}${href}log/" | head -c 100)
curlCommand "${tmp}" "${BASE_URL}${href}log/"
## If the URL was unreachable then the file won't exist.
## For such use case, then avoid any transformation.
if [ -e "${tmp}" ] ; then
cachedout marked this conversation as resolved.
Show resolved Hide resolved
new=$(head -c 100 "${tmp}")
jq --arg id "${id}" --arg new "${new}" '(.[] | select(.result=="FAILURE" and .displayDescription==null and .id==$id) | .displayDescription) |= $new' "${output}" > "$tmp" && mv "$tmp" "${output}"
fi
done
fi
fi

## Normalise later on
normaliseSteps "${file}"
normaliseSteps "${output}"
}

function fetchAndDefaultTestsErrors() {
Expand All @@ -225,7 +253,7 @@ function fetchAndDefaultTestsErrors() {
## Tests json response differs when there were tests executed in
## the pipeline, otherwise it returns:
## { message: "no tests", code: 404, errors: [] }
if jq -e 'select(.code==404)' "${file}" > /dev/null ; then
if jq -e 'select(.code==404)' "${file}" > /dev/null 2>&1 ; then
echo "${default}" > "${file}"
else
normaliseTests "${file}"
Expand Down
18 changes: 18 additions & 0 deletions src/test/groovy/GenerateBuildDataIntegrationTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

import com.github.tomakehurst.wiremock.junit.WireMockRule
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import net.sf.json.JSONSerializer
import org.junit.Rule
Expand Down Expand Up @@ -106,6 +107,23 @@ class GenerateBuildDataIntegrationTests {
assertFalse(obj.get("build").isEmpty())
}

@Test
public void errorBuild() {
String jobUrl = this.URL + "/error/"
Process process = runCommand(jobUrl, jobUrl + "runs/1", "UNSTABLE", "1")
assertEquals("Process did finish successfully", 0, process.waitFor())

// Tests were not executed
JSONObject obj = JSONSerializer.toJSON(new File("target/tests-info.json").text)
assertTrue(obj.isEmpty())

JSONArray errors = JSONSerializer.toJSON(new File("target/steps-errors.json").text)
assertFalse("There are steps errors", errors.isEmpty())
obj = errors.get(0)
assertEquals("Log transformation happens successfully", "foo", obj.get("displayDescription"))
assertEquals("It was an error signal", "Error signal", obj.get("displayName"))
}

Process runCommand(String jobUrl, String buildUrl, String status, String runTime) {
//Build command
List<String> commands = new ArrayList<String>()
Expand Down
39 changes: 39 additions & 0 deletions src/test/resources/jobs/getBuildInfoJsonFiles/error.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
NAME = 'it/getBuildInfoJsonFiles/error'
DSL = '''pipeline {
agent { label 'local' }
stages {
stage('error') {
steps { error ('foo') }
}
}
post {
cleanup {
deleteDir()
getBuildInfoJsonFiles(env.JOB_URL, env.BUILD_NUMBER)
archiveArtifacts artifacts: '*.json'
sh """#!/bin/bash -xe
## Assert json modifications
jq '.build.result' build-report.json | grep 'FAILURE'
jq '.build.state' build-report.json | grep 'FINISHED'
jq '.test_summary.total' build-report.json | grep '0'
jq 'map(select(.type=="STEP" and .result=="FAILURE" and .displayDescription=="foo"))' steps-errors.json | grep "foo"
## Assert all the files are there
[ -e 'artifacts-info.json' ] && echo yeah || exit 1
[ -e 'changeSet-info.json' ] && echo yeah || exit 1
[ -e 'job-info.json' ] && echo yeah || exit 1
[ -e 'tests-summary.json' ] && echo yeah || exit 1
[ -e 'tests-info.json' ] && echo yeah || exit 1
[ -e 'steps-info.json' ] && echo yeah || exit 1
[ -e 'steps-error.json' ] && echo yeah || exit 1
"""
}
}
}'''

pipelineJob(NAME) {
definition {
cps {
script(DSL.stripIndent())
}
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id" : "e7860b67-f182-4323-af71-44bb34eceb3b",
"name" : "blue_rest_organizations_jenkins_pipelines_it_getbuildinfojsonfiles_error_runs_1_artifacts",
"request" : {
"url" : "/blue/rest/organizations/jenkins/pipelines/it/getBuildInfoJsonFiles/error/runs/1/artifacts/",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "[{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Aartifacts-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:artifacts-info.json\",\"name\":\"artifacts-info.json\",\"path\":\"artifacts-info.json\",\"size\":3,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/artifacts-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Abuild-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:build-info.json\",\"name\":\"build-info.json\",\"path\":\"build-info.json\",\"size\":21427,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/build-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Abuild-report.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:build-report.json\",\"name\":\"build-report.json\",\"path\":\"build-report.json\",\"size\":54858,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/build-report.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253AchangeSet-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:changeSet-info.json\",\"name\":\"changeSet-info.json\",\"path\":\"changeSet-info.json\",\"size\":14753,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/changeSet-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Ajob-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:job-info.json\",\"name\":\"job-info.json\",\"path\":\"job-info.json\",\"size\":18250,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/job-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Asteps-errors.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:steps-errors.json\",\"name\":\"steps-errors.json\",\"path\":\"steps-errors.json\",\"size\":261,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/steps-errors.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Asteps-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:steps-info.json\",\"name\":\"steps-info.json\",\"path\":\"steps-info.json\",\"size\":2011,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/steps-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Atests-errors.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:tests-errors.json\",\"name\":\"tests-errors.json\",\"path\":\"tests-errors.json\",\"size\":4,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/tests-errors.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Atests-info.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:tests-info.json\",\"name\":\"tests-info.json\",\"path\":\"tests-info.json\",\"size\":4,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/tests-info.json\"},{\"_class\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1/artifacts/io.jenkins.blueocean.service.embedded.rest.ArtifactImpl%253Atests-summary.json/\"}},\"downloadable\":true,\"id\":\"io.jenkins.blueocean.service.embedded.rest.ArtifactImpl:tests-summary.json\",\"name\":\"tests-summary.json\",\"path\":\"tests-summary.json\",\"size\":343,\"url\":\"/job/it/job/getBuildInfoJsonFiles/job/error/1/artifact/tests-summary.json\"}]",
"headers" : {
"Date" : "Thu, 30 Apr 2020 16:36:18 GMT",
"X-Content-Type-Options" : "nosniff",
"X-Blueocean-Refresher" : "662a605d",
"Cache-Control" : "no-cache, no-store, no-transform",
"Link" : "</blue/rest/organizations/jenkins/pipelines/it/getBuildInfoJsonFiles/error/runs/1/artifacts/?start=100&limit=100>; rel=\"next\"",
"X-Jenkins" : "2.230",
"X-Jenkins-Session" : "662a605d",
"Content-Type" : "application/json;charset=utf-8",
"Server" : "Jetty(9.4.27.v20200227)"
}
},
"uuid" : "e7860b67-f182-4323-af71-44bb34eceb3b",
"persistent" : true,
"insertionIndex" : 41
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id" : "99dd832b-b8ee-4db2-9982-dca134cae081",
"name" : "blue_rest_organizations_jenkins_pipelines_it_getbuildinfojsonfiles_error_runs_1_bluetestsummary",
"request" : {
"url" : "/blue/rest/organizations/jenkins/pipelines/it/getBuildInfoJsonFiles/error/runs/1/blueTestSummary/",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "{\"_class\":\"io.jenkins.blueocean.rest.model.BlueTestSummary\",\"_links\":{\"self\":{\"_class\":\"io.jenkins.blueocean.rest.hal.Link\",\"href\":\"/blue/rest/organizations/jenkins/pipelines/it/pipelines/getBuildInfoJsonFiles/pipelines/error/runs/1//blueTestSummary/\"}},\"existingFailed\":0,\"failed\":0,\"fixed\":0,\"passed\":0,\"regressions\":0,\"skipped\":0,\"total\":0}",
"headers" : {
"Date" : "Thu, 30 Apr 2020 16:36:18 GMT",
"X-Content-Type-Options" : "nosniff",
"X-Blueocean-Refresher" : "662a605d",
"Cache-Control" : "no-cache, no-store, no-transform",
"X-Jenkins" : "2.230",
"X-Jenkins-Session" : "662a605d",
"Content-Type" : "application/json;charset=utf-8",
"Server" : "Jetty(9.4.27.v20200227)"
}
},
"uuid" : "99dd832b-b8ee-4db2-9982-dca134cae081",
"persistent" : true,
"insertionIndex" : 39
}
Loading