From 6cb0fdfecf527229d86c414a592fdc74abc768ed Mon Sep 17 00:00:00 2001 From: Diogo Lemos Date: Tue, 31 May 2022 11:56:11 +0100 Subject: [PATCH] SQQGGHA-1 Add support for SONAR_HOST_URL env variable Co-Authored-By: Wouter Admiraal --- README.md | 3 +++ script/check-quality-gate.sh | 15 ++++++++++----- test/check-quality-gate-test.bats | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e1f2688..04a5e15 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/script/check-quality-gate.sh b/script/check-quality-gate.sh index f7ecfd6..64174e0 100755 --- a/script/check-quality-gate.sh +++ b/script/check-quality-gate.sh @@ -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 diff --git a/test/check-quality-gate-test.bats b/test/check-quality-gate-test.bats index 3950a68..1be8d0b 100755 --- a/test/check-quality-gate-test.bats +++ b/test/check-quality-gate-test.bats @@ -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"