0

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".

Santosh Kumar
  • 53
  • 1
  • 10
  • Sorry I have missed out to say, I want to send output of CURL statement to a keyword(eg: $quality_gatesstatus) but how to send required output only. – Santosh Kumar May 15 '22 at 12:27
  • How to you transform the JSON output into Bash variables ? Some magic is missing here. – Necklondon May 15 '22 at 12:30
  • Sorry that's far from clear. Please be more precise. Which part of the JSON is the "required output" ? – Necklondon May 15 '22 at 12:32
  • @Necklondon Sorry I forgot to mention that, from the JSON the required output is `"status"` value i.e: `"status":"OK"` or `"status":"ERROR"`. And transform the JSON output to the variable is I have some doubt, right now I using like this `quality_gatesstatus=$(curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY)` – Santosh Kumar May 15 '22 at 13:08
  • So the content of `quality_gatesstatus` is the whole JSON string, right ? – Necklondon May 15 '22 at 13:50
  • Yes correct!!!! – Santosh Kumar May 15 '22 at 13:52
  • There are multiple "status" fields, which one is to be checked ? – Necklondon May 15 '22 at 14:15
  • Yes correct, there are multiple status fields as they are for different attributes inside the quality gate. So I need to check as a combined output the `"status"` should only have `OK` then it should pass, if any one of the `"status"` field is showing `ERROR` it should fail. – Santosh Kumar May 15 '22 at 14:31
  • Finally we are getting to the question. Do you realize the amount of effort required by readers, just to UNDERSTAND what you need ? – Necklondon May 15 '22 at 14:32
  • Extremely sorry for that, I couldnot make you to understand. But please let me know is still I am missing anything for asking question. – Santosh Kumar May 15 '22 at 14:38

1 Answers1

0

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
Necklondon
  • 928
  • 4
  • 12
  • `quality_gatesstatus=$(curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY | grep '"status":"ERROR"' > /dev/null) if [ $? -eq 0 ]; then echo OK else echo FAIL exit 1 fi` Just wanted to check the alignment and the statement is correct? – Santosh Kumar May 15 '22 at 14:57
  • Actually, I want to print the whole json content also if the condition "Fails". I mean `"status":"ERROR"` – Santosh Kumar May 15 '22 at 15:03
  • Yes, its working. Can we make the json output content as a alligned one. As of now its printing in a single line so if it comes in a multiple line it looks better. – Santosh Kumar May 15 '22 at 15:52
  • 1
    Just add `| tr ',' '\n'` at the end of the echo command – Necklondon May 15 '22 at 16:22
  • Thanks alot, all my requirements are working properly with your provided solution. – Santosh Kumar May 15 '22 at 16:54