forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ADORepository.ps1
371 lines (317 loc) · 14.5 KB
/
Get-ADORepository.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
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
function Get-ADORepository
{
<#
.Synopsis
Gets repositories from Azure DevOps
.Description
Gets the repositories from Azure DevOps.
By default, this will return the project's git repositories.
Azure DevOps repositories can have more than one type of SourceProvider.
To list the Source Providers, use -SourceProvider
We can get repositories for a given -ProviderName
.Example
Get-ADORepository -Organization StartAutomating -Project PSDevOps
.Link
Remove-ADORepository
.Link
https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/list%20repositories
#>
[CmdletBinding(DefaultParameterSetName='git/repositories')]
[OutputType('PSDevOps.Repository',
'PSDevOps.Repository.SourceProvider',
'PSDevOps.Repository.File',
'PSDevOps.Repoistory.Recycled')]
param(
# The Organization
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('Org')]
[string]
$Organization,
# The Project
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]
$Project,
# The Repository ID
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)]
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[string]
$RepositoryID,
# If set, will list repository source providers
[Parameter(Mandatory,ParameterSetName='sourceproviders',ValueFromPipelineByPropertyName)]
[Alias('SourceProviders')]
[switch]
$SourceProvider,
# The name of the Source Provider. This will get all repositories associated with the project.
# If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required
[Parameter(Mandatory,ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)]
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
[Alias('EndpointType')]
[string]
$ProviderName,
# The name of the Source Provider. This will get all repositories associated with the project.
# If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required
[Parameter(ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)]
[Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
[string]
$EndpointID,
# The name of the repository
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
[string]
$RepositoryName,
# The path within the repository.
# To use this parameter, -ProviderName is also required, and -EndpointID will be required if the -ProviderName is not TFVC or TFGit
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
[string]
$Path,
# The commit or branch. By default, Master.
[Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
[string]
$CommitOrBranch = 'master',
# If set, will get the file list from a repository
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[Alias('Item','Items','Files')]
[switch]
$FileList,
# When getting a -FileList, the recursion level. By default, full.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[ValidateSet('full','None','oneLevel','oneLevelPlusNestedEmptyFolders')]
[string]
$RecursionLevel = 'full',
# When getting a -FileList, the path scope.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[Alias('PathScope')]
[string]
$ScopePath,
# The version string identifier (name of tag/branch, SHA1 of commit)
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[Alias('Version')]
[string]
$VersionDescriptor,
# The version options (e.g. firstParent, previousChange)
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[ValidateSet('none','firstParent','previousChange')]
[string]
$VersionOption,
# The version type (e.g. branch, commit, or tag)
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[ValidateSet('branch','commit','tag')]
[string]
$VersionType,
# If -IncludeContentMetadata is set a -FileList will include content metadata.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[Alias('IncludeContentMetadata')]
[switch]
$IncludeMetadata,
# If set, will include the parent repository
[Parameter(ParameterSetName='git/repositories/{repositoryId}/items',ValueFromPipelineByPropertyName)]
[switch]
$Download,
# If set, will list pull requests related to a git repository.
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[Alias('PR')]
[switch]
$PullRequest,
# Filters pull requests, returning requests created by the -CreatorIdentity.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[Alias('CreatorID')]
[string]
$CreatorIdentity,
# Filters pull requests where the -ReviewerIdentity is a reviewer.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[Alias('ReviewerID')]
[string]
$ReviewerIdentity,
# Filters pull requests where the source branch is the -SourceReference.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[Alias('SourceRef','SourceRefName')]
[string]
$SourceReference,
# Filters pull requests where the target branch is the -TargetReference.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[Alias('TargetRef','TargetRefName')]
[string]
$TargetReference,
# Filters pull requests with a paricular status. If not specified, will default to Active.
[Parameter(ParameterSetName='git/repositories/{repositoryId}/pullrequests',ValueFromPipelineByPropertyName)]
[ValidateSet('abandoned','active', 'all','completed', 'notset')]
[Alias('PRStatus')]
[string]
$PullRequestStatus,
# If set, will include the parent repository
[Parameter(ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)]
[switch]
$IncludeParent,
# If set, will get repositories from the recycle bin
[Parameter(Mandatory,ParameterSetName='git/recycleBin/repositories',ValueFromPipelineByPropertyName)]
[Alias('RecycleBin')]
[switch]
$Recycled,
# If set, will include hidden repositories.
[Parameter(ParameterSetName='git/repositories')]
[Alias('IncludeHiddenRepository','IncludeHiddenRepositories')]
[switch]
$IncludeHidden,
# If set, will include all related links to a repository.
[Parameter(ParameterSetName='git/repositories')]
[Alias('IncludeLinks')]
[switch]
$IncludeLink,
# If set, will return all GitHub remote URLs associated with a repository.
[Parameter(ParameterSetName='git/repositories')]
[Alias('IncludeRemoteURLs')]
[switch]
$IncludeRemoteUrl,
# The server. By default https://dev.azure.com/.
# To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs).
[Parameter(ValueFromPipelineByPropertyName)]
[uri]
$Server = "https://dev.azure.com/",
# The api version. By default, 5.1-preview.
# If targeting TFS, this will need to change to match your server version.
# See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops
[string]
$ApiVersion = "5.1-preview"
)
dynamicParam { . $GetInvokeParameters -DynamicParameter }
begin {
#region Copy Invoke-ADORestAPI parameters
$invokeParams = & $getInvokeParameters $PSBoundParameters
#endregion Copy Invoke-ADORestAPI parameters
$q = [Collections.Queue]::new()
}
process {
$q.Enqueue(@{psParameterSet= $psCmdlet.ParameterSetName} + $psBoundParameters)
}
end {
$c, $t, $progId =0, $q.Count, [Random]::new().Next()
while ($q.Count) {
. $DQ $q
if ($t -gt 1) {
$c++
Write-Progress "Getting Repositories" "$Server $Organization/$Project" -PercentComplete ($c * 100 / $t) -Id $progId
}
$uri = # The URI is comprised of:
@(
"$server".TrimEnd('/') # the Server (minus any trailing slashes),
$Organization # the Organization,
$Project # the Project,
'_apis' # the API Root ('_apis'),
(. $ReplaceRouteParameter $PSParameterSet)
# and any parameterized URLs in this parameter set.
) -as [string[]] -ne '' -join '/'
$uri += '?' # The URI has a query string containing:
$uri += @(
if ($IncludeParent) { # includeParent=True (if it was set)
"includeParent=True"
}
if ($EndpointID) {
"serviceEndpointId=$EndpointID"
}
if ($repositoryName) {
"repository=$repositoryName"
"commitOrBranch=$CommitOrBranch"
}
switch ($psParameterSet) {
'git/repositories/{repositoryId}/items' {
if ($IncludeMetadata) {
"includeContentMetadata=true"
}
if ($RecursionLevel) {
"recursionLevel=$recursionLevel"
}
if ($Download) {
"download=true"
} else {
'$format=json'
}
if ($scopePath) {
"scopePath=$scopePath"
}
if ($Latest) {
"latestProcessedChange=true"
}
if ($VersionDescriptor) {
"versionDescriptor.version=$VersionDescriptor"
}
if ($VersionOption) {
"versionDescriptor.option=$VersionOption"
}
if ($VersionType) {
"versionDescriptor.type=$VersionType"
}
}
'git/repositories/{repositoryId}/pullRequests' {
if ($CreatorIdentity) {
"searchCriteria.creatorId=$CreatorIdentity"
}
if ($SourceReference) {
"searchCriteria.sourceRefName=$SourceReference"
}
if ($PullRequestStatus) {
"searchCriteria.status=$PullRequestStatus"
}
if ($TargetReference) {
"searchCriteria.targetRefName=$TargetReference"
}
if ($ReviewerIdentity) {
"searchCriteria.reviewerID=$ReviewerIdentity"
}
}
'git/repositories' {
"includeHidden=true"
"includeLinks=true"
"includeAllUrls=true"
}
}
if ($path) {
"path=$path"
}
if ($Server -ne 'https://dev.azure.com/' -and
-not $PSBoundParameters.ApiVersion) {
$ApiVersion = '2.0'
}
if ($ApiVersion) { # and the apiVersion
"api-version=$ApiVersion"
}
) -join '&'
$InvokeParams.Property =
@{
# Because we want to pipeline properly, also return the -Organization and -Project as properties.
Organization = $Organization
Project = $Project
Server = $Server
}
$subTypeName =
if ($psParameterSet -eq 'git/recycleBin/repositories') {
'.Recycled'
}
elseif ($psParameterSet -eq 'sourceProviders') {
'.SourceProvider'
}
elseif ($psParameterSet -eq 'sourceproviders/{ProviderName}/repositories') {
".$ProviderName.Repository"
$invokeParams.ExpandProperty = 'repositories'
$invokeParams.Property.EndpointID = $EndpointID
$invokeParams.Property.ProviderName = $ProviderName
}
elseif ($psParameterSet -eq 'git/repositories/{repositoryId}/items') {
".File"
}
elseif ($psParameterSet -eq 'git/repositories/{repositoryId}/pullrequests') {
".PullRequest"
}
else {
''
}
# Invoke the ADO Rest API.
Invoke-ADORestAPI @invokeParams -Uri $uri -PSTypeName @(
# Because we want to format the output, decorate the object with the following typenames:
"$Organization.$Project.Repository$subTypeName" # * $Organization.$Project.Repository$SubTypeName
"$Organization.Repository$subTypeName" # * $Organization.Repository$SubTypeName
"PSDevOps.Repository$subTypeName" # * PSDevOps.Repository$SubTypeName
)
}
Write-Progress "Getting Repositories" ' ' -Completed -Id $progId
}
}