bashshellquality-gate

Use output of a CURL and fail if the output contains ERROR


I am using this below CURL statement:

curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY

Output: Actual Output

Using IF statement to pass/fail the above scenario.

if [ "$quality_gatesstatus" != "OK" ] && [ "$quality_gatesstatus" != "WARN" ]; then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Sonar Quality gate succeeded!!!"  
fi

But either of the true/false condition it is only printing first echo statement and failing the job. So what is the mistake I am doing here, one more things I cannot use "jq".


Solution

  • Without jq this should to the trick:

    quality_gatesstatus=$(curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY)
    echo "$quality_gatesstatus" | grep '"status":"ERROR"' > /dev/null
    if [ $? -eq 0 ]; then
        echo "check sonar server and fix the issues: $quality_gatesstatus"
        exit 1
    else
        echo "Sonar Quality gate succeeded!!!"  
    fi