forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubComments.ps1
503 lines (390 loc) · 18.4 KB
/
GitHubComments.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Get-GitHubComment
{
<#
.DESCRIPTION
Get the comments for a given Github repository.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER CommentID
The ID of a specific comment to get. If not supplied, will return back all comments for this repository.
.PARAMETER Issue
Issue number to get comments for. If not supplied, will return back all comments for this repository.
.PARAMETER Sort
How to sort the results.
.PARAMETER Direction
How to list the results. Ignored without the sort parameter.
.PARAMETER Since
Only comments updated at or after this time are returned.
.PARAMETER MediaType
The format in which the API will return the body of the comment.
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
Text - Return a text only representation of the markdown body. Response will include body_text.
Html - Return HTML rendered from the body's markdown. Response will include body_html.
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
.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-GitHubComment-OwnerName Microsoft -RepositoryName PowerShellForGitHub
Get the comments for the Microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='RepositoryElements')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(Mandatory, ParameterSetName='RepositoryElements')]
[Parameter(Mandatory, ParameterSetName='IssueElements')]
[Parameter(Mandatory, ParameterSetName='CommentElements')]
[string] $OwnerName,
[Parameter(Mandatory, ParameterSetName='RepositoryElements')]
[Parameter(Mandatory, ParameterSetName='IssueElements')]
[Parameter(Mandatory, ParameterSetName='CommentElements')]
[string] $RepositoryName,
[Parameter(Mandatory, ParameterSetName='RepositoryUri')]
[Parameter(Mandatory, ParameterSetName='IssueUri')]
[Parameter(Mandatory, ParameterSetName='CommentUri')]
[string] $Uri,
[Parameter(Mandatory, ParameterSetName='CommentUri')]
[Parameter(Mandatory, ParameterSetName='CommentElements')]
[string] $CommentID,
[Parameter(Mandatory, ParameterSetName='IssueUri')]
[Parameter(Mandatory, ParameterSetName='IssueElements')]
[int64] $Issue,
[Parameter(ParameterSetName='RepositoryUri')]
[Parameter(ParameterSetName='RepositoryElements')]
[Parameter(ParameterSetName='IssueElements')]
[Parameter(ParameterSetName='IssueUri')]
[DateTime] $Since,
[Parameter(ParameterSetName='RepositoryUri')]
[Parameter(ParameterSetName='RepositoryElements')]
[ValidateSet('Created', 'Updated')]
[string] $Sort,
[Parameter(ParameterSetName='RepositoryUri')]
[Parameter(ParameterSetName='RepositoryElements')]
[ValidateSet('Ascending', 'Descending')]
[string] $Direction,
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$uriFragment = [String]::Empty
$description = [String]::Empty
$sinceFormattedTime = [String]::Empty
if ($null -ne $Since)
{
$sinceFormattedTime = $Since.ToUniversalTime().ToString('o')
}
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'ProvidedIssue' = $PSBoundParameters.ContainsKey('Issue')
'ProvidedComment' = $PSBoundParameters.ContainsKey('CommentID')
}
if ($PSBoundParameters.ContainsKey('CommentID'))
{
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentId"
$description = "Getting comment $CommentID for $RepositoryName"
}
elseif ($PSBoundParameters.ContainsKey('Issue'))
{
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/$Issue/comments`?"
if ($PSBoundParameters.ContainsKey('Since'))
{
$uriFragment += "since=$sinceFormattedTime"
}
$description = "Getting comments for issue $Issue in $RepositoryName"
}
else
{
$getParams = @()
if ($PSBoundParameters.ContainsKey('Sort'))
{
$getParams += "sort=$($Sort.ToLower())"
}
if ($PSBoundParameters.ContainsKey('Direction'))
{
$directionConverter = @{
'Ascending' = 'asc'
'Descending' = 'desc'
}
$getParams += "direction=$($directionConverter[$Direction])"
}
if ($PSBoundParameters.ContainsKey('Since'))
{
$getParams += "since=$sinceFormattedTime"
}
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/comments`?" + ($getParams -join '&')
$description = "Getting comments for $RepositoryName"
}
$params = @{
'UriFragment' = $uriFragment
'Description' = $description
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AsJson -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethodMultipleResult @params
}
function New-GitHubComment
{
<#
.DESCRIPTION
Creates a new Github comment in an issue for the given repository
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER Issue
The number for the issue that the comment will be filed under.
.PARAMETER Body
The contents of the comment.
.PARAMETER MediaType
The format in which the API will return the body of the comment.
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
Text - Return a text only representation of the markdown body. Response will include body_text.
Html - Return HTML rendered from the body's markdown. Response will include body_html.
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
.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
New-GitHubComment -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Issue 1 -Body "Testing this API"
Creates a new Github comment in an issue for the Microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[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)]
[int64] $Issue,
[Parameter(Mandatory)]
[string] $Body,
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'Issue' = (Get-PiiSafeString -PlainText $Issue)
}
$hashBody = @{
'body' = $Body
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/comments"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = "Creating comment under issue $Issue for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AsJson -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function Set-GitHubComment
{
<#
.DESCRIPTION
Set an existing comment in an issue for the given repository
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER CommentID
The comment ID of the comment to edit.
.PARAMETER Body
The new contents of the comment.
.PARAMETER MediaType
The format in which the API will return the body of the comment.
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
Text - Return a text only representation of the markdown body. Response will include body_text.
Html - Return HTML rendered from the body's markdown. Response will include body_html.
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
.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
Set-GitHubComment -OwnerName Microsoft -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API"
Update an existing comment in an issue for the Microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[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)]
[string] $CommentID,
[Parameter(Mandatory)]
[string] $Body,
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'CommentID' = (Get-PiiSafeString -PlainText $CommentID)
}
$hashBody = @{
'body' = $Body
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentID"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Patch'
'Description' = "Update comment $CommentID for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AsJson -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}
function Remove-GitHubComment
{
<#
.DESCRIPTION
Deletes a Github comment for the given repository
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OwnerName
Owner of the repository.
If not supplied here, the DefaultOwnerName configuration property value will be used.
.PARAMETER RepositoryName
Name of the repository.
If not supplied here, the DefaultRepositoryName configuration property value will be used.
.PARAMETER Uri
Uri for the repository.
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER CommentID
The id of the comment to delete.
.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
Remove-GitHubComment -OwnerName Microsoft -RepositoryName PowerShellForGitHub -CommentID 1
Deletes a Github comment from the Microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[Alias('Delete-GitHubComment')]
[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)]
[string] $CommentID,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'CommentID' = (Get-PiiSafeString -PlainText $CommentID)
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentID"
'Method' = 'Delete'
'Description' = "Removing comment $CommentID for $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}