Skip to content

Commit 0e75b8e

Browse files
committed
create ODS quality gate and make default
1 parent 8f4894a commit 0e75b8e

1 file changed

Lines changed: 117 additions & 1 deletion

File tree

sonarqube/configure.sh

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ authTokenVerified=""
241241
if [ "${configuredToken}" == "${sampleToken}" ]; then
242242
echo_info "Auth token in ods-core.env is the sample value."
243243
else
244-
echo_info "Checking if login with token from ods-core.env is possible ..."
244+
echo_info "Checking if login with token from ods.core.env is possible ..."
245245
if curl ${INSECURE} -sSf --user "${configuredToken}": "${SONARQUBE_URL}/api/user_tokens/search?login=cd_user" > /dev/null; then
246246
echo_info "Configured token for '${PIPELINE_USER_NAME}' verified."
247247
authTokenVerified="y"
@@ -279,6 +279,122 @@ if [ -z "${authTokenVerified}" ]; then
279279
fi
280280
fi
281281

282+
# Create and configure a quality gate and make it default.
283+
echo_info "Ensuring quality gate 'ODS Default Quality Gate' exists and is set as default ..."
284+
GATE_NAME="ODS Default Quality Gate"
285+
encodedGateName="$(uriencode "${GATE_NAME}")"
286+
287+
# Check if gate exists (search by name). Fetch list first, then query with jq to avoid
288+
# complex command substitution that can introduce syntax issues.
289+
resp="$(curl ${INSECURE} -sS --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
290+
"${SONARQUBE_URL}/api/qualitygates/list" 2>/dev/null || echo '{"qualitygates": []}')"
291+
292+
gateCheck="$(echo "${resp}" | jq -r --arg name "${GATE_NAME}" '.qualitygates[]? | select(.name == $name) | .name' 2>/dev/null || echo "")"
293+
294+
if [ -z "${gateCheck}" ]; then
295+
echo_info "Quality gate '${GATE_NAME}' not found, creating ..."
296+
createResp=$(curl ${INSECURE} -sS -X POST --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
297+
"${SONARQUBE_URL}/api/qualitygates/create?name=${encodedGateName}" || true)
298+
299+
# try to get id or name from response, but continue using name for further calls
300+
gateName=$(echo "${createResp}" | jq -r '.name // empty' 2>/dev/null || echo "")
301+
302+
if [ -z "${gateName}" ]; then
303+
# creation returned only errors or minimal info — log and continue using name for further calls
304+
echo_info "Quality gate '${GATE_NAME}' creation response: ${createResp}"
305+
else
306+
echo_info "Quality gate '${GATE_NAME}' is created."
307+
fi
308+
else
309+
echo_info "Quality gate '${GATE_NAME}' already exists."
310+
fi
311+
312+
# Helper to add a condition (ignores errors if duplicate)
313+
add_condition() {
314+
local metric="$1"; shift
315+
local op="$1"; shift
316+
local error="$1"; shift
317+
local scope="${1:-}" # optional: "new" for new code conditions; anything else => overall
318+
319+
# decide onNewCode parameter
320+
local onNewParam=""
321+
if [ "${scope}" == "new" ]; then
322+
onNewParam="&onNewCode=true"
323+
echo_info "Adding condition for NEW CODE: metric='${metric}' op='${op}' error='${error}'"
324+
else
325+
onNewParam="&onNewCode=false"
326+
echo_info "Adding condition for OVERALL CODE: metric='${metric}' op='${op}' error='${error}'"
327+
fi
328+
329+
# Use gateName (encoded) instead of gateId
330+
if ! curl ${INSECURE} -sS -X POST --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
331+
"${SONARQUBE_URL}/api/qualitygates/create_condition?gateName=${encodedGateName}&metric=${metric}&op=${op}&error=${error}${onNewParam}" >/dev/null 2>&1; then
332+
echo_warn "Could not add condition (might already exist): metric='${metric}' scope='${scope}'"
333+
else
334+
echo_info "Condition for '${metric}' added (scope='${scope}')."
335+
fi
336+
}
337+
338+
# Helper to remove overall (non-new-code) condition(s) for a metric if present
339+
remove_overall_condition() {
340+
local metric="$1"
341+
echo_info "Checking for overall (non-new-code) condition(s) for metric='${metric}' to remove ..."
342+
# Fetch gate details and extract condition ids where onNewCode is false or absent (overall)
343+
gateResp=$(curl ${INSECURE} -sS --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
344+
"${SONARQUBE_URL}/api/qualitygates/show?name=${encodedGateName}" 2>/dev/null || echo '{"conditions": []}')
345+
ids=$(echo "${gateResp}" | jq -r --arg m "${metric}" '.conditions[]? | select(.metric == $m and (.onNewCode == false or .onNewCode == null)) | .id' 2>/dev/null || echo "")
346+
if [ -z "${ids}" ]; then
347+
echo_info "No overall condition for metric='${metric}' found."
348+
return 0
349+
fi
350+
for id in ${ids}; do
351+
echo_info "Removing overall condition id='${id}' for metric='${metric}' ..."
352+
if curl ${INSECURE} -sS -X POST --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
353+
"${SONARQUBE_URL}/api/qualitygates/delete_condition?id=${id}" >/dev/null 2>&1; then
354+
echo_info "Removed condition id='${id}'."
355+
else
356+
echo_warn "Failed to remove condition id='${id}' for metric='${metric}'."
357+
fi
358+
done
359+
}
360+
361+
if true; then
362+
# Conditions required by the request:
363+
# - For NEW CODE only:
364+
# - Issues is greater than 0
365+
# - Security Hotspots Reviewed is less than 100%
366+
# - Coverage is less than 80%
367+
# - Duplicated Lines (%) is greater than 3%
368+
#
369+
# - For OVERALL code:
370+
# - Security Rating is worse than A (A maps to 1 => worse than A is > 1)
371+
# - Security Hotspots Reviewed is less than 100%
372+
# - Reliability Rating is worse than C (C maps to 3 => worse than C is > 3)
373+
374+
# New-code-only conditions
375+
add_condition "issues" "GT" "0" "new"
376+
add_condition "security_hotspots_reviewed" "LT" "100" "new"
377+
add_condition "coverage" "LT" "80" "new"
378+
add_condition "duplicated_lines_density" "GT" "3" "new"
379+
380+
# Overall conditions
381+
add_condition "security_rating" "GT" "1"
382+
add_condition "reliability_rating" "GT" "3"
383+
384+
# Remove unwanted overall conditions first (coverage & duplicated lines)
385+
remove_overall_condition "coverage"
386+
remove_overall_condition "duplicated_lines_density"
387+
388+
# Set gate as default using name parameter (ignore absence of id)
389+
echo_info "Setting '${GATE_NAME}' as default quality gate (using name) ..."
390+
if curl ${INSECURE} -sS -X POST --user "${ADMIN_USER_NAME}:${ADMIN_USER_PASSWORD}" \
391+
"${SONARQUBE_URL}/api/qualitygates/set_as_default?name=${encodedGateName}"; then
392+
echo_info "Quality gate '${GATE_NAME}' set as default."
393+
else
394+
echo_warn "Failed to set '${GATE_NAME}' as default using name."
395+
fi
396+
fi
397+
282398
if [ -n "${VALUES_WRITTEN_TO_CONFIG}" ]; then
283399
echo_warn "Some values in '${ODS_CONFIGURATION_DIR}/ods-core.env' have been updated."
284400
echo_warn "Commit and push the changes to Bitbucket."

0 commit comments

Comments
 (0)