mirror of
https://github.com/SonarSource/sonarqube-quality-gate-action.git
synced 2026-10-05 19:31:31 +00:00
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:
1 parent
72f24ebf1f
commit
dc2f7b0dd9
3 files changed
+74
-11
No files matched your search
+5
-1
@@ -8,13 +8,17 @@ runs:
|
|||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- id: quality-gate-check
|
- id: quality-gate-check
|
||||||
run: $GITHUB_ACTION_PATH/script/check-quality-gate.sh ${{ inputs.scanMetadataReportFile }}
|
run: $GITHUB_ACTION_PATH/script/check-quality-gate.sh "${{ inputs.scanMetadataReportFile }}" "${{ inputs.pollingTimeoutSec }}"
|
||||||
shell: bash
|
shell: bash
|
||||||
inputs:
|
inputs:
|
||||||
scanMetadataReportFile:
|
scanMetadataReportFile:
|
||||||
description: Location of the scanner metadata report file
|
description: Location of the scanner metadata report file
|
||||||
required: false
|
required: false
|
||||||
default: .scannerwork/report-task.txt
|
default: .scannerwork/report-task.txt
|
||||||
|
pollingTimeoutSec:
|
||||||
|
description: "The maximum time (in seconds) to poll for SonarQube's Quality Gate status. Default: 300."
|
||||||
|
required: false
|
||||||
|
default: "300"
|
||||||
outputs:
|
outputs:
|
||||||
quality-gate-status:
|
quality-gate-status:
|
||||||
description: >
|
description: >
|
||||||
|
|||||||
@@ -8,12 +8,19 @@ if [[ -z "${SONAR_TOKEN}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
metadataFile="$1"
|
metadataFile="$1"
|
||||||
|
pollingTimeoutSec="$2"
|
||||||
|
|
||||||
|
|
||||||
if [[ ! -f "$metadataFile" ]]; then
|
if [[ ! -f "$metadataFile" ]]; then
|
||||||
echo "$metadataFile does not exist."
|
echo "$metadataFile does not exist."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
if [[ ! -z "${SONAR_HOST_URL}" ]]; then
|
||||||
serverUrl="${SONAR_HOST_URL%/}"
|
serverUrl="${SONAR_HOST_URL%/}"
|
||||||
ceTaskUrl="${SONAR_HOST_URL%/}/api$(sed -n 's/^ceTaskUrl=.*api//p' "${metadataFile}")"
|
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}")"
|
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
|
||||||
status="$(jq -r '.task.status' <<< "$task")"
|
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 '.'
|
printf '.'
|
||||||
sleep 5
|
sleep 5
|
||||||
task="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${ceTaskUrl}")"
|
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
|
done
|
||||||
printf '\n'
|
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}")"
|
analysisId="$(jq -r '.task.analysisId' <<< "${task}")"
|
||||||
qualityGateUrl="${serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}"
|
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')"
|
qualityGateStatus="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${qualityGateUrl}" | jq -r '.projectStatus.status')"
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,11 +52,39 @@ teardown() {
|
|||||||
|
|
||||||
@test "fail when empty metadata file" {
|
@test "fail when empty metadata file" {
|
||||||
export SONAR_TOKEN="test"
|
export SONAR_TOKEN="test"
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
[ "$output" = "Invalid report metadata file." ]
|
[ "$output" = "Invalid report metadata file." ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "fail when no polling timeout is provided" {
|
||||||
|
export SONAR_TOKEN="test"
|
||||||
|
run script/check-quality-gate.sh metadata_tmp
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[ "$output" = "'' is an invalid value for the polling timeout. Please use a positive, non-zero number." ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "fail when polling timeout is not a number" {
|
||||||
|
export SONAR_TOKEN="test"
|
||||||
|
run script/check-quality-gate.sh metadata_tmp metadata_tmp
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[ "$output" = "'metadata_tmp' is an invalid value for the polling timeout. Please use a positive, non-zero number." ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "fail when polling timeout is zero" {
|
||||||
|
export SONAR_TOKEN="test"
|
||||||
|
run script/check-quality-gate.sh metadata_tmp 0
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[ "$output" = "'0' is an invalid value for the polling timeout. Please use a positive, non-zero number." ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "fail when polling timeout is negative" {
|
||||||
|
export SONAR_TOKEN="test"
|
||||||
|
run script/check-quality-gate.sh metadata_tmp -1
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[ "$output" = "'-1' is an invalid value for the polling timeout. Please use a positive, non-zero number." ]
|
||||||
|
}
|
||||||
|
|
||||||
@test "fail when no Quality Gate status" {
|
@test "fail when no Quality Gate status" {
|
||||||
export SONAR_TOKEN="test"
|
export SONAR_TOKEN="test"
|
||||||
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
||||||
@@ -68,7 +96,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < ${GITHUB_OUTPUT}
|
read -r github_out_actual < ${GITHUB_OUTPUT}
|
||||||
|
|
||||||
@@ -77,6 +105,23 @@ 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."* ]]
|
[[ "$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 polling timeout is reached" {
|
||||||
|
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":{"analysisId":"AXlCe3jz9LkwR9Gs0pBY","status":"PENDING"}}'
|
||||||
|
}
|
||||||
|
export -f curl
|
||||||
|
|
||||||
|
run script/check-quality-gate.sh metadata_tmp 5
|
||||||
|
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[[ "$output" = *"Polling timeout reached for waiting for finishing of the Sonar scan! Aborting the check for SonarQube's Quality Gate."* ]]
|
||||||
|
}
|
||||||
|
|
||||||
@test "fail when Quality Gate status WARN" {
|
@test "fail when Quality Gate status WARN" {
|
||||||
export SONAR_TOKEN="test"
|
export SONAR_TOKEN="test"
|
||||||
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
||||||
@@ -94,7 +139,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < ${GITHUB_OUTPUT}
|
read -r github_out_actual < ${GITHUB_OUTPUT}
|
||||||
|
|
||||||
@@ -121,7 +166,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < ${GITHUB_OUTPUT}
|
read -r github_out_actual < ${GITHUB_OUTPUT}
|
||||||
|
|
||||||
@@ -147,7 +192,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < ${GITHUB_OUTPUT}
|
read -r github_out_actual < ${GITHUB_OUTPUT}
|
||||||
|
|
||||||
@@ -187,7 +232,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f sleep
|
export -f sleep
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < ${GITHUB_OUTPUT}
|
read -r github_out_actual < ${GITHUB_OUTPUT}
|
||||||
|
|
||||||
@@ -220,7 +265,7 @@ teardown() {
|
|||||||
export GITHUB_OUTPUT="${github_output_dir}/github_output"
|
export GITHUB_OUTPUT="${github_output_dir}/github_output"
|
||||||
touch "${GITHUB_OUTPUT}"
|
touch "${GITHUB_OUTPUT}"
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
read -r github_out_actual < "${GITHUB_OUTPUT}"
|
read -r github_out_actual < "${GITHUB_OUTPUT}"
|
||||||
|
|
||||||
@@ -246,7 +291,7 @@ teardown() {
|
|||||||
}
|
}
|
||||||
export -f curl
|
export -f curl
|
||||||
|
|
||||||
run script/check-quality-gate.sh metadata_tmp
|
run script/check-quality-gate.sh metadata_tmp 300
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[[ "$output" = *"::set-output name=quality-gate-status::PASSED"* ]]
|
[[ "$output" = *"::set-output name=quality-gate-status::PASSED"* ]]
|
||||||
|
|||||||
Reference in new issue
Block a user