How to use REST API to know if the report is finished from SONAR

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    Using version : Community Edition Version 9.9 (build 65466)

  • how is SonarQube deployed: zip, Docker, Helm
    It is a centralized deployment in our company so don’t know

  • what are you trying to achieve:
    We are using Jenkins and a Git event trigger on Jenkins for SonarQube
    And that works fine but right now in our script in Jenkins we are just waiting for the report to be finished.
    We have put in a 3 minute sleep and that works but would be nice to be able to poll the SONAR API to instead know when it is ready? Maybe with a check every 10 seconds but I can’t find any API request that is suitable?

This is how the script looks like right now:

#!/bin/bash -e

# # Load required software modules (see ~/.modules file for account)
#module add jq/1.6        # <== MAY NOT EXIST ON DESIRED MACHINES
#module add git/2.9.3     # <== VERSION IS TOO OLD


MAVEN_PHASE="clean compile"

# Project key for this project.

# Page size is set to maximum during get data from Sonarqube server

# Set of filter rule to filter sonar report got from Sonarqube DB

# Output file after filter to pass Sonar Gerrit plugin

# Define Sonarqube server
# Define Sonarqube API URL
# Credential to access Sonarqube

# Sonar plugin
# Sonar maven plugin version
# Sonar maven plugin goal
# Sonar maven plugin id


module add ${MAVEN_MODULE}

# Trigger a sonar scanner to check the code
-Dsonar.analysis.mode=publish \

# Sleep a certain time to wait for sonar report is ready
# The time was chosen by a 'hacked' number. Not to have any standard
#  Choose a reasonable time instead of 'hacked' time OR
#  Find a reasonable mechanism to wait
echo -e "\nSleeping 3 minute before get report to 'hope' it's ready.\n"
sleep 180

# Get the date of commit with specific format base on patchset revision
# E.g 2020-08-06T13:07:08+02:00
commit_date=$(git show -s --format=%aI ${GERRIT_PATCHSET_REVISION})

# Reformat the datetime to fit with Sonarqube server API
# E.g 2020-08-06T13:07:08%2b0200
commit_date=$(sed -e "s/\(.*\)[+-]\(.*\):\(.*\)/\1%2b\2\3/" <<< ${commit_date})

# Parameters for search action

echo -e "\nSEARCH_PARAM: ${search_params}\n"

# Create JSON report file and store into target/sonar directory
curl --netrc-file ~/.netrc-sonarqube "${SONARQUBE_API_URL}/issues/search?${search_params}" | jq -f ${SONAR_REPORT_BUILDER} > ${JSON_OUTPUT_FILE}

As I mentioned this works but would be better to have like a poll instead to the REST API of some sort instead of a the sleep for 3 minutes.

I have tried this this instead of the sleep but that was not really the full report that was finished.

# Poll SonarQube API to check analysis status
MAX_POLL_ATTEMPTS=30  # Set your desired number of polling attempts
POLL_INTERVAL_SECONDS=10  # Set your desired polling interval in seconds

while [ $poll_attempts -lt $MAX_POLL_ATTEMPTS ] && [ "$analysis_status" != "SUCCESS" ]; do
    poll_attempts=$((poll_attempts + 1))
    analysis_status=$(curl -s "${SONARQUBE_API_URL}/ce/component?component=${PROJECT_KEY}" | jq -r '.current.status')

if [ "$analysis_status" == "SUCCESS" ]; then
    echo "SonarQube analysis is complete. Proceed with further actions."
    echo "SonarQube analysis did not complete successfully after $MAX_POLL_ATTEMPTS attempts. Check SonarQube server logs for details."
    exit 1

Maybe we are doing this wrong :slight_smile:

Sorry for long post :slight_smile:

Welcome Daniel :slight_smile:

you don’t need to use the rest api.

Normally in Jenkins you use the Sonarqube for Jenkins extension which provides a
waitForQualityGate() Jenkins pipeline step - this requires a configured webhook in Sonarqube server.
see Jenkins extension for SonarQube
Jenkins integration

As an alternative you may use the property sonar.qualitygate.wait which is meant as a generic
CI solution for the polling - no need for a selfimplemented solution.
see CI integration overview

We use the first solution with Sonarqube Enterprise and it works fine.


not really sure I understand you answer completely but will try to read and see if I get it :slight_smile:

As you see in the script we create a SONAR json report file. Would your suggestion do the same?
Also as you may have seen that we use a SONAR community edition so maybe the way you do it will not work for us?

Check the docs and come back if any questions :slight_smile:

AFAIK, the sonar json report was available in older Sonarqube 7.x versions, but doesn’t work anymore.
If you need reporting for Community edition you may:

If something is only available starting with Developer or Enterprise edition it should be mentioned in the docs, i.e.

but the webhooks page has no such hint

For property sonar.qualitygate.wait the doc has

Forces the analysis step to poll the SonarQube instance and wait for the Quality Gate status. If there are no other options, you can use this to fail a pipeline build when the Quality Gate is failing. See the CI integration page for more information.

and there’s also sonar.qualitygate.timeout

Sets the number of seconds that the scanner should wait for a report to be processed, default 300

Thx for the answer I will check it out but that is why we tried to do it our self with REST API and that was really what my question was about.
How to do it with the REST API because I have not found a good way.

We are using the community edition so cant use the webhooks.
And I am not in charge of our Sonarqube instance so I am not allowed to manipulate Sonarqube.

I appreciate your answers but I am still looking for how to do this report with API. So far we do it as the script I posted it works fine I just wanted to remove the 3 minute wait and make that more dynamic.
But maybe there is no way really with REST API to understand when we can create the json report.

So if anyone has a good idea on how to know when the issues “report” is finished and then after that can create the json sonar report as we do now with a script .

As I said the script works and we create the sonare json report ourselves but we now randomly wait 3 seconds before we can start creating the report. Would be good to know when we can start creating the report instead of the wait.