mirror of
https://github.com/SonarSource/sonarqube-quality-gate-action.git
synced 2026-09-23 13:38:32 +00:00
59 lines
2.1 KiB
Bash
Executable File
59 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
source "$(dirname "$0")/common.sh"
|
|
|
|
if [[ -z "${SONAR_TOKEN}" ]]; then
|
|
echo "Set the SONAR_TOKEN env variable."
|
|
exit 1
|
|
fi
|
|
|
|
metadataFile="$1"
|
|
|
|
if [[ ! -f "$metadataFile" ]]; then
|
|
echo "$metadataFile does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -z "${SONAR_HOST_URL}" ]]; then
|
|
serverUrl="${SONAR_HOST_URL%/}"
|
|
ceTaskUrl="${SONAR_HOST_URL%/}/api$(sed -n 's/^ceTaskUrl=.*api//p' "${metadataFile}")"
|
|
else
|
|
serverUrl="$(sed -n 's/serverUrl=\(.*\)/\1/p' "${metadataFile}")"
|
|
ceTaskUrl="$(sed -n 's/ceTaskUrl=\(.*\)/\1/p' "${metadataFile}")"
|
|
fi
|
|
|
|
if [ -z "${serverUrl}" ] || [ -z "${ceTaskUrl}" ]; then
|
|
echo "Invalid report metadata file."
|
|
exit 1
|
|
fi
|
|
|
|
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
|
|
status="$(jq -r '.task.status' <<< "$task")"
|
|
|
|
until [[ ${status} != "PENDING" && ${status} != "IN_PROGRESS" ]]; do
|
|
printf '.'
|
|
sleep 5s
|
|
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
|
|
status="$(jq -r '.task.status' <<< "$task")"
|
|
done
|
|
|
|
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'
|
|
if [[ ${qualityGateStatus} == "OK" ]]; then
|
|
echo 'quality-gate-status=PASSED' >> ${GITHUB_OUTPUT}
|
|
success "Quality Gate has PASSED."
|
|
elif [[ ${qualityGateStatus} == "WARN" ]]; then
|
|
echo 'quality-gate-status=WARN' >> ${GITHUB_OUTPUT}
|
|
warn "Warnings on Quality Gate."
|
|
elif [[ ${qualityGateStatus} == "ERROR" ]]; then
|
|
echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT}
|
|
fail "Quality Gate has FAILED."
|
|
else
|
|
echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT}
|
|
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
|
|
|