-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNew-AzDoProject.ps1
More file actions
182 lines (163 loc) · 6.03 KB
/
New-AzDoProject.ps1
File metadata and controls
182 lines (163 loc) · 6.03 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
function New-AzDoProject {
<#
.SYNOPSIS
Function to create an Azure DevOps project
.DESCRIPTION
Function to create an Azure DevOps project
.EXAMPLE
New-AzDoProject -CollectionUri "https://dev.azure.com/contoso" -PAT "***" -ProjectName "Project 1"
This example creates a new private Azure DevOps project
.EXAMPLE
New-AzDoProject -CollectionUri "https://dev.azure.com/contoso" -PAT "***" -ProjectName "Project 1" -Visibility 'public'
This example creates a new public Azure DevOps project
.EXAMPLE
@("MyProject1","Myproject2") | New-AzDoProject -CollectionUri "https://dev.azure.com/contoso" -PAT "***"
This example creates two new Azure DevOps projects using the pipeline.
.EXAMPLE
[pscustomobject]@{
ProjectName = 'Project 1'
Visibility = 'public'
Description = 'This is the best project'
},
[pscustomobject]@{
ProjectName = 'Project 1'
Description = 'This is the best project'
} | New-AzDoProject -PAT $PAT -CollectionUri $CollectionUri
This example creates two new Azure DevOps projects using the pipeline.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param (
# Collection URI. e.g. https://dev.azure.com/contoso.
# Azure Pipelines has a predefined variable for this.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
[string]
$CollectionUri,
# Name of the project.
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string[]]
[ValidateScript(
{
# Length
if (($_).Length -gt 63) {
throw "The project name cannot be longer than 63 characters"
}
# - Must not be a system reserved name.
$forbiddenNames = @(
'COM1',
'COM2',
'COM3',
'COM4',
'COM5',
'COM6',
'COM7',
'COM8',
'COM9',
'COM10',
'CON',
'DefaultCollection',
'LPT1',
'LPT2',
'LPT3',
'LPT4',
'LPT5',
'LPT6',
'LPT7',
'LPT8',
'LPT9',
'NUL',
'PRN',
'SERVER',
'SignalR',
'Web',
'WEB')
if ($forbiddenNames -contains $_) {
throw "The project name cannot be one of the following values: $forbiddenNames"
}
# - Must not be one of the hidden segments used for IIS request filtering like App_Browsers, App_code, App_Data, App_GlobalResources, App_LocalResources, App_Themes, App_WebResources, bin, or web.config.
if ($_ -match "App_Browsers|App_code|App_Data|App_GlobalResources|App_LocalResources|App_Themes|App_WebResources|bin|web.config") {
throw "The project name cannot be one of the following values: App_Browsers, App_code, App_Data, App_GlobalResources, App_LocalResources, App_Themes, App_WebResources, bin, or web.config"
}
# - Must not contain any Unicode control characters or surrogate characters.
if ($_ -match "[\p{C}\p{Cs}]") {
throw "The project name cannot contain any Unicode control characters or surrogate characters"
}
# - Must not contain the following printable characters: \ / : * ? " < > | ; # $ * { } , + = [ ].
if ($_ -match "[\\/:*?`"<>|;#$*{},+=\[\]]") {
throw "The project name cannot contain the following printable characters: \ / : * ? `"< > | ; # $ * { } , + = [ ]"
}
# - Must not start with an underscore _.
if ($_ -match "^_") {
throw "The project name cannot start with an underscore _"
}
# - Must not start or end with a period ..
if ($_ -match "^\." -or $_ -match "\.$") {
throw "The project name cannot start or end with a period ."
}
$true
}
)]
$ProjectName,
# Description of the project
[Parameter()]
[string]
$Description = '',
# Type of source control.
[Parameter()]
[string]
$SourceControlType = 'GIT',
# Visibility of the project (private or public).
[Parameter()]
[ValidateSet('private', 'public')]
[string]
$Visibility = 'private'
)
process {
Write-Verbose "Starting function: New-AzDoProject"
$params = @{
uri = "$CollectionUri/_apis/projects"
version = "7.2-preview.4"
method = 'POST'
}
foreach ($name in $ProjectName) {
$body = @{
name = $name
description = $Description
visibility = $Visibility
capabilities = @{
versioncontrol = @{
sourceControlType = $SourceControlType
}
processTemplate = @{
templateTypeId = '6b724908-ef14-45cf-84f8-768b5384da45'
}
}
}
if ($PSCmdlet.ShouldProcess($CollectionUri, "Create project named: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
try {
$body | Invoke-AzDoRestMethod @params | Out-Null
} catch {
if ($_ -match 'TF200019') {
Write-Warning "Project $name already exists, trying to get it"
} else {
$PSCmdlet.ThrowTerminatingError((Write-AzDoError -message "Failed to create project: $name Error: $_"))
}
}
do {
Start-Sleep 5
Write-Verbose "Fetching creation state of $name"
$getAzDoProjectSplat = @{
CollectionUri = $CollectionUri
ProjectName = $name
}
$response = Get-AzDoProject @getAzDoProjectSplat
} while (
$response.State -ne 'wellFormed'
)
$response
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}
}
}
}