Running SonarQube without TFS Build? - c#

I was asked to integrate another project (C#) into SonarQube. We use TFS 2013, SonarQube 5.1.2 with Sonar.MSBuild.Runner 1.0.
The Build process template is old (does not support pre and post Powershell scripts) and it is customized. Visual Studio 2013 is unable to edit the old build process template.
I tried to do just the static code analysis and run the MSBuild.SonarQube.Runner with the start parameter and a second time with the end parameter without doing a build in between (The code coverage does not seem important to them). But then I get the error "No ProjectInfo.xml files were found. Check that the analysis targets are referenced by the MSBuild projects being build"?
What options do I have to integrate this project into SonarQube?

To use the MSBuild SonarQube Runner from the command line, you have to:
Prepare the environment: MSBuild.SonarQube.Runner.exe begin /k:SQ_project_key /n:SQ_project_name /v:SQ_project_version (pick arbitrary SonarQube project key, name and version)
Run your build: msbuild
Upload analysis results: MSBuild.SonarQube.Runner.exe end
The MSBuild SonarQube Runner integrates with MSBuild and collects informations as it builds your project: You must run the build during the begin and end invocations, or this mandatory information won't be available and the MSBuild SonarQube Runner will fail with the message you reported. The MSBuild integration is compatible with MSBuild versions 4.0, 12 and 14.
Regarding the integration with TFS XAML builds, if you're using a customized one, you'll have to customize it a bit more to integrate the calls to the beginand end commands of the MSBuild SonarQube Runner.
Code Coverage is supported out of the box on TFS 2013 with the default build definition template, you just need to run tests with coverage enabled. If that doesn't work for you, see this link on how to manually pass the code coverage results path property: http://docs.sonarqube.org/x/CoBh

Related

Run SonarCloud Analyze Manually From Visual Studio

I have paid account in Sonarcloud and Gitlab CI for automation, I use Visual Studio with integrated SonarLint for C#. Sometimes I want to run analyzing manually from VS and not using Gitlab runner every time. Is there a way to bypass pipeline? The reason is that I have limitations for runner minutes and I want to make commit and run pipeline only when I cleanup code warnings from SonarLint.
SonarScanner
You can trigger a SonarCloud analysis locally using SonarScanner:
SonarScanner for MSBuild as a stand-alone executable.
.NET Core global Tool installed with NuGet, also called as executable.
Download the SonarScanner CLI binary and run it from the command line.
The MSBuild option looks something like this:
SonarScanner.MSBuild.exe begin /k:"project-key"
MSBuild.exe <path to solution.sln> /t:Rebuild
SonarScanner.MSBuild.exe end
The .NET Core global tool looks like this:
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"project-key" /d:sonar.login="myAuthenticationToken"
dotnet build <path to solution.sln>
dotnet sonarscanner end /d:sonar.login="myAuthenticationToken"
Since You need to call SonarScanner before and after a build. you can integrate the SonarScanner commands in Visual Studio using Build Events. Add the commands to PreBuildEvent.bat and PostBuildEvent.bat to run the analysis automatically every time you build. If you don't want to run SonarScanner every time you build, create a new custom build configuration (e.g. Debug, Release, Analyze).
You will need to generate a private token for your project key in SonarCloud. The final report will also be available in that project when it is ready.
SonarLint
If you don't need to update SonarCloud and just want to view the code analysis results, you can use SonarLint Visual Studio extension. You can connect SonarLint with SonarCloud to download your common configuration using a feature called Connected Mode.
Connected mode does not push issues to the server. Rather, its purpose is to configure the IDE so that it uses the same settings as the server.
A summary of instructions (see Connected Mode for details):
Open the Team Explorer Home tab and click on the SonarQube icon
Click on Connect... to display the connection dialogue
Select the server and enter your credentials
Select the Organization (SonarCloud only)
Select the Sonar project to bind to
SonarLint will then fetch the required settings from the server and create local configuration files
Your code will be analyzed live (as you type), or you can run a full code analysis as follows:
Right click solution -> Analysis -> Run Code Analysis
You can't do this from Visual Studio, but you can make this like powershell script.

The SonarQube MSBuild integration failed - SonarQube 7 with Scanner 4.0.2.892 and VS2017

I'm using SonarQube 7.0 and SonarQube Scanner for MSBuild 4.0.2.892 and attempting to follow these instructions to scan my .NET application on Windows (ASP.NET Core). The three commands below are part of the instructions:
C:\SonarQube\Scanner\SonarQube.Scanner.MSBuild.exe begin /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"TradingPlatform" /v:"1.0"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" /t:Rebuild
C:\SonarQube\Scanner\SonarQube.Scanner.MSBuild.exe end
However, while pre-processing succeeds on the fist command, and the build completes on the second command, the third command always fails as shown below:
====
SonarQube Scanner for MSBuild 4.0.2
Default properties file was found at C:\SonarQube\Scanner\SonarQube.Analysis.xml
Loading analysis properties from C:\SonarQube\Scanner\SonarQube.Analysis.xml
Post-processing started.
The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
Possible causes:
The project has not been built - the project must be built in between the begin and end steps
An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
The begin, build or end steps have not all been launched from the same folder
Generation of the sonar-properties file failed. Unable to complete SonarQube analysis.
Exit Code 1.
====
Does anyone have any idea why it's failing? Cause 1 can't be the case as I'm building the project in step 2. Cause 2 seems very unlikely (but not impossible) as I'm using MSBuild 15. Cause 3 also can't be the case as I'm running all three commands from the same location
Other possible reasons are:
your build server is executing the builds using Local System account. Try changing the build user to a normal domain or local user.
you are building a .NET Core project that has no ProjectGuid property (the default) and that is not part of a solution. To resolve this you need to do one of these:
Add <ProjectGuid>XXXXX</ProjectGuid> element in the first PropertyGroup in your csproj, where XXXXX is a newly generated Guid.
Add your project to a Visual Studio solution and build the sln file instead of the csproj
If this does not help, add /d:sonar.verbose=true on the begin command of the scanner and save all logs into a file. Zip the logs and the .sonarqube folder from your build workspace and upload them here: https://www.dropbox.com/request/CszJAO1ZNXFVl1gwo5hA

SonarQube not able to record the coverage

I am using SonarQube 6.7 and TFS Plugin Sonar version 4.0.
Before the upgrade from of both the server and plugin I am able to get the coverage but now not no coverage is recorded from the MSTest. The same code with coverage is recorded when used with dot cover.
Sonar is able to record the number of tests and the coverage in TFS Build is also recorded yet it doesn't work. I have checked for the trx and the coverage files as well which is available but only *.coverage file is available not the coveragexml file. Is there any solution for this.
Seems to be the same issue as this one-- Code Coverage report is no longer imported
Try to use the workaround provided by Evangelink :
It seems like we have a regression on the Scanner for MSBuild (ticket:
https://jira.sonarsource.com/browse/SONARMSBRU-339).
While this ticket is being addressed you can fix this issue by simply
adding /d:sonar.cs.vscoveragexml.reportsPaths="**\*.coveragexml"
into the Additional Settings of the SonarQube Scanner for MSBuild -
Begin Analysis (new) task.
Just as you analyzed, with this command the .coveragexml will generated correctly and able to record the coverage.

Analyze c# code with sonarQube: MSBUILD version problems

I need to analyze various C# files (not a project, just .cs files put under the same directory).
I tried the analysis in 2 different ways and I have errors in each of it. Could someone help solve any of those errors?
1- I tried the analysis like mentioned here
By executing C:\sonar-scanner\bin\sonar-scanner.bat in the root file, I have this error
ERROR: Error during SonarQube Scanner execution
java.lang.IllegalArgumentException: Custom and 3rd party Roslyn analyzers
are only by MSBuild 14. Either use MSBuild 14, or disable the custom/3rd
party Roslyn analyzers in your quality profile.
2- I tired creating a project and analyzing with SonarQube Scanner for MSBuild
But at the end, I have this error that appears
The SonarQube MSBuild integration failed: SonarQube was unable to collect
the required information about your projects.
Possible causes:
1. The project has not been built - the project must be built in between
the begin and end steps
2. An unsupported version of MSBuild has been used to build the project.
Currently MSBuild 14.0 upwards are supported
3. The begin, build or end steps have not all been launched from the
same folder
Generation of the sonar-properties file failed. Unable to complete
SonarQube analysis.
I have Visual Studio 2015,2012,2010,2008 installed. How can I verify that I am using the correct MSBUILD verison (14) because I checked C:\Program Files (x86)\MSBuild and I found the version 14 there.
Is there another issue to handle ?
At the end, I need to be able to do the analysis in both the first and seocnd way.
Thank you in advance!
2- I tired creating a project and analyzing with SonarQube Scanner for MSBuild
According to the document, we need execute following commands from the root folder of the project you want to analyze:
SonarQube.Scanner.MSBuild.exe begin /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"Project Name" /v:"1.0"
MSBuild.exe /t:Rebuild
SonarQube.Scanner.MSBuild.exe end
But the command MSBuild.exe is a external command, can not be recognized by Windows in an arbitrary directory, you should add the MSBuild path "C:\Program Files (x86)\MSBuild\14.0\Bin" in to your system environment to make the begin, build or end steps are all launched from the same folder.

TFS Build Agent skipping all tests

I've set up a build controller and build agent for my TFS 2012.
I have a .NET 4.0 project with coded UI tests that I would like to run there.
I set the build configuration to run with VS Test Runner.
The problem is that the test runner skipping all of my tests!
(I can see Skipped <test_name> for all my tests in the log view when I run the build with Diagnostic logging verbosity)
Any ideas?
update -
I've googled a lot on this and found that adding a .testsettings file in the build configuration should solve this, but when I added one, the build fails with the following error (found nothing about this error :( ):
MSTestAdapter could not run the test as the classic mode is not available. Reason: Exception has been thrown by the target of invocation.
Directory C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\DataCollectors does not exist
and with this warning:
No tests is available in . Make sure that installed test discovers & executors, platform & framework version settings are appropriate and try again.
To run coded ui test in build server, you needed to run the build agent as an interactive process (Build service property, Run as Interactive Process), you also need setup a test controller and an interactive test agent.
http://msdn.microsoft.com/en-us/library/vstudio/ms181712.aspx#interactive
http://msdn.microsoft.com/en-us/library/dd648127.aspx
in your testsettings you can sepcify the testagent and other settings
http://msdn.microsoft.com/en-us/library/ee256991.aspx

Categories

Resources