How do I get SonarQube to ignore the GlobalSuppressions.cs file?

僤鯓⒐⒋嵵緔 提交于 2020-01-04 05:17:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!