问题
I want to exclude the GlobalSuppressions.cs file from SonarQube analysis so that it doesn't look at the System.Diagnostics.CodeAnalysis.SuppressMessage directives in there.
These are in the root of each project, but not in the root where the solution is: E.g.
I set this in the admin:
Namely, **/GlobalSuppressions.cs.
There are many projects in each solution, so I would like to avoid referencing each individually if possible.
回答1:
My solution to this problem was to use a Powershell script to clear the globalsupressions.cs file and alter the ruleset from one that errored (CodeAnalysisRulesErrors.ruleset or CodeAnalysisRulesUnitTestsErrors.ruleset) to one that threw warnings (CodeAnalysisRules.ruleset or CodeAnalysisRulesUnitTests.ruleset), that way SonarQube correctly reported on the technical debt.
[CmdletBinding()]
param (
[string]$localWorkspace
)
begin{}
process
{
try
{
$localWorkspace = "$($env:SYSTEM_DEFAULTWORKINGDIRECTORY)\$($localWorkspace)"
$localWorkspace = $localWorkspace -replace "/" , "\"
$localWorkspace = $localWorkspace -replace "\\" , "\"
Write-Verbose $localWorkspace
#Work out top level directories, excluding system dirs
[System.Collections.ArrayList]$topLevelDirs = @()
$topLevelItem = Get-ChildItem $localWorkspace -Exclude @("node_modules", "packages", "Common", ".nuget", ".vs", "_Resharper.Caches", "Javascript")
foreach ($item in $topLevelItem)
{
if (Test-Path $item -PathType Container) {
Write-Verbose $item
$topLevelDirs.Add($item)
}
}
foreach ($topLevelFolder in $topLevelDirs)
{
Write-Verbose $topLevelFolder
$ServiceDirs = Get-ChildItem -Path $topLevelFolder -Filter GlobalSuppressions.cs -Recurse
foreach ($sd in $ServiceDirs)
{
Write-Verbose $sd
Clear-Content $sd.FullName
}
Get-ChildItem -Path $topLevelFolder -Filter *.csproj -Recurse | ForEach {
Write-Verbose $_.FullName
(Get-Content $_.FullName | ForEach { $_ -replace 'CodeAnalysisRulesErrors.ruleset', 'CodeAnalysisRules.ruleset' }) | Set-Content $_.FullName
(Get-Content $_.FullName | ForEach { $_ -replace 'CodeAnalysisRulesUnitTestsErrors.ruleset', 'CodeAnalysisRulesUnitTests.ruleset' }) | Set-Content $_.FullName
}
}
}
catch
{
write-host "Caught an exception:"
write-host "Exception Type: $($_.Exception.GetType().FullName)"
write-host "Exception Message: $($_.Exception.Message)"
}
}
end{}
来源:https://stackoverflow.com/questions/37841029/how-do-i-get-sonarqube-to-ignore-the-globalsuppressions-cs-file