-
Notifications
You must be signed in to change notification settings - Fork 188
/
GitHubConfiguration.ps1
1096 lines (862 loc) · 39.9 KB
/
GitHubConfiguration.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.
# The GitHub API token is stored in the password field.
[PSCredential] $script:accessTokenCredential = $null
# The location of the file that we'll store any settings that can/should roam with the user.
[string] $script:configurationFilePath = [System.IO.Path]::Combine(
[Environment]::GetFolderPath('ApplicationData'),
'Microsoft',
'PowerShellForGitHub',
'config.json')
# The location of the file that we'll store the Access Token SecureString
# which cannot/should not roam with the user.
[string] $script:accessTokenFilePath = [System.IO.Path]::Combine(
[Environment]::GetFolderPath('LocalApplicationData'),
'Microsoft',
'PowerShellForGitHub',
'accessToken.txt')
# Only tell users about needing to configure an API token once per session.
$script:seenTokenWarningThisSession = $false
# The session-cached copy of the module's configuration properties
[PSCustomObject] $script:configuration = $null
function Initialize-GitHubConfiguration
{
<#
.SYNOPSIS
Populates the configuration of the module for this session, loading in any values
that may have been saved to disk.
.DESCRIPTION
Populates the configuration of the module for this session, loading in any values
that may have been saved to disk.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.NOTES
Internal helper method. This is actually invoked at the END of this file.
#>
[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()
$script:seenTokenWarningThisSession = $false
$script:configuration = Import-GitHubConfiguration -Path $script:configurationFilePath
}
function Set-GitHubConfiguration
{
<#
.SYNOPSIS
Change the value of a configuration property for the PowerShellForGitHub module,
for the session only, or globally for this user.
.DESCRIPTION
Change the value of a configuration property for the PowerShellForGitHub module,
for the session only, or globally for this user.
A single call to this method can set any number or combination of properties.
To change any of the boolean/switch properties to false, specify the switch,
immediately followed by ":$false" with no space.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER ApiHostName
The hostname of the GitHub instance to communicate with. Defaults to 'github.com'. Provide a
different hostname when using a GitHub Enterprise server. Do not include the HTTP/S prefix,
and do not include 'api'. For example, use "github.contoso.com".
.PARAMETER ApplicationInsightsKey
Change the Application Insights instance that telemetry will be reported to (if telemetry
hasn't been disabled via DisableTelemetry).
.PARAMETER DefaultNoStatus
Control if the -NoStatus switch should be passed-in by default to all methods.
.PARAMETER DefaultOwnerName
The owner name that should be used with a command that takes OwnerName as a parameter
when no value has been supplied.
.PARAMETER DefaultRepositoryName
The owner name that should be used with a command that takes RepositoryName as a parameter
when no value has been supplied.
.PARAMETER DisableLogging
Specify this switch to stop the module from logging all activity to a log file located
at the location specified by LogPath.
.PARAMETER DisablePiiProtection
Specify this switch to disable the hashing of potential PII data prior to submitting the
data to telemetry (if telemetry hasn't been disabled via DisableTelemetry).
.PARAMETER DisablePipelineSupport
By default, this module will modify all objects returned by the API calls by adding
additional, consistent properties to those objects which ease pipelining those objects
into other functions. This is highly convenient functionality. You would only want to
disable this functionality if you are experiencing some edge case problems and are awaiting
a proper fix.
.PARAMETER DisableSmarterObjects
By default, this module will modify all objects returned by the API calls to update
any properties that can be converted to objects (like strings for Date/Time's being
converted to real DateTime objects). Enable this property if you desire getting back
the unmodified version of the object from the API.
.PARAMETER DisableTelemetry
Specify this switch to stop the module from reporting any of its usage (which would be used
for diagnostics purposes).
.PARAMETER DisableUpdateCheck
Specify this switch to stop the daily update check with PowerShellGallery which can
inform you when there is a newer version of this module available.
.PARAMETER LogPath
The location of the log file that all activity will be written to if DisableLogging remains
$false.
.PARAMETER LogProcessId
If specified, the Process ID of the current PowerShell session will be included in each
log entry. This can be useful if you have concurrent PowerShell sessions all logging
to the same file, as it would then be possible to filter results based on ProcessId.
.PARAMETER LogRequestBody
If specified, the JSON body of the REST request will be logged to verbose output.
This can be helpful for debugging purposes.
.PARAMETER LogTimeAsUtc
If specified, all times logged will be logged as UTC instead of the local timezone.
.PARAMETER RetryDelaySeconds
The number of seconds to wait before retrying a command again after receiving a 202 response.
.PARAMETER SuppressNoTokenWarning
If an Access Token has not been configured, this module will provide a warning to the user
informing them of this, once per session. If it is expected that this module will regularly
be used without configuring an Access Token, specify this switch to always suppress that
warning message.
.PARAMETER SuppressTelemetryReminder
When telemetry is enabled, a warning will be printed to the console once per session
informing users that telemetry is occurring. Setting this value will suppress that
message from showing up ever again.
.PARAMETER WebRequestTimeoutSec
The number of seconds that should be allowed before an API request times out. A value of
0 indicates an infinite timeout, however experience has shown that PowerShell doesn't seem
to always honor inifinite timeouts. Hence, this value can be configured if need be.
.PARAMETER SessionOnly
By default, this method will store the configuration values in a local file so that changes
persist across PowerShell sessions. If this switch is provided, the file will not be
created/updated and the specified configuration changes will only remain in memory/effect
for the duration of this PowerShell session.
.EXAMPLE
Set-GitHubConfiguration -WebRequestTimeoutSec 120 -SuppressNoTokenWarning
Changes the timeout permitted for a web request to two minutes, and additionally tells
the module to never warn about no Access Token being configured. These settings will be
persisted across future PowerShell sessions.
.EXAMPLE
Set-GitHubConfiguration -DisableLogging -SessionOnly
Disables the logging of any activity to the logfile specified in LogPath, but for this
session only.
.EXAMPLE
Set-GitHubConfiguration -ApiHostName "github.contoso.com"
Sets all requests to connect to a GitHub Enterprise server running at
github.contoso.com.
#>
[CmdletBinding(
PositionalBinding = $false,
SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[ValidatePattern('^(?!https?:)(?!api\.)(?!www\.).*')]
[string] $ApiHostName,
[string] $ApplicationInsightsKey,
[switch] $DefaultNoStatus,
[string] $DefaultOwnerName,
[string] $DefaultRepositoryName,
[switch] $DisableLogging,
[switch] $DisablePiiProtection,
[switch] $DisablePipelineSupport,
[switch] $DisableSmarterObjects,
[switch] $DisableTelemetry,
[switch] $DisableUpdateCheck,
[string] $LogPath,
[switch] $LogProcessId,
[switch] $LogRequestBody,
[switch] $LogTimeAsUtc,
[int] $RetryDelaySeconds,
[switch] $SuppressNoTokenWarning,
[switch] $SuppressTelemetryReminder,
[ValidateRange(0, 3600)]
[int] $WebRequestTimeoutSec,
[switch] $SessionOnly
)
$persistedConfig = $null
if (-not $SessionOnly)
{
$persistedConfig = Read-GitHubConfiguration -Path $script:configurationFilePath
}
$properties = Get-Member -InputObject $script:configuration -MemberType NoteProperty | Select-Object -ExpandProperty Name
foreach ($name in $properties)
{
if ($PSBoundParameters.ContainsKey($name))
{
$value = $PSBoundParameters.$name
if ($value -is [switch]) { $value = $value.ToBool() }
$script:configuration.$name = $value
if (-not $SessionOnly)
{
Add-Member -InputObject $persistedConfig -Name $name -Value $value -MemberType NoteProperty -Force
}
}
}
if (-not $SessionOnly)
{
Save-GitHubConfiguration -Configuration $persistedConfig -Path $script:configurationFilePath
}
}
function Get-GitHubConfiguration
{
<#
.SYNOPSIS
Gets the currently configured value for the requested configuration setting.
.DESCRIPTION
Gets the currently configured value for the requested configuration setting.
Always returns the value for this session, which may or may not be the persisted
setting (that all depends on whether or not the setting was previously modified
during this session using Set-GitHubConfiguration -SessionOnly).
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Name
The name of the configuration whose value is desired.
.EXAMPLE
Get-GitHubConfiguration -Name WebRequestTimeoutSec
Gets the currently configured value for WebRequestTimeoutSec for this PowerShell session
(which may or may not be the same as the persisted configuration value, depending on
whether this value was modified during this session with Set-GitHubConfiguration -SessionOnly).
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateSet(
'ApiHostName',
'ApplicationInsightsKey',
'DefaultNoStatus',
'DefaultOwnerName',
'DefaultRepositoryName',
'DisableLogging',
'DisablePiiProtection',
'DisablePipelineSupport',
'DisableSmarterObjects',
'DisableTelemetry',
'DisableUpdateCheck',
'LogPath',
'LogProcessId',
'LogRequestBody',
'LogTimeAsUtc',
'RetryDelaySeconds',
'SuppressNoTokenWarning',
'SuppressTelemetryReminder',
'TestConfigSettingsHash',
'WebRequestTimeoutSec')]
[string] $Name
)
return $script:configuration.$Name
}
function Save-GitHubConfiguration
{
<#
.SYNOPSIS
Serializes the provided settings object to disk as a JSON file.
.DESCRIPTION
Serializes the provided settings object to disk as a JSON file.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Configuration
The configuration object to persist to disk.
.PARAMETER Path
The path to the file on disk that Configuration should be persisted to.
.NOTES
Internal helper method.
.EXAMPLE
Save-GitHubConfiguration -Configuration $config -Path 'c:\foo\config.json'
Serializes $config as a JSON object to 'c:\foo\config.json'
#>
[CmdletBinding()]
[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)]
[PSCustomObject] $Configuration,
[Parameter(Mandatory)]
[string] $Path
)
$null = New-Item -Path $Path -Force
ConvertTo-Json -InputObject $Configuration |
Set-Content -Path $Path -Force -ErrorAction SilentlyContinue -ErrorVariable ev
if (($null -ne $ev) -and ($ev.Count -gt 0))
{
$message = "Failed to persist these updated settings to disk. They will remain for this PowerShell session only."
Write-Log -Message $message -Level Warning -Exception $ev[0]
}
}
function Test-PropertyExists
{
<#
.SYNOPSIS
Determines if an object contains a property with a specified name.
.DESCRIPTION
Determines if an object contains a property with a specified name.
This is essentially using Get-Member to verify that a property exists,
but additionally adds a check to ensure that InputObject isn't null.
.PARAMETER InputObject
The object to check to see if it has a property named Name.
.PARAMETER Name
The name of the property on InputObject that is being tested for.
.EXAMPLE
Test-PropertyExists -InputObject $listing -Name 'title'
Returns $true if $listing is non-null and has a property named 'title'.
Returns $false otherwise.
.NOTES
Internal-only helper method.
#>
[CmdletBinding()]
[OutputType([bool])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Exists isn't a noun and isn't violating the intention of this rule.")]
param(
[Parameter(Mandatory)]
[AllowNull()]
$InputObject,
[Parameter(Mandatory)]
[String] $Name
)
return (($null -ne $InputObject) -and
($null -ne (Get-Member -InputObject $InputObject -Name $Name -MemberType Properties)))
}
function Resolve-PropertyValue
{
<#
.SYNOPSIS
Returns the requested property from the provided object, if it exists and is a valid
value. Otherwise, returns the default value.
.DESCRIPTION
Returns the requested property from the provided object, if it exists and is a valid
value. Otherwise, returns the default value.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER InputObject
The object to check the value of the requested property.
.PARAMETER Name
The name of the property on InputObject whose value is desired.
.PARAMETER Type
The type of the value stored in the Name property on InputObject. Used to validate
that the property has a valid value.
.PARAMETER DefaultValue
The value to return if Name doesn't exist on InputObject or is of an invalid type.
.EXAMPLE
Resolve-PropertyValue -InputObject $config -Name defaultOwnerName -Type String -DefaultValue $null
Checks $config to see if it has a property named "defaultOwnerName". If it does, and it's a
string, returns that value, otherwise, returns $null (the DefaultValue).
#>
[CmdletBinding()]
param(
[PSCustomObject] $InputObject,
[Parameter(Mandatory)]
[string] $Name,
[Parameter(Mandatory)]
[ValidateSet('String', 'Boolean', 'Int32', 'Int64')]
[String] $Type,
$DefaultValue
)
if ($null -eq $InputObject)
{
return $DefaultValue
}
$typeType = [String]
if ($Type -eq 'Boolean') { $typeType = [Boolean] }
if ($Type -eq 'Int32') { $typeType = [Int32] }
if ($Type -eq 'Int64') { $typeType = [Int64] }
if (Test-PropertyExists -InputObject $InputObject -Name $Name)
{
if ($InputObject.$Name -is $typeType)
{
return $InputObject.$Name
}
else
{
$message = "The locally cached $Name configuration was not of type $Type. Reverting to default value."
Write-Log -Message $message -Level Warning
return $DefaultValue
}
}
else
{
return $DefaultValue
}
}
function Reset-GitHubConfiguration
{
<#
.SYNOPSIS
Clears out the user's configuration file and configures this session with all default
configuration values.
.DESCRIPTION
Clears out the user's configuration file and configures this session with all default
configuration values.
This would be the functional equivalent of using this on a completely different computer.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER SessionOnly
By default, this will delete the location configuration file so that all defaults are used
again. If this is specified, then only the configuration values that were made during
this session will be discarded.
.EXAMPLE
Reset-GitHubConfiguration
Deletes the local configuration file and loads in all default configuration values.
.NOTES
This command will not clear your authentication token.
Please use Clear-GitHubAuthentication to accomplish that.
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[switch] $SessionOnly
)
Set-TelemetryEvent -EventName Reset-GitHubConfiguration
if (-not $SessionOnly)
{
if ($PSCmdlet.ShouldProcess($script:configurationFilePath, "Delete configuration file"))
{
$null = Remove-Item -Path $script:configurationFilePath -Force -ErrorAction SilentlyContinue -ErrorVariable ev
}
if (($null -ne $ev) -and ($ev.Count -gt 0) -and ($ev[0].FullyQualifiedErrorId -notlike 'PathNotFound*'))
{
$message = "Reset was unsuccessful. Experienced a problem trying to remove the file [$script:configurationFilePath]."
Write-Log -Message $message -Level Warning -Exception $ev[0]
}
}
Initialize-GitHubConfiguration
$message = "This has not cleared your authentication token. Call Clear-GitHubAuthentication to accomplish that."
Write-Log -Message $message -Level Verbose
}
function Read-GitHubConfiguration
{
<#
.SYNOPSIS
Loads in the default configuration values and returns the deserialized object.
.DESCRIPTION
Loads in the default configuration values and returns the deserialized object.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Path
The file that may or may not exist with a serialized version of the configuration
values for this module.
.OUTPUTS
PSCustomObject
.NOTES
Internal helper method.
No side-effects.
.EXAMPLE
Read-GitHubConfiguration -Path 'c:\foo\config.json'
Returns back an object with the deserialized object contained in the specified file,
if it exists and is valid.
#>
[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(
[string] $Path
)
$content = Get-Content -Path $Path -Encoding UTF8 -ErrorAction Ignore
if (-not [String]::IsNullOrEmpty($content))
{
try
{
return ($content | ConvertFrom-Json)
}
catch
{
$message = 'The configuration file for this module is in an invalid state. Use Reset-GitHubConfiguration to recover.'
Write-Log -Message $message -Level Warning
}
}
return [PSCustomObject]@{}
}
function Import-GitHubConfiguration
{
<#
.SYNOPSIS
Loads in the default configuration values, and then updates the individual properties
with values that may exist in a file.
.DESCRIPTION
Loads in the default configuration values, and then updates the individual properties
with values that may exist in a file.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Path
The file that may or may not exist with a serialized version of the configuration
values for this module.
.OUTPUTS
PSCustomObject
.NOTES
Internal helper method.
No side-effects.
.EXAMPLE
Import-GitHubConfiguration -Path 'c:\foo\config.json'
Creates a new default config object and updates its values with any that are found
within a deserialized object from the content in $Path. The configuration object
is then returned.
#>
[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(
[string] $Path
)
# Create a configuration object with all the default values. We can then update the values
# with any that we find on disk.
$logPath = [String]::Empty
$documentsFolder = [System.Environment]::GetFolderPath('MyDocuments')
if (-not [System.String]::IsNullOrEmpty($documentsFolder))
{
$logPath = Join-Path -Path $documentsFolder -ChildPath 'PowerShellForGitHub.log'
}
$config = [PSCustomObject]@{
'apiHostName' = 'github.com'
'applicationInsightsKey' = '66d83c52-3070-489b-886b-09860e05e78a'
'disableLogging' = ([String]::IsNullOrEmpty($logPath))
'disablePiiProtection' = $false
'disablePipelineSupport' = $false
'disableSmarterObjects' = $false
'disableTelemetry' = $false
'disableUpdateCheck' = $false
'defaultNoStatus' = $false
'defaultOwnerName' = [String]::Empty
'defaultRepositoryName' = [String]::Empty
'logPath' = $logPath
'logProcessId' = $false
'logRequestBody' = $false
'logTimeAsUtc' = $false
'retryDelaySeconds' = 30
'suppressNoTokenWarning' = $false
'suppressTelemetryReminder' = $false
'webRequestTimeoutSec' = 0
# This hash is generated by using Helper.ps1's Get-Sha512Hash in Tests/Config/Settings.ps1
# like so:
# . ./Helpers.ps1; Get-Sha512Hash -PlainText (Get-Content -Path ./Tests/Config/Settings.ps1 -Raw -Encoding Utf8)
# The hash is used to identify if the user has made changes to the config file prior to
# running the UT's locally. It intentionally cannot be modified via Set-GitHubConfiguration
# and must be updated directly in the source code here should the default Settings.ps1 file
# ever be changed.
'testConfigSettingsHash' = '272EE14CED396100A7AFD23EA21CA262470B7F4D80E47B7ABD90508B86210775F020EEF79D322F4C22A53835F700E1DFD13D0509C1D08DD6F9771B3F0133EDAB'
}
$jsonObject = Read-GitHubConfiguration -Path $Path
Get-Member -InputObject $config -MemberType NoteProperty |
ForEach-Object {
$name = $_.Name
$type = $config.$name.GetType().Name
$config.$name = Resolve-PropertyValue -InputObject $jsonObject -Name $name -Type $type -DefaultValue $config.$name
}
return $config
}
function Backup-GitHubConfiguration
{
<#
.SYNOPSIS
Exports the user's current configuration file.
.DESCRIPTION
Exports the user's current configuration file.
This is primarily used for unit testing scenarios.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Path
The path to store the user's current configuration file.
.PARAMETER Force
If specified, will overwrite the contents of any file with the same name at the
location specified by Path.
.EXAMPLE
Backup-GitHubConfiguration -Path 'c:\foo\config.json'
Writes the user's current configuration file to c:\foo\config.json.
#>
[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(
[string] $Path,
[switch] $Force
)
# Make sure that the path that we're going to be storing the file exists.
$null = New-Item -Path (Split-Path -Path $Path -Parent) -ItemType Directory -Force
if (Test-Path -Path $script:configurationFilePath -PathType Leaf)
{
$null = Copy-Item -Path $script:configurationFilePath -Destination $Path -Force:$Force
}
else
{
ConvertTo-Json -InputObject @{} | Set-Content -Path $Path -Force:$Force
}
}
function Restore-GitHubConfiguration
{
<#
.SYNOPSIS
Sets the specified file to be the user's configuration file.
.DESCRIPTION
Sets the specified file to be the user's configuration file.
This is primarily used for unit testing scenarios.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Path
The path to store the user's current configuration file.
.EXAMPLE
Restore-GitHubConfiguration -Path 'c:\foo\config.json'
Makes the contents of c:\foo\config.json be the user's configuration for the module.
#>
[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(
[ValidateScript({
if (Test-Path -Path $_ -PathType Leaf) { $true }
else { throw "$_ does not exist." }})]
[string] $Path
)
# Make sure that the path that we're going to be storing the file exists.
$null = New-Item -Path (Split-Path -Path $script:configurationFilePath -Parent) -ItemType Directory -Force
$null = Copy-Item -Path $Path -Destination $script:configurationFilePath -Force
Initialize-GitHubConfiguration
}
function Resolve-ParameterWithDefaultConfigurationValue
{
<#
.SYNOPSIS
Some of the configuration properties act as default values to be used for some functions.
This will determine what the correct final value should be by inspecting the calling
functions inbound parameters, along with the corresponding configuration value.
.DESCRIPTION
Some of the configuration properties act as default values to be used for some functions.
This will determine what the correct final value should be by inspecting the calling
functions inbound parameters, along with the corresponding configuration value.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER BoundParameters
The inbound parameters from the calling method.
No need to explicitly provide this if you're using the PSBoundParameters from the
function that is calling this directly.
.PARAMETER Name
The name of the parameter in BoundParameters.
.PARAMETER ConfigValueName
The name of the configuration property that should be used as default if Name doesn't exist
in BoundParameters.
.PARAMETER NonEmptyStringRequired
If specified, will throw an exception if the resolved value to be returned would end up
being null or an empty string.
.EXAMPLE
Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus
Checks to see if the NoStatus switch was provided by the user from the calling method. If
so, uses that value. otherwise uses the DefaultNoStatus value currently configured.
#>
[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(
$BoundParameters = (Get-Variable -Name PSBoundParameters -Scope 1 -ValueOnly),
[Parameter(Mandatory)]
[string] $Name,
[Parameter(Mandatory)]
[String] $ConfigValueName,
[switch] $NonEmptyStringRequired
)
$value = $null
if ($BoundParameters.ContainsKey($Name))
{
$value = $BoundParameters[$Name]
}
else
{
$value = (Get-GitHubConfiguration -Name $ConfigValueName)
}
if ($NonEmptyStringRequired -and [String]::IsNullOrEmpty($value))
{
$message = "A value must be provided for $Name either as a parameter, or as a default configuration value ($ConfigValueName) via Set-GitHubConfiguration."
Write-Log -Message $message -Level Error
throw $message
}
else
{
return $value
}
}
function Set-GitHubAuthentication
{
<#
.SYNOPSIS
Allows the user to configure the API token that should be used for authentication
with the GitHub API.
.DESCRIPTION
Allows the user to configure the API token that should be used for authentication
with the GitHub API.
The token will be stored on the machine as a SecureString and will automatically
be read on future PowerShell sessions with this module. If the user ever wishes
to remove their authentication from the system, they simply need to call
Clear-GitHubAuthentication.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Credential
If provided, instead of prompting the user for their API Token, it will be extracted
from the password field of this credential object.
.PARAMETER SessionOnly
By default, this method will store the provided API Token as a SecureString in a local
file so that it can be restored automatically in future PowerShell sessions. If this
switch is provided, the file will not be created/updated and the authentication information
will only remain in memory for the duration of this PowerShell session.
.EXAMPLE
Set-GitHubAuthentication
Prompts the user for their GitHub API Token and stores it in a file on the machine as a
SecureString for use in future PowerShell sessions.
.EXAMPLE
$secureString = ("<Your Access Token>" | ConvertTo-SecureString -AsPlainText -Force)
$cred = New-Object System.Management.Automation.PSCredential "username is ignored", $secureString
Set-GitHubAuthentication -Credential $cred
$secureString = $null # clear this out now that it's no longer needed
$cred = $null # clear this out now that it's no longer needed
Allows you to specify your access token as a plain-text string ("<Your Access Token>")
which will be securely stored on the machine for use in all future PowerShell sessions.
.EXAMPLE
Set-GitHubAuthentication -SessionOnly
Prompts the user for their GitHub API Token, but keeps it in memory only for the duration
of this PowerShell session.
.EXAMPLE
Set-GitHubAuthentication -Credential $cred -SessionOnly
Uses the API token stored in the password field of the provided credential object for
authentication, but keeps it in memory only for the duration of this PowerShell session..
#>
[CmdletBinding(SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUsePSCredentialType", "", Justification="The System.Management.Automation.Credential() attribute does not appear to work in PowerShell v4 which we need to support.")]
param(
[PSCredential] $Credential,
[switch] $SessionOnly
)
Write-InvocationLog
if (-not $PSBoundParameters.ContainsKey('Credential'))
{
$message = 'Please provide your GitHub API Token in the Password field. You can enter anything in the username field (it will be ignored).'
if (-not $SessionOnly)
{
$message = $message + ' ***The token is being cached across PowerShell sessions. To clear caching, call Clear-GitHubAuthentication.***'
}
Write-Log -Message $message
$Credential = Get-Credential -Message $message
}
if ([String]::IsNullOrWhiteSpace($Credential.GetNetworkCredential().Password))
{
$message = "The API Token was not provided in the password field. Nothing to do."
Write-Log -Message $message -Level Error
throw $message
}
$script:accessTokenCredential = $Credential
if (-not $SessionOnly)
{
if ($PSCmdlet.ShouldProcess("Store API token as a SecureString in a local file"))
{
$null = New-Item -Path $script:accessTokenFilePath -Force
$script:accessTokenCredential.Password |
ConvertFrom-SecureString |
Set-Content -Path $script:accessTokenFilePath -Force
}
}
}
function Clear-GitHubAuthentication
{
<#
.SYNOPSIS
Clears out any GitHub API token from memory, as well as from local file storage.
.DESCRIPTION
Clears out any GitHub API token from memory, as well as from local file storage.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER SessionOnly
By default, this will clear out the cache in memory, as well as in the local
configuration file. If this switch is specified, authentication will be cleared out
in this session only -- the local configuration file cache will remain
(and thus still be available in a new PowerShell session).
.EXAMPLE
Clear-GitHubAuthentication
Clears out any GitHub API token from memory, as well as from local file storage.
.NOTES
This command will not clear your configuration settings.
Please use Reset-GitHubConfiguration to accomplish that.
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[switch] $SessionOnly
)
Write-InvocationLog
Set-TelemetryEvent -EventName Clear-GitHubAuthentication
if ($PSCmdlet.ShouldProcess("Clear memory cache"))
{
$script:accessTokenCredential = $null
}
if (-not $SessionOnly)
{
if ($PSCmdlet.ShouldProcess("Clear file-based cache"))
{
Remove-Item -Path $script:accessTokenFilePath -Force -ErrorAction SilentlyContinue -ErrorVariable ev
if (($null -ne $ev) -and
($ev.Count -gt 0) -and
($ev[0].FullyQualifiedErrorId -notlike 'PathNotFound*'))
{
$message = "Experienced a problem trying to remove the file that persists the Access Token [$script:accessTokenFilePath]."
Write-Log -Message $message -Level Warning -Exception $ev[0]
}
}
}
$message = "This has not cleared your configuration settings. Call Reset-GitHubConfiguration to accomplish that."
Write-Log -Message $message -Level Verbose
}
function Get-AccessToken
{
<#
.SYNOPSIS
Retrieves the API token for use in the rest of the module.
.DESCRIPTION
Retrieves the API token for use in the rest of the module.
First will try to use the one that may have been provided as a parameter.
If not provided, then will try to use the one already cached in memory.
If still not found, will look to see if there is a file with the API token stored
as a SecureString.
Finally, if there is still no available token, none will be used. The user will then be
subjected to tighter hourly query restrictions.