-
Notifications
You must be signed in to change notification settings - Fork 188
/
GitHubProjects.ps1
717 lines (562 loc) · 22.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
@{
GitHubProjectTypeName = 'GitHub.Project'
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
filter 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Release
GitHub.Repository
.OUTPUTS
GitHub.Project
.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')]
[OutputType({$script:GitHubPullRequestTypeName})]
[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,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='ProjectObject')]
[Alias('RepositoryUrl')]
[string] $Uri,
[Parameter(
Mandatory,
ParameterSetName = 'Organization')]
[string] $OrganizationName,
[Parameter(
Mandatory,
ParameterSetName = 'User')]
[string] $UserName,
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Project')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='ProjectObject')]
[Alias('ProjectId')]
[int64] $Project,
[ValidateSet('Open', 'Closed', 'All')]
[string] $State,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = [String]::Empty
$description = [String]::Empty
if ($PSCmdlet.ParameterSetName -in @('Project', 'ProjectObject'))
{
$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' = $script:inertiaAcceptHeader
}
return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubProjectAdditionalProperties)
}
filter 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Release
GitHub.Repository
.OUTPUTS
GitHub.Project
.EXAMPLE
New-GitHubProject -OwnerName microsoft -RepositoryName PowerShellForGitHub -ProjectName TestProject
Creates a project called 'TestProject' for the microsoft\PowerShellForGitHub repository.
.EXAMPLE
New-GitHubProject -OrganizationName Microsoft -ProjectName 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 -ProjectName TestProject
Create a project for the microsoft\PowerShellForGitHub repository
using the Uri called 'TestProject'.
.EXAMPLE
New-GitHubProject -UserProject -ProjectName 'TestProject'
Creates a project for the signed in user called 'TestProject'.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubPullRequestTypeName})]
[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,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[Parameter(
Mandatory,
ParameterSetName = 'Organization')]
[string] $OrganizationName,
[Parameter(
Mandatory,
ParameterSetName = 'User')]
[switch] $UserProject,
[Parameter(
Mandatory,
ValueFromPipeline)]
[Alias('Name')]
[string] $ProjectName,
[string] $Description,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$telemetryProperties['ProjectName'] = Get-PiiSafeString -PlainText $ProjectName
$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' = $ProjectName
}
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' = $script:inertiaAcceptHeader
}
return (Invoke-GHRestMethod @params | Add-GitHubProjectAdditionalProperties)
}
filter 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Release
GitHub.Repository
.OUTPUTS
GitHub.Project
.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)]
[OutputType({$script:GitHubPullRequestTypeName})]
[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,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[Alias('ProjectId')]
[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' = $script:inertiaAcceptHeader
}
return (Invoke-GHRestMethod @params | Add-GitHubProjectAdditionalProperties)
}
filter 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 Force
If this switch is specified, you will not be prompted for confirmation of command execution.
.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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Release
GitHub.Repository
.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
Remove-GitHubProject -Project 4387531 -Force
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,
ValueFromPipelineByPropertyName)]
[Alias('ProjectId')]
[int64] $Project,
[switch] $Force,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = "projects/$Project"
$description = "Deleting project $Project"
if ($Force -and (-not $Confirm))
{
$ConfirmPreference = 'None'
}
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' = $script:inertiaAcceptHeader
}
return Invoke-GHRestMethod @params
}
}
filter Add-GitHubProjectAdditionalProperties
{
<#
.SYNOPSIS
Adds type name and additional properties to ease pipelining to GitHub Project objects.
.PARAMETER InputObject
The GitHub object to add additional properties to.
.PARAMETER TypeName
The type that should be assigned to the object.
.INPUTS
[PSCustomObject]
.OUTPUTS
GitHub.Project
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")]
param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[AllowNull()]
[AllowEmptyCollection()]
[PSCustomObject[]] $InputObject,
[ValidateNotNullOrEmpty()]
[string] $TypeName = $script:GitHubProjectTypeName
)
foreach ($item in $InputObject)
{
$item.PSObject.TypeNames.Insert(0, $TypeName)
if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport))
{
$elements = Split-GitHubUri -Uri $item.html_url
$repositoryUrl = Join-GitHubUri @elements
# A "user" project has no associated repository, and adding this in that scenario
# would cause API-level errors with piping further on,
if ($elements.OwnerName -ne 'users')
{
Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force
}
Add-Member -InputObject $item -Name 'ProjectId' -Value $item.id -MemberType NoteProperty -Force
if ($null -ne $item.creator)
{
$null = Add-GitHubUserAdditionalProperties -InputObject $item.creator
}
}
Write-Output $item
}
}