Skip to content

Commit

Permalink
Fix SonarQube Scanning (#494)
Browse files Browse the repository at this point in the history
* Upgrade sonarscanner

* dotnet-sonarscanner

* check $LastExitCode

* Java 11

* Update run-sonar.ps1

* pullrequest.key / pullrequest.base

* cleanup, remove special base branch mode

* fixup

* cleanup output, write-host
  • Loading branch information
304NotModified authored Mar 26, 2021
1 parent 4bd1909 commit 084cd50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ test_script:
- OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.1 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test"
- OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net5.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test"
- pip install codecov
- pip install codecov
- codecov -f "coverage.xml"
- set JAVA_HOME=C:\Program Files\Java\jdk11
- set PATH=%JAVA_HOME%\bin;%PATH%
- ps: .\run-sonar.ps1
- ps: .\run-tests.ps1

Expand Down
46 changes: 25 additions & 21 deletions run-sonar.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,50 @@ if ($env:APPVEYOR_REPO_NAME -eq $github) {
}

$prMode = $false;
$branchMode = $false;

if ($env:APPVEYOR_PULL_REQUEST_NUMBER) {
# first check PR as that is on the base branch
$prMode = $true;
Write-Output "Sonar: on PR $env:APPVEYOR_PULL_REQUEST_NUMBER"
}
elseif ($env:APPVEYOR_REPO_BRANCH -eq $baseBranch) {
Write-Output "Sonar: on base branch ($baseBranch)"
}
else {
$branchMode = $true;
Write-Output "Sonar: on branch $env:APPVEYOR_REPO_BRANCH"
}

choco install "msbuild-sonarqube-runner" -y
dotnet tool install --global dotnet-sonarscanner
if (-Not $LastExitCode -eq 0) {
exit $LastExitCode
}

$sonarUrl = "https://sonarcloud.io"
$sonarToken = $env:sonar_token
$buildVersion = $env:APPVEYOR_BUILD_VERSION

if ($prMode) {
$branch = $env:APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH
$prBaseBranch = $env:APPVEYOR_REPO_BRANCH;
$pr = $env:APPVEYOR_PULL_REQUEST_NUMBER
Write-Output "Sonar: Running Sonar for PR $pr"
SonarScanner.MSBuild.exe begin /o:"$sonarOrg" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.cs.opencover.reportsPaths=coverage.xml" /d:"sonar.analysis.mode=preview" /d:"sonar.github.pullRequest=$pr" /d:"sonar.github.repository=$github" /d:"sonar.github.oauth=$env:github_auth_token"

Write-Host "Sonar: on PR $pr from $branch to $prBaseBranch" -ForegroundColor DarkGreen -BackgroundColor White
dotnet-sonarscanner begin /o:"$sonarOrg" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.cs.opencover.reportsPaths=coverage.xml" /d:"sonar.pullrequest.key=$pr" /d:"sonar.pullrequest.branch=$branch" /d:"sonar.pullrequest.base=$prBaseBranch" /d:"sonar.github.repository=$github" /d:"sonar.github.oauth=$env:github_auth_token"
}
elseif ($branchMode) {
else {
$branch = $env:APPVEYOR_REPO_BRANCH;
Write-Output "Sonar: Running Sonar in branch mode for branch $branch"
SonarScanner.MSBuild.exe begin /o:"$sonarOrg" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.cs.opencover.reportsPaths=coverage.xml" /d:"sonar.branch.name=$branch"

Write-Host "Sonar: on branch $branch" -ForegroundColor DarkGreen -BackgroundColor White
dotnet-sonarscanner begin /o:"$sonarOrg" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.cs.opencover.reportsPaths=coverage.xml" /d:"sonar.branch.name=$branch"
}
else {
Write-Output "Sonar: Running Sonar in non-preview mode, on branch $env:APPVEYOR_REPO_BRANCH"
SonarScanner.MSBuild.exe begin /o:"$sonarOrg" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.cs.opencover.reportsPaths=coverage.xml"

if (-Not $LastExitCode -eq 0) {
exit $LastExitCode
}

msbuild /t:Rebuild $projectFile /p:targetFrameworks=$framework /verbosity:minimal
if (-Not $LastExitCode -eq 0) {
exit $LastExitCode
}

SonarScanner.MSBuild.exe end /d:"sonar.login=$env:sonar_token"
dotnet-sonarscanner end /d:"sonar.login=$env:sonar_token"
if (-Not $LastExitCode -eq 0) {
exit $LastExitCode
}
}
else {
Write-Output "Sonar: not running as we're on '$env:APPVEYOR_REPO_NAME'"
}
Write-Host "Sonar: not running as we're on '$env:APPVEYOR_REPO_NAME'"
}

0 comments on commit 084cd50

Please sign in to comment.