Skip to content

Commit b3303f7

Browse files
authored
Merge pull request #44 from WeAreInSpark/dylan/check-params
refactor: Checking param convention and removing redundant code
2 parents 351f276 + 0b47f9c commit b3303f7

29 files changed

Lines changed: 407 additions & 343 deletions

AzureDevOpsPowerShell/Private/Invoke-AzDoRestMethod.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ function Invoke-AzDoRestMethod {
2929

3030
[Parameter()]
3131
[string]
32-
$PAT,
32+
$Pat,
3333

3434
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
3535
[PSCustomObject[]]
3636
$Body
3737
)
3838

3939
begin {
40+
Write-Verbose "Starting function: Invoke-AzDoRestMethod"
41+
Write-Debug "uri: $uri"
42+
Write-Debug "version: $Version"
43+
Write-Debug "method: $Method"
44+
Write-Debug "body: $($body | ConvertTo-Json -Depth 10)"
45+
4046
if (-not($script:header)) {
4147
try {
42-
New-AzDoAuthHeader -PAT $PAT -ErrorAction Stop
48+
New-AzDoAuthHeader -PAT $Pat -ErrorAction Stop
4349
} catch {
4450
throw $_
4551
}

AzureDevOpsPowerShell/Private/New-AzDoAuthHeader.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ function New-AzDoAuthHeader {
44
# PAT to authenticate with the organization
55
[Parameter()]
66
[String]
7-
$PAT
7+
$Pat
88
)
99
if ($PSCmdlet.ShouldProcess("Creating new authentication header")) {
1010
Write-Verbose "Function: New-AzDoAuthHeader"
11-
if ($PAT -eq '') {
11+
if ($Pat -eq '') {
1212
# validate if user is logged in to Azure PowerShell
1313
Write-Verbose "Using Access Token"
1414
try {

AzureDevOpsPowerShell/Public/Api/ApprovalsAndChecks/CheckConfigurations/Add-AzDoPipelineBranchControl.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ function Add-AzDoPipelineBranchControl {
8484
)
8585

8686
begin {
87-
$body = New-Object -TypeName "System.Collections.ArrayList"
87+
Write-Verbose "Starting function: Add-AzDoPipelineBranchControl"
8888
}
8989

90-
Process {
90+
process {
91+
9192
$projectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).projectId
9293

9394
foreach ($name in $ResourceName) {
@@ -139,7 +140,7 @@ function Add-AzDoPipelineBranchControl {
139140
body = $body
140141
}
141142

142-
if ($PSCmdlet.ShouldProcess($ProjectName, "Create envrironment named: $($PSStyle.Bold)$PolicyName$($PSStyle.Reset)")) {
143+
if ($PSCmdlet.ShouldProcess($ProjectName, "Create build-validation policy named: $($PSStyle.Bold)$PolicyName$($PSStyle.Reset)")) {
143144
Invoke-AzDoRestMethod @params | ForEach-Object {
144145
[PSCustomObject]@{
145146
CollectionUri = $CollectionUri
@@ -148,7 +149,7 @@ function Add-AzDoPipelineBranchControl {
148149
}
149150
}
150151
} else {
151-
$Body | Format-List
152+
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
152153
}
153154
}
154155
}

AzureDevOpsPowerShell/Public/Api/Build/GeneralSettings/Set-AzDOProjectSetting.ps1

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ function Set-AzDoProjectSetting {
3131
[string]
3232
$CollectionUri,
3333

34-
# PAT to get access to Azure DevOps.
35-
[Parameter()]
36-
[string]
37-
$PAT,
38-
3934
# Name of the project
4035
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
4136
[string]
@@ -136,22 +131,14 @@ function Set-AzDoProjectSetting {
136131
[switch]
137132
$StatusBadgesArePrivate
138133
)
139-
Begin {
140-
begin {
141-
if (-not($script:header)) {
142-
143-
try {
144-
New-ADOAuthHeader -PAT $PAT -ErrorAction Stop
145-
} catch {
146-
$PSCmdlet.ThrowTerminatingError($_)
147-
}
148-
}
149-
}
150134

151-
$Projects = (Get-AzDoProject -CollectionUri = $CollectionUri -ProjectName $ProjectName -PAT $PAT | Where-Object ProjectName -EQ $ProjectName).Projectid
152-
}
153-
Process {
154-
$Body = @{
135+
begin {
136+
Write-Verbose "Starting function: Set-AzDOProjectSetting"
137+
}
138+
139+
process {
140+
141+
$body = @{
155142
buildsEnabledForForks = [bool]$BuildsEnabledForForks
156143
disableClassicBuildPipelineCreation = [bool]$DisableClassicBuildPipelineCreation
157144
disableClassicPipelineCreation = [bool]$DisableClassicPipelineCreation
@@ -174,14 +161,15 @@ function Set-AzDoProjectSetting {
174161
}
175162

176163
$params = @{
177-
uri = "$CollectionUri/$ProjectId/_apis/build/generalsettings?api-version=7.2-preview.1"
164+
uri = "$CollectionUri/$ProjectName/_apis/build/generalsettings?api-version=7.2-preview.1"
178165
Method = 'PATCH'
179166
Headers = $script:header
180-
body = $Body
167+
body = $body
181168
ContentType = 'application/json'
182169
}
183170

184171
if ($PSCmdlet.ShouldProcess($CollectionUri, "Set provided settings at the project named: $($PSStyle.Bold)$Projectname$($PSStyle.Reset)")) {
172+
185173
$response = Invoke-RestMethod @params
186174

187175
[PSCustomObject]@{
@@ -207,7 +195,7 @@ function Set-AzDoProjectSetting {
207195
StatusBadgesArePrivate = $response.statusBadgesArePrivate
208196
}
209197
} else {
210-
$body
198+
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
211199
}
212200
}
213201
}

AzureDevOpsPowerShell/Public/Api/Core/Projects/Get-AzDoProject.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ function Get-AzDoProject {
6363
$ProjectName
6464
)
6565

66-
Process {
66+
begin {
67+
Write-Verbose "Starting function: Get-AzDoProject"
68+
}
69+
70+
process {
6771

6872
$params = @{
6973
uri = "$CollectionUri/_apis/projects"
@@ -72,12 +76,7 @@ function Get-AzDoProject {
7276
}
7377

7478
if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Project(s)")) {
75-
Write-Debug "Calling Invoke-AzDoRestMethod with"
76-
Write-Debug ($params | Out-String)
77-
7879
$result = (Invoke-AzDoRestMethod @params).value | Where-Object { -not $ProjectName -or $_.Name -in $ProjectName }
79-
80-
8180
} else {
8281
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
8382
}

AzureDevOpsPowerShell/Public/Api/Core/Projects/New-AzDoProject.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ function New-AzDoProject {
124124
$Visibility = 'private'
125125
)
126126

127-
Process {
127+
begin {
128+
Write-Verbose "Starting function: New-AzDoProject"
129+
}
130+
131+
process {
128132

129133
$params = @{
130134
uri = "$CollectionUri/_apis/projects"
@@ -134,7 +138,7 @@ function New-AzDoProject {
134138

135139
foreach ($name in $ProjectName) {
136140

137-
$Body = @{
141+
$body = @{
138142
name = $name
139143
description = $Description
140144
visibility = $Visibility
@@ -149,9 +153,6 @@ function New-AzDoProject {
149153
}
150154

151155
if ($PSCmdlet.ShouldProcess($CollectionUri, "Create project named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
152-
Write-Debug "Calling Invoke-AzDoRestMethod with"
153-
Write-Debug ($params | Out-String)
154-
155156
try {
156157
$body | Invoke-AzDoRestMethod @params | Out-Null
157158
} catch {
@@ -181,7 +182,7 @@ function New-AzDoProject {
181182
}
182183
}
183184

184-
End {
185+
end {
185186
if ($result) {
186187
$result
187188
}

AzureDevOpsPowerShell/Public/Api/Core/Projects/Remove-AzDoProject.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ function Remove-AzDoProject {
4646
)
4747

4848
begin {
49+
Write-Verbose "Starting function: Remove-AzDoProject"
4950
$result = @()
5051
}
5152

5253
Process {
54+
5355
foreach ($name in $ProjectName) {
5456

5557
$projectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $name).ProjectID
@@ -61,9 +63,6 @@ function Remove-AzDoProject {
6163
}
6264

6365
if ($PSCmdlet.ShouldProcess($CollectionUri, "Delete project named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
64-
Write-Debug "Calling Invoke-AzDoRestMethod with"
65-
Write-Debug ($params | Out-String)
66-
6766
$result += Invoke-AzDoRestMethod @params
6867
} else {
6968
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"

AzureDevOpsPowerShell/Public/Api/DistributedTask/VariableGroups/Add-AzDoVariableGroupVariable.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ function Add-AzDoVariableGroupVariable {
6161
$Variables
6262
)
6363

64-
Begin {
65-
$result = New-Object -TypeName "System.Collections.ArrayList"
64+
begin {
65+
$result = @()
66+
Write-Verbose "Starting function: Add-AzDoVariableGroupVariable"
6667
}
6768

68-
Process {
69+
process {
70+
Write-Information "Starting function: Add-AzDoVariableGroupVariable"
6971
$groups = Get-AzDoVariableGroup -CollectionUri $CollectionUri -ProjectName $ProjectName
7072

7173
# Get the variable group based on it's name and match to ID for URI
@@ -86,20 +88,18 @@ function Add-AzDoVariableGroupVariable {
8688
$params = @{
8789
uri = "$CollectionUri/$ProjectName/_apis/distributedtask/variablegroups/$($group.VariableGroupId)?api-version=7.1-preview.1"
8890
Method = 'PUT'
89-
Headers = $script:header
9091
body = $Body | ConvertTo-Json -Depth 99
9192
ContentType = 'application/json'
9293
}
9394

9495
if ($PSCmdlet.ShouldProcess($CollectionUri, "Add Variables to Variable Group named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
95-
Write-Information "Creating variables in variable group $VariableGroupName"
96-
$result.add((Invoke-AzDoRestMethod @params)) | Out-Null
96+
$result += Invoke-AzDoRestMethod @params
9797
} else {
98-
$body | Out-String
98+
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
9999
}
100100
}
101101

102-
End {
102+
end {
103103
if ($result) {
104104
$result | ForEach-Object {
105105
[PSCustomObject]@{

AzureDevOpsPowerShell/Public/Api/DistributedTask/VariableGroups/Get-AzDoVariableGroup.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@ function Get-AzDoVariableGroup {
4646
$VariableGroupName
4747
)
4848

49-
Begin {
49+
begin {
5050
$result = @()
51+
Write-Verbose "Starting function: Get-AzDoVariableGroupVariable"
5152
}
5253

53-
Process {
54+
process {
55+
5456
$params = @{
5557
uri = "$CollectionUri/$ProjectName/_apis/distributedtask/variablegroups"
5658
version = "7.1-preview.2"
5759
method = 'GET'
5860
}
5961

6062
if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Variable groups from: $($PSStyle.Bold)$ProjectName$($PSStyle.Reset)")) {
61-
Write-Debug "Calling Invoke-AzDoRestMethod with"
62-
Write-Debug ($params | Out-String)
63-
6463
$variableGroups = (Invoke-AzDoRestMethod @params).value
6564
if ($VariableGroupName) {
6665
foreach ($name in $VariableGroupName) {
@@ -75,7 +74,7 @@ function Get-AzDoVariableGroup {
7574
}
7675
}
7776

78-
End {
77+
end {
7978
if ($result) {
8079
$result | ForEach-Object {
8180
[PSCustomObject]@{

AzureDevOpsPowerShell/Public/Api/DistributedTask/VariableGroups/New-AzDoVariableGroup.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ function New-AzDoVariableGroup {
6565
$Description
6666
)
6767

68-
Begin {
69-
$body = New-Object -TypeName "System.Collections.ArrayList"
70-
$result = New-Object -TypeName "System.Collections.ArrayList"
68+
begin {
69+
$result = @()
70+
Write-Verbose "Starting function: New-AzDoVariableGroupVariable"
7171
}
7272

73-
Process {
73+
process {
7474

7575
$params = @{
7676
uri = "$CollectionUri/$ProjectName/_apis/distributedtask/variablegroups"
@@ -100,9 +100,9 @@ function New-AzDoVariableGroup {
100100
}
101101

102102
if ($PSCmdlet.ShouldProcess($ProjectName, "Create Variable Group named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
103-
$result.add(($body | Invoke-AzDoRestMethod @params)) | Out-Null
103+
$result += ($body | Invoke-AzDoRestMethod @params)
104104
} else {
105-
$body | Out-String
105+
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
106106
}
107107
}
108108
}

0 commit comments

Comments
 (0)