Skip to content

Commit

Permalink
SQQGGHA-5 Ambiguous redirect when GITHUB_OUTPUT is not set (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
poconnor-lab49 authored Oct 25, 2022
1 parent 745e48e commit d304d05
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
8 changes: 4 additions & 4 deletions script/check-quality-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ qualityGateStatus="$(curl --location --location-trusted --max-redirs 10 --silent

printf '\n'
if [[ ${qualityGateStatus} == "OK" ]]; then
echo 'quality-gate-status=PASSED' >> ${GITHUB_OUTPUT}
set_output "quality-gate-status" "PASSED"
success "Quality Gate has PASSED."
elif [[ ${qualityGateStatus} == "WARN" ]]; then
echo 'quality-gate-status=WARN' >> ${GITHUB_OUTPUT}
set_output "quality-gate-status" "WARN"
warn "Warnings on Quality Gate."
elif [[ ${qualityGateStatus} == "ERROR" ]]; then
echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT}
set_output "quality-gate-status" "FAILED"
fail "Quality Gate has FAILED."
else
echo 'quality-gate-status=FAILED' >> ${GITHUB_OUTPUT}
set_output "quality-gate-status" "FAILED"
fail "Quality Gate not set for the project. Please configure the Quality Gate in SonarQube or remove sonarqube-quality-gate action from the workflow."
fi

9 changes: 9 additions & 0 deletions script/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ success() { echo -e "${green}✔ $*${reset}"; }
warn() { echo -e "${yellow}$*${reset}"; exit 1; }
fail() { echo -e "${red}$*${reset}"; exit 1; }

# support old GH Actions runners
set_output () {
if [[ -n "${GITHUB_OUTPUT}" ]]; then
echo "${1}=${2}" >> "${GITHUB_OUTPUT}"
else
echo "::set-output name=${1}::${2}"
fi
}

## Enable debug mode.
enable_debug() {
if [[ "${DEBUG}" == "true" ]]; then
Expand Down
55 changes: 55 additions & 0 deletions test/check-quality-gate-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,58 @@ teardown() {
[[ "${lines[0]}" = "....." ]]
[[ "${lines[1]}" = *"Quality Gate has PASSED."* ]]
}

@test "pass spaces in GITHUB_OUTPUT path are handled" {
export SONAR_TOKEN="test"
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 == *"/api/qualitygates/project_status?analysisId"* ]]; then
echo '{"projectStatus":{"status":"OK"}}'
else
echo '{"task":{"analysisId":"AXlCe3jz9LkwR9Gs0pBY","status":"SUCCESS"}}'
fi
}
export -f curl

# GITHUB_OUTPUT is a path with spaces
github_output_dir="${BATS_TEST_TMPDIR}/some subdir"
mkdir -p "${github_output_dir}"
export GITHUB_OUTPUT="${github_output_dir}/github_output"
touch "${GITHUB_OUTPUT}"

run script/check-quality-gate.sh metadata_tmp

read -r github_out_actual < "${GITHUB_OUTPUT}"

[ "$status" -eq 0 ]
[[ "${github_out_actual}" = "quality-gate-status=PASSED" ]]
[[ "$output" = *"Quality Gate has PASSED."* ]]
}

@test "pass fall back to set-output if GITHUB_OUTPUT unset" {
export SONAR_TOKEN="test"
unset GITHUB_OUTPUT
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 == *"/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 ]
[[ "$output" = *"::set-output name=quality-gate-status::PASSED"* ]]
[[ "$output" = *"Quality Gate has PASSED."* ]]
}

0 comments on commit d304d05

Please sign in to comment.