-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDevOps.Repos.ps1
More file actions
532 lines (481 loc) · 19.6 KB
/
DevOps.Repos.ps1
File metadata and controls
532 lines (481 loc) · 19.6 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<#
.SYNOPSIS
Get all repos from Azure DevOps project
.DESCRIPTION
Get all repos from Azure DevOps project using Azure DevOps Rest API
.PARAMETER Project
Project name for Azure DevOps
.EXAMPLE
Get-AzDevOpsRepos -Project $Project
#>
Function Get-AzDevOpsRepos {
[CmdletBinding()]
[OutputType([System.Object[]])]
param (
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory=$false)]
[switch]
$IncludeDisabled
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$header = $script:connection.GetHeader()
Write-Verbose "Getting repos for project $Project"
$uri = "https://dev.azure.com/$Organization/$Project/_apis/git/repositories?api-version=6.0"
Write-Verbose "URI: $uri"
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
}
catch {
throw $_.Exception.Message
}
# If the IncludeDisabled switch is set, return all repos including disabled ones
if ($IncludeDisabled -eq $true) {
return @($response.value)
}
return @($response.value | Where-Object { $_.isDisabled -eq $false })
}
Export-ModuleMember -Function Get-AzDevOpsRepos
# End of Function Get-AzDevOpsRepos
<#
.SYNOPSIS
Get Azure DevOps branches for a repo
.DESCRIPTION
Get Azure DevOps branches for a repo using Azure DevOps Rest API
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER Repository
Repository name for Azure DevOps
.EXAMPLE
Get-AzDevOpsBranches -Project $Project -Repository $Repository
#>
Function Get-AzDevOpsBranches {
[CmdletBinding()]
[OutputType([System.Object[]])]
param (
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[string]
$Repository
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$header = $script:connection.GetHeader()
Write-Verbose "Getting branches for repo $Repository in project $Project"
$uri = "https://dev.azure.com/$Organization/_apis/git/repositories/$Repository/refs?filter=heads&includeStatuses=true&latestStatusesOnly=true&api-version=7.2-preview.2"
$statsUri = "https://dev.azure.com/$Organization/$Project/_apis/git/repositories/$Repository/stats/branches?api-version=7.2-preview.2"
Write-Verbose "URI: $uri"
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
}
catch {
throw $_.Exception.Message
}
try {
$statsResponse = Invoke-RestMethod -Uri $statsUri -Method Get -Headers $header
}
catch {
$statsResponse = $null
}
$result = @($response.value | ForEach-Object {
$branch = $_
If($null -eq $statsResponse) {
$branchStats = $null
} else {
$branchStats = $statsResponse.value | Where-Object {$_.name -eq ($branch.name -replace "refs/heads/","")}
}
$branch | Add-Member -MemberType NoteProperty -Name Stats -Value $branchStats
$branch
})
return @($result)
}
Export-ModuleMember -Function Get-AzDevOpsBranches
<#
.SYNOPSIS
Get Azure DevOps branch policy for a branch in a repo
.DESCRIPTION
Get Azure DevOps branch policy for a branch in a repo using Azure DevOps Rest API
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER Repository
Repository name for Azure DevOps
.PARAMETER Branch
Branch name for Azure DevOps as a git ref. Example: refs/heads/main
.EXAMPLE
Get-AzDevOpsBranchPolicy -Project $Project -Repository $Repository -Branch $Branch
.NOTES
This function returns an empty object if no branch policy is found for the branch
#>
Function Get-AzDevOpsBranchPolicy {
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[string]
$Repository,
[Parameter(Mandatory)]
[string]
$Branch
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$header = $script:connection.GetHeader()
Write-Verbose "Getting branch policy for branch $Branch in repo $Repository in project $Project"
$uri = "https://dev.azure.com/$Organization/$Project/_apis/policy/configurations?api-version=6.0"
Write-Verbose "URI: $uri"
# Try to get the branch policy, return an empty object if no branch policy is found for the branch
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
}
catch {
throw $_.Exception.Message
}
$branchPolicy = @($response.value | Where-Object {$_.settings.scope.refName -eq $Branch -and $_.settings.scope.repositoryId -eq $Repository})
return $branchPolicy
}
Export-ModuleMember -Function Get-AzDevOpsBranchPolicy
# End of Function Get-AzDevOpsBranchPolicy
<#
.SYNOPSIS
Get Repository pipeline permissions for a repo
.DESCRIPTION
Get Repository pipeline permissions for a repo using Azure DevOps Rest API
.PARAMETER ProjectId
Project ID for Azure DevOps project
.PARAMETER RepositoryId
Repository ID for Azure DevOps
.EXAMPLE
Get-AzDevOpsRepositoryPipelinePermissions -ProjectId $ProjectId -RepositoryId $RepositoryId
#>
Function Get-AzDevOpsRepositoryPipelinePermissions {
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory)]
[string]
$ProjectId,
[Parameter(Mandatory)]
[string]
$RepositoryId
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$header = $script:connection.GetHeader()
$uri = "https://dev.azure.com/{0}/{1}/_apis/pipelines/pipelinePermissions/repository/{2}.{3}" -f $Organization, $ProjectId, $ProjectId, $RepositoryId
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header -ContentType "application/json"
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
}
catch {
throw $_.Exception.Message
}
return $response
}
Export-ModuleMember -Function Get-AzDevOpsRepositoryPipelinePermissions
# End of Function Get-AzDevOpsRepositoryPipelinePermissions
<#
.SYNOPSIS
Get Azure DevOps repos ACLs
.DESCRIPTION
Get Azure DevOps repos ACLs using Azure DevOps Rest API
.PARAMETER ProjectId
Project ID for Azure DevOps project
.PARAMETER RepositoryId
Repository ID for Azure DevOps
.EXAMPLE
Get-AzDevOpsRepositoryAcls -ProjectId $ProjectId -RepositoryId $RepositoryId
#>
Function Get-AzDevOpsRepositoryAcls {
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory)]
[string]
$ProjectId,
[Parameter(Mandatory)]
[string]
$RepositoryId
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$TokenType = $script:connection.TokenType
# If the token type is ReadOnly, write a warning and return null
if ($TokenType -eq "ReadOnly") {
Write-Warning "The ReadOnly token type does not have access to the Repositories ACLs API, returning null"
return $null
} else {
$header = $script:connection.GetHeader()
$uri = "https://dev.azure.com/{0}/_apis/accesscontrollists/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87?api-version=7.2-preview.1&token=repoV2/{1}/{2}" -f $Organization, $ProjectId, $RepositoryId
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header -ContentType "application/json"
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
$thisRepoPerms = $response.value
}
catch {
throw $_.Exception.Message
}
return $thisRepoPerms
}
}
Export-ModuleMember -Function Get-AzDevOpsRepositoryAcls
# End of Function Get-AzDevOpsRepositoryAcls
<#
.SYNOPSIS
Check the existance of a file in an Azure DevOps repo
.DESCRIPTION
Check the existance of a file in an Azure DevOps repo using Azure DevOps Rest API
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER Repository
Repository name for Azure DevOps
.PARAMETER Path
Path to file in repository
.EXAMPLE
Test-AzDevOpsFileExists -Project $Project -Repository $Repository -Path $Path
.NOTES
This function return $true if the file exists and $false if it does not
#>
function Test-AzDevOpsFileExists {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[string]
$Repository,
[Parameter(Mandatory)]
[string]
$Path
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$header = $script:connection.GetHeader()
Write-Verbose "Checking if file $Path exists in repo $Repository in project $Project"
$uri = "https://dev.azure.com/$Organization/$Project/_apis/git/repositories/$Repository/items?path=$Path&api-version=6.0"
Write-Verbose "URI: $uri"
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
}
catch {
return $false
}
return $true
}
Export-ModuleMember -Function Test-AzDevOpsFileExists
# End of Function Test-AzDevOpsFileExists
<#
.SYNOPSIS
Get GitHub Advanced Security (GHAS) data for a repository
.DESCRIPTION
Get GitHub Advanced Security (GHAS) data for a repository using Azure DevOps Rest API
.PARAMETER ProjectId
Project ID for Azure DevOps
.PARAMETER Repository
Repository name for Azure DevOps
.EXAMPLE
Get-AzDevOpsRepositoryGhas -Project $Project -Repository $Repository
#>
Function Get-AzDevOpsRepositoryGhas {
[CmdletBinding()]
[OutputType([object])]
param (
[Parameter(Mandatory)]
[string]
$ProjectId,
[Parameter(Mandatory)]
[string]
$RepositoryId
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$TokenType = $script:connection.TokenType
$Organization = $script:connection.Organization
# token is not FullAccess, write a warning and return null
if ($TokenType -ne "FullAccess") {
Write-Warning "The $TokenType token type does not have access to the Repositories API, returning null"
return $null
} else {
$header = $script:connection.GetHeader()
$payload = @{
contributionIds = @(
"ms.vss-features.my-organizations-data-provider"
"ms.vss-advsec.advanced-security-enablement-data-provider"
)
dataProviderContext = @{
properties = @{
givenProjectId = $ProjectId
givenRepoId = $RepositoryId
}
}
}
$url = "https://dev.azure.com/$Organization/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1"
try {
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $header -Body ($payload | ConvertTo-Json) -ContentType "application/json"
# If the response is a string and not an object, throw an exception for authentication failure or project not found
if ($response -is [string]) {
throw "Authentication failed or project not found"
}
}
catch {
throw $_.Exception.Message
}
return $response.dataProviders.'ms.vss-advsec.advanced-security-enablement-data-provider'
}
}
Export-ModuleMember -Function Get-AzDevOpsRepositoryGhas
# End of Function Get-AzDevOpsRepositoryGhas
<#
.SYNOPSIS
Get and export all Azure DevOps repos in a project with default, main and master branches and branch policies and export to JSON with 1 file per repo
.DESCRIPTION
Get and export all Azure DevOps repos in a project with default, main and master branches and branch policies and export to JSON using Azure DevOps Rest API and this modules functions
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER OutputPath
Output path for JSON file
.PARAMETER PassThru
Return the exported repos as objects to the pipeline instead of writing to a file
.EXAMPLE
Export-AzDevOpsReposAndBranchPolicies -Project $Project -OutputPath $OutputPath
.NOTES
This function returns an empty object if no branch policy is found for the branch
#>
function Export-AzDevOpsReposAndBranchPolicies {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
}
$Organization = $script:connection.Organization
$TokenType = $script:connection.TokenType
$repos = Get-AzDevOpsRepos -Project $Project
$repos | ForEach-Object {
if ($null -ne $_) {
$repo = $_
# Add ObjectType Azure.DevOps.Repo to repo object
$repo | Add-Member -MemberType NoteProperty -Name ObjectType -Value "Azure.DevOps.Repo"
# Add ObjectName to repo object
$repo | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.{2}" -f $Organization,$Project,$repo.name)
Write-Verbose "Getting branch policy for repo $($repo.name)"
If($repo.defaultBranch) {
$branchPolicy = Get-AzDevOpsBranchPolicy -Project $Project -Repository $repo.id -Branch $repo.defaultBranch
}
$repo | Add-Member -MemberType NoteProperty -Name MainBranchPolicy -Value $branchPolicy
# Add a property indicating if a file named README.md or README exists in the repo
$readmeExists = ((Test-AzDevOpsFileExists -Project $Project -Repository $repo.id -Path "README.md") -or (Test-AzDevOpsFileExists -Project $Project -Repository $repo.id -Path "README"))
$repo | Add-Member -MemberType NoteProperty -Name ReadmeExists -Value $readmeExists
# Get all branches for the repo
$branches = @()
$branches += Get-AzDevOpsBranches -Project $Project -Repository $repo.id
# add branch policies for each branch to the branches object
$branches = @($branches | ForEach-Object {
$branch = $_
$branchPolicy = @(Get-AzDevOpsBranchPolicy -Project $Project -Repository $repo.id -Branch $branch.name)
$branch | Add-Member -MemberType NoteProperty -Name BranchPolicy -Value $branchPolicy
$branch
})
# Add an ObjectType Azure.DevOps.Repo.Branch to each branch object
$branches = @($branches | ForEach-Object {
$branch = $_
$branch | Add-Member -MemberType NoteProperty -Name ObjectType -Value "Azure.DevOps.Repo.Branch"
# Add ObjectName to branch object
$branch | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.{2}.{3}" -f $Organization,$Project,$repo.name,$branch.name)
$id = @{
originalId = $branch.objectId;
resourceName = "$($repo.name)/$($branch.name)"
project = $Project;
organization = $Organization
} | ConvertTo-Json -Depth 100
$branch | Add-Member -MemberType NoteProperty -Name id -Value $id
$branch
})
# Add a property indicating if a file named LICENSE or LICENSE.md exists in the repo
$licenseExists = ((Test-AzDevOpsFileExists -Project $Project -Repository $repo.id -Path "LICENSE") -or (Test-AzDevOpsFileExists -Project $Project -Repository $repo.id -Path "LICENSE.md"))
$repo | Add-Member -MemberType NoteProperty -Name LicenseExists -Value $licenseExists
# Add a property for GitHub Advanced Security (GHAS) data if the token type is FullAccess
if ($TokenType -eq "FullAccess") {
$ghas = Get-AzDevOpsRepositoryGhas -ProjectId $repo.project.id -RepositoryId $repo.id
$repo | Add-Member -MemberType NoteProperty -Name Ghas -Value $ghas
} else {
Write-Warning "The $TokenType token type does not have access to the GHAS API, returning null"
}
# Add a property with pipeline permissions
$pipelinePermissions = Get-AzDevOpsRepositoryPipelinePermissions -ProjectId $repo.project.id -RepositoryId $repo.id
$repo | Add-Member -MemberType NoteProperty -Name PipelinePermissions -Value $pipelinePermissions
$repoId = $repo.id
# Set the id property to a hash table with the original id, organization and project name
$repo.id = @{
originalId = $repo.id;
resourceName = $repo.name;
project = $Project;
organization = $Organization
} | ConvertTo-Json -Depth 100
# Add a property with repo ACLs if the token type is not ReadOnly
if ($TokenType -ne "ReadOnly") {
$repoAcls = Get-AzDevOpsRepositoryAcls -ProjectId $repo.project.id -RepositoryId $repoId
$repo | Add-Member -MemberType NoteProperty -Name Acls -Value $repoAcls
}
$branches += $repo
# If the PassThru switch is set, return the repo object
if ($PassThru) {
Write-Output $branches
} else {
# Export repo object to JSON file
Write-Verbose "Exporting repo $($repo.name) and its branches to JSON as file $($repo.name).ado.repo.json"
$branches | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($repo.name).ado.repo.json"
}
}
}
}
Export-ModuleMember -Function Export-AzDevOpsReposAndBranchPolicies
# End of Function Export-AzDevOpsReposAndBranchPolicies