Skip to content

Add ability to specify a MediaType for issues #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 15 additions & 48 deletions GitHubComments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function Get-GitHubComment
.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.
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
Expand Down Expand Up @@ -181,7 +181,7 @@ function Get-GitHubComment
'UriFragment' = $uriFragment
'Description' = $description
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
Expand Down Expand Up @@ -220,10 +220,10 @@ function New-GitHubComment
.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.
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
Expand Down Expand Up @@ -292,7 +292,7 @@ function New-GitHubComment
'Method' = 'Post'
'Description' = "Creating comment under issue $Issue for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
Expand Down Expand Up @@ -331,10 +331,10 @@ function Set-GitHubComment
.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.
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
Expand Down Expand Up @@ -403,7 +403,7 @@ function Set-GitHubComment
'Method' = 'Patch'
'Description' = "Update comment $CommentID for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType)
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $squirrelAcceptHeader)
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
Expand Down Expand Up @@ -501,36 +501,3 @@ function Remove-GitHubComment
return Invoke-GHRestMethod @params
}

function Get-CommentAcceptHeader
{
<#
.DESCRIPTION
Returns a formatted AcceptHeader based on the requested MediaType

The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub

.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.

.EXAMPLE
Get-CommentAcceptHeader -MediaType Raw

Returns a formatted AcceptHeader for v3 of the response object
#>
[CmdletBinding()]
param(
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw'
)

$acceptHeaders = @(
'application/vnd.github.squirrel-girl-preview',
"application/vnd.github.$mediaTypeVersion.$($MediaType.ToLower())+json")

return ($acceptHeaders -join ',')
}
43 changes: 43 additions & 0 deletions GitHubCore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
gitHubApiOrgsUrl = 'https://api.github.com/orgs'
defaultAcceptHeader = 'application/vnd.github.v3+json'
mediaTypeVersion = 'v3'
squirrelAcceptHeader = 'application/vnd.github.squirrel-girl-preview'
symmetraAcceptHeader = 'application/vnd.github.symmetra-preview+json'

}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
Expand Down Expand Up @@ -922,3 +924,44 @@ filter ConvertTo-SmarterObject
Write-Output -InputObject $InputObject
}
}

function Get-MediaAcceptHeader
{
<#
.DESCRIPTION
Returns a formatted AcceptHeader based on the requested MediaType

The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub

.PARAMETER MediaType
The format in which the API will return the body of the comment or issue.

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 AcceptHeader
The accept header that should be included with the MediaType accept header.

.EXAMPLE
Get-MediaAcceptHeader -MediaType Raw

Returns a formatted AcceptHeader for v3 of the response object
#>
[CmdletBinding()]
param(
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType = 'Raw',

[Parameter(Mandatory)]
[string] $AcceptHeader
)

$acceptHeaders = @(
$AcceptHeader,
"application/vnd.github.$mediaTypeVersion.$($MediaType.ToLower())+json")

return ($acceptHeaders -join ',')
}

39 changes: 36 additions & 3 deletions GitHubIssues.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ function Get-GitHubIssue
.PARAMETER Mentioned
Only issues that mention this specified user will be returned.

.PARAMETER MediaType
The format in which the API will return the body of the issue.

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.
Expand Down Expand Up @@ -162,6 +170,9 @@ function Get-GitHubIssue

[string] $Mentioned,

[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',

[string] $AccessToken,

[switch] $NoStatus
Expand Down Expand Up @@ -304,7 +315,7 @@ function Get-GitHubIssue
$params = @{
'UriFragment' = $uriFragment + '?' + ($getParams -join '&')
'Description' = $description
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
Expand Down Expand Up @@ -450,6 +461,14 @@ function New-GitHubIssue
.PARAMETER Label
Label(s) to associate with this issue.

.PARAMETER MediaType
The format in which the API will return the body of the issue.

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.
Expand Down Expand Up @@ -491,6 +510,9 @@ function New-GitHubIssue

[string[]] $Label,

[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',

[string] $AccessToken,

[switch] $NoStatus
Expand Down Expand Up @@ -521,7 +543,7 @@ function New-GitHubIssue
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = "Creating new Issue ""$Title"" on $RepositoryName"
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
Expand Down Expand Up @@ -579,6 +601,14 @@ function Update-GitHubIssue
.PARAMETER State
Modify the current state of the issue.

.PARAMETER MediaType
The format in which the API will return the body of the issue.

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.
Expand Down Expand Up @@ -624,6 +654,9 @@ function Update-GitHubIssue
[ValidateSet('Open', 'Closed')]
[string] $State,

[ValidateSet('Raw', 'Text', 'Html', 'Full')]
[string] $MediaType ='Raw',

[string] $AccessToken,

[switch] $NoStatus
Expand Down Expand Up @@ -661,7 +694,7 @@ function Update-GitHubIssue
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Patch'
'Description' = "Updating Issue #$Issue on $RepositoryName"
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader)
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
Expand Down
9 changes: 9 additions & 0 deletions Tests/GitHubAnalytics.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ try
}
}

Context 'When issues are retrieved with a specific MediaTypes' {
$newIssue = New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Title ([guid]::NewGuid()) -Body ([guid]::NewGuid())

$issues = @(Get-GitHubIssue -Uri $repo.svn_url -Issue $newIssue.number -MediaType 'Html')
It 'Should return an issue with body_html' {
$issues[0].body_html | Should not be $null
}
}

$null = Remove-GitHubRepository -Uri ($repo.svn_url)
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/GitHubComments.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ try
}
}

Context 'For getting comments from an issue with a specific MediaType' {
$existingComments = @(Get-GitHubComment -Uri $repo.svn_url -Issue $issue.number -MediaType 'Html')

It 'Should have the expected body_html on the first comment' {
$existingComments[0].body_html | Should not be $null
}
}

Context 'For editing a comment' {
$newComment = New-GitHubComment -Uri $repo.svn_url -Issue $issue.number -Body $defaultCommentBody
$editedComment = Set-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id -Body $defaultEditedCommentBody
Expand Down