We have a script that checks the status of pull requests on sonar. After sending the scan we keep checking the completion of the scan because when the scan is not finished the sonar responds like this:
{"errors": [{"msg": "Pull request '175309' in project 'myProject' not found"}]}
Our analysis script:
gradlew sonarqube -Dsonar.pullrequest.key=$ghprbPullId -Dsonar.pullrequest.branch=$ghprbSourceBranch -Dsonar.pullrequest.base=$ghprbTargetBranch .......
while ! curl -f -s -X POST "http://my.sonar.host/api/qualitygates/project_status?projectKey=myProject&pullRequest=$ghprbPullId" > /dev/null; do
echo "Waiting for analysis results, don't exists yet"
sleep 1;
done
When sonar answers, checking .projectStatus.status in returned json will let us know how pull request analisys was.
if [ $(curl -s -X POST "http://my.sonar.host/api/qualitygates/project_status?projectKey=myProject&pullRequest=$ghprbPullId" | jq -r .projectStatus.status) == "OK" ]; then
echo -e "\n\n\e[1m\e[32mOK, this pull request pass quality gate validations\e[0m\n\n"
exit 0
fi
if [ $(curl -s -X POST "http://my.sonar.host/api/qualitygates/project_status?projectKey=myProject&pullRequest=$ghprbPullId" | jq -r .projectStatus.status) == "ERROR" ]; then
echo -e "\n\n\e[1m\e[31mError, this pull request does not pass quality gate validations\e[0m\n\n"
exit 1
fi
Problem comes now, developer’s fix what sonar says, then re-check that pull request. But when analysys has been sent, but not processed by sonar yet; curl answers saying ERROR because sonar has not finished current fixed pull request analysis.
Problem comes now, developers fix what sonar says and then recheck that pull request. But when the analysis has been sent, but not processed by sonar yet; curl:
curl -s -X POST "http://my.sonar.host/api/qualitygates/project_status?projectKey=myProject&pullRequest=$ghprbPullId" | jq -r .projectStatus.status
responds ERROR because sonar has not finished the analysis of the current pull request fixed and is showing previous analysis results.
I have seen that there is a analysisId
at GET method api/qualitygates/project_status
api, but gradle analysis does not return that id. With that id i could differentiate several analysis over same pull request, checking just current analysis.
I also try to sleep one minute after sending analysis to ensure sonar has finished with it, but is a bad solution because normally it takes seconds and i am lossing time.
How could i differentiate analysis of same pr using api?
Maybe is there some api methor to query about pull request analysis status instead of waiting for .projectStatus.status? I don’t find it
Regards
Eloy