Created
December 2, 2019 23:36
-
-
Save mmercan/7d784b9039600f764263cfe3e9536eb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on: https://github.com/michaelcostabr/SonarQubeBuildBreaker/blob/master/SonarQubeBuildBreaker.ps1 | |
Param ( | |
[String] | |
[Parameter(Mandatory = $true)] | |
$SonarToken, | |
[String] | |
[Parameter(Mandatory = $true)] | |
$DotSonarQubeFolder, | |
[String] | |
[Parameter(Mandatory = $false)] | |
$backupServerUrl, | |
[String] | |
[Parameter(Mandatory = $false)] | |
$backupProjectkey | |
) | |
$TokenAsBytes = [System.Text.Encoding]::UTF8.GetBytes(("$SonarToken" + ":")) | |
$Base64Token = [System.Convert]::ToBase64String($TokenAsBytes) | |
$AuthorizationHeaderValue = [String]::Format("Basic {0}", $Base64Token) | |
$Headers = @{ | |
Authorization = $AuthorizationHeaderValue; | |
AcceptType = "application/json" | |
} | |
function GetStatus { | |
param ( | |
[Parameter(Mandatory = $true)] $ServerUrl, | |
[Parameter(mandatory = $false)] $analysisId, | |
[Parameter(mandatory = $false)] $projectkey | |
) | |
if ($analysisId) { | |
$AnalysisUrl = "{0}/api/qualitygates/project_status?analysisId={1}" -f $ServerUrl, $analysisId #$Response.task.analysisId | |
} | |
else { | |
$AnalysisUrl = "{0}/api/qualitygates/project_status?projectKey={1}" -f $ServerUrl, $projectkey #$Response.task.analysisId | |
} | |
Write-Host "AnalysisUrl : $AnalysisUrl" | |
$ResponseAnalysis = Invoke-WebRequest -Uri $AnalysisUrl -Headers $Headers -UseBasicParsing | ConvertFrom-Json | |
Write-Host "ResponseAnalysis" | |
$ResponseAnalysis | |
Write-Host "ResponseAnalysis Status" $ResponseAnalysis.projectStatus.status | |
#return $ResponseAnalysis | |
if (($ResponseAnalysis.projectStatus.status -ne 'OK') -and ($ResponseAnalysis.projectStatus.status -ne 'NONE')) { | |
$ErrorMsg = "##vso[task.LogIssue type=error;] Quality gate FAILED. Please check it here: {0}/dashboard?id={1}" -f $ServerUrl, $ProjectKey | |
Write-Output $ErrorMsg | |
Write-Output "##vso[task.complete result=Failed;]" | |
Write-Error $ErrorMsg | |
} | |
} | |
$SonarTaskFile = "$DotSonarQubeFolder\out\.sonar\report-task.txt" | |
if ([System.IO.File]::Exists($SonarTaskFile)) { | |
$ProjectKey = Get-Content -Path $SonarTaskFile | Where-Object { $_ -Match 'projectKey=' } | |
$ProjectKey = $ProjectKey -replace "projectKey=" -replace "" | |
Write-Host "ProjectKey: $ProjectKey" | |
$ServerUrl = Get-Content -Path $SonarTaskFile | Where-Object { $_ -Match 'serverUrl=' } | |
$ServerUrl = $ServerUrl.Split('=')[1] | |
Write-Host "ServerUrl : $ServerUrl " | |
$CeTaskUrl = Get-Content -Path $SonarTaskFile | Where-Object { $_ -Match 'ceTaskUrl=' } | |
$CeTaskUrl = $CeTaskUrl -replace "ceTaskUrl=" -replace "" | |
Write-Host "CeTaskUrl : $CeTaskUrl" | |
$Response = Invoke-WebRequest -Uri $CeTaskUrl -Headers $Headers -UseBasicParsing | ConvertFrom-Json | |
Write-Host "analysisId : " $Response.task.analysisId | |
Start-Sleep -s 15 | |
GetStatus -ServerUrl $ServerUrl -analysisId $Response.task.analysisId | |
} | |
else { | |
GetStatus -ServerUrl $backupServerUrl -projectkey $backupProjectkey | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment