forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert-ADOPipeline.ps1
171 lines (150 loc) · 5.91 KB
/
Convert-ADOPipeline.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function Convert-ADOPipeline
{
<#
.Synopsis
Converts builds to Azure DevOps Pipelines
.Description
Converts builds TFS or "Classic" builds to Azure DevOps YAML Pipelines.
.Link
New-ADOPipeline
.Link
Get-ADOTask
.Example
$taskList = (Get-ADOTask -Server $tfsRootUrl -Org $projectCollectionName)
Get-ADOBuild -Definition -Server $tfsRootUrl -Org $projectCollection |
Convert-ADOPipeline -TaskList $taskList
#>
[OutputType([string],[PSObject])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidAssignmentToAutomaticVariable", "", Justification="Functionality is Desired")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ForUnusedVariable", "", Justification="Functionality is Desired")]
param(
# A list of build steps.
# This will be automatically populated when piping in a TFS Build definition.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('Build')]
[PSObject[]]
$BuildStep,
# An object containing build variables.
# This will be automatically populated when piping in a TFS build definition.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Variable','Variables')]
[PSObject]
$BuildVariable,
# A list of task definitions. This will normally be the output from Get-ADOTask.
[Parameter(Mandatory)]
[PSObject[]]
$TaskList,
# A dictionary of conditional transformations.
[Alias('WhereForeach','WhereFor')]
[ValidateScript({
foreach ($k in $_.Keys) {
if ($k -isnot [string] -and $k -isnot [ScriptBlock]) {
throw "Key must be a string or ScriptBlock"
}
}
foreach ($v in $_.Values) {
if ($v -isnot [ScriptBlock]) {
throw "Values must be script blocks"
}
}
return $true
})]
[Collections.IDictionary]
$WhereFore,
# If set, will output the dictionary used to create each pipeline.
# If not set, will output the pipeline YAML.
[switch]
$Passthru
)
begin {
$q = [Collections.Queue]::new()
}
process {
#region Enqueue Input
$in = $_
if (-not $BuildStep.Task) {
Write-Error "Build Step must contain tasks"
return
}
$q.Enqueue(
@{psParameterSet= $PSCmdlet.ParameterSetName;InputObject= $in} + $PSBoundParameters
)
#endregion Enqueue Input
}
end {
$c, $t, $id = 0, $q.Count, [Random]::new().Next()
while ($q.Count) {
$dequeued = $q.Dequeue()
$BuildVariable = $null
foreach ($kv in $dequeued.GetEnumerator()) {
$ExecutionContext.SessionState.PSVariable.Set($kv.Key, $kv.Value)
}
$C++
Write-Progress "Converting Builds" "[$c of $t]" -PercentComplete ($c * 100 / $t) -Id $id
$buildSteps =
@(foreach ($bs in $BuildStep) {
$matchingTask = $TaskList |
Where-Object ID -eq $bs.task.id
if (-not $matchingTask) {
Write-Warning "Could not find task $($bs.task.id)"
} else {
$taskName= $matchingTask | Select-Object -ExpandProperty Name -Unique
$newTask = [Ordered]@{
task = $taskName + '@' + ($bs.task.versionSpec -replace '\.\*')
inputs = [Ordered]@{}
}
foreach ($prop in $bs.inputs.psobject.properties) {
if (-not [String]::IsNullOrWhiteSpace($prop.Value)) {
$newTask.inputs[$prop.Name] = $prop.Value
}
}
if (-not $newTask.inputs.count) {
$newTask.Remove('inputs')
}
if (-not $WhereFore.Count) {
$newTask
} else {
:outputted do {
foreach ($wf in $WhereFore.GetEnumerator()) {
$this = $_ = $newTask
if (
$wf.Key -is [string] -and $newTask.task -like $wf.Key -or
$wf.Key -is [ScriptBlock] -and (. $wf.Key)
) {
$this = $_ = $newTask
. $wf.Value
break outputted
}
}
$newTask
} while ($false)
}
}
})
$buildVariables = @(
if ($BuildVariable) {
foreach ($bv in $BuildVariable.psobject.properties) {
[Ordered]@{name=$bv.Name;value=$bv.value.value}
}
}
)
if ($PassThru) {
$out = ([Ordered]@{variables=$BuildVariables;steps=$buildSteps})
if ($inputObject) {
Add-Member -InputObject $out -MemberType NoteProperty -Name InputObject -Value $inputObject -Force
}
$out
} else {
$newPipeline =
New-ADOPipeline -InputObject ([Ordered]@{variables=$BuildVariables;steps=$buildSteps})
if ($inputObject) {
$newPipeline |
Add-Member NoteProperty InputObject $inputObject -Force
}
$newPipeline
}
}
$C++
Write-Progress "Converting Builds" "[$c of $t]" -Completed -Id $id
}
}