From 28b314bd7c0a810848e1acb3df43a1d83291be7b Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 15:00:22 -0800 Subject: [PATCH] Add support for comment APIs (#53) Add support for the [Issue Comments](https://developer.github.com/v3/issues/comments/) API's. --- GitHubComments.ps1 | 528 +++++++++++++++++++++++++++++++++ GitHubCore.ps1 | 15 +- PowerShellForGitHub.psd1 | 6 + Tests/GitHubComments.tests.ps1 | 153 ++++++++++ USAGE.md | 42 ++- 5 files changed, 738 insertions(+), 6 deletions(-) create mode 100644 GitHubComments.ps1 create mode 100644 Tests/GitHubComments.tests.ps1 diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 new file mode 100644 index 00000000..aff1deae --- /dev/null +++ b/GitHubComments.ps1 @@ -0,0 +1,528 @@ +# 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, either created or updated. Default: created + + .PARAMETER Direction + How to list the results, either asc or desc. 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 Powershell -RepositoryName PowerShellForGitHub + + Get the comments for the PowerShell\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')] + [int] $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('asc', 'desc')] + [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 + + 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" + } + + if ($PSBoundParameters.ContainsKey('Direction')) + { + $getParams += "direction=$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-CommentAcceptHeader -MediaType $MediaType) + '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 Powershell -RepositoryName PowerShellForGitHub -Issue 1 -Body "Testing this API" + + Creates a new Github comment in an issue for the PowerShell\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] $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-CommentAcceptHeader -MediaType $MediaType) + '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 Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" + + Update an existing comment in an issue for the PowerShell\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-CommentAcceptHeader -MediaType $MediaType) + '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 Powershell -RepositoryName PowerShellForGitHub -CommentID 1 + + Deletes a Github comment from the PowerShell\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 +} + +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+json") + + return ($acceptHeaders -join ',') +} diff --git a/GitHubCore.ps1 b/GitHubCore.ps1 index d682c2ac..2b1d5668 100644 --- a/GitHubCore.ps1 +++ b/GitHubCore.ps1 @@ -1,11 +1,16 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -$script:gitHubApiUrl = "https://api.github.com" -$script:gitHubApiReposUrl = "https://api.github.com/repos" -$script:gitHubApiOrgsUrl = "https://api.github.com/orgs" - -$script:defaultAcceptHeader = 'application/vnd.github.v3+json' +@{ + gitHubApiUrl = 'https://api.github.com' + gitHubApiReposUrl = 'https://api.github.com/repos' + gitHubApiOrgsUrl = 'https://api.github.com/orgs' + defaultAcceptHeader = 'application/vnd.github.v3+json' + mediaTypeVersion = 'v3' + + }.GetEnumerator() | ForEach-Object { + Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value + } Set-Variable -Scope Script -Option ReadOnly -Name ValidBodyContainingRequestMethods -Value ('post', 'patch', 'put', 'delete') diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index b26c3c7b..344dda33 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -23,6 +23,7 @@ 'GitHubAssignees.ps1', 'GitHubBranches.ps1', 'GitHubCore.ps1', + 'GitHubComments.ps1', 'GitHubIssues.ps1', 'GitHubLabels.ps1', 'GitHubMiscellaneous.ps1', @@ -47,6 +48,7 @@ 'Get-GitHubAssignee', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', + 'Get-GitHubComment', 'Get-GitHubConfiguration', 'Get-GitHubEmoji', 'Get-GitHubGitIgnore', @@ -79,16 +81,19 @@ 'Lock-GitHubIssue', 'Move-GitHubRepositoryOwnership', 'New-GithubAssignee', + 'New-GitHubComment', 'New-GitHubIssue', 'New-GitHubLabel', 'New-GitHubRepository', 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', + 'Remove-GitHubComment', 'Remove-GitHubLabel', 'Remove-GitHubRepository', 'Reset-GitHubConfiguration', 'Restore-GitHubConfiguration', 'Set-GitHubAuthentication', + 'Set-GitHubComment', 'Set-GitHubConfiguration', 'Set-GitHubLabel', 'Set-GitHubRepositoryTopic', @@ -103,6 +108,7 @@ ) AliasesToExport = @( + 'Delete-GitHubComment', 'Delete-GitHubLabel', 'Delete-GitHubRepository', 'Get-GitHubBranch', diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 new file mode 100644 index 00000000..c7ec7a34 --- /dev/null +++ b/Tests/GitHubComments.tests.ps1 @@ -0,0 +1,153 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Tests for GitHubComments.ps1 module +#> + +[String] $root = Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) +. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') +Import-Module -Name $root -Force + +function Initialize-AppVeyor +{ +<# + .SYNOPSIS + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + .DESCRIPTION + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .NOTES + Internal-only helper method. + + The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, + which can only be applied to functions. + + We call this immediately after the declaration so that AppVeyor initialization can heppen + (if applicable). + +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] + param() + + if ($env:AppVeyor) + { + $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force + $cred = New-Object System.Management.Automation.PSCredential "", $secureString + Set-GitHubAuthentication -Credential $cred + + $script:ownerName = $env:avOwnerName + $script:organizationName = $env:avOrganizationName + + $message = @( + 'This run is executed in the AppVeyor environment.', + 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.', + 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', + 'and run tests on your machine first.') + Write-Warning -Message ($message -join [Environment]::NewLine) + } +} + +Initialize-AppVeyor + +$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured +if (-not $script:accessTokenConfigured) +{ + $message = @( + 'GitHub API Token not defined, some of the tests will be skipped.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.') + Write-Warning -Message ($message -join [Environment]::NewLine) +} + +# Backup the user's configuration before we begin, and ensure we're at a pure state before running +# the tests. We'll restore it at the end. +$configFile = New-TemporaryFile +try +{ + Backup-GitHubConfiguration -Path $configFile + Reset-GitHubConfiguration + Set-GitHubConfiguration -DisableTelemetry # We don't want UT's to impact telemetry + + # Define Script-scoped, readonly, hidden variables. + + @{ + defaultIssueTitle = "Test Title" + defaultCommentBody = "This is a test body." + defaultEditedCommentBody = "This is an edited test body." + }.GetEnumerator() | ForEach-Object { + Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value + } + + Describe 'Creating, modifying and deleting comments' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + $issue = New-GitHubIssue -Uri $repo.svn_url -Title $defaultIssueTitle + + Context 'For creating a new comment' { + $newComment = New-GitHubComment -Uri $repo.svn_url -Issue $issue.number -Body $defaultCommentBody + $existingComment = Get-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id + + It "Should have the expected body text" { + $existingComment.body | Should be $defaultCommentBody + } + } + + Context 'For getting comments from an issue' { + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url -Issue $issue.number) + + It 'Should have the expected number of comments' { + $existingComments.Count | Should be 1 + } + + It 'Should have the expected body text on the first comment' { + $existingComments[0].body | Should be $defaultCommentBody + } + } + + 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 + + It 'Should have a body that is not equal to the original body' { + $editedComment.body | Should not be $newComment.Body + } + + It 'Should have the edited content' { + $editedComment.body | Should be $defaultEditedCommentBody + } + } + + Context 'For getting comments from a repository and deleting them' { + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) + + It 'Should have the expected number of comments' { + $existingComments.Count | Should be 2 + } + + foreach($comment in $existingComments) { + Remove-GitHubComment -Uri $repo.svn_url -CommentID $comment.id + } + + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) + + It 'Should have no comments' { + $existingComments.Count | Should be 0 + } + } + + Remove-GitHubRepository -Uri $repo.svn_url + } +} +finally +{ + # Restore the user's configuration to its pre-test state + Restore-GitHubConfiguration -Path $configFile +} \ No newline at end of file diff --git a/USAGE.md b/USAGE.md index 9e4d7e93..0495d294 100644 --- a/USAGE.md +++ b/USAGE.md @@ -35,7 +35,13 @@ * [Check assignee permission](#check-assignee-permission) * [Add assignee to an issue](#add-assignee-to-an-issue) * [Remove assignee from an issue](#remove-assignee-from-an-issue) - + * [Comments](#comments) + * [Get comments from an issue](#get-comments-from-an-issue) + * [Get comments from a repository](#get-comments-from-a-repository) + * [Get a single comment](#get-a-single-comment) + * [Adding a new comment to an issue](#adding-a-new-comment-to-an-issue) + * [Editing an existing comment](#editing-an-existing-comment) + * [Removing a comment](#removing-a-comment) ---------- ## Logging @@ -371,3 +377,37 @@ New-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -As ```powershell Remove-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -Assignees $assignees -Issue 1 ``` + +---------- + +### Comments + +#### Get comments from an issue +```powershell +Get-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Issue 1 +``` + +#### Get comments from a repository +```powershell +Get-GitHubRepositoryComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Sort created -Direction asc -Since '2011-04-14T16:00:49Z' +``` + +#### Get a single comment +```powershell +Get-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 +``` + +#### Adding a new comment to an issue +```powershell +New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Issue 1 -Body "Testing this API" +``` + +#### Editing an existing comment +```powershell +Set-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" +``` + +#### Removing a comment +```powershell +Remove-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 +```