mirror of
https://github.com/SonarSource/sonarqube-quality-gate-action.git
synced 2026-09-23 13:38:32 +00:00
SQQGGHA-1 Add support for SONAR_HOST_URL env variable
Co-Authored-By: Wouter Admiraal <wouter.admiraal@sonarsource.com>
This commit is contained in:
committed by
Wouter Admiraal
co-authored by
Wouter Admiraal
parent
aefc85a4c6
commit
6cb0fdfecf
@@ -50,6 +50,7 @@ jobs:
|
||||
timeout-minutes: 5
|
||||
env:
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} #OPTIONAL
|
||||
|
||||
# Optionally you can use the output from the Quality Gate in another step.
|
||||
# The possible outputs of the `quality-gate-status` variable are `PASSED`, `WARN` or `FAILED`.
|
||||
@@ -81,6 +82,8 @@ Example usage:
|
||||
|
||||
- `SONAR_TOKEN` – **Required** this is the token used to authenticate access to SonarQube. You can read more about security tokens [here](https://docs.sonarqube.org/latest/user-guide/user-token/). You can set the `SONAR_TOKEN` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).
|
||||
|
||||
- `SONAR_HOST_URL` – **Optional** this tells the scanner where SonarQube is hosted, otherwise it will get the one from the scan report. You can set the `SONAR_HOST_URL` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).
|
||||
|
||||
## Quality Gate check run
|
||||
|
||||
<img src="./images/QualityGate-check-screen.png">
|
||||
|
||||
@@ -14,8 +14,13 @@ if [[ ! -f "$metadataFile" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
serverUrl="$(sed -n 's/serverUrl=\(.*\)/\1/p' "${metadataFile}")"
|
||||
ceTaskUrl="$(sed -n 's/ceTaskUrl=\(.*\)/\1/p' "${metadataFile}")"
|
||||
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."
|
||||
@@ -36,13 +41,13 @@ analysisId="$(jq -r '.task.analysisId' <<< "${task}")"
|
||||
qualityGateUrl="${serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}"
|
||||
qualityGateStatus="$(curl --silent --fail --show-error --user "${SONAR_TOKEN}": "${qualityGateUrl}" | jq -r '.projectStatus.status')"
|
||||
|
||||
if [[ ${qualityGateStatus} == "OK" ]];then
|
||||
if [[ ${qualityGateStatus} == "OK" ]]; then
|
||||
echo '::set-output name=quality-gate-status::PASSED'
|
||||
success "Quality Gate has PASSED."
|
||||
elif [[ ${qualityGateStatus} == "WARN" ]];then
|
||||
elif [[ ${qualityGateStatus} == "WARN" ]]; then
|
||||
echo '::set-output name=quality-gate-status::WARN'
|
||||
warn "Warnings on Quality Gate."
|
||||
elif [[ ${qualityGateStatus} == "ERROR" ]];then
|
||||
elif [[ ${qualityGateStatus} == "ERROR" ]]; then
|
||||
echo '::set-output name=quality-gate-status::FAILED'
|
||||
fail "Quality Gate has FAILED."
|
||||
else
|
||||
|
||||
@@ -16,6 +16,29 @@ teardown() {
|
||||
[ "$output" = "Set the SONAR_TOKEN env variable." ]
|
||||
}
|
||||
|
||||
@test "use URL from SONAR_HOST_URL instead of metadata file when it is provided" {
|
||||
export SONAR_TOKEN="test"
|
||||
export SONAR_HOST_URL="http://sonarqube.org/" # Add a trailing slash, so we validate it correctly removes it.
|
||||
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 == "http://localhost:9000/"* ]]; then
|
||||
echo '{"error":["Not found"]}'
|
||||
elif [[ $url == "http://sonarqube.org/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 ]
|
||||
}
|
||||
|
||||
@test "fail when metadata file not exist" {
|
||||
rm -f metadata_tmp
|
||||
export SONAR_TOKEN="test"
|
||||
|
||||
Reference in New Issue
Block a user