As we all know, SonarQube is an excellent code quality assurance tool, but the use of SonarQube in many factories is limited to the horizontal comparison and vertical trend analysis of code warehouse quality after the code is submitted. As an ordinary brick builder, we should pay more attention to the following points in order to improve our brick quality: What can SonarQube do for us before the code is submitted?
Search sonar for the VScode plugin library and you can see that the top one is: SonarLint, as long as you have JDK 8/11 and Node10 + installed in your development environment, this plugin is available almost out of the box. We can see the problems panel’s strict reminder from SonarLint:
SonarLint’s default rule set can be seen in the left pane of vscode:
Rules can be customized, activated or suspended as required.
But in reality, as a member of the front line, we don’t work alone, everyone on the team has to follow the same coding rules, and that’s where SonarQube comes in.
As a native code specification warning service, we don’t need SonarQube to display problem data with fancy ICONS and text. Instead, we need the SonarQube service to provide our team with a remote, public set of rules. The SonarLint plugin will do the downloading, detecting and testing for rules for us. We just need to do a simple configuration in the editor.
What configuration information does SonarLint need?
If we want to use remote rule sets, we need to add a configuration in vsCode’s User Settings:
{
"sonarlint.connectedMode.connections.sonarqube": [
{ "serverUrl": "https://sonarqube.mycompany.com", "token": "<generated from SonarQube account/security page>" }
]
}
Copy the code
Next, create.vscode/settings.json under the project and configure it as follows:
{
"sonarlint.connectedMode.project": {
"projectKey": "the-project-key"
}
}
Copy the code
Look at the configuration, our SonarQube service, to be able to provide three information: URL, project key, project token.
Install SonarQube
Find a machine and deploy the SonarQube service. According to the steps in Try out, we choose the simpler method of deploying docker images.
$ docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest
Copy the code
If you want to deploy to another port, such as 7000, you can simply change the 9000:9000 command to 7000:9000.
$ docker run -d --name mySonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 7000:9000 sonarqube:latest
Copy the code
View services:
$ docker ps
Copy the code
For now, visit http://ip:7000:
This http://ip:7000 is the serverUrl required in vscode configuration, and then some project information needs to be configured.
Configure the SonarQube project information
Login, the user name password is admin, create a new project, literally call what, we can even called “commonProjectForShareRules”
Create a token:
You probably guessed, the configuration information needed in the project – the key is commonProjectForShareRules, the token is a string of complex e83c6119c94266aaf8a50ea477b3b4c9b398ae02.
Then choose the language, platform:
Configuration rules
Now, we can customize some rules. When we want to customize a rule, we see that the button is disabled.
We need to customize a quality configuration first:
Then select a project for this configuration, needless to say, select the project we created earlier:
Go back to the rules page and select our custom quality configuration:
You can choose to activate or suspend a certain rule according to actual requirements. After determining the rule set, VSCode only needs command + Shift + p, and select:
The Output panel of the editor prompts:
SonarLint in VSCode then checks the code against the updated rule set.
A few small questions
After all information is configured, if performed vscode SonarLint found an error: the command ‘SonarLint. ShowSonarLintOutput’ not found or the command Update… Not found: vscode has just restarted, or vscode has opened a new window.
Tip: if start a minute or so the output panel org. Sonarsource. Nodejs. NodeCommandException: Failed to start server (60s timeout) You can add a line of your own node configuration to vscode user settings.json:
"sonarlint.pathToNodeExecutable": "xxx/bin/node",
Copy the code
In general, SonarLint will automatically look for Java. If no error is found, add a configuration in vscode user Settings. json:
"sonarlint.ls.javaHome": "/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home",
Copy the code
The above. Good luck with the poetic code.