SQQGGHA-1 Add support for SONAR_HOST_URL env variable

Co-Authored-By: Wouter Admiraal <wouter.admiraal@sonarsource.com>
This commit is contained in:
Diogo Lemos
2022-07-14 16:37:52 +02:00
committed by Wouter Admiraal
co-authored by Wouter Admiraal
parent aefc85a4c6
commit 6cb0fdfecf
3 changed files with 36 additions and 5 deletions
+3
View File
@@ -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">
+5
View File
@@ -14,8 +14,13 @@ if [[ ! -f "$metadataFile" ]]; then
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."
+23
View File
@@ -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"