-
Notifications
You must be signed in to change notification settings - Fork 187
/
GitHubMiscellaneous.ps1
541 lines (418 loc) · 19.6 KB
/
GitHubMiscellaneous.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Get-GitHubRateLimit
{
<#
.SYNOPSIS
Gets the current rate limit status for the GitHub API based on the currently configured
authentication (Access Token).
.DESCRIPTION
Gets the current rate limit status for the GitHub API based on the currently configured
authentication (Access Token).
Use Set-GitHubAuthentication to change your current authentication (Access Token).
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.OUTPUTS
[PSCustomObject]
Limits returned are _per hour_.
The Search API has a custom rate limit, separate from the rate limit
governing the rest of the REST API. The GraphQL API also has a custom
rate limit that is separate from and calculated differently than rate
limits in the REST API.
For these reasons, the Rate Limit API response categorizes your rate limit.
Under resources, you'll see three objects:
The core object provides your rate limit status for all non-search-related resources in the REST API.
The search object provides your rate limit status for the Search API.
The graphql object provides your rate limit status for the GraphQL API.
Deprecation notice
The rate object is deprecated.
If you're writing new API client code or updating existing code,
you should use the core object instead of the rate object.
The core object contains the same information that is present in the rate object.
.EXAMPLE
Get-GitHubRateLimit
#>
[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("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$params = @{
'UriFragment' = 'rate_limit'
'Method' = 'Get'
'Description' = "Getting your API rate limit"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function ConvertFrom-GitHubMarkdown
{
<#
.SYNOPSIS
Converts arbitrary Markdown into HTML.
.DESCRIPTION
Converts arbitrary Markdown into HTML.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Content
The Markdown text to render to HTML. Content must be 400 KB or less.
.PARAMETER Mode
The rendering mode for the Markdown content.
Markdown - Renders Content in plain Markdown, just like README.md files are rendered
GitHubFlavoredMarkdown - Creates links for user mentions as well as references to
SHA-1 hashes, issues, and pull requests.
.PARAMETER Context
The repository to use when creating references in 'githubFlavoredMarkdown' mode.
Specify as [ownerName]/[repositoryName].
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.OUTPUTS
[String] The HTML version of the Markdown content.
.EXAMPLE
ConvertFrom-GitHubMarkdown -Content '**Bolded Text**' -Mode Markdown
Returns back '<p><strong>Bolded Text</strong></p>'
#>
[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("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[ValidateScript({if ([System.Text.Encoding]::UTF8.GetBytes($_).Count -lt 400000) { $true } else { throw "Content must be less than 400 KB." }})]
[string] $Content,
[ValidateSet('Markdown', 'GitHubFlavoredMarkdown')]
[string] $Mode = 'markdown',
[string] $Context,
[string] $AccessToken,
[switch] $NoStatus
)
begin
{
Write-InvocationLog
$telemetryProperties = @{
'Mode' = $Mode
}
$modeConverter = @{
'Markdown' = 'markdown'
'GitHubFlavoredMarkdown' = 'gfm'
}
}
process
{
$hashBody = @{
'text' = $Content
'mode' = $modeConverter[$Mode]
}
if (-not [String]::IsNullOrEmpty($Context)) { $hashBody['context'] = $Context }
$params = @{
'UriFragment' = 'markdown'
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = "Converting Markdown to HTML"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
Write-Output -InputObject (Invoke-GHRestMethod @params)
}
}
function Get-GitHubLicense
{
<#
.SYNOPSIS
Gets a license list or license content from GitHub.
.DESCRIPTION
Gets a license list or license content 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 Name
The name of the license to retrieve the content for. If not specified, all licenses
will be returned.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Get-GitHubLicense
Returns metadata about popular open source licenses
.EXAMPLE
Get-GitHubLicense -Name mit
Gets the content of the mit license file
.EXAMPLE
Get-GitHubLicense -OwnerName Microsoft -RepositoryName PowerShellForGitHub
Gets the content of the license file for the Microsoft\PowerShellForGitHub repository.
It may be necessary to convert the content of the file. Check the 'encoding' property of
the result to know how 'content' is encoded. As an example, to convert from Base64, do
the following:
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($result.content))
#>
[CmdletBinding(SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ParameterSetName='Uri')]
[string] $Uri,
[Parameter(
Mandatory,
ParameterSetName='Individual')]
[string] $Name,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements -DisableValidation
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{}
$uriFragment = 'licenses'
$description = 'Getting all licenses'
if ($PSBoundParameters.ContainsKey('Name'))
{
$telemetryProperties['Name'] = $Name
$uriFragment = "licenses/$Name"
$description = "Getting the $Name license"
}
elseif ((-not [String]::IsNullOrEmpty($OwnerName)) -and (-not [String]::IsNullOrEmpty($RepositoryName)))
{
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "repos/$OwnerName/$RepositoryName/license"
$description = "Getting the license for $RepositoryName"
}
$params = @{
'UriFragment' = $uriFragment
'Method' = 'Get'
'Description' = $description
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function Get-GitHubEmoji
{
<#
.SYNOPSIS
Gets all the emojis available to use on GitHub.
.DESCRIPTION
Gets all the emojis available to use on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.OUTPUTS
[PSCustomObject]
The emoji name and a link to its image.
.EXAMPLE
Get-GitHubEmoji
#>
[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("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$params = @{
'UriFragment' = 'emojis'
'Method' = 'Get'
'Description' = "Getting all GitHub emojis"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function Get-GitHubCodeOfConduct
{
<#
.SYNOPSIS
Gets license or license content from GitHub.
.DESCRIPTION
Gets license or license content 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 Name
The name of the license to retrieve the content for. If not specified, all licenses
will be returned.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Get-GitHubCodeOfConduct
Returns metadata about popular Codes of Conduct
.EXAMPLE
Get-GitHubCodeOfConduct -Name citizen_code_of_conduct
Gets the content of the 'Citizen Code of Conduct'
.EXAMPLE
Get-GitHubCodeOfConduct -OwnerName Microsoft -RepositoryName PowerShellForGitHub
Gets the content of the Code of Conduct file for the Microsoft\PowerShellForGitHub repository
if one is detected.
It may be necessary to convert the content of the file. Check the 'encoding' property of
the result to know how 'content' is encoded. As an example, to convert from Base64, do
the following:
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($result.content))
#>
[CmdletBinding(SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ParameterSetName='Uri')]
[string] $Uri,
[Parameter(
Mandatory,
ParameterSetName='Individual')]
[string] $Name,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements -DisableValidation
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{}
$uriFragment = 'codes_of_conduct'
$description = 'Getting all Codes of Conduct'
if ($PSBoundParameters.ContainsKey('Name'))
{
$telemetryProperties['Name'] = $Name
$uriFragment = "codes_of_conduct/$Name"
$description = "Getting the $Name Code of Conduct"
}
elseif ((-not [String]::IsNullOrEmpty($OwnerName)) -and (-not [String]::IsNullOrEmpty($RepositoryName)))
{
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
$uriFragment = "repos/$OwnerName/$RepositoryName/community/code_of_conduct"
$description = "Getting the Code of Conduct for $RepositoryName"
}
$params = @{
'UriFragment' = $uriFragment
'Method' = 'Get'
'AcceptHeader' = 'application/vnd.github.scarlet-witch-preview+json'
'Description' = $description
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function Get-GitHubGitIgnore
{
<#
.SYNOPSIS
Gets the list of available .gitignore templates, or their content, from GitHub.
.DESCRIPTION
Gets the list of available .gitignore templates, or their content, from GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Name
The name of the .gitignore template whose content should be fetched.
Not providing this will cause a list of all available templates to be returned.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.
.EXAMPLE
Get-GitHubGitIgnore
Returns the list of all available .gitignore templates.
.EXAMPLE
Get-GitHubGitIgnore -Name VisualStudio
Returns the content of the VisualStudio.gitignore template.
#>
[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] $Name,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$telemetryProperties = @{}
$uriFragment = 'gitignore/templates'
$description = 'Getting all gitignore templates'
if ($PSBoundParameters.ContainsKey('Name'))
{
$telemetryProperties['Name'] = $Name
$uriFragment = "gitignore/templates/$Name"
$description = "Getting $Name.gitignore"
}
$params = @{
'UriFragment' = $uriFragment
'Method' = 'Get'
'Description' = $description
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}