Skip to content

Commit e3b6c53

Browse files
joseartriveraHowardWolosky
authored andcommitted
Add ability to specify a MediaType for issues (microsoft#83)
Updates Issues API's to have a configurable MediaType.
1 parent 6d5f889 commit e3b6c53

File tree

5 files changed

+111
-51
lines changed

5 files changed

+111
-51
lines changed

GitHubComments.ps1

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ function Get-GitHubComment
4040
.PARAMETER MediaType
4141
The format in which the API will return the body of the comment.
4242
43-
raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
44-
text - Return a text only representation of the markdown body. Response will include body_text.
45-
html - Return HTML rendered from the body's markdown. Response will include body_html.
46-
full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
43+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
44+
Text - Return a text only representation of the markdown body. Response will include body_text.
45+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
46+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
4747
4848
.PARAMETER AccessToken
4949
If provided, this will be used as the AccessToken for authentication with the
@@ -181,7 +181,7 @@ function Get-GitHubComment
181181
'UriFragment' = $uriFragment
182182
'Description' = $description
183183
'AccessToken' = $AccessToken
184-
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
184+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
185185
'TelemetryEventName' = $MyInvocation.MyCommand.Name
186186
'TelemetryProperties' = $telemetryProperties
187187
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
@@ -220,10 +220,10 @@ function New-GitHubComment
220220
.PARAMETER MediaType
221221
The format in which the API will return the body of the comment.
222222
223-
raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
224-
text - Return a text only representation of the markdown body. Response will include body_text.
225-
html - Return HTML rendered from the body's markdown. Response will include body_html.
226-
full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
223+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
224+
Text - Return a text only representation of the markdown body. Response will include body_text.
225+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
226+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
227227
228228
.PARAMETER AccessToken
229229
If provided, this will be used as the AccessToken for authentication with the
@@ -292,7 +292,7 @@ function New-GitHubComment
292292
'Method' = 'Post'
293293
'Description' = "Creating comment under issue $Issue for $RepositoryName"
294294
'AccessToken' = $AccessToken
295-
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
295+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
296296
'TelemetryEventName' = $MyInvocation.MyCommand.Name
297297
'TelemetryProperties' = $telemetryProperties
298298
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
@@ -331,10 +331,10 @@ function Set-GitHubComment
331331
.PARAMETER MediaType
332332
The format in which the API will return the body of the comment.
333333
334-
raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
335-
text - Return a text only representation of the markdown body. Response will include body_text.
336-
html - Return HTML rendered from the body's markdown. Response will include body_html.
337-
full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
334+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
335+
Text - Return a text only representation of the markdown body. Response will include body_text.
336+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
337+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
338338
339339
.PARAMETER AccessToken
340340
If provided, this will be used as the AccessToken for authentication with the
@@ -403,7 +403,7 @@ function Set-GitHubComment
403403
'Method' = 'Patch'
404404
'Description' = "Update comment $CommentID for $RepositoryName"
405405
'AccessToken' = $AccessToken
406-
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
406+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
407407
'TelemetryEventName' = $MyInvocation.MyCommand.Name
408408
'TelemetryProperties' = $telemetryProperties
409409
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
@@ -501,36 +501,3 @@ function Remove-GitHubComment
501501
return Invoke-GHRestMethod @params
502502
}
503503

504-
function Get-CommentAcceptHeader
505-
{
506-
<#
507-
.DESCRIPTION
508-
Returns a formatted AcceptHeader based on the requested MediaType
509-
510-
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
511-
512-
.PARAMETER MediaType
513-
The format in which the API will return the body of the comment.
514-
515-
raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
516-
text - Return a text only representation of the markdown body. Response will include body_text.
517-
html - Return HTML rendered from the body's markdown. Response will include body_html.
518-
full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
519-
520-
.EXAMPLE
521-
Get-CommentAcceptHeader -MediaType Raw
522-
523-
Returns a formatted AcceptHeader for v3 of the response object
524-
#>
525-
[CmdletBinding()]
526-
param(
527-
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
528-
[string] $MediaType ='Raw'
529-
)
530-
531-
$acceptHeaders = @(
532-
'application/vnd.github.squirrel-girl-preview',
533-
"application/vnd.github.$mediaTypeVersion.$($MediaType.ToLower())+json")
534-
535-
return ($acceptHeaders -join ',')
536-
}

GitHubCore.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
gitHubApiOrgsUrl = 'https://api.github.com/orgs'
88
defaultAcceptHeader = 'application/vnd.github.v3+json'
99
mediaTypeVersion = 'v3'
10+
squirrelAcceptHeader = 'application/vnd.github.squirrel-girl-preview'
11+
symmetraAcceptHeader = 'application/vnd.github.symmetra-preview+json'
1012

1113
}.GetEnumerator() | ForEach-Object {
1214
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
@@ -922,3 +924,44 @@ filter ConvertTo-SmarterObject
922924
Write-Output -InputObject $InputObject
923925
}
924926
}
927+
928+
function Get-MediaAcceptHeader
929+
{
930+
<#
931+
.DESCRIPTION
932+
Returns a formatted AcceptHeader based on the requested MediaType
933+
934+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
935+
936+
.PARAMETER MediaType
937+
The format in which the API will return the body of the comment or issue.
938+
939+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
940+
Text - Return a text only representation of the markdown body. Response will include body_text.
941+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
942+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
943+
944+
.PARAMETER AcceptHeader
945+
The accept header that should be included with the MediaType accept header.
946+
947+
.EXAMPLE
948+
Get-MediaAcceptHeader -MediaType Raw
949+
950+
Returns a formatted AcceptHeader for v3 of the response object
951+
#>
952+
[CmdletBinding()]
953+
param(
954+
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
955+
[string] $MediaType = 'Raw',
956+
957+
[Parameter(Mandatory)]
958+
[string] $AcceptHeader
959+
)
960+
961+
$acceptHeaders = @(
962+
$AcceptHeader,
963+
"application/vnd.github.$mediaTypeVersion.$($MediaType.ToLower())+json")
964+
965+
return ($acceptHeaders -join ',')
966+
}
967+

