SonarQube Web API
SonarQube Web API
Copyright © Tony's Studio 2020 - 2022
1. Intro
Web API is a convenient way to communicate with SonarQube server, but it is quite complicated at the same time.
It is recommended to use Postman to check API quickly, and the following contents are developed with API categories.
In this passage, I’ll introduce some basic SonarQube Web APIs, and how to use these stuffs to complete a analyze, emm… actually a simple scan.
If you do not have a SonarQube Server right now, you can use the example site of it: https://next.sonarqube.com/
2. Web API
In this section, API is presented in the format as shown below. Asterisk(*) is applied to required parameters. Notice that, here I only listed some relatively more important properties. For more usage, please check SonarQube Web API documentation. :)
1 | [REQUEST_METHOD] API_FULL_NAME |
1 | [OMITABLE_PARAM] -- MEANING |
1 | RESPONSE EXAMPLE |
2.0 Preparation
Before you start, there’s one thing you have to know. SonarQube Web API use authentication by default, which is so called Basic Auth
. To use API without repetitively getting 401 Unauthorized
, you may have to set authorization for your request. Just set it to Basic Auth
with your login
and password
.
2.1 Authentication
Authenticate a user. This is the first thing to do when you want to communicate with SonarQube server.
2.1.1 login
To use many more features, you have to login first. This request doesn’t have a response.
1 | [POST] api/authentication/login |
1 | [login]* -- username |
None.
2.1.2 logout
To log out.
1 | [POST] api/authentication/logout |
None.
None.
2.1.3 validate
Check credentials. You are validated only when response is true.
1 | [GET] api/authentication/validate |
None.
1 | {"valid": true} |
2.2 Quality Profile
This section deals with SonarQube quality profile. Err… duh.
2.2.1 search
Search quality profiles.
1 | [GET] api/qualityprofiles/search |
1 | [language] -- indicate profiles for which language is searched. e.g. java |
1 | { |
2.2.2 projects
List projects with their association status regarding a quality profile. See
api/qualityprofiles/search
in order to get the Quality Profile Key
1 | [GET] api/qualityprofiles/projects |
1 | [key]* -- quality profile key, can be obtained by search in 2.2.1. |
Tips: It seems that by default none project is selected, so you may want to set selected to
all
to show all projects.
1 | { |
2.2.3 add_project
Associate a project with a quality profile. Requires one of the following permissions:
- ‘Administer Quality Profiles’
- Edit right on the specified quality profile
- Administer right on the specified project
1 | [POST] api/qualityprofiles/add_project |
1 | [language]* -- quality profile language, e.g. java |
None.
Tips: Its weird, you must logout first, and then use authorization to avoid 401 Unauthorized.
2.3 Quality Gate
2.3.1 select
Associate a project to a quality gate. Requires one of the following permissions!
- ‘Administer Quality Gates’
- ‘Administer’ right on the specified project
I mean it! Administer permission required!
1 | [POST] api/qualitygates/select |
1 | [gateName] -- name of the quality gate |
None.
2.4 Projects
This section manages project existence. Requires ‘Create Projects’ permission, and remember to add authorization mentioned in 2.0 to the request!
2.4.1 create
Notice
1 | [POST] api/projects/create |
1 | [name]* -- name of the project, no longer than 500 characters. |
1 | { |
Tips: Its weird, you must logout first, and then use authorization to avoid 401 Unauthorized.
2.4.2 delete
Delete a project. Requires ‘Administer System’ permission or ‘Administer’ permission on the project.
1 | [POST] api/projects/delete |
1 | [project]* -- project key |
None.
delete
has a brother -bulk_delete
, which provideprojects
parameter to take multiple project keys separated by comma, instead of a singleproject
.
2.4.3 search
Search for projects or views to administrate them.
1 | [GET] api/projects/search |
1 | [projects] -- comma-separated list of project keys |
1 | { |
2.5 Issues
To read and update issues. This is mainly used to get issues of projects after a successful scan.
2.5.1 search
Search for issues.
Require
Browse
permission on the specified project(s). For applications, it also requires ‘Browse’ permission on its child projects.When issue indexation is in progress returns 503 service unavailable HTTP code.
PS:
- Only primary parameters are listed here. (There are too many. >_<)
- For more details on issues, you can visit online SonarQube Server: https://next.sonarqube.com/sonarqube/issues.
1 | [GET] api/issues/search |
1 | [componentKeys] -- comma-separated list of component keys. |
1 | { |
3. Application
Notice:
- In this section, server name is omitted, e.g.
www.server-name:9000/api/authentication/login
is presented asapi/authentication/login
.- Request details are omitted, please refer to the previous section. It is recommended that you open two pages in separate vertical tabs.
3.0 Preparation
Before you start, you may want to take some advice.
SonarQube Web API is sometimes weird, if you logged in use api/authentication/login
first, some request will not succeed and receive 401 Unauthorized
even you added Authorization field. If you encountered this, please use api/authentication/logout
to logout first. However, some other requests do not authenticate your Authorization field and relies on login
!!! Karabast!
3.1 Create A Project
To perform a scan, we should create a project first. However, this procedure is not necessary, because a new project will be created when Sonar Scanner completes a successful scan. If we create a project first, we can configure it before the first scan.
1 | [POST] api/projects/create |
Tips: This request is a good example of 3.0. See above.
3.2 Associate Quality Profile
Default quality profile will be associated to our newly created project, and we might not want it.
1 | [POST] api/qualityprofiles/add_project |
3.3 Associate Quality Gate
Default quality gate will be associated to our newly created project, too.
1 | [POST] api/qualitygates/select |
3.4 Scan Project
SonarQube can not scan your project by itself. You have to use Sonar Scanner. Just refer to my previous post.
Remember to set sonar.projectKey
and sonar.projectName
to what you assigned when create the project. :)
3.5 View Issues
After a successful scan is performed, you can simple view your issues by a simple request.
1 | [GET] api/issues/search |
This request has a lot of parameters, but generally, you only need to specify projects
parameter. It will then return all the issues of the selected projects. And if you want certain tags or types, just add them.
Be aware that there might be a lot of issues, and possibly more than 500, which is the maximum page size! You may need more than one request for different pages.
So, I guess this is it, good luck! :P