From fa75e0297b11a87812265dc5a4548f8a51eea780 Mon Sep 17 00:00:00 2001 From: techyian Date: Fri, 17 Jan 2020 11:22:18 +0000 Subject: [PATCH] #116. Add SonarCloud integration. --- appveyor.yml | 6 +++++ run-sonar.ps1 | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 run-sonar.ps1 diff --git a/appveyor.yml b/appveyor.yml index 6ea5c9f7..c454197a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -36,6 +36,9 @@ - dotnet pack .\src\MMALSharp.FFmpeg\MMALSharp.FFmpeg.csproj -c %CONFIGURATION% --version-suffix %APPVEYOR_BUILD_NUMBER% - dotnet pack .\src\MMALSharp.Processing\MMALSharp.Processing.csproj -c %CONFIGURATION% --version-suffix %APPVEYOR_BUILD_NUMBER% + test_script: + - ps: .\run-sonar.ps1 + deploy: # MyGet Deployment for builds & releases - provider: NuGet @@ -86,6 +89,9 @@ - dotnet pack .\src\MMALSharp.FFmpeg\MMALSharp.FFmpeg.csproj -c %CONFIGURATION% - dotnet pack .\src\MMALSharp.Processing\MMALSharp.Processing.csproj -c %CONFIGURATION% + test_script: + - ps: .\run-sonar.ps1 + deploy: # NuGet deployment - provider: NuGet diff --git a/run-sonar.ps1 b/run-sonar.ps1 new file mode 100644 index 00000000..891c292f --- /dev/null +++ b/run-sonar.ps1 @@ -0,0 +1,62 @@ +# Adapted from: https://github.com/NLog/NLog.Extensions.Logging/blob/master/run-sonar.ps1 + +$projectFile = "src\MMALSharp\MMALSharp.csproj" +$sonarQubeId = "MMALSharp" +$github = "techyian/MMALSharp" +$baseBranch = "master" +$framework = "netstandard2.0" + + +if ($env:APPVEYOR_REPO_NAME -eq $github) { + + if (-not $env:SONAR_TOKEN) { + Write-warning "Sonar: not running SonarQube, no SONAR_KEY" + return; + } + + $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 + + $sonarUrl = "https://sonarcloud.io" + $sonarKey = $env:SONAR_KEY + $sonarToken = $env:SONAR_TOKEN + $buildVersion = $env:APPVEYOR_BUILD_VERSION + + + if ($prMode) { + $pr = $env:APPVEYOR_PULL_REQUEST_NUMBER + Write-Output "Sonar: Running Sonar for PR $pr" + SonarScanner.MSBuild.exe begin /o:"$sonarKey" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /d:"sonar.analysis.mode=preview" /d:"sonar.github.pullRequest=$pr" /d:"sonar.github.repository=$github" /d:"sonar.github.oauth=$env:GITHUB_AUTH" + } + elseif ($branchMode) { + $branch = $env:APPVEYOR_REPO_BRANCH; + Write-Output "Sonar: Running Sonar in branch mode for branch $branch" + SonarScanner.MSBuild.exe begin /o:"$sonarKey" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" /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:"$sonarKey" /k:"$sonarQubeId" /d:"sonar.host.url=$sonarUrl" /d:"sonar.login=$sonarToken" /v:"$buildVersion" + } + + msbuild /t:Rebuild $projectFile /p:targetFrameworks=$framework /verbosity:minimal + + SonarScanner.MSBuild.exe end /d:"sonar.login=$env:sonar_token" +} +else { + Write-Output "Sonar: not running as we're on '$env:APPVEYOR_REPO_NAME'" +} \ No newline at end of file