-
Notifications
You must be signed in to change notification settings - Fork 187
/
GitHubGists.ps1
1831 lines (1467 loc) · 54.4 KB
/
GitHubGists.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.
@{
GitHubGistTypeName = 'GitHub.Gist'
GitHubGistCommitTypeName = 'GitHub.GistCommit'
GitHubGistForkTypeName = 'GitHub.GistFork'
GitHubGistSummaryTypeName = 'GitHub.GistSummary'
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
filter Get-GitHubGist
{
<#
.SYNOPSIS
Retrieves gist information from GitHub.
.DESCRIPTION
Retrieves gist information from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific gist that you wish to retrieve.
.PARAMETER Sha
The specific revision of the gist that you wish to retrieve.
.PARAMETER Forks
Gets the forks of the specified gist.
.PARAMETER Commits
Gets the commits of the specified gist.
.PARAMETER UserName
Gets public gists for the specified user.
.PARAMETER Path
Download the files that are part of the specified gist to this path.
.PARAMETER Force
If downloading files, this will overwrite any files with the same name in the provided path.
.PARAMETER Current
Gets the authenticated user's gists.
.PARAMETER Starred
Gets the authenticated user's starred gists.
.PARAMETER Public
Gets public gists sorted by most recently updated to least recently updated.
The results will be limited to the first 3000.
.PARAMETER Since
Only gists updated at or after this time 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.
.INPUTS
GitHub.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.OUTPUTS
GitHub.Gist
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.EXAMPLE
Get-GitHubGist -Starred
Gets all starred gists for the current authenticated user.
.EXAMPLE
Get-GitHubGist -Public -Since ((Get-Date).AddDays(-2))
Gets all public gists that have been updated within the past two days.
.EXAMPLE
Get-GitHubGist -Gist 6cad326836d38bd3a7ae
Gets octocat's "hello_world.rb" gist.
#>
[CmdletBinding(
DefaultParameterSetName='Current',
PositionalBinding = $false)]
[OutputType({$script:GitHubGistTypeName})]
[OutputType({$script:GitHubGistCommitTypeName})]
[OutputType({$script:GitHubGistForkTypeName})]
[OutputType({$script:GitHubGistSummaryTypeName})]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Id',
Position = 1)]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Download',
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[Parameter(ParameterSetName='Id')]
[Parameter(ParameterSetName='Download')]
[ValidateNotNullOrEmpty()]
[string] $Sha,
[Parameter(ParameterSetName='Id')]
[switch] $Forks,
[Parameter(ParameterSetName='Id')]
[switch] $Commits,
[Parameter(
Mandatory,
ParameterSetName='User')]
[ValidateNotNullOrEmpty()]
[string] $UserName,
[Parameter(
Mandatory,
ParameterSetName='Download',
Position = 2)]
[ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(ParameterSetName='Download')]
[switch] $Force,
[Parameter(ParameterSetName='Current')]
[switch] $Current,
[Parameter(ParameterSetName='Current')]
[switch] $Starred,
[Parameter(ParameterSetName='Public')]
[switch] $Public,
[Parameter(ParameterSetName='User')]
[Parameter(ParameterSetName='Current')]
[Parameter(ParameterSetName='Public')]
[DateTime] $Since,
[string] $AccessToken
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = [String]::Empty
$description = [String]::Empty
$outputType = $script:GitHubGistSummaryTypeName
if ($PSCmdlet.ParameterSetName -in ('Id', 'Download'))
{
$telemetryProperties['ById'] = $true
if ($PSBoundParameters.ContainsKey('Sha'))
{
if ($Forks -or $Commits)
{
$message = 'Cannot check for forks or commits of a specific SHA. Do not specify SHA if you want to list out forks or commits.'
Write-Log -Message $message -Level Error
throw $message
}
$telemetryProperties['SpecifiedSha'] = $true
$uriFragment = "gists/$Gist/$Sha"
$description = "Getting gist $Gist with specified Sha"
$outputType = $script:GitHubGistTypeName
}
elseif ($Forks)
{
$uriFragment = "gists/$Gist/forks"
$description = "Getting forks of gist $Gist"
$outputType = $script:GitHubGistForkTypeName
}
elseif ($Commits)
{
$uriFragment = "gists/$Gist/commits"
$description = "Getting commits of gist $Gist"
$outputType = $script:GitHubGistCommitTypeName
}
else
{
$uriFragment = "gists/$Gist"
$description = "Getting gist $Gist"
$outputType = $script:GitHubGistTypeName
}
}
elseif ($PSCmdlet.ParameterSetName -eq 'User')
{
$telemetryProperties['ByUserName'] = $true
$uriFragment = "users/$UserName/gists"
$description = "Getting public gists for $UserName"
$outputType = $script:GitHubGistSummaryTypeName
}
elseif ($PSCmdlet.ParameterSetName -eq 'Current')
{
$telemetryProperties['CurrentUser'] = $true
$outputType = $script:GitHubGistSummaryTypeName
if ((Test-GitHubAuthenticationConfigured) -or (-not [String]::IsNullOrEmpty($AccessToken)))
{
if ($Starred)
{
$uriFragment = 'gists/starred'
$description = 'Getting starred gists for current authenticated user'
}
else
{
$uriFragment = 'gists'
$description = 'Getting gists for current authenticated user'
}
}
else
{
if ($Starred)
{
$message = 'Starred can only be specified for authenticated users. Either call Set-GitHubAuthentication first, or provide a value for the AccessToken parameter.'
Write-Log -Message $message -Level Error
throw $message
}
$message = 'Specified -Current, but not currently authenticated. Either call Set-GitHubAuthentication first, or provide a value for the AccessToken parameter.'
Write-Log -Message $message -Level Error
throw $message
}
}
elseif ($PSCmdlet.ParameterSetName -eq 'Public')
{
$telemetryProperties['Public'] = $true
$outputType = $script:GitHubGistSummaryTypeName
$uriFragment = "gists/public"
$description = 'Getting public gists'
}
$getParams = @()
$sinceFormattedTime = [String]::Empty
if ($null -ne $Since)
{
$sinceFormattedTime = $Since.ToUniversalTime().ToString('o')
$getParams += "since=$sinceFormattedTime"
}
$params = @{
'UriFragment' = $uriFragment + '?' + ($getParams -join '&')
'Description' = $description
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = (Invoke-GHRestMethodMultipleResult @params |
Add-GitHubGistAdditionalProperties -TypeName $outputType)
if ($PSCmdlet.ParameterSetName -eq 'Download')
{
Save-GitHubGist -GistObject $result -Path $Path -Force:$Force
}
else
{
if ($result.truncated -eq $true)
{
$message = @(
'Response has been truncated. The API will only return the first 3000 gist results',
'the first 300 files within the gist, and the first 1 Mb of an individual',
'file. If the file has been truncated, you can call',
'(Invoke-WebRequest -UseBasicParsing -Method Get -Uri <raw_url>).Content)',
'where <raw_url> is the value of raw_url for the file in question. Be aware that',
'for files larger than 10 Mb, you''ll need to clone the gist via the URL provided',
'by git_pull_url.')
Write-Log -Message ($message -join ' ') -Level Warning
}
return $result
}
}
function Save-GitHubGist
{
<#
.SYNOPSIS
Downloads the contents of a gist to the specified file path.
.DESCRIPTION
Downloads the contents of a gist to the specified file path.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER GistObject
The Gist PSCustomObject
.PARAMETER Path
Download the files that are part of the specified gist to this path.
.PARAMETER Force
If downloading files, this will overwrite any files with the same name in the provided path.
.NOTES
Internal-only helper
#>
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory)]
[PSCustomObject] $GistObject,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $Path,
[switch] $Force
)
# First, check to see if the response is missing files.
if ($GistObject.truncated)
{
$message = @(
'Gist response has been truncated. The API will only return information on',
'the first 300 files within a gist. To download this entire gist,',
'you''ll need to clone it via the URL provided by git_pull_url',
"[$($GistObject.git_pull_url)].")
Write-Log -Message ($message -join ' ') -Level Error
throw $message
}
# Then check to see if there are files we won't be able to download
$files = $GistObject.files | Get-Member -Type NoteProperty | Select-Object -ExpandProperty Name
foreach ($fileName in $files)
{
if ($GistObject.files.$fileName.truncated -and
($GistObject.files.$fileName.size -gt 10mb))
{
$message = @(
"At least one file ($fileName) in this gist is larger than 10mb.",
'In order to download this gist, you''ll need to clone it via the URL',
"provided by git_pull_url [$($GistObject.git_pull_url)].")
Write-Log -Message ($message -join ' ') -Level Error
throw $message
}
}
# Finally, we're ready to directly save the non-truncated files,
# and download the ones that are between 1 - 10mb.
$originalSecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
try
{
$headers = @{}
$AccessToken = Get-AccessToken -AccessToken $AccessToken
if (-not [String]::IsNullOrEmpty($AccessToken))
{
$headers['Authorization'] = "token $AccessToken"
}
$Path = Resolve-UnverifiedPath -Path $Path
$null = New-Item -Path $Path -ItemType Directory -Force
foreach ($fileName in $files)
{
$filePath = Join-Path -Path $Path -ChildPath $fileName
if ((Test-Path -Path $filePath -PathType Leaf) -and (-not $Force))
{
$message = "File already exists at path [$filePath]. Choose a different path or specify -Force"
Write-Log -Message $message -Level Error
throw $message
}
if ($GistObject.files.$fileName.truncated)
{
# Disable Progress Bar in function scope during Invoke-WebRequest
$ProgressPreference = 'SilentlyContinue'
$webRequestParams = @{
UseBasicParsing = $true
Method = 'Get'
Headers = $headers
Uri = $GistObject.files.$fileName.raw_url
OutFile = $filePath
}
Invoke-WebRequest @webRequestParams
}
else
{
$stream = New-Object -TypeName System.IO.StreamWriter -ArgumentList ($filePath)
try
{
$stream.Write($GistObject.files.$fileName.content)
}
finally
{
$stream.Close()
}
}
}
}
finally
{
[Net.ServicePointManager]::SecurityProtocol = $originalSecurityProtocol
}
}
filter Remove-GitHubGist
{
<#
.SYNOPSIS
Removes/deletes a gist from GitHub.
.DESCRIPTION
Removes/deletes a gist from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific gist that you wish to retrieve.
.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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.EXAMPLE
Remove-GitHubGist -Gist 6cad326836d38bd3a7ae
Removes octocat's "hello_world.rb" gist (assuming you have permission).
.EXAMPLE
Remove-GitHubGist -Gist 6cad326836d38bd3a7ae -Confirm:$false
Removes octocat's "hello_world.rb" gist (assuming you have permission).
Will not prompt for confirmation, as -Confirm:$false was specified.
.EXAMPLE
Remove-GitHubGist -Gist 6cad326836d38bd3a7ae -Force
Removes octocat's "hello_world.rb" gist (assuming you have permission).
Will not prompt for confirmation, as -Force was specified.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false,
ConfirmImpact = 'High')]
[Alias('Delete-GitHubGist')]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[switch] $Force,
[string] $AccessToken
)
Write-InvocationLog
if ($Force -and (-not $Confirm))
{
$ConfirmPreference = 'None'
}
if (-not $PSCmdlet.ShouldProcess($Gist, "Delete gist"))
{
return
}
$telemetryProperties = @{}
$params = @{
'UriFragment' = "gists/$Gist"
'Method' = 'Delete'
'Description' = "Removing gist $Gist"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return Invoke-GHRestMethod @params
}
filter Copy-GitHubGist
{
<#
.SYNOPSIS
Forks a gist from GitHub.
.DESCRIPTION
Forks a gist from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific gist that you wish to fork.
.PARAMETER PassThru
Returns the newly created gist fork. 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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.OUTPUTS
GitHub.GistSummary
.EXAMPLE
Copy-GitHubGist -Gist 6cad326836d38bd3a7ae
Forks octocat's "hello_world.rb" gist.
.EXAMPLE
$result = Fork-GitHubGist -Gist 6cad326836d38bd3a7ae -PassThru
Forks octocat's "hello_world.rb" gist. This is using the alias for the command.
The result is the same whether you use Copy-GitHubGist or Fork-GitHubGist.
Specifying the -PassThru switch enables you to get a reference to the newly created fork.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false)]
[OutputType({$script:GitHubGistSummaryTypeName})]
[Alias('Fork-GitHubGist')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="PassThru is accessed indirectly via Resolve-ParameterWithDefaultConfigurationValue")]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[switch] $PassThru,
[string] $AccessToken
)
Write-InvocationLog
if (-not $PSCmdlet.ShouldProcess($Gist, "Forking gist"))
{
return
}
$telemetryProperties = @{}
$params = @{
'UriFragment' = "gists/$Gist/forks"
'Method' = 'Post'
'Description' = "Forking gist $Gist"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = (Invoke-GHRestMethod @params |
Add-GitHubGistAdditionalProperties -TypeName $script:GitHubGistSummaryTypeName)
if (Resolve-ParameterWithDefaultConfigurationValue -Name PassThru -ConfigValueName DefaultPassThru)
{
return $result
}
}
filter Set-GitHubGistStar
{
<#
.SYNOPSIS
Changes the starred state of a gist on GitHub for the current authenticated user.
.DESCRIPTION
Changes the starred state of a gist on GitHub for the current authenticated user.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific Gist that you wish to change the starred state for.
.PARAMETER Star
Include this switch to star the gist. Exclude the switch (or use -Star:$false) to
remove the star.
.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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.EXAMPLE
Set-GitHubGistStar -Gist 6cad326836d38bd3a7ae -Star
Stars octocat's "hello_world.rb" gist.
.EXAMPLE
Set-GitHubGistStar -Gist 6cad326836d38bd3a7ae
Unstars octocat's "hello_world.rb" gist.
.EXAMPLE
Get-GitHubGist -Gist 6cad326836d38bd3a7ae | Set-GitHubGistStar -Star:$false
Unstars octocat's "hello_world.rb" gist.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false)]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[switch] $Star,
[string] $AccessToken
)
Write-InvocationLog
Set-TelemetryEvent -EventName $MyInvocation.MyCommand.Name
$PSBoundParameters.Remove('Star')
if ($Star)
{
return Add-GitHubGistStar @PSBoundParameters
}
else
{
return Remove-GitHubGistStar @PSBoundParameters
}
}
filter Add-GitHubGistStar
{
<#
.SYNOPSIS
Star a gist from GitHub.
.DESCRIPTION
Star a gist from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific Gist that you wish to star.
.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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.EXAMPLE
Add-GitHubGistStar -Gist 6cad326836d38bd3a7ae
Stars octocat's "hello_world.rb" gist.
.EXAMPLE
Star-GitHubGist -Gist 6cad326836d38bd3a7ae
Stars octocat's "hello_world.rb" gist. This is using the alias for the command.
The result is the same whether you use Add-GitHubGistStar or Star-GitHubGist.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false)]
[Alias('Star-GitHubGist')]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[string] $AccessToken
)
Write-InvocationLog
if (-not $PSCmdlet.ShouldProcess($Gist, "Starring gist"))
{
return
}
$telemetryProperties = @{}
$params = @{
'UriFragment' = "gists/$Gist/star"
'Method' = 'Put'
'Description' = "Starring gist $Gist"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return Invoke-GHRestMethod @params
}
filter Remove-GitHubGistStar
{
<#
.SYNOPSIS
Unstar a gist from GitHub.
.DESCRIPTION
Unstar a gist from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific gist that you wish to unstar.
.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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.EXAMPLE
Remove-GitHubGistStar -Gist 6cad326836d38bd3a7ae
Unstars octocat's "hello_world.rb" gist.
.EXAMPLE
Unstar-GitHubGist -Gist 6cad326836d38bd3a7ae
Unstars octocat's "hello_world.rb" gist. This is using the alias for the command.
The result is the same whether you use Remove-GitHubGistStar or Unstar-GitHubGist.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false)]
[Alias('Unstar-GitHubGist')]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[string] $AccessToken
)
Write-InvocationLog
if (-not $PSCmdlet.ShouldProcess($Gist, "Unstarring gist"))
{
return
}
$telemetryProperties = @{}
$params = @{
'UriFragment' = "gists/$Gist/star"
'Method' = 'Delete'
'Description' = "Unstarring gist $Gist"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return Invoke-GHRestMethod @params
}
filter Test-GitHubGistStar
{
<#
.SYNOPSIS
Checks if a gist from GitHub is starred.
.DESCRIPTION
Checks if a gist from GitHub is starred.
Will return $false if it isn't starred, as well as if it couldn't be checked
(due to permissions or non-existence).
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Gist
The ID of the specific gist that you wish to check.
.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.Gist
GitHub.GistComment
GitHub.GistCommit
GitHub.GistFork
GitHub.GistSummary
.OUTPUTS
Boolean indicating if the gist was both found and determined to be starred.
.EXAMPLE
Test-GitHubGistStar -Gist 6cad326836d38bd3a7ae
Returns $true if the gist is starred, or $false if isn't starred or couldn't be checked
(due to permissions or non-existence).
#>
[CmdletBinding(PositionalBinding = $false)]
[OutputType([bool])]
param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1)]
[Alias('GistId')]
[ValidateNotNullOrEmpty()]
[string] $Gist,
[string] $AccessToken
)
Write-InvocationLog
$telemetryProperties = @{}
$params = @{
'UriFragment' = "gists/$Gist/star"
'Method' = 'Get'
'Description' = "Checking if gist $Gist is starred"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'ExtendedResult' = $true
}
try
{
$response = Invoke-GHRestMethod @params
return $response.StatusCode -eq 204
}
catch
{
return $false
}
}
filter New-GitHubGist
{
<#
.SYNOPSIS
Creates a new gist on GitHub.
.DESCRIPTION
Creates a new gist on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER File
An array of filepaths that should be part of this gist.
Use this when you have multiple files that should be part of a gist, or when you simply
want to reference an existing file on disk.
.PARAMETER FileName
The name of the file that Content should be stored in within the newly created gist.
.PARAMETER Content
The content of a single file that should be part of the gist.
.PARAMETER Description
A descriptive name for this gist.
.PARAMETER Public
When specified, the gist will be public and available for anyone to see.
.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
String - Filename(s) of file(s) that should be the content of the gist.
.OUTPUTS
GitHub.GitDetail
.EXAMPLE
New-GitHubGist -FileName 'sample.txt' -Content 'Body of my file.' -Description 'This is my gist!' -Public
Creates a new public gist with a single file named 'sample.txt' that has the body of "Body of my file."
.EXAMPLE
New-GitHubGist -File 'c:\files\foo.txt' -Description 'This is my gist!'
Creates a new private gist with a single file named 'foo.txt'. Will populate it with the
content of the file at c:\files\foo.txt.
.EXAMPLE
New-GitHubGist -File ('c:\files\foo.txt', 'c:\other\bar.txt', 'c:\octocat.ps1') -Description 'This is my gist!'
Creates a new private gist with a three files named 'foo.txt', 'bar.txt' and 'octocat.ps1'.
Each will be populated with the content from the file on disk at the specified location.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='FileRef',
PositionalBinding = $false)]
[OutputType({$script:GitHubGistTypeName})]
param(
[Parameter(
Mandatory,
ValueFromPipeline,
ParameterSetName='FileRef',
Position = 1)]
[ValidateNotNullOrEmpty()]
[string[]] $File,
[Parameter(
Mandatory,
ParameterSetName='Content',
Position = 1)]
[ValidateNotNullOrEmpty()]
[string] $FileName,
[Parameter(
Mandatory,
ParameterSetName='Content',
Position = 2)]
[ValidateNotNullOrEmpty()]
[string] $Content,
[string] $Description,
[switch] $Public,
[string] $AccessToken
)
begin
{
$files = @{}
}
process
{
foreach ($path in $File)
{
$path = Resolve-UnverifiedPath -Path $path
if (-not (Test-Path -Path $path -PathType Leaf))
{
$message = "Specified file [$path] could not be found or was inaccessible."
Write-Log -Message $message -Level Error
throw $message
}
$content = [System.IO.File]::ReadAllText($path)