-
Notifications
You must be signed in to change notification settings - Fork 5
/
Test-YamlADOPipeline.ps1
103 lines (87 loc) · 3.2 KB
/
Test-YamlADOPipeline.ps1
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<#
.SYNOPSIS
Performs actions to validate the given YAML file in Azure DevOps using an official Microsoft API.
.EXAMPLE
PS> .\Test-YamlADOPipeline.ps1 -OrganizationName my-org-name -ProjectName my-project-name -PipelineId 24 -YamlFilePath TestFiles/test.yml -PersonalAccessToken dSRjMqskBhxWlKwUOEGJzbYNLVmeXfaHATgZDcnoupIFPivCytQr
Validates the given YAML file on an existing pipeline definition (PAT is an example and randomized)
.PARAMETER OrganizationName
This describes name of the Azure DevOps organization name. [string]
.PARAMETER ProjectName
This describes name of the project. [string]
.PARAMETER PipelineId
This describes the definition id of the pipeline where the YAML needs to be validated. [int]
.PARAMETER YamlFilePath
This describes the path to the YAML file (e.g. ./TestFiles/test.yml). [string]
.PARAMETER PersonalAccessToken
This describes the authentication token for Azure DevOps. [string]
#>
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $OrganizationName,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $ProjectName,
[Parameter(Mandatory = $true)]
[int] $PipelineId,
[Parameter(Mandatory = $true)]
[ValidateScript({
if (-not ($_ | Test-Path)) {
throw "File or Path does not exist."
}
if ($_ -notmatch "(\.ya?ml)") {
throw "The file specified in the path must be of type yml or yaml."
}
return $true
})]
[string] $YamlFilePath,
# Build Read & Execute permissions!
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $PersonalAccessToken
)
function Add-CustomObjectProperties {
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory = $true)]
[PSCustomObject] $Object
)
if ($null -ne $Object) {
$Object | Add-Member -MemberType NoteProperty -Name "ResultMessage" -Value "Validation success"
$Object | Add-Member -MemberType NoteProperty -Name "StatusCode" -Value "200"
return $Object
}
}
$content = @"
$(Get-Content -Raw $YamlFilePath)
"@
$Body = @{
"PreviewRun" = "true"
"YamlOverride" = $content
}
$Url = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/pipelines/$PipelineId/runs?api-version=7.0"
$Arguments = @{
Method = "POST"
Uri = $Url
Body = $Body | ConvertTo-Json
ContentType = "application/json"
Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)")) }
}
try {
[PSCustomObject] $restOutput = Invoke-RestMethod @Arguments
Add-CustomObjectProperties -Object $restOutput
}
catch {
$ErrorMessage = $_.Exception.Message
try {
# If it's json
Write-Error ($ErrorMessage | ConvertFrom-Json).Message
}
catch {
# In case it's something else, when it's not parsable....
Write-Host "Make sure the PersonalAccessToken is valid."
Write-Host "To update the PAT, edit the Pipeline and update the Variable with a valid token with permissions: Build read+execute!"
Write-Error $ErrorMessage
}
exit 1
}