-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNew-AzDoRepo.ps1
More file actions
106 lines (96 loc) · 2.96 KB
/
New-AzDoRepo.ps1
File metadata and controls
106 lines (96 loc) · 2.96 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
function New-AzDoRepo {
<#
.SYNOPSIS
Creates a repo in Azure DevOps.
.DESCRIPTION
Creates a repo in Azure DevOps.
.EXAMPLE
$params = @{
CollectionUri = "https://dev.azure.com/contoso"
Name = "Repo 1"
ProjectName = "Project 1"
}
New-AzDoRepo @params
This example creates a new Azure DevOps repo with splatting parameters
.EXAMPLE
'test', 'test2' | New-AzDoRepo -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1"
This example creates a new Azure DevOps repo for each in pipeline
.OUTPUTS
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $res.name
RepoId = $res.id
RepoURL = $res.url
WebUrl = $res.webUrl
HttpsUrl = $res.remoteUrl
SshUrl = $res.sshUrl
}
.NOTES
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param (
# Collection Uri of the organization
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$CollectionUri,
# Name of the new repository
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string[]]
$RepoName,
# Name of the project where the new repository has to be created
[Parameter(Mandatory)]
[string]
$ProjectName
)
begin {
Write-Verbose "Starting function: New-AzDoRepo"
}
process {
$ProjectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).Projectid
$params = @{
uri = "$CollectionUri/$ProjectName/_apis/git/repositories"
version = "7.1-preview.1"
Method = 'POST'
}
foreach ($name in $RepoName) {
$body = @{
name = $name
project = @{
id = $ProjectId
}
}
if ($PSCmdlet.ShouldProcess($CollectionUri, "Create repo named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
try {
$result += ($body | Invoke-AzDoRestMethod @params)
} catch {
if ($_ -match 'TF400948') {
Write-Warning "Repo $name already exists, trying to get it"
$params.Method = 'GET'
$result += (Invoke-AzDoRestMethod @params).value | Where-Object { $_.name -eq $name }
} else {
Write-AzDoError -message $_
}
}
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}
}
}
end {
if ($result) {
$result | ForEach-Object {
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $_.name
RepoId = $_.id
RepoURL = $_.url
WebUrl = $_.webUrl
RemoteUrl = $_.remoteUrl
SshUrl = $_.sshUrl
}
}
}
}
}