GitHubIssues.ps1

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ function Get-GitHubIssue
8787
.PARAMETER Mentioned
8888
Only issues that mention this specified user will be returned.
8989
90+
.PARAMETER MediaType
91+
The format in which the API will return the body of the issue.
92+
93+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
94+
Text - Return a text only representation of the markdown body. Response will include body_text.
95+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
96+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
97+
9098
.PARAMETER AccessToken
9199
If provided, this will be used as the AccessToken for authentication with the
92100
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -162,6 +170,9 @@ function Get-GitHubIssue
162170

163171
[string] $Mentioned,
164172

173+
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
174+
[string] $MediaType ='Raw',
175+
165176
[string] $AccessToken,
166177

167178
[switch] $NoStatus
@@ -304,7 +315,7 @@ function Get-GitHubIssue
304315
$params = @{
305316
'UriFragment' = $uriFragment + '?' + ($getParams -join '&')
306317
'Description' = $description
307-
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
318+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
308319
'AccessToken' = $AccessToken
309320
'TelemetryEventName' = $MyInvocation.MyCommand.Name
310321
'TelemetryProperties' = $telemetryProperties
@@ -450,6 +461,14 @@ function New-GitHubIssue
450461
.PARAMETER Label
451462
Label(s) to associate with this issue.
452463
464+
.PARAMETER MediaType
465+
The format in which the API will return the body of the issue.
466+
467+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
468+
Text - Return a text only representation of the markdown body. Response will include body_text.
469+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
470+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
471+
453472
.PARAMETER AccessToken
454473
If provided, this will be used as the AccessToken for authentication with the
455474
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -491,6 +510,9 @@ function New-GitHubIssue
491510

492511
[string[]] $Label,
493512

513+
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
514+
[string] $MediaType ='Raw',
515+
494516
[string] $AccessToken,
495517

496518
[switch] $NoStatus
@@ -521,7 +543,7 @@ function New-GitHubIssue
521543
'Body' = (ConvertTo-Json -InputObject $hashBody)
522544
'Method' = 'Post'
523545
'Description' = "Creating new Issue ""$Title"" on $RepositoryName"
524-
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
546+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
525547
'AccessToken' = $AccessToken
526548
'TelemetryEventName' = $MyInvocation.MyCommand.Name
527549
'TelemetryProperties' = $telemetryProperties
@@ -579,6 +601,14 @@ function Update-GitHubIssue
579601
.PARAMETER State
580602
Modify the current state of the issue.
581603
604+
.PARAMETER MediaType
605+
The format in which the API will return the body of the issue.
606+
607+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
608+
Text - Return a text only representation of the markdown body. Response will include body_text.
609+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
610+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
611+
582612
.PARAMETER AccessToken
583613
If provided, this will be used as the AccessToken for authentication with the
584614
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -624,6 +654,9 @@ function Update-GitHubIssue
624654
[ValidateSet('Open', 'Closed')]
625655
[string] $State,
626656

657+
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
658+
[string] $MediaType ='Raw',
659+
627660
[string] $AccessToken,
628661

629662
[switch] $NoStatus
@@ -661,7 +694,7 @@ function Update-GitHubIssue
661694
'Body' = (ConvertTo-Json -InputObject $hashBody)
662695
'Method' = 'Patch'
663696
'Description' = "Updating Issue #$Issue on $RepositoryName"
664-
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
697+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
665698
'AccessToken' = $AccessToken
666699
'TelemetryEventName' = $MyInvocation.MyCommand.Name
667700
'TelemetryProperties' = $telemetryProperties

Tests/GitHubAnalytics.tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ try
126126
}
127127
}
128128

129+
Context 'When issues are retrieved with a specific MediaTypes' {
130+
$newIssue = New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Title ([guid]::NewGuid()) -Body ([guid]::NewGuid())
131+
132+
$issues = @(Get-GitHubIssue -Uri $repo.svn_url -Issue $newIssue.number -MediaType 'Html')
133+
It 'Should return an issue with body_html' {
134+
$issues[0].body_html | Should not be $null
135+
}
136+
}
137+
129138
$null = Remove-GitHubRepository -Uri ($repo.svn_url)
130139
}
131140

Tests/GitHubComments.tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ try
113113
}
114114
}
115115

116+
Context 'For getting comments from an issue with a specific MediaType' {
117+
$existingComments = @(Get-GitHubComment -Uri $repo.svn_url -Issue $issue.number -MediaType 'Html')
118+
119+
It 'Should have the expected body_html on the first comment' {
120+
$existingComments[0].body_html | Should not be $null
121+
}
122+
}
123+
116124
Context 'For editing a comment' {
117125
$newComment = New-GitHubComment -Uri $repo.svn_url -Issue $issue.number -Body $defaultCommentBody
118126
$editedComment = Set-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id -Body $defaultEditedCommentBody

0 commit comments

Comments
 (0)