Add a polling timeout (#50)

The property timeout-minutes in a GitHub step is not supported in composed actions. So the workaround to stop the waiting for finishing the sonar scan is not working in such cases.
To fix this issue, a polling timeout variable was introduced, which stops the waiting if the timeout is reached.
This commit is contained in:
C. Münker
2024-08-30 10:37:47 +02:00
committed by GitHub
parent 72f24ebf1f
commit dc2f7b0dd9
3 changed files with 74 additions and 11 deletions
+15 -1
View File
@@ -8,12 +8,19 @@ if [[ -z "${SONAR_TOKEN}" ]]; then
fi
metadataFile="$1"
pollingTimeoutSec="$2"
if [[ ! -f "$metadataFile" ]]; then
echo "$metadataFile does not exist."
exit 1
fi
if [[ ! $pollingTimeoutSec =~ ^[0-9]+$ || $pollingTimeoutSec -le 0 ]]; then
echo "'$pollingTimeoutSec' is an invalid value for the polling timeout. Please use a positive, non-zero number."
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}")"
@@ -37,7 +44,9 @@ 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
endTime=$(( ${SECONDS} + ${pollingTimeoutSec} ))
until [[ ${status} != "PENDING" && ${status} != "IN_PROGRESS" || ${SECONDS} -ge ${endTime} ]]; do
printf '.'
sleep 5
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
@@ -45,6 +54,11 @@ until [[ ${status} != "PENDING" && ${status} != "IN_PROGRESS" ]]; do
done
printf '\n'
if [[ ${status} == "PENDING" || ${status} == "IN_PROGRESS" ]] && [[ ${SECONDS} -ge ${endTime} ]]; then
echo "Polling timeout reached for waiting for finishing of the Sonar scan! Aborting the check for SonarQube's Quality Gate."
exit 1
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')"