Extension for Visual Studio - Roslyn based static code analysis: Find and instantly fix nasty bugs and code smells in C#, VB.Net, C, C and JS. SonarLint can be connected to a SonarQube server or SonarCloud to share rulesets, get event notifications and use a resolution flow.
SonarQube is a static code analyzer for your project. It makes sure your code is up to the mark and will not break in production. It will only work for Public projects which doesn't need any Authentication with the SonarQube server. A Future release for the private Sonar projects is in roadmap. This extension can help you get the build status like:
right inside your favorite IDE - VSCode How to Use
Viola.. Features
Extension SettingsThe extension gets data from a configuration file .vscode > project.json Here is how the configuration file looks: NOTE: Please make sure to add http:// with your sonarURL and should not have a trailing / .For Eg: http://sonar.myserver.com - Rightwww.sonar.myserver.com/ - WrongJust fill the file and you are good to go. Enjoy! |
SonarQube's integration with Azure DevOps allows you to maintain code quality and security in your Azure DevOps repositories. It is compatible with both Azure DevOps Server and Azure DevOps Services.
With this integration, you'll be able to:
- Import your Azure DevOps repositories - Import your Azure DevOps repositories into SonarQube to easily set up SonarQube projects.
- Analyze projects with Azure Pipelines - Integrate analysis into your build pipeline. Starting in Developer Edition, SonarScanners running in Azure Pipelines jobs can automatically detect branches or pull requests being built, so you don't need to specifically pass them as parameters to the scanner.
- Add pull request decoration - (starting in Developer Edition) See your Quality Gate and code metric results right in Azure DevOps so you know if it's safe to merge your changes.
Prerequisites
SonarLint is integrated with Microsoft Code Analysis framework, so rules for C# and VB.NET can be fine-tuned in the. Ruleset file used by your project. The set of active rules can also be tuned for JavaScript, C and C. Find logs if you need them In the Output panel, show output from SonarLint. By default, tool-generated code files are skipped from analysis. Scanner compatibility. To analyze C# code, you need to use the SonarScanner for.NET version 4.x or newer. Excluding External Roslyn Issues (See 'Notes on external.NET issues') Test Coverage & Execution (Visual Studio Code Coverage, dotCover, OpenCover. I have installed SonarQube server and we have a requirement to integrate one of our visual studio project solution with SonarQube to analyse our code and receive code quality suggestions. Currently we were not able to find a suitable guideline in how to set it up. Appreciate if you can provide a clear guideline to solve our problem.
Integration with Azure DevOps server requires at least Azure DevOps Server 2019, TFS 2018, or TFS 2017 Update 2+.
Branch Analysis
Community Edition doesn't support the analysis of multiple branches, so you can only analyze your main branch. Starting in Developer Edition, you can analyze multiple branches and pull requests.
Importing your Azure DevOps repositories into SonarQube
Setting up the import of Azure DevOps repositories into SonarQube allows you to easily create SonarQube projects from your Azure DevOps repositories. If you're using Developer Edition or above, this is also the first step in adding pull request decoration.
To set up the import of Azure DevOps repositories:
- Set your global settings
- Add a personal access token for importing repositories
Setting your global settings
To import your Azure DevOps repositories into SonarQube, you need to first set your global SonarQube settings. Navigate to Administration > Configuration > General Settings > ALM Integrations, select the Azure DevOps tab, and click the Create configuration button. Specify the following settings:
- Configuration Name (Enterprise and Data Center Edition only) – The name used to identify your Azure DevOps configuration at the project level. Use something succinct and easily recognizable.
- Azure DevOps collection/organization URL – If you are using Azure DevOps Server, provide your full Azure DevOps collection URL. For example,
https://ado.your-company.com/DefaultCollection
. If you are using Azure DevOps Services, provide your full Azure DevOps organization URL. For example,https://dev.azure.com/your_organization
. - Personal Access Token – An Azure DevOps user account is used to decorate Pull Requests. We recommend using a dedicated Azure DevOps account with Administrator permissions. You need a personal access token from this account with the scope authorized for Code > Read & Write for the repositories that will be analyzed. This personal access token is used for pull request decoration, and you'll be asked for another personal access token for importing projects in the following section.
Adding a personal access token for importing repositories
After setting your global settings, you can add a project from Azure DevOps by clicking the Add project button in the upper-right corner of the Projects homepage and selecting Azure DevOps.
Then, you'll be asked to provide a personal access token with
Code (Read & Write)
scope so SonarQube can access and list your Azure DevOps projects. This token will be stored in SonarQube and can be revoked at anytime in Azure DevOps.After saving your personal access token, you'll see a list of your Azure DevOps projects that you can set up to add them to SonarQube. Setting up your projects this way also sets your project settings for pull request decoration.
For information on analyzing your projects with Azure Pipelines, see the Analyzing projects with Azure Pipelines section below.
Analyzing projects with Azure Pipelines
SonarScanners running in Azure Pipelines jobs can automatically detect branches or pull requests being built, so you don't need to specifically pass them as parameters to the scanner.
Automatic branch detection is only available when using Git.
Installing your extension
From Visual Studio Marketplace, install the SonarQube extension by clicking the Get it free button.
Azure DevOps Server - build agents
If you are using Microsoft-hosted build agents then there is nothing else to install. The extension will work with all of the hosted agents (Windows, Linux, and macOS).
If you are self-hosting the build agents, make sure you have at least the minimum SonarQube-supported version of Java installed.
Adding a new SonarQube Service Endpoint
After installing your extension, you need to declare your SonarQube server as a service endpoint in your Azure DevOps project settings:
- In Azure DevOps, go to Project Settings > Service connections.
- Click New service connection and select SonarQube from the service connection list.
- Enter your SonarQube Server URL, an Authentication Token, and a memorable Service connection name. Then, click Save.
Configuring branch analysis
After adding your SonarQube service endpoint, you'll need to configure branch analysis. You'll use the following tasks in your build definitions to analyze your projects:
- Prepare Analysis Configuration - This task configures the required settings before executing the build.
- Run Code Analysis - (Not used in Maven or Gradle projects) This task executes the analysis of source code.
- Publish Quality Gate Result - this task displays the Quality Gate status in the build summary letting you know if your code meets quality standards for production. This task may increase your build time as your pipeline has to wait for SonarQube to process the analysis report. It is highly recommended but optional.
Sonarqube Visual Studio 2017 Code Coverage
Select your build technology below to expand the instructions for configuring branch analysis and to see an example
.yml
file..NET
- In Azure DevOps, create or edit a Build Pipeline, and add a new Prepare Analysis Configuration task before your build task:
- Select the SonarQube server endpoint you created in the Adding a new SonarQube Service Endpoint section.
- Under Choose a way to run the analysis, select Integrate with MSBuild.
- In the project key field, enter your project key.
- Add a new Run Code Analysis task after your build task.
- Add a new Publish Quality Gate Result on your build pipeline summary.
- Under the Triggers tab of your pipeline, check Enable continuous integration, and select all of the branches for which you want SonarQube analysis to run automatically.
- Save your pipeline.
.yml example:
Maven or Gradle
- In Azure DevOps, create or edit a Build Pipeline, and add a new Prepare Analysis Configuration task before your build task: Web scrape node js online.
- Select the SonarQube server endpoint you created in the Adding a new SonarQube Service Endpoint section.
- Under Choose a way to run the analysis, select Integrate with Maven or Gradle.
- Expand the Advanced section and replace the Additional Properties with the following snippet:
- Edit or add a new Maven or Gradle task
- Under Code Analysis, check Run SonarQube or SonarCloud Analysis.
- Add a new Publish Quality Gate Result on your build pipeline summary.
- Under the Triggers tab of your pipeline, check Enable continuous integration, and select all of the branches for which you want SonarQube analysis to run automatically.
- Save your pipeline.
.yml example:
Other (JavaScript, TypeScript, Go, Python, PHP, etc.)
- In Azure DevOps, create or edit a Build Pipeline, and add a new Prepare Analysis Configuration task before your build task:
- Select the SonarQube server endpoint you created in the Adding a new SonarQube Service Endpoint section.
- Under Choose a way to run the analysis, select Use standalone scanner.
- Select the Manually provide configuration mode.
- In the project key field, enter your project key.
- Add a new Run Code Analysis task after your build task.
- Add a new Publish Quality Gate Result on your build pipeline summary.
- Under the Triggers tab of your pipeline, check Enable continuous integration, and select all of the branches for which you want SonarQube analysis to run automatically.
- Save your pipeline.
.yml example:
Analyzing a C/C++/Obj-C project
In your build pipeline, insert the following steps in the order they appear here. These steps can be interweaved with other steps of your build as long as the following order is followed. All steps have to be executed on the same agent.
- Make the Build Wrapper available on the build agent:Download and unzip the Build Wrapper on the build agent (see the Prerequisites section of the C/C++/Objective-C page). The archive to download and decompress depends on the platform of the host.Please, note that:
- For the Microsoft-hosted build agent, you will need to make the Build Wrapper available on the build agent every time (as part of the build pipeline). To accomplish this, you can add a PowerShell script task by inserting a Command Line task.Example of PowerShell commands on a Windows host:Example of bash commands on a linux host:Example of bash commands on a macos host:
- For the self-hosted build agent you can either download it every time (using the same scripts) or only once (as part of manual setup of build agent).
- Add a Prepare analysis Configuration task and configure it as follow:Click on the Prepare analysis on SonarQube task to configure it:
- Select the SonarQube Server
- In Choose the way to run the analysis, select standalone scanner (even if you build with Visual Studio/MSBuild)
- In Additional Properties in the Advanced section, add the property
sonar.cfamily.build-wrapper-output
with, as its value, the output directory to which the Build Wrapper should write its results:sonar.cfamily.build-wrapper-output=<output directory>
- Add a Command Line task to run your build.For the analysis to happen, your build has to be run through a command line so that it can be wrapped-up by the build-wrapper.To do so,
- Run Build Wrapper executable. Pass in as the arguments (1) the output directory configured in the previous task and (2) the command that runs a clean build of your project (not an incremental build).Example of PowerShell commands on a Windows host with an MSBuild build:Example of bash commands on a linux host with a make build:Example of bash commands on a macos host with a xcodebuild build:
- Add a Run Code Analysis task to run the code analysis and make the results available to SonarQube. Consider running this task right after the previous one as the build environment should not be significantly altered before running the analysis.
- Add a Publish Quality Gate Result task.
.yml example:
Note: You need to choose your correct image and adapt the correct wrapper depending on the agent os. See above example to have the correct wrapper.
Running your pipeline
Sonarqube Vs Visual Studio Code Analysis
Commit and push your code to trigger the pipeline execution and SonarQube analysis. New pushes on your branches (and pull requests if you set up pull request analysis) trigger a new analysis in SonarQube.
Maintaining pull request code quality and security
Using pull requests allows you to prevent unsafe or substandard code from being merged with your main branch. The following branch policies can help you maintain your code quality and safety by analyzing code and identifying issues in all of the pull requests on your project. These policies are optional, but they're highly recommended so you can quickly track, identify, and remediate issues in your code.
Ensuring your pull requests are automatically analyzed
Ensure all of your pull requests get automatically analyzed by adding a build validation branch policy on the target branch.
Preventing pull request merges when the Quality Gate fails
Prevent the merge of pull requests with a failed Quality Gate by adding a
SonarQube/quality gate
status check branch policy on the target branch.If your SonarQube project is configured as part of a mono repository in Enterprise Edition or above, you need to use a status check branch policy that uses a SonarQube project key (
SonarQube/quality_gate_[SQ_project_key]
) instead of SonarQube/quality gate
.Check out this video for a quick overview on preventing pull requests from being merged when they are failing the Quality Gate.
Adding pull request decoration to Azure DevOps
Pull request decoration shows your Quality Gate and analysis metrics directly in Azure DevOps.
After you've set up SonarQube to import your Azure DevOps repositories as shown in the Importing your Azure DevOps repositories into SonarQube above, the simplest way to add pull request decoration is by adding a project from Azure DevOps by clicking the Add project button in the upper-right corner of the Projects homepage and selecting Azure DevOps.
Then, follow the steps in SonarQube to analyze your project. The project settings for pull request decoration are set automatically.
The T5 is a 5 speed manual transmission that is compatible with a longitudinal engine. The transmission comes with one overdrive gear, a light aluminum housing case, and can be adapted for use in 4 wheel drive. These transmissions can be found in both GM as well as Ford vehicles. The T5 World Class is a five speed, RWD or 4WD transmission. The T5 World Class is a American engineering design, this transmission is all aluminum top loaded with internal single rail shift linkage. This transmission the T5 World Class has tapered bearings. Ford T-5 Front Shift Tailhousing – Rear position $ 715.00 MD-510-1000. Ford T-5 Front Shift Tail Housing – Front Position. TREMEC Super-Duty SN95 T5 Transmission for 94-95 Ford Mustang $ 2,445.00 MD-552-0251-94. TREMEC T5 5-Speed Super-Duty Front Shift /Front Socket MD-552-0251-FS-F $ 2,865.00 MD. T5 transmission for sale.
To decorate Pull Requests, a SonarQube analysis needs to be run on your code. You can find the additional parameters required for Pull Request analysis on the Pull Request Analysis page.
Adding pull request decoration to a manually created or existing project
To add pull request decoration to a manually created or existing project, make sure your global ALM Integration settings are set as shown above in the Importing your Azure DevOps repositories into SonarQube section, and set the following project settings at Project Settings > General Settings > Pull Request Decoration:
- Project name
- Repository name
Advanced pull request decoration configuration
Adding pull request decoration to projects that are part of a mono repository
Pull request decoration for a mono repository setup is supported starting in Enterprise Edition.
In a mono repository setup, multiple SonarQube projects, each corresponding to a separate mono repository project, are all bound to the same Azure DevOps repository. You'll need to set up pull request decoration for each SonarQube project that is part of a mono repository.
To add pull request decoration to a project that's part of a mono repository, set your project up manually as shown in the Adding pull request decoration to a manually created or existing project section above. You also need to set the Enable mono repository support setting to true at Project Settings > General Settings > Pull Request Decoration.
After setting your project settings, you need to ensure the correct project is being analyzed by adjusting the analysis scope and pass your project names to the scanner. See the following sections for more information.
Ensuring the correct project is analyzed
You need to adjust the analysis scope to make sure SonarQube doesn't analyze code from other projects in your mono repository. To do this set up a Source File Inclusion for your project at Project Settings > Analysis Scope with a pattern that will only include files from the appropriate folder. For example, adding
./MyFolderName/**/*
to your inclusions would only include analysis of code in the MyFolderName
folder. See Narrowing the Focus for more information on setting your analysis scope.Passing project names to the scanner
Because of the nature of a mono repository, SonarQube scanners might read all project names of your mono repository as identical. To avoid having multiple projects with the same name, you need to pass the
sonar.projectName
parameter to the scanner. For example, if you're using the Maven scanner, you would pass mvn sonar:sonar -Dsonar.projectName=YourProjectName
.Configuring multiple ALM instances
You can decorate pull requests from multiple ALM instances by creating a configuration for each ALM instance and then assigning that instance configuration to the appropriate projects.
- As part of Developer Edition, you can create one configuration for each ALM.
- Starting in Enterprise Edition, you can create multiple configurations for each ALM. If you have multiple configurations of the same ALM connected to SonarQube, you have to create projects manually.
Linking issues
During pull request decoration, individual issues will be linked to their SonarQube counterparts automatically. For this to work correctly, you need to set the instance's Server base URL (Administration > Configuration > General Settings > General > General) correctly. Otherwise, the links will default to
localhost
.FAQ
Missing Build Agent Capability
If you add a Windows Build Agent and install a non-oracle Java version on it, the agent will fail to detect a needed capability for the SonarQube Azure DevOps plugin. If you are sure that the
java
executable is available in the PATH
environment variable, you can add the missing capability manually by going to your build agent > capabilities > user capabilities > add capability. Here, you can add the key, value pair java, and null which should allow the SonarQube plugin to be scheduled on that build agent.This Bug has been reported to the Microsoft Team with azure-pipelines-agent#2046 but is currently not followed up upon.