diff --git a/script/check-quality-gate.sh b/script/check-quality-gate.sh index 8b0ae3c..bea66f2 100755 --- a/script/check-quality-gate.sh +++ b/script/check-quality-gate.sh @@ -43,16 +43,16 @@ qualityGateStatus="$(curl --location --location-trusted --max-redirs 10 --silent printf '\n' if [[ ${qualityGateStatus} == "OK" ]]; then - echo 'quality-gate-status=PASSED' >> ${GITHUB_OUTPUT} + set_output "quality-gate-status" "PASSED" success "Quality Gate has PASSED." elif [[ ${qualityGateStatus} == "WARN" ]]; then - echo 'quality-gate-status=WARN' >> ${GITHUB_OUTPUT} + set_output "quality-gate-status" "WARN" warn "Warnings on Quality Gate." elif [[ ${qualityGateStatus} == "ERROR" ]]; then - echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT} + set_output "quality-gate-status" "FAILED" fail "Quality Gate has FAILED." else - echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT} + set_output "quality-gate-status" "FAILED" fail "Quality Gate not set for the project. Please configure the Quality Gate in SonarQube or remove sonarqube-quality-gate action from the workflow." fi diff --git a/script/common.sh b/script/common.sh index a347869..b890acf 100755 --- a/script/common.sh +++ b/script/common.sh @@ -23,6 +23,15 @@ success() { echo -e "${green}✔ $*${reset}"; } warn() { echo -e "${yellow}✖ $*${reset}"; exit 1; } fail() { echo -e "${red}✖ $*${reset}"; exit 1; } +# support old GH Actions runners +set_output () { + if [[ -n "${GITHUB_OUTPUT}" ]]; then + echo "${1}=${2}" >> "${GITHUB_OUTPUT}" + else + echo "::set-output name=${1}::${2}" + fi +} + ## Enable debug mode. enable_debug() { if [[ "${DEBUG}" == "true" ]]; then diff --git a/test/check-quality-gate-test.bats b/test/check-quality-gate-test.bats index 29d2016..1e5c534 100755 --- a/test/check-quality-gate-test.bats +++ b/test/check-quality-gate-test.bats @@ -192,3 +192,58 @@ teardown() { [[ "${lines[0]}" = "....." ]] [[ "${lines[1]}" = *"Quality Gate has PASSED."* ]] } + +@test "pass spaces in GITHUB_OUTPUT path are handled" { + export SONAR_TOKEN="test" + echo "serverUrl=http://localhost:9000" >> metadata_tmp + echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3gsFwOUsY8YKHTn" >> metadata_tmp + + #mock curl + function curl() { + url="${@: -1}" + if [[ $url == *"/api/qualitygates/project_status?analysisId"* ]]; then + echo '{"projectStatus":{"status":"OK"}}' + else + echo '{"task":{"analysisId":"AXlCe3jz9LkwR9Gs0pBY","status":"SUCCESS"}}' + fi + } + export -f curl + + # GITHUB_OUTPUT is a path with spaces + github_output_dir="${BATS_TEST_TMPDIR}/some subdir" + mkdir -p "${github_output_dir}" + export GITHUB_OUTPUT="${github_output_dir}/github_output" + touch "${GITHUB_OUTPUT}" + + run script/check-quality-gate.sh metadata_tmp + + read -r github_out_actual < "${GITHUB_OUTPUT}" + + [ "$status" -eq 0 ] + [[ "${github_out_actual}" = "quality-gate-status=PASSED" ]] + [[ "$output" = *"Quality Gate has PASSED."* ]] +} + +@test "pass fall back to set-output if GITHUB_OUTPUT unset" { + export SONAR_TOKEN="test" + unset GITHUB_OUTPUT + echo "serverUrl=http://localhost:9000" >> metadata_tmp + echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3gsFwOUsY8YKHTn" >> metadata_tmp + + #mock curl + function curl() { + url="${@: -1}" + if [[ $url == *"/api/qualitygates/project_status?analysisId"* ]]; then + echo '{"projectStatus":{"status":"OK"}}' + else + echo '{"task":{"analysisId":"AXlCe3jz9LkwR9Gs0pBY","status":"SUCCESS"}}' + fi + } + export -f curl + + run script/check-quality-gate.sh metadata_tmp + + [ "$status" -eq 0 ] + [[ "$output" = *"::set-output name=quality-gate-status::PASSED"* ]] + [[ "$output" = *"Quality Gate has PASSED."* ]] +}