forked from microsoft/PowerPlatformConnectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger_validation.yml
87 lines (68 loc) · 3.31 KB
/
swagger_validation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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$'}
$Headers = @{
Authorization = $env:token
}
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"
$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)