diff --git a/script/check-quality-gate.sh b/script/check-quality-gate.sh index 5c51963..1576db0 100755 --- a/script/check-quality-gate.sh +++ b/script/check-quality-gate.sh @@ -59,6 +59,15 @@ if [[ ${status} == "PENDING" || ${status} == "IN_PROGRESS" ]] && [[ ${SECONDS} - exit 1 fi +if [[ ${status} == "CANCELED" ]]; then + fail "The SonarQube background task was CANCELED." +fi + +if [[ ${status} == "FAILED" ]]; then + errorMessage="$(jq -r '.task.errorMessage // "No error message provided."' <<< "${task}")" + fail "The SonarQube background task ${status}.${reset}\n\n${errorMessage}" +fi + analysisId="$(jq -r '.task.analysisId' <<< "${task}")" qualityGateUrl="${serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}" qualityGateStatus="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${qualityGateUrl}" | jq -r '.projectStatus.status')" diff --git a/script/common.sh b/script/common.sh index b890acf..794358f 100755 --- a/script/common.sh +++ b/script/common.sh @@ -4,12 +4,12 @@ set -e set -o pipefail -gray="\\e[37m" -blue="\\e[36m" -red="\\e[31m" -yellow="\\e[33m" -green="\\e[32m" -reset="\\e[0m" +gray="\\033[37m" +blue="\\033[36m" +red="\\033[31m" +yellow="\\033[33m" +green="\\033[32m" +reset="\\033[0m" info() { echo -e "${blue}INFO: $*${reset}"; } error() { echo -e "${red}ERROR: $*${reset}"; } diff --git a/test/check-quality-gate-test.bats b/test/check-quality-gate-test.bats index 49bf4f3..4e44dff 100755 --- a/test/check-quality-gate-test.bats +++ b/test/check-quality-gate-test.bats @@ -105,6 +105,43 @@ teardown() { [[ "$output" = *"Quality Gate not set for the project. Please configure the Quality Gate in SonarQube or remove sonarqube-quality-gate action from the workflow."* ]] } +@test "fail when Sonar background task failed with an error message" { + export SONAR_TOKEN="test" + echo "serverUrl=http://localhost:9000" >> metadata_tmp + echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3jz9LkwR9Gs0pBY" >> metadata_tmp + + #mock curl + function curl() { + echo '{"task":{"status":"FAILED","errorMessage":"Fact of life: analysis cannot be processed"}}' + } + export -f curl + + run script/check-quality-gate.sh metadata_tmp 300 + + [ "$status" -eq 1 ] + [[ "$output" = *"The SonarQube background task FAILED."* ]] + [[ "$output" = *"Fact of life: analysis cannot be processed"* ]] +} + +@test "fail when Sonar background task canceled" { + export SONAR_TOKEN="test" + echo "serverUrl=http://localhost:9000" >> metadata_tmp + echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3jz9LkwR9Gs0pBY" >> metadata_tmp + + #mock curl + function curl() { + echo '{"task":{"status":"CANCELED","errorMessage":"Analysis was canceled"}}' + } + export -f curl + + run script/check-quality-gate.sh metadata_tmp 300 + + [ "$status" -eq 1 ] + [[ "$output" = *"The SonarQube background task was CANCELED."* ]] + [[ "$output" != *"Analysis was canceled"* ]] + [[ "$output" != *"No error message provided."* ]] +} + @test "fail when polling timeout is reached" { export SONAR_TOKEN="test" echo "serverUrl=http://localhost:9000" >> metadata_tmp