Sync Sonar Cloud rules with Sonar Lint in your IDE

Chameera Rupasinghe
5 min readSep 13, 2022

--

If you are a developer who works in project where the code quality is monitored by a SonarCloud server, this article will be really helpful. If you are already have SonarLint in your IDE and you only need to know how to sync the rules with SonarCloud, jump to Syncing Sonar Rules part below.

Code Quality Check with Sonar

Often the code quality is checked whenever your code is pushed to the remote repository via a workflow (checks or actions in github, jobs in gitlab, etc.) attached to the repository. Without pushing your code to the repository to find out whether your code is up to the quality standards of your organisation, you can add simple plugin called SonarLint to your IDE to check your code quality while you are working on the code.

You can find the suitable SonarLint plugin for your IDE in here — https://www.sonarsource.com/products/sonarlint/

To determine the quality of the code, there are certain set of rules that can be defined in SonarColud where these rules will be checked against the code which is pushed to the repository. These rules can be changed based on the programming language that the code is written in, the version of the programming language that the code is written in, and the preferences of your organisation. Any deviations from this rules are called violations and the violations are categorised based on their severity.

Syncing Sonar Rules

When SonarLint plugin is installed to your IDE, it will identify the violations of your code based on some basic rules comes by default in the plugin. However the rules configured in the SonarCloud server of your organisation can be (most of the time is) different from these basic by default rules. Hence you might see some violations detected in your code by SonarCloud after pushing it to the repository, which you didn’t see when checking in with SonarLint plugin in your IDE.

To overcome this, you have to configure the SonarLint plugin in your IDE to synchronise with the rules defined in the SonarCloud server of your organisation. To show you how to do it I’ll use IntellijIDE community version IDE with the SonarLint plugin already installed in. Please follow the steps below.

  1. Open IDEA, Go to Preferences > Tools > SonarLint > Settings
  2. Click the + button under SonarQube / SonarCloud connections

3. Now add the Connection Name. Connection Name is the URL to the SonarCloud server of organisation.

To obtain the Connection Name, go to the SonarCloud server of your organisation and copy the URL from the browser and paste it in the above text box

4. Next you’ll have to provide a Token. Paste the token if you already have a one, or click on Create Token otherwise.

Assuming you are doing this for the first time, leat create a token first.

5. After clicking Create Token, you’ll have to log in to the SonarCloud server of your organisation. Use a preferred authentication method and you’ll be redirected to SonarCloud Security page.

Enter a token name and click on Generate to generate a new token.

6. Once the token is generated, copy and paste it to on the place where your IDE asked for the token in step 4 above.

7. After adding your newly created token, click Next to proceed.

8. Next you’ll have to select your organisation. Select the relevant option and click Next.

9. Then you choose whether you like to receive notifications form SonarCloud. If you don’t want to receive notification, uncheck the checkbox.

Click Next to continue.

10. Finally you’ll see the connection is successfully created. Click Create to finish creating the connection with SonarCloud.

Then apply these changes by clicking Apply and exit the Preferences window by clicking OK.

Now you have successfully synced rules from SonarCloud server to your SonarLint plugin in your IDE.

Conclusion

With the rules synced with the server, all the violations in the code can be identified and corrected before pushing it to the repository. This will reduce the workload on the developer. Have you spent your time on correcting all the sonar violations in your code shown by the SonarLint plugin in your IDE, only to find out there are more after pushing it to the repo? That won’t happen again.

--

--

Chameera Rupasinghe

Senior Software Engineer @WSO2 | Computer Science and Engineering