-
Notifications
You must be signed in to change notification settings - Fork 187
/
GitHubProjects.ps1
517 lines (387 loc) · 17.4 KB
/
GitHubProjects.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
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Get-GitHubProject
{
<#
.DESCRIPTION
Get the projects for a given Github user, repository or organization.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER OrganizationName
The name of the organization to get projects for.
.PARAMETER UserName
The name of the user to get projects for.
.PARAMETER Project
ID of the project to retrieve.
.PARAMETER State
Only projects with this state are returned.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no command line status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Get-GitHubProject -OwnerName Microsoft -RepositoryName PowerShellForGitHub
Get the projects for the Microsoft\PowerShellForGitHub repository.
.EXAMPLE
Get-GitHubProject -OrganizationName Microsoft
Get the projects for the Microsoft organization.
.EXAMPLE
Get-GitHubProject -Uri https://github.com/Microsoft/PowerShellForGitHub
Get the projects for the Microsoft\PowerShellForGitHub repository using the Uri.
.EXAMPLE
Get-GitHubProject -UserName GitHubUser
Get the projects for the user GitHubUser.
.EXAMPLE
Get-GitHubProject -OwnerName Microsoft -RepositoryName PowerShellForGitHub -State Closed
Get closed projects from the Microsoft\PowerShellForGitHub repo.
.EXAMPLE
Get-GitHubProject -Project 4378613
Get a project by id, with this parameter you don't need any other information.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName = 'Elements')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(Mandatory, ParameterSetName = 'Elements')]
[string] $OwnerName,
[Parameter(Mandatory, ParameterSetName = 'Elements')]
[string] $RepositoryName,
[Parameter(Mandatory, ParameterSetName = 'Uri')]
[string] $Uri,
[Parameter(Mandatory, ParameterSetName = 'Organization')]
[string] $OrganizationName,
[Parameter(Mandatory, ParameterSetName = 'User')]
[string] $UserName,
[Parameter(Mandatory, ParameterSetName = 'Project')]
[int64] $Project,
[ValidateSet('Open', 'Closed', 'All')]
[string] $State,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = [String]::Empty
$description = [String]::Empty
if ($PSCmdlet.ParameterSetName -eq 'Project')
{
$telemetryProperties['Project'] = Get-PiiSafeString -PlainText $Project
$uriFragment = "/projects/$Project"
$description = "Getting project $project"
}
elseif ($PSCmdlet.ParameterSetName -in ('Elements', 'Uri'))
{
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "/repos/$OwnerName/$RepositoryName/projects"
$description = "Getting projects for $RepositoryName"
}
elseif ($PSCmdlet.ParameterSetName -eq 'Organization')
{
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
$uriFragment = "/orgs/$OrganizationName/projects"
$description = "Getting projects for $OrganizationName"
}
elseif ($PSCmdlet.ParameterSetName -eq 'User')
{
$telemetryProperties['UserName'] = Get-PiiSafeString -PlainText $UserName
$uriFragment = "/users/$UserName/projects"
$description = "Getting projects for $UserName"
}
if ($PSBoundParameters.ContainsKey('State'))
{
$getParams = @()
$State = $State.ToLower()
$getParams += "state=$State"
$uriFragment = "$uriFragment`?" + ($getParams -join '&')
$description += " with state '$state'"
}
$params = @{
'UriFragment' = $uriFragment
'Description' = $description
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
'AcceptHeader' = 'application/vnd.github.inertia-preview+json'
}
return Invoke-GHRestMethodMultipleResult @params
}
function New-GitHubProject
{
<#
.DESCRIPTION
Creates a new Github project for the given repository
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER OrganizationName
The name of the organization to create the project under.
.PARAMETER UserProject
If this switch is specified creates a project for your user.
.PARAMETER Name
The name of the project to create.
.PARAMETER Description
Short description for the new project.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no command line status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
New-GitHubProject -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Name TestProject
Creates a project called 'TestProject' for the Microsoft\PowerShellForGitHub repository.
.EXAMPLE
New-GitHubProject -OrganizationName Microsoft -Name TestProject -Description 'This is just a test project'
Create a project for the Microsoft organization called 'TestProject' with a description.
.EXAMPLE
New-GitHubProject -Uri https://github.com/Microsoft/PowerShellForGitHub -Name TestProject
Create a project for the Microsoft\PowerShellForGitHub repository using the Uri called 'TestProject'.
.EXAMPLE
New-GitHubProject -UserProject -Name 'TestProject'
Creates a project for the signed in user called 'TestProject'.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName = 'Elements')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(Mandatory, ParameterSetName = 'Elements')]
[string] $OwnerName,
[Parameter(Mandatory, ParameterSetName = 'Elements')]
[string] $RepositoryName,
[Parameter(Mandatory, ParameterSetName = 'Uri')]
[string] $Uri,
[Parameter(Mandatory, ParameterSetName = 'Organization')]
[string] $OrganizationName,
[Parameter(Mandatory, ParameterSetName = 'User')]
[switch] $UserProject,
[Parameter(Mandatory)]
[string] $Name,
[string] $Description,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$telemetryProperties['Name'] = Get-PiiSafeString -PlainText $Name
$uriFragment = [String]::Empty
$apiDescription = [String]::Empty
if ($PSCmdlet.ParameterSetName -in ('Elements', 'Uri'))
{
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "/repos/$OwnerName/$RepositoryName/projects"
$apiDescription = "Creating project for $RepositoryName"
}
elseif ($PSCmdlet.ParameterSetName -eq 'Organization')
{
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
$uriFragment = "/orgs/$OrganizationName/projects"
$apiDescription = "Creating project for $OrganizationName"
}
elseif ($PSCmdlet.ParameterSetName -eq 'User')
{
$telemetryProperties['User'] = $true
$uriFragment = "/user/projects"
$apiDescription = "Creating project for user"
}
$hashBody = @{
'name' = $Name
}
if ($PSBoundParameters.ContainsKey('Description'))
{
$hashBody.add('body', $Description)
}
$params = @{
'UriFragment' = $uriFragment
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = $apiDescription
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
'AcceptHeader' = 'application/vnd.github.inertia-preview+json'
}
return Invoke-GHRestMethod @params
}
function Set-GitHubProject
{
<#
.DESCRIPTION
Modify a GitHub Project.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Project
ID of the project to modify.
.PARAMETER Description
Short description for the project.
.PARAMETER State
Set the state of the project.
.PARAMETER OrganizationPermission
Set the permission level that determines whether all members of the project's
organization can see and/or make changes to the project.
Only available for organization projects.
.PARAMETER Private
Sets the visibility of a project board.
Only available for organization and user projects.
Note: Updating a project's visibility requires admin access to the project.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no command line status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Set-GitHubProject -Project 999999 -State Closed
Set the project with ID '999999' to closed.
.EXAMPLE
$project = Get-GitHubProject -OwnerName Microsoft -RepositoryName PowerShellForGitHub | Where-Object Name -eq 'TestProject'
Set-GitHubProject -Project $project.id -State Closed
Get the ID for the 'TestProject' project for the Microsoft\PowerShellForGitHub
repository and set state to closed.
#>
[CmdletBinding(
SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(Mandatory)]
[int64] $Project,
[string] $Description,
[ValidateSet('Open', 'Closed')]
[string] $State,
[ValidateSet('Read', 'Write', 'Admin', 'None')]
[string] $OrganizationPermission,
[switch] $Private,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = "projects/$Project"
$apiDescription = "Updating project $Project"
$hashBody = @{}
if ($PSBoundParameters.ContainsKey('Description'))
{
$hashBody.add('body', $Description)
$apiDescription += " description"
}
if ($PSBoundParameters.ContainsKey('State'))
{
$hashBody.add('state', $State)
$apiDescription += ", state to '$State'"
}
if ($PSBoundParameters.ContainsKey('Private'))
{
$hashBody.add('private', $Private.ToBool())
$apiDescription += ", private to '$Private'"
}
if ($PSBoundParameters.ContainsKey('OrganizationPermission'))
{
$hashBody.add('organization_permission', $OrganizationPermission.ToLower())
$apiDescription += ", organization_permission to '$OrganizationPermission'"
}
$params = @{
'UriFragment' = $uriFragment
'Description' = $apiDescription
'Body' = (ConvertTo-Json -InputObject $hashBody)
'AccessToken' = $AccessToken
'Method' = 'Patch'
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
'AcceptHeader' = 'application/vnd.github.inertia-preview+json'
}
return Invoke-GHRestMethod @params
}
function Remove-GitHubProject
{
<#
.DESCRIPTION
Removes the projects for a given Github repository.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Project
ID of the project to remove.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no command line status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Remove-GitHubProject -Project 4387531
Remove project with ID '4387531'.
.EXAMPLE
Remove-GitHubProject -Project 4387531 -Confirm:$false
Remove project with ID '4387531' without prompting for confirmation.
.EXAMPLE
$project = Get-GitHubProject -OwnerName Microsoft -RepositoryName PowerShellForGitHub | Where-Object Name -eq 'TestProject'
Remove-GitHubProject -Project $project.id
Get the ID for the 'TestProject' project for the Microsoft\PowerShellForGitHub
repository and then remove the project.
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'High')]
[Alias('Delete-GitHubProject')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(Mandatory)]
[int64] $Project,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = "projects/$Project"
$description = "Deleting project $Project"
if ($PSCmdlet.ShouldProcess($project, "Remove project"))
{
$params = @{
'UriFragment' = $uriFragment
'Description' = $description
'AccessToken' = $AccessToken
'Method' = 'Delete'
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
'AcceptHeader' = 'application/vnd.github.inertia-preview+json'
}
return Invoke-GHRestMethod @params
}
}