Skip to content

Commit

Permalink
Display dashboard URL if qualityGateStatus is WARN or ERROR
Browse files Browse the repository at this point in the history
  • Loading branch information
bxsx authored and jacek-poreda-sonarsource committed Aug 22, 2023
1 parent c32e6c5 commit f9fe214
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions script/check-quality-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,24 @@ until [[ ${status} != "PENDING" && ${status} != "IN_PROGRESS" ]]; do
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
status="$(jq -r '.task.status' <<< "$task")"
done
printf '\n'

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')"

printf '\n'
dashboardUrl="$(sed -n 's/dashboardUrl=\(.*\)/\1/p' "${metadataFile}")"
analysisResultMsg="Detailed information can be found at: ${dashboardUrl}\n"

if [[ ${qualityGateStatus} == "OK" ]]; then
set_output "quality-gate-status" "PASSED"
success "Quality Gate has PASSED."
elif [[ ${qualityGateStatus} == "WARN" ]]; then
set_output "quality-gate-status" "WARN"
warn "Warnings on Quality Gate."
warn "Warnings on Quality Gate.${reset}\n\n${analysisResultMsg}"
elif [[ ${qualityGateStatus} == "ERROR" ]]; then
set_output "quality-gate-status" "FAILED"
fail "Quality Gate has FAILED."
fail "Quality Gate has FAILED.${reset}\n\n${analysisResultMsg}"
else
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."
Expand Down
5 changes: 5 additions & 0 deletions test/check-quality-gate-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ teardown() {
export SONAR_TOKEN="test"
echo "serverUrl=http://localhost:9000" >> metadata_tmp
echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3gsFwOUsY8YKHTn" >> metadata_tmp
echo "dashboardUrl=http://localhost:9000/dashboard?id=project&branch=master" >> metadata_tmp

#mock curl
function curl() {
Expand All @@ -100,12 +101,14 @@ teardown() {
[ "$status" -eq 1 ]
[[ "${github_out_actual}" = "quality-gate-status=WARN" ]]
[[ "$output" = *"Warnings on Quality Gate."* ]]
[[ "$output" = *"Detailed information can be found at: http://localhost:9000/dashboard?id=project&branch=master"* ]]
}

@test "fail when Quality Gate status ERROR" {
export SONAR_TOKEN="test"
echo "serverUrl=http://localhost:9000" >> metadata_tmp
echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3gsFwOUsY8YKHTn" >> metadata_tmp
echo "dashboardUrl=http://localhost:9000/dashboard?id=project&branch=master" >> metadata_tmp

#mock curl
function curl() {
Expand All @@ -125,6 +128,7 @@ teardown() {
[ "$status" -eq 1 ]
[[ "${github_out_actual}" = "quality-gate-status=FAILED" ]]
[[ "$output" = *"Quality Gate has FAILED."* ]]
[[ "$output" = *"Detailed information can be found at: http://localhost:9000/dashboard?id=project&branch=master"* ]]
}

@test "pass when Quality Gate status OK" {
Expand All @@ -150,6 +154,7 @@ teardown() {
[ "$status" -eq 0 ]
[[ "${github_out_actual}" = "quality-gate-status=PASSED" ]]
[[ "$output" = *"Quality Gate has PASSED."* ]]
[[ "$output" != *"Detailed information can be found at:"* ]]
}

@test "pass when Quality Gate status OK and status starts from IN_PROGRESS" {
Expand Down

0 comments on commit f9fe214

Please sign in to comment.