Skip to content

Commit 523d7f1

Browse files
author
Nico Orschel
committed
Refactor parameter formatting and add API version compatibility warning in Remove-ADOPicklist function
1 parent 8cf2bec commit 523d7f1

1 file changed

Lines changed: 58 additions & 52 deletions

File tree

Functions/AzureDevOps/Remove-ADOPicklist.ps1

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@
1111
.Link
1212
https://docs.microsoft.com/en-us/rest/api/azure/devops/processes/lists/delete
1313
#>
14-
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
14+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
1515
[OutputType([Nullable], [Hashtable])]
1616
param(
17-
# The Organization.
18-
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
19-
[Alias('Org')]
20-
[string]
21-
$Organization,
22-
23-
# The PicklistID.
24-
[Parameter(Mandatory,ValueFromPipelineByPropertyName,
25-
ParameterSetName='work/processes/lists/{PicklistId}')]
26-
[string]
27-
$PicklistID,
28-
29-
# A list of items to remove.
30-
# If this parameter is provided, the picklist items will be removed, and the picklist will not be deleted.
31-
# If this parameter is not provided, the picklist will not be deleted.
32-
[Parameter(ParameterSetName='work/processes/lists/{PicklistId}')]
33-
[Alias('Value', 'Items','Values')]
34-
[string[]]
35-
$Item,
36-
37-
# The server. By default https://dev.azure.com/.
38-
# To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs).
39-
[Parameter(ValueFromPipelineByPropertyName)]
40-
[uri]
41-
$Server = "https://dev.azure.com/",
42-
43-
# The api version. By default, 5.1-preview.
44-
# If targeting TFS, this will need to change to match your server version.
45-
# See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops
46-
[string]
47-
$ApiVersion = "5.1-preview"
17+
# The Organization.
18+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
19+
[Alias('Org')]
20+
[string]
21+
$Organization,
22+
23+
# The PicklistID.
24+
[Parameter(Mandatory, ValueFromPipelineByPropertyName,
25+
ParameterSetName = 'work/processes/lists/{PicklistId}')]
26+
[string]
27+
$PicklistID,
28+
29+
# A list of items to remove.
30+
# If this parameter is provided, the picklist items will be removed, and the picklist will not be deleted.
31+
# If this parameter is not provided, the picklist will not be deleted.
32+
[Parameter(ParameterSetName = 'work/processes/lists/{PicklistId}')]
33+
[Alias('Value', 'Items', 'Values')]
34+
[string[]]
35+
$Item,
36+
37+
# The server. By default https://dev.azure.com/.
38+
# To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs).
39+
[Parameter(ValueFromPipelineByPropertyName)]
40+
[uri]
41+
$Server = "https://dev.azure.com/",
42+
43+
# The api version. By default, 5.1-preview.
44+
# If targeting TFS, this will need to change to match your server version.
45+
# See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops
46+
[string]
47+
$ApiVersion = "5.1-preview"
4848
)
4949

5050
dynamicParam { . $GetInvokeParameters -DynamicParameter }
@@ -56,8 +56,12 @@
5656
}
5757

5858
process {
59+
if ($ApiVersion -like '5.*') {
60+
Write-Warning "The API version '$ApiVersion' may not be compatible with field operations. Consider using '7.0' or later."
61+
}
62+
5963
$ParameterSet = $psCmdlet.ParameterSetName
60-
$q.Enqueue(@{ParameterSet=$ParameterSet} + $PSBoundParameters)
64+
$q.Enqueue(@{ParameterSet = $ParameterSet } + $PSBoundParameters)
6165
}
6266
end {
6367
$c, $t, $id = 0, $q.Count, [Random]::new().Next()
@@ -67,27 +71,28 @@
6771

6872

6973
$uri = # The URI is comprised of:
70-
@(
71-
"$server".TrimEnd('/') # the Server (minus any trailing slashes),
72-
$Organization # the Organization,
73-
'_apis' # the API Root ('_apis'),
74-
(. $ReplaceRouteParameter $ParameterSet)
75-
# and any parameterized URLs in this parameter set.
76-
) -as [string[]] -ne '' -join '/'
74+
@(
75+
"$server".TrimEnd('/') # the Server (minus any trailing slashes),
76+
$Organization # the Organization,
77+
'_apis' # the API Root ('_apis'),
78+
(. $ReplaceRouteParameter $ParameterSet)
79+
# and any parameterized URLs in this parameter set.
80+
) -as [string[]] -ne '' -join '/'
7781

7882
$uri += '?' # The URI has a query string containing:
7983
$uri += @(
8084
if ($Server -ne 'https://dev.azure.com/' -and
8185
-not $PSBoundParameters.ApiVersion) {
8286
$ApiVersion = '2.0'
8387
}
84-
if ($ApiVersion) { # the api-version
88+
if ($ApiVersion) {
89+
# the api-version
8590
"api-version=$apiVersion"
8691
}
8792
) -join '&'
8893

8994
$c++
90-
Write-Progress "Removing $($Item -join ' ')" "[$c/$t] $uri" -Id $id -PercentComplete ($c * 100/$t)
95+
Write-Progress "Removing $($Item -join ' ')" "[$c/$t] $uri" -Id $id -PercentComplete ($c * 100 / $t)
9196

9297
$invokeParams.Uri = $uri
9398

@@ -98,25 +103,26 @@
98103
$getPicklistSplat.Remove('Name')
99104
$getPicklistSplat.Remove('Item')
100105
$picklistItems = Get-ADOPicklist @getPicklistSplat
101-
$picklistItems.items = @(
106+
$picklistItems.items = @(
102107
$picklistItems.items |
103-
Where-Object {
104-
foreach ($i in $item) {
105-
if ( $_ -like $i) { return }
106-
}
107-
$_
108-
})
108+
Where-Object {
109+
foreach ($i in $item) {
110+
if ( $_ -like $i) { return }
111+
}
112+
$_
113+
})
109114
$invokeParams.body = $picklistItems
110115
$invokeParams.pstypename = "$Organization.Picklist.Detail", "PSDevOps.Picklist.Detail"
111-
} else {
112-
$invokeParams.Method = 'DELETE'
116+
}
117+
else {
118+
$invokeParams.Method = 'DELETE'
113119
}
114120
if ($WhatIfPreference) {
115121
$invokeParams.Remove('PersonalAccessToken')
116122
$invokeParams
117123
continue
118124
}
119-
if (-not $psCmdlet.ShouldProcess("$($invokeParams.Method) $($item -join ' ') $($invokeParams.uri)")) {continue }
125+
if (-not $psCmdlet.ShouldProcess("$($invokeParams.Method) $($item -join ' ') $($invokeParams.uri)")) { continue }
120126
Invoke-ADORestAPI @invokeParams
121127
}
122128

0 commit comments

Comments
 (0)