-
Notifications
You must be signed in to change notification settings - Fork 187
/
GitHubRepositories.ps1
4114 lines (3279 loc) · 133 KB
/
GitHubRepositories.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
@{
GitHubRepositoryActionsPermissionTypeName = 'GitHub.RepositoryActionsPermission'
GitHubRepositoryTypeName = 'GitHub.Repository'
GitHubRepositoryTopicTypeName = 'GitHub.RepositoryTopic'
GitHubRepositoryContributorTypeName = 'GitHub.RepositoryContributor'
GitHubRepositoryCollaboratorTypeName = 'GitHub.RepositoryCollaborator'
GitHubRepositoryContributorStatisticsTypeName = 'GitHub.RepositoryContributorStatistics'
GitHubRepositoryLanguageTypeName = 'GitHub.RepositoryLanguage'
GitHubRepositoryTagTypeName = 'GitHub.RepositoryTag'
GitHubRepositoryTeamPermissionTypeName = 'GitHub.RepositoryTeamPermission'
SquashMergeCommitTitleConversion = @{
'PRTitle' = 'PR_TITLE'
'CommitOrPRTitle' = 'COMMIT_OR_PR_TITLE'
'MergeMessage' = 'MERGE_MESSAGE'
}
SquashMergeCommitMessageConversion = @{
'PRBody' = 'PR_BODY'
'CommitMessages' = 'COMMIT_MESSAGES'
'Blank' = 'BLANK'
}
MergeCommitTitleConversion = @{
'PRTitle' = 'PR_TITLE'
'MergeMessage' = 'MERGE_MESSAGE'
}
MergeCommitMessageConversion = @{
'PRTitle' = 'PR_TITLE'
'PRBody' = 'PR_BODY'
'Blank' = 'BLANK'
}
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
filter New-GitHubRepository
{
<#
.SYNOPSIS
Creates a new repository on GitHub.
.DESCRIPTION
Creates a new repository on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER RepositoryName
Name of the repository to be created.
.PARAMETER OrganizationName
Name of the organization that the repository should be created under.
If not specified, will be created under the current user's account.
.PARAMETER Description
A short description of the repository.
.PARAMETER Homepage
A URL with more information about the repository.
.PARAMETER GitIgnoreTemplate
Desired language or platform .gitignore template to apply.
For supported values, call Get-GitHubGitIgnore.
Values are case-sensitive.
.PARAMETER LicenseTemplate
Choose an open source license template that best suits your needs.
For supported values, call Get-GitHubLicense
Values are case-sensitive.
.PARAMETER TeamId
The id of the team that will be granted access to this repository.
This is only valid when creating a repository in an organization.
.PARAMETER Private
By default, this repository will be created Public. Specify this to create
a private repository.
.PARAMETER NoIssues
By default, this repository will support Issues. Specify this to disable Issues.
.PARAMETER NoProjects
By default, this repository will support Projects. Specify this to disable Projects.
If you're creating a repository in an organization that has disabled repository projects,
this will be true by default.
.PARAMETER NoWiki
By default, this repository will have a Wiki. Specify this to disable the Wiki.
.PARAMETER HasDiscussions
By default, this repository will not have Discussions. Specify this to enable Discussions.
.PARAMETER AutoInit
Specify this to create an initial commit with an empty README.
.PARAMETER DisallowSquashMerge
By default, squash-merging pull requests will be allowed.
Specify this to disallow.
.PARAMETER DisallowMergeCommit
By default, merging pull requests with a merge commit will be allowed.
Specify this to disallow.
.PARAMETER DisallowRebaseMerge
By default, rebase-merge pull requests will be allowed.
Specify this to disallow.
.PARAMETER SquashMergeCommitTitle
Specifies the default value for a squash merge commit title. This can be one of the
following values:
- 'PRTitle' - default to the pull request's title.
- 'CommitOrPRTitle' - default to the commit's title (if only one commit) or the pull
request's title (when more than one commit).
.PARAMETER SquashMergeCommitMessage
Specifies the default value for a squash merge commit message. This can be one of the
following values:
- 'PRBody' - default to the pull request's body.
- 'CommitMessages' - default to the branch's commit messages.
- Blank - default to a blank commit message.
.PARAMETER MergeCommitTitle
Specifies the default value for a merge commit title. This can be one of the
following values:
- 'PRTitle' - default to the pull request's title.
- 'MergeMessage' - default to the classic title for a merge message (e.g., Merge pull request
#123 from branch-name).
.PARAMETER MergeCommitMessage
Specifies the default vaule for a merge commit message. This can be one of the
following values:
- 'PRTitle' - default to the pull request's title.
- 'PRBody' - default to the pull request's body.
- 'Blank' - default to a blank commit message.
.PARAMETER AllowAutoMerge
Specifies whether to allow auto-merge on pull requests.
.PARAMETER AllowUpdateBranch
Specifies whether to always allow a pull request head branch that is behind its base branch
to be updated even if it is not required to be up-to-date before merging.
.PARAMETER DeleteBranchOnMerge
Specifies the automatic deleting of head branches when pull requests are merged.
.PARAMETER IsTemplate
Specifies whether the repository is made available as a template.
.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.
.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.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.Repository
.EXAMPLE
New-GitHubRepository -RepositoryName MyNewRepo -AutoInit
.EXAMPLE
'MyNewRepo' | New-GitHubRepository -AutoInit
.EXAMPLE
New-GitHubRepository -RepositoryName MyNewRepo -Organization MyOrg -DisallowRebaseMerge
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType({$script:GitHubRepositoryTypeName})]
param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[Alias('Name')]
[string] $RepositoryName,
[Parameter(ValueFromPipelineByPropertyName)]
[string] $OrganizationName,
[string] $Description,
[string] $Homepage,
[string] $GitIgnoreTemplate,
[string] $LicenseTemplate,
[int64] $TeamId,
[switch] $Private,
[switch] $NoIssues,
[switch] $NoProjects,
[switch] $NoWiki,
[switch] $HasDiscussions,
[switch] $AutoInit,
[switch] $DisallowSquashMerge,
[switch] $DisallowMergeCommit,
[switch] $DisallowRebaseMerge,
[ValidateSet('PRTitle', 'CommitOrPRTitle')]
[string] $SquashMergeCommitTitle,
[ValidateSet('PRBody', 'CommitMessages', 'Blank')]
[string] $SquashMergeCommitMessage,
[ValidateSet('PRTitle', 'MergeMessage')]
[string] $MergeCommitTitle,
[ValidateSet('PRTitle', 'PRBody', 'Blank')]
[string] $MergeCommitMessage,
[switch] $AllowAutoMerge,
[switch] $AllowUpdateBranch,
[switch] $DeleteBranchOnMerge,
[switch] $IsTemplate,
[string] $AccessToken
)
Write-InvocationLog
$telemetryProperties = @{
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
}
$uriFragment = 'user/repos'
if ($PSBoundParameters.ContainsKey('OrganizationName') -and
(-not [String]::IsNullOrEmpty($OrganizationName)))
{
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
$uriFragment = "orgs/$OrganizationName/repos"
}
if ($PSBoundParameters.ContainsKey('TeamId') -and (-not $PSBoundParameters.ContainsKey('OrganizationName')))
{
$message = 'TeamId may only be specified when creating a repository under an organization.'
Write-Log -Message $message -Level Error
throw $message
}
$hashBody = @{
'name' = $RepositoryName
}
if ($PSBoundParameters.ContainsKey('Description')) { $hashBody['description'] = $Description }
if ($PSBoundParameters.ContainsKey('Homepage')) { $hashBody['homepage'] = $Homepage }
if ($PSBoundParameters.ContainsKey('GitIgnoreTemplate')) { $hashBody['gitignore_template'] = $GitIgnoreTemplate }
if ($PSBoundParameters.ContainsKey('LicenseTemplate')) { $hashBody['license_template'] = $LicenseTemplate }
if ($PSBoundParameters.ContainsKey('TeamId')) { $hashBody['team_id'] = $TeamId }
if ($PSBoundParameters.ContainsKey('Private')) { $hashBody['private'] = $Private.ToBool() }
if ($PSBoundParameters.ContainsKey('NoIssues')) { $hashBody['has_issues'] = (-not $NoIssues.ToBool()) }
if ($PSBoundParameters.ContainsKey('NoProjects')) { $hashBody['has_projects'] = (-not $NoProjects.ToBool()) }
if ($PSBoundParameters.ContainsKey('NoWiki')) { $hashBody['has_wiki'] = (-not $NoWiki.ToBool()) }
if ($PSBoundParameters.ContainsKey('HasDiscussions')) { $hashBody['has_discussions'] = ( $HasDiscussions.ToBool()) }
if ($PSBoundParameters.ContainsKey('AutoInit')) { $hashBody['auto_init'] = $AutoInit.ToBool() }
if ($PSBoundParameters.ContainsKey('DisallowSquashMerge')) { $hashBody['allow_squash_merge'] = (-not $DisallowSquashMerge.ToBool()) }
if ($PSBoundParameters.ContainsKey('DisallowMergeCommit')) { $hashBody['allow_merge_commit'] = (-not $DisallowMergeCommit.ToBool()) }
if ($PSBoundParameters.ContainsKey('DisallowRebaseMerge')) { $hashBody['allow_rebase_merge'] = (-not $DisallowRebaseMerge.ToBool()) }
if ($PSBoundParameters.ContainsKey('SquashMergeCommitTitle')) { $hashBody['squash_merge_commit_title'] = $script:SquashMergeCommitTitleConversion.$SquashMergeCommitTitle }
if ($PSBoundParameters.ContainsKey('SquashMergeCommitMessage')) { $hashBody['squash_merge_commit_message'] = $script:SquashMergeCommitMessageConversion.$SquashMergeCommitMessage }
if ($PSBoundParameters.ContainsKey('MergeCommitTitle')) { $hashBody['merge_commit_title'] = $script:MergeCommitTitleConversion.$MergeCommitTitle }
if ($PSBoundParameters.ContainsKey('MergeCommitMessage')) { $hashBody['merge_commit_message'] = $script:MergeCommitMessageConversion.$MergeCommitMessage }
if ($PSBoundParameters.ContainsKey('AllowAutoMerge')) { $hashBody['allow_auto_merge'] = $AllowAutoMerge.ToBool() }
if ($PSBoundParameters.ContainsKey('AllowUpdateBranch')) { $hashBody['allow_update_branch'] = $AllowUpdateBranch.ToBool() }
if ($PSBoundParameters.ContainsKey('DeleteBranchOnMerge')) { $hashBody['delete_branch_on_merge'] = $DeleteBranchOnMerge.ToBool() }
if ($PSBoundParameters.ContainsKey('IsTemplate')) { $hashBody['is_template'] = $IsTemplate.ToBool() }
if (-not $PSCmdlet.ShouldProcess($RepositoryName, 'Create GitHub Repository'))
{
return
}
$params = @{
'UriFragment' = $uriFragment
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'AcceptHeader' = $script:baptisteAcceptHeader
'Description' = "Creating $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties)
}
filter New-GitHubRepositoryFromTemplate
{
<#
.SYNOPSIS
Creates a new repository on GitHub from a template repository.
.DESCRIPTION
Creates a new repository on GitHub from a template repository.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the template repository.
If no value is specified, the DefaultOwnerName configuration property value will be used,
and if there is no configuration value defined, the current authenticated user will be used.
.PARAMETER RepositoryName
Name of the template repository.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER TargetOwnerName
The organization or person who will own the new repository.
To create a new repository in an organization, the authenticated user must be a member
of the specified organization.
.PARAMETER TargetRepositoryName
Name of the repository to be created.
.PARAMETER Description
A short description of the repository.
.PARAMETER Private
By default, this repository will created Public. Specify this to create a private
repository.
.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.
.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.Repository
.NOTES
The authenticated user must own or be a member of an organization that owns the repository.
To check if a repository is available to use as a template, call `Get-GitHubRepository` on the
repository in question and check that the is_template property is $true.
.EXAMPLE
New-GitHubRepositoryFromTemplate -OwnerName MyOrg -RepositoryName MyTemplateRepo -TargetRepositoryName MyNewRepo -TargetOwnerName Me
Creates a new GitHub repository from the specified template repository.
.EXAMPLE
$repo = Get-GitHubRepository -OwnerName MyOrg -RepositoryName MyTemplateRepo
$repo | New-GitHubRepositoryFromTemplate -TargetRepositoryName MyNewRepo -TargetOwnerName Me
You can also pipe in a repo that was returned from a previous command.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false)]
[OutputType({$script:GitHubRepositoryTypeName})]
param(
[Parameter(ParameterSetName = 'Elements')]
[string] $OwnerName,
[Parameter(
Mandatory,
Position = 1,
ParameterSetName = 'Elements')]
[ValidateNotNullOrEmpty()]
[string] $RepositoryName,
[Parameter(
Mandatory,
Position = 2,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[Parameter(
Mandatory,
Position = 3)]
[ValidateNotNullOrEmpty()]
[string] $TargetOwnerName,
[Parameter(
Mandatory,
Position = 4)]
[ValidateNotNullOrEmpty()]
[string] $TargetRepositoryName,
[string] $Description,
[switch] $Private,
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
RepositoryName = (Get-PiiSafeString -PlainText $RepositoryName)
OwnerName = (Get-PiiSafeString -PlainText $OwnerName)
TargetRepositoryName = (Get-PiiSafeString -PlainText $TargetRepositoryName)
TargetOwnerName = (Get-PiiSafeString -PlainText $TargetOwnerName)
}
$uriFragment = "repos/$OwnerName/$RepositoryName/generate"
$hashBody = @{
owner = $TargetOwnerName
name = $TargetRepositoryName
}
if ($PSBoundParameters.ContainsKey('Description')) { $hashBody['description'] = $Description }
if ($PSBoundParameters.ContainsKey('Private')) { $hashBody['private'] = $Private.ToBool() }
if (-not $PSCmdlet.ShouldProcess(
$TargetRepositoryName,
"Create GitHub Repository From Template $RepositoryName"))
{
return
}
$params = @{
'UriFragment' = $uriFragment
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = "Creating $TargetRepositoryName from Template"
'AcceptHeader' = $script:baptisteAcceptHeader
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties)
}
filter Remove-GitHubRepository
{
<#
.SYNOPSIS
Removes/deletes a repository from GitHub.
.DESCRIPTION
Removes/deletes a repository from GitHub.
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 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.EXAMPLE
Remove-GitHubRepository -OwnerName You -RepositoryName YourRepoToDelete
.EXAMPLE
Remove-GitHubRepository -Uri https://github.com/You/YourRepoToDelete
.EXAMPLE
Remove-GitHubRepository -Uri https://github.com/You/YourRepoToDelete -Confirm:$false
Remove repository with the given URI, without prompting for confirmation.
.EXAMPLE
Remove-GitHubRepository -Uri https://github.com/You/YourRepoToDelete -Force
Remove repository with the given URI, without prompting for confirmation.
.EXAMPLE
$repo = Get-GitHubRepository -Uri https://github.com/You/YourRepoToDelete
$repo | Remove-GitHubRepository -Force
You can also pipe in a repo that was returned from a previous command.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements',
ConfirmImpact="High")]
[Alias('Delete-GitHubRepository')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[switch] $Force,
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
}
if ($Force -and (-not $Confirm))
{
$ConfirmPreference = 'None'
}
if (-not $PSCmdlet.ShouldProcess($RepositoryName, 'Remove GitHub Repository'))
{
return
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName"
'Method' = 'Delete'
'Description' = "Deleting $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return Invoke-GHRestMethod @params
}
filter Get-GitHubRepository
{
<#
.SYNOPSIS
Retrieves information about a repository or list of repositories on GitHub.
.DESCRIPTION
Retrieves information about a repository or list of repositories on GitHub.
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 retrieve the repositories for.
.PARAMETER Visibility
The type of visibility/accessibility for the repositories to return.
.PARAMETER Affiliation
Can be one or more of:
owner - Repositories that are owned by the authenticated user
collaborator - Repositories that the user has been added to as a collaborator
organization_member - Repositories that the user has access to through being
a member of an organization. This includes every repository on every team that the user
is on.
.PARAMETER Type
The type of repository to return.
.PARAMETER Sort
Property that the results should be sorted by
.PARAMETER Direction
Direction of the sort that is to be applied to the results.
.PARAMETER GetAllPublicRepositories
If this is specified with no other parameter, then instead of returning back all
repositories for the current authenticated user, it will instead return back all
public repositories on GitHub in the order in which they were created.
.PARAMETER Since
The ID of the last public repository that you have seen. If specified with
-GetAllPublicRepositories, will only return back public repositories created _after_ this
one.
.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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.Repository
.EXAMPLE
Get-GitHubRepository
Gets all repositories for the current authenticated user.
.EXAMPLE
Get-GitHubRepository -GetAllPublicRepositories
Gets all public repositories on GitHub.
.EXAMPLE
Get-GitHubRepository -OwnerName octocat
Gets all of the repositories for the user octocat
.EXAMPLE
Get-GitHubUser -UserName octocat | Get-GitHubRepository
Gets all of the repositories for the user octocat
.EXAMPLE
Get-GitHubRepository -Uri https://github.com/microsoft/PowerShellForGitHub
Gets information about the microsoft/PowerShellForGitHub repository.
.EXAMPLE
$repo | Get-GitHubRepository
You can pipe in a previous repository to get its refreshed information.
.EXAMPLE
Get-GitHubRepository -OrganizationName PowerShell
Gets all of the repositories in the PowerShell organization.
#>
[CmdletBinding(DefaultParameterSetName = 'AuthenticatedUser')]
[OutputType({$script:GitHubRepositoryTypeName})]
param(
[Parameter(
ValueFromPipelineByPropertyName,
ParameterSetName='ElementsOrUser')]
[Alias('UserName')]
[string] $OwnerName,
[Parameter(ParameterSetName='ElementsOrUser')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[Parameter(
ValueFromPipelineByPropertyName,
ParameterSetName='Organization')]
[string] $OrganizationName,
[Parameter(ParameterSetName='AuthenticatedUser')]
[ValidateSet('All', 'Public', 'Private')]
[string] $Visibility,
[Parameter(ParameterSetName='AuthenticatedUser')]
[ValidateSet('Owner', 'Collaborator', 'OrganizationMember')]
[string[]] $Affiliation,
[Parameter(ParameterSetName='AuthenticatedUser')]
[Parameter(ParameterSetName='ElementsOrUser')]
[Parameter(ParameterSetName='Organization')]
[ValidateSet('All', 'Owner', 'Public', 'Private', 'Member', 'Forks', 'Sources')]
[string] $Type,
[Parameter(ParameterSetName='AuthenticatedUser')]
[Parameter(ParameterSetName='ElementsOrUser')]
[Parameter(ParameterSetName='Organization')]
[ValidateSet('Created', 'Updated', 'Pushed', 'FullName')]
[string] $Sort,
[Parameter(ParameterSetName='AuthenticatedUser')]
[Parameter(ParameterSetName='ElementsOrUser')]
[Parameter(ParameterSetName='Organization')]
[ValidateSet('Ascending', 'Descending')]
[string] $Direction,
[Parameter(ParameterSetName='PublicRepos')]
[switch] $GetAllPublicRepositories,
[Parameter(ParameterSetName='PublicRepos')]
[int64] $Since,
[string] $AccessToken
)
Write-InvocationLog
# We are explicitly disabling validation here because a valid parameter set for this function
# allows the OwnerName to be passed in, but not the RepositoryName. That would allow the caller
# to get all of the repositories owned by a specific username. Therefore, we don't want to fail
# if both have not been supplied...we'll do the extra validation within the function.
$elements = Resolve-RepositoryElements -DisableValidation
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'UsageType' = $PSCmdlet.ParameterSetName
}
$uriFragment = [String]::Empty
$description = [String]::Empty
switch ($PSCmdlet.ParameterSetName)
{
'ElementsOrUser' {
# This is a little tricky. Ideally we'd have two separate ParameterSets (Elements, User),
# however PowerShell would be unable to disambiguate between the two, so unfortunately
# we need to do some additional work here. And because fallthru doesn't appear to be
# working right, we're combining both of those.
if ([String]::IsNullOrWhiteSpace($OwnerName))
{
$message = 'OwnerName could not be determined.'
Write-Log -Message $message -Level Error
throw $message
}
elseif ([String]::IsNullOrWhiteSpace($RepositoryName))
{
$telemetryProperties['UsageType'] = 'User'
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$uriFragment = "users/$OwnerName/repos"
$description = "Getting repos for $OwnerName"
}
else
{
if ($PSBoundParameters.ContainsKey('Type') -or
$PSBoundParameters.ContainsKey('Sort') -or
$PSBoundParameters.ContainsKey('Direction'))
{
$message = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.'
Write-Log -Message $message -Level Error
throw $message
}
$telemetryProperties['UsageType'] = 'Elements'
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "repos/$OwnerName/$RepositoryName"
$description = "Getting $OwnerName/$RepositoryName"
}
break
}
'Uri' {
if ($PSBoundParameters.ContainsKey('Type') -or
$PSBoundParameters.ContainsKey('Sort') -or
$PSBoundParameters.ContainsKey('Direction'))
{
$message = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.'
Write-Log -Message $message -Level Error
throw $message
}
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "repos/$OwnerName/$RepositoryName"
$description = "Getting $OwnerName/$RepositoryName"
break
}
'Organization' {
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
$uriFragment = "orgs/$OrganizationName/repos"
$description = "Getting repos for $OrganizationName"
break
}
'AuthenticatedUser' {
if ($PSBoundParameters.ContainsKey('Type') -and
($PSBoundParameters.ContainsKey('Visibility') -or
$PSBoundParameters.ContainsKey('Affiliation')))
{
$message = 'Unable to specify -Type when using -Visibility and/or -Affiliation.'
Write-Log -Message $message -Level Error
throw $message
}
$uriFragment = 'user/repos'
$description = 'Getting repos for current authenticated user'
break
}
'PublicRepos' {
$uriFragment = 'repositories'
$description = "Getting all public repositories"
if ($PSBoundParameters.ContainsKey('Since'))
{
$description += " since $Since"
}
break
}
}
$sortConverter = @{
'Created' = 'created'
'Updated' = 'updated'
'Pushed' = 'pushed'
'FullName' = 'full_name'
}
$directionConverter = @{
'Ascending' = 'asc'
'Descending' = 'desc'
}
$getParams = @()
if ($PSBoundParameters.ContainsKey('Visibility')) { $getParams += "visibility=$($Visibility.ToLower())" }
if ($PSBoundParameters.ContainsKey('Sort')) { $getParams += "sort=$($sortConverter[$Sort])" }
if ($PSBoundParameters.ContainsKey('Type')) { $getParams += "type=$($Type.ToLower())" }
if ($PSBoundParameters.ContainsKey('Direction')) { $getParams += "direction=$($directionConverter[$Direction])" }
if ($PSBoundParameters.ContainsKey('Affiliation') -and $Affiliation.Count -gt 0)
{
$affiliationMap = @{
Owner = 'owner'
Collaborator = 'collaborator'
OrganizationMember = 'organization_member'
}
$affiliationParam = @()
foreach ($member in $Affiliation)
{
$affiliationParam += $affiliationMap[$member]
}
$getParams += "affiliation=$($affiliationParam -join ',')"
}
if ($PSBoundParameters.ContainsKey('Since')) { $getParams += "since=$Since" }
$params = @{
'UriFragment' = $uriFragment + '?' + ($getParams -join '&')
'Description' = $description
'AcceptHeader' = "$script:nebulaAcceptHeader,$script:baptisteAcceptHeader,$script:mercyAcceptHeader"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubRepositoryAdditionalProperties)
}
filter Rename-GitHubRepository
{
<#
.SYNOPSIS
Rename a GitHub repository
.DESCRIPTION
Renames a GitHub repository with the new name provided.
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 to rename. You can supply this directly, or more easily by
using Get-GitHubRepository to get the repository as you please,
and then piping the result to this cmdlet.
.PARAMETER NewName
The new name to set for the given GitHub repository
.PARAMETER Force
If this switch is specified, you will not be prompted for confirmation of command execution.
.PARAMETER PassThru
Returns the renamed Repository. By default, this cmdlet does not generate any output.
You can use "Set-GitHubConfiguration -DefaultPassThru" to control the default behavior
of this switch.
.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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository