PowerPlatformConnectors/.pipelines/swagger_validation.yml

89 строки
3.5 KiB
YAML

pool:
vmImage: "ubuntu-latest"
steps:
- pwsh: |
$errorsTotal = 0
$warningsTotal = 0
# Get the list of files for the given PR
$currentLocation = Get-Location
$files = git diff HEAD~1 --name-only
$swaggerFiles = $files | where {$_ -match '.+?apidefinition.swagger.json$'}
$token2 = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$(userpass)"))
$Headers = @{ Authorization = $token2 }
foreach ($file in $swaggerFiles) {
$currentFilePath = Join-Path $currentLocation ($file.Replace('/', '\'))
$previousCommitHash = git log --max-count=1 --skip=1 --first-parent --pretty=format:%H $currentFilePath
$newFileContent = Get-Content $file -Raw
# Validate swagger
$isCertValidation = $file -Like "*certified-connectors/*" -Or $file -Like "*independent-publisher-connectors/*"
$swaggerValidatorUri = "$($env:swaggerValidator)?IsCertificationValidation=$isCertValidation"
Write-Host "Uri = $swaggerValidatorUri"
$results = Invoke-RestMethod -Uri $swaggerValidatorUri -Headers $Headers -Method Post -ContentType "application/json; charset=utf-8" -Body $newFileContent
$errors = $results.Errors
$warnings = $results.Warnings
if ($errors) {
$errorsTotal += $errors.Count
$errors | foreach { Write-Host "##vso[task.logissue type=error;]$_" }
} # If Swagger Error
if ($warnings) {
$warningsTotal += $warnings.Count
$warnings | foreach { Write-Host "##vso[task.logissue type=warning;sourcepath=$file;]$_" }
} # If Swagger Warnings
# Check for breaking changes when previous commit exits
# And there is NO swagger validation error
if ($previousCommitHash -AND -NOT $errors) {
$oldSwagger = git show "$($previousCommitHash):$($file)" | Out-String | ConvertFrom-Json
$newSwagger = $newFileContent | ConvertFrom-Json
$changeValidation = @{
OldSwagger = $oldSwagger
NewSwagger = $newSwagger
}
$changeValidationJson = ConvertTo-Json $changeValidation -Depth 100 -Compress
# Validate changes
$results = Invoke-RestMethod -Uri $env:changeValidator -Headers $Headers -Method Post -ContentType "application/json; charset=utf-8" -Body $changeValidationJson
$errors = $results.Errors
$warnings = $results.Warnings
if ($errors) {
$errorsTotal += $errors.Count
$errors | foreach { Write-Host "##vso[task.LogIssue type=error;]$_" }
} # If there are any errors
if ($warnings) {
$warningsTotal += $warnings.Count
$warnings | foreach { Write-Host "##vso[task.LogIssue type=warning;sourcepath=$file;]$_" }
} # If there are any warnings
} # If previous commit exists perform breaking change validation
} # For each file
if ($errorsTotal -gt 0) {
Write-Host "##vso[task.complete result=Failed;]Errors encountered."
}
elseif ($warningsTotal -gt 0) {
Write-Host "##vso[task.complete result=SucceededWithIssues;]Warnings encountered."
}
else {
Write-Host "##vso[task.complete result=Succeeded;]No error or warnings encountered."
}
env:
swaggerValidator: "$(ValidatorUrl)/ValidateSwagger"
changeValidator: "$(ValidatorUrl)/ValidateChange"
token: $(token)