Nemanja Beric
Nemanja Beric

Reputation: 241

Accessing SonarQube quality gate API for private projects

How do I access SonarQube quality gate API in case of a private project? I tried sending the "User token" for a user as Authorization Bearer token and Basic login. However, it did not work. The only way I can access it is from a browser when a user is logged in, i.e. login token is present. Accessing like that from Postman is also possible. How could I get the token, then and is that a right way to access the API?

Upvotes: 0

Views: 806

Answers (1)

ttoshev
ttoshev

Reputation: 156

Tokens are indeed the right way to access the API.

1) You can obtain a new token by going to Administration > Security > Users under tokens (click the list icon) then enter a name and click Generate

Make sure to copy the token as you won't be able to see it once you close.

2) Postman has an option to generate code based on a request (click the "code" option). If it works through Postman, it should work in your code with the copied request.

3) As a last resort if it doesn't work, here is a Groovy function I wrote to send a Get request.

// Returns response of SonarQube web API call with admin auth in text format
def getFromApi(String getURL){

    def Res = ("curl -u "+AuthenticationToken+": "+getURL).execute().text

    if (Res){
        return Res
    }

    else{ 
        return "error"
    }
}

For post request you just need to add -X POST before the authentication token. Modify it to suit your needs. Hope it helps!

Upvotes: 1

Related Questions