From 219104cd174bf57d0ece20c5089bc72653b14d95 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 19 Nov 2018 15:05:40 -0800 Subject: [PATCH 01/20] Initial work --- GitHubComments.ps1 | 577 +++++++++++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 7 + 2 files changed, 584 insertions(+) create mode 100644 GitHubComments.ps1 diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 new file mode 100644 index 00000000..bee8b28e --- /dev/null +++ b/GitHubComments.ps1 @@ -0,0 +1,577 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +function Get-GitHubIssueComment +{ +<# + .DESCRIPTION + Get the commens for an issues in a 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 IssueNumber + Issue number to receive comments for. + + .PARAMETER Since + Only comments updated at or after this time are returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. + + .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-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 + + Get the top 10 referrers over the last 14 days from 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)] + [int] $IssueNumber, + + [string] $Since, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Number' = $IssueNumber + 'Since' = $Since + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$Since" + 'Description' = "Getting comments for issue $IssueNumber in $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} + +function Get-GitHubRepositoryComment +{ +<# + .DESCRIPTION + Get the issues for a given repository in 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 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. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. + + .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-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from 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, + + [ValidateSet('created', 'updated')] + [string] $Sort, + + [ValidateSet('asc', 'desc')] + [string] $Direction, + + [string] $Since, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Number' = $IssueNumber + 'Sort' = $State + 'Direction' = $Sort + 'Since' = $Direction + } + + $getParams = @( + "sort=$Sort", + "direction=$Direction", + "since=$Since" + ) + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments`?" + ($getParams -join '&') + 'Method' = 'Get' + 'Description' = "Getting comments for $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Get-GitHubComment +{ +<# + .DESCRIPTION + Get the issues for a given repository in 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 CommentID + How to sort the results, either created or updated. Default: created + + .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-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from 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, + + [string] $CommentID, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Number' = $IssueNumber + 'CommentID' = $CommentID + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentId" + 'Method' = 'Get' + 'Description' = "Getting comment $CommentID for $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @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 IssueNumber + The number for the issue that the comment will be filed under. + + .PARAMETER Body + The contents of the comment. + + .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-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from 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] $IssueNumber, + + [Parameter(Mandatory)] + [string] $Body, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'IssueNumber' = $IssueNumber + } + + $hashBody = @{ + 'body' = $Body + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments" + 'Body' = ($hashBody | ConvertTo-Json) + 'Method' = 'Post' + 'Description' = "Getting comment $CommentID for $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Edit-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 CommentID + The number for the issue that the comment will be filed under. + + .PARAMETER body + The contents of the comment. + + .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-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from 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, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'CommentID' = $CommentID + } + + $hashBody = @{ + 'body' = $Body + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentID" + 'Body' = ($hashBody | ConvertTo-Json) + 'Method' = 'Patch' + 'Description' = "Editing comment $CommentID for $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -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 number for the issue that the comment will be filed under. + + .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-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from 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, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'CommentID' = $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 -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} \ No newline at end of file diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 098e5aaa..4b68da8b 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -22,6 +22,7 @@ 'GitHubAnalytics.ps1', 'GitHubBranches.ps1', 'GitHubCore.ps1', + 'GitHubComments.ps1', 'GitHubIssues.ps1', 'GitHubLabels.ps1', 'GitHubMiscellaneous.ps1', @@ -42,13 +43,16 @@ FunctionsToExport = @( 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', + 'Edit-GitHubComment', 'ConvertFrom-Markdown', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', + 'Get-GitHubComment', 'Get-GitHubConfiguration', 'Get-GitHubEmoji', 'Get-GitHubGitIgnore', 'Get-GitHubIssue', + 'Get-GitHubIssueComment', 'Get-GitHubIssueTimeline', 'Get-GitHubLabel', 'Get-GitHubLicense', @@ -60,6 +64,7 @@ 'Get-GitHubRepository', 'Get-GitHubRepositoryBranch', 'Get-GitHubRepositoryCollaborator', + 'Get-GitHubRepositoryComment', 'Get-GitHubRepositoryContributor', 'Get-GitHubRepositoryFork', 'Get-GitHubRepositoryLanguage', @@ -76,10 +81,12 @@ 'Invoke-GHRestMethodMultipleResult', 'Lock-GitHubIssue', 'Move-GitHubRepositoryOwnership', + 'New-GitHubComment', 'New-GitHubIssue', 'New-GitHubLabel', 'New-GitHubRepository', 'New-GitHubRepositoryFork', + 'Remove-GitHubComment', 'Remove-GitHubLabel', 'Remove-GitHubRepository', 'Reset-GitHubConfiguration', From 45049f33682d27251ccd331232130904cc321e37 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 19 Nov 2018 16:02:32 -0800 Subject: [PATCH 02/20] add test --- GitHubComments.ps1 | 2 +- Tests/GitHubComments.tests.ps1 | 129 +++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 Tests/GitHubComments.tests.ps1 diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index bee8b28e..e7e94e79 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -98,7 +98,7 @@ function Get-GitHubRepositoryComment { <# .DESCRIPTION - Get the issues for a given repository in Github. + Get the comments for a given repository in Github. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 new file mode 100644 index 00000000..9558f778 --- /dev/null +++ b/Tests/GitHubComments.tests.ps1 @@ -0,0 +1,129 @@ +# 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 +Backup-GitHubConfiguration -Path $configFile +Reset-GitHubConfiguration + +# Define Script-scoped, readonly, hidden variables. + +@{ + defaultIssueTitle = "Test Title" + defaultCommentBody = "Test Body" +}.GetEnumerator() | ForEach-Object { + Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value +} + +Describe 'Creating a new comment' { + $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 -IssueNumber $issue.id -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-GitHubIssueComment -Uri $repo.svn_url -IssueNumber $issue.id) + + 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 getting comments from a repository' + { + $existingComments = @(Get-GitHubRepositoryComment -Uri $repo.svn_url) + + 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 + } + } + + Remove-GitHubRepository -Uri $repo.svn_url +} + +# Restore the user's configuration to its pre-test state +Restore-GitHubConfiguration -Path $configFile From 10e6b1f1e3acbe74db3911a7f8fa44b0e5fa83a3 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 26 Nov 2018 10:04:56 -0800 Subject: [PATCH 03/20] Finish tests and clean up docs --- GitHubComments.ps1 | 28 ++++++++++++------------- Tests/GitHubComments.tests.ps1 | 38 +++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index e7e94e79..bdfe32ab 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -137,7 +137,7 @@ function Get-GitHubRepositoryComment .EXAMPLE Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + Get the comments for the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -206,7 +206,7 @@ function Get-GitHubComment { <# .DESCRIPTION - Get the issues for a given repository in Github. + Get a single comment for a given repository in Github. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -224,7 +224,7 @@ function Get-GitHubComment them individually. .PARAMETER CommentID - How to sort the results, either created or updated. Default: created + The ID of the comment to get. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -237,9 +237,9 @@ function Get-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Get-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + Get a single comment from the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -328,7 +328,7 @@ function New-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 -Body "Testing this API" Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. #> @@ -379,7 +379,7 @@ function New-GitHubComment 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments" 'Body' = ($hashBody | ConvertTo-Json) 'Method' = 'Post' - 'Description' = "Getting comment $CommentID for $RepositoryName" + 'Description' = "Creating comment under issue $IssueNumber for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -393,7 +393,7 @@ function Edit-GitHubComment { <# .DESCRIPTION - Creates a new Github comment in an issue for the given repository + Edits an existing comment in an issue for the given repository The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -411,10 +411,10 @@ function Edit-GitHubComment them individually. .PARAMETER CommentID - The number for the issue that the comment will be filed under. + The comment ID of the comment to edit. .PARAMETER body - The contents of the comment. + The new contents of the comment. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -427,7 +427,7 @@ function Edit-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Edit-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. #> @@ -510,7 +510,7 @@ function Remove-GitHubComment them individually. .PARAMETER CommentID - The number for the issue that the comment will be filed under. + The id of the comment to delete. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -523,9 +523,9 @@ function Remove-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Remove-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + Deletes a Github comment from the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index 9558f778..6941d7d3 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -77,7 +77,8 @@ Reset-GitHubConfiguration @{ defaultIssueTitle = "Test Title" - defaultCommentBody = "Test Body" + 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 } @@ -88,7 +89,7 @@ Describe 'Creating a new comment' { $issue = New-GitHubIssue -Uri $repo.svn_url -Title $defaultIssueTitle Context 'For creating a new comment' { - $newComment = New-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.id -Body $defaultCommentBody + $newComment = New-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.number -Body $defaultCommentBody $existingComment = Get-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id It "Should have the expected body text" { @@ -96,9 +97,8 @@ Describe 'Creating a new comment' { } } - Context 'For getting comments from an issue' - { - $existingComments = @(Get-GitHubIssueComment -Uri $repo.svn_url -IssueNumber $issue.id) + Context 'For getting comments from an issue' { + $existingComments = @(Get-GitHubIssueComment -Uri $repo.svn_url -IssueNumber $issue.number) It 'Should have the expected number of comments' { $existingComments.Count | Should be 1 @@ -109,16 +109,34 @@ Describe 'Creating a new comment' { } } - Context 'For getting comments from a repository' - { + Context 'For editing a comment' { + $newComment = New-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.number -Body $defaultCommentBody + $editedComment = Edit-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-GitHubRepositoryComment -Uri $repo.svn_url) It 'Should have the expected number of comments' { - $existingComments.Count | Should be 1 + $existingComments.Count | Should be 2 } - It 'Should have the expected body text on the first comment' { - $existingComments[0].body | Should be $defaultCommentBody + foreach($comment in $existingComments) { + Remove-GitHubComment -Uri $repo.svn_url -CommentID $comment.id + } + + $existingComments = @(Get-GitHubRepositoryComment -Uri $repo.svn_url) + + It 'Should have no comments' { + $existingComments.Count | Should be 0 } } From 8e11d539a2dee3d96bc29146951b557875d1ed03 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 26 Nov 2018 10:20:04 -0800 Subject: [PATCH 04/20] Adding to usage.md --- GitHubComments.ps1 | 2 +- Tests/GitHubComments.tests.ps1 | 2 +- USAGE.md | 40 +++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index bdfe32ab..f747d509 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -135,7 +135,7 @@ function Get-GitHubRepositoryComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Get-GitHubRepositoryComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Sort created -Direction asc -Since 2011-04-14T16:00:49Z Get the comments for the PowerShell\PowerShellForGitHub project. #> diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index 6941d7d3..713f99b9 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -83,7 +83,7 @@ Reset-GitHubConfiguration Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value } -Describe 'Creating a new comment' { +Describe 'Creating, modifying and deleting comments' { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit $issue = New-GitHubIssue -Uri $repo.svn_url -Title $defaultIssueTitle diff --git a/USAGE.md b/USAGE.md index da6af709..8f14c16e 100644 --- a/USAGE.md +++ b/USAGE.md @@ -30,7 +30,13 @@ * [Get the popular content for a repository](#get-the-popular-content-for-a-repository) * [Get the number of views for a repository](#get-the-number-of-views-for-a-repository) * [Get the number of clones for a repository](#get-the-number-of-clones-for-a-repository) - + * [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 @@ -342,3 +348,35 @@ Get-GitHubViewTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub ```powershell Get-GitHubCloneTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Per 'day' ``` + +### Comments + +#### Get comments from an issue +```powershell +Get-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 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 -IssueNumber 1 -Body "Testing this API" +``` + +#### Editing an existing comment +```powershell +Edit-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" +``` + +#### Removing a comment +```powershell +Remove-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 +``` From 49f7863211d322ec35186c6b3deb5a45c2e25959 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 26 Nov 2018 10:33:46 -0800 Subject: [PATCH 05/20] Few tweaks --- GitHubComments.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index f747d509..ed8785bd 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -273,7 +273,6 @@ function Get-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'Number' = $IssueNumber 'CommentID' = $CommentID } @@ -429,7 +428,7 @@ function Edit-GitHubComment .EXAMPLE Edit-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + Edits an existing comment in an issue for the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, From cf4f95c7525f18547589c78e396bb7a08ae027a7 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 27 Nov 2018 09:53:44 -0800 Subject: [PATCH 06/20] Address PR comments --- GitHubComments.ps1 | 68 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index ed8785bd..c8bed7b8 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -26,7 +26,7 @@ function Get-GitHubIssueComment Issue number to receive comments for. .PARAMETER Since - Only comments updated at or after this time are returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. + Only comments updated at or after this time are returned. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -62,33 +62,37 @@ function Get-GitHubIssueComment [Parameter(Mandatory)] [int] $IssueNumber, - [string] $Since, + [DateTime] $Since, [string] $AccessToken, [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName + if ($Since -ne $null) { + $SinceFormattedTime = $Since.ToUniversalTime().ToString('o') + } + $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'Number' = $IssueNumber - 'Since' = $Since + 'IssueNumber' = $IssueNumber + 'Since' = $SinceFormattedTime } $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$Since" + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$SinceFormattedTime" 'Description' = "Getting comments for issue $IssueNumber in $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethodMultipleResult @params @@ -122,7 +126,7 @@ function Get-GitHubRepositoryComment 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. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. + Only comments updated at or after this time are returned. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -161,32 +165,36 @@ function Get-GitHubRepositoryComment [ValidateSet('asc', 'desc')] [string] $Direction, - [string] $Since, + [DateTime] $Since, [string] $AccessToken, [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName + if ($Since -ne $null) { + $SinceFormattedTime = $Since.ToUniversalTime().ToString('o') + } + $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'Number' = $IssueNumber - 'Sort' = $State - 'Direction' = $Sort - 'Since' = $Direction + 'IssueNumber' = $IssueNumber + 'Sort' = $Sort + 'Direction' = $Direction + 'Since' = $SinceFormattedTime } $getParams = @( "sort=$Sort", "direction=$Direction", - "since=$Since" + "since=$SinceFormattedTime" ) $params = @{ @@ -196,7 +204,7 @@ function Get-GitHubRepositoryComment 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethod @params @@ -264,9 +272,9 @@ function Get-GitHubComment [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -283,7 +291,7 @@ function Get-GitHubComment 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethod @params @@ -358,9 +366,9 @@ function New-GitHubComment [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -382,7 +390,7 @@ function New-GitHubComment 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethod @params @@ -457,9 +465,9 @@ function Edit-GitHubComment [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -481,7 +489,7 @@ function Edit-GitHubComment 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethod @params @@ -550,9 +558,9 @@ function Remove-GitHubComment [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -569,7 +577,7 @@ function Remove-GitHubComment 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } return Invoke-GHRestMethod @params From 977e9b06fd901c0aec458cf313bdc136123a58d3 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 27 Nov 2018 10:17:15 -0800 Subject: [PATCH 07/20] Style fixes --- GitHubComments.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index c8bed7b8..543d9500 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -75,7 +75,8 @@ function Get-GitHubIssueComment $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName - if ($Since -ne $null) { + if ($null -ne $Since) + { $SinceFormattedTime = $Since.ToUniversalTime().ToString('o') } @@ -178,7 +179,8 @@ function Get-GitHubRepositoryComment $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName - if ($Since -ne $null) { + if ($null -ne $Since) + { $SinceFormattedTime = $Since.ToUniversalTime().ToString('o') } From f3949d95febae9c3cb215b54056f9dad74d0d45c Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 28 Nov 2018 16:08:29 -0800 Subject: [PATCH 08/20] Make get comments one method --- GitHubComments.ps1 | 268 +++++++++------------------------------ PowerShellForGitHub.psd1 | 1 + 2 files changed, 62 insertions(+), 207 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 543d9500..6c3b94e0 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -1,11 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -function Get-GitHubIssueComment +function Get-GitHubComment { <# .DESCRIPTION - Get the commens for an issues in a Github repository. + Get the comments for a given Github repository. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -22,103 +22,11 @@ function Get-GitHubIssueComment The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER IssueNumber - Issue number to receive comments for. - - .PARAMETER Since - Only comments updated at or after this time are 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-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 - - Get the top 10 referrers over the last 14 days from 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)] - [int] $IssueNumber, - - [DateTime] $Since, - - [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) - 'IssueNumber' = $IssueNumber - 'Since' = $SinceFormattedTime - } - - $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$SinceFormattedTime" - 'Description' = "Getting comments for issue $IssueNumber in $RepositoryName" - 'AccessToken' = $AccessToken - 'TelemetryEventName' = $MyInvocation.MyCommand.Name - 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) - } - - return Invoke-GHRestMethodMultipleResult @params -} - -function Get-GitHubRepositoryComment -{ -<# - .DESCRIPTION - Get the comments for a given repository in 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 CommentID + The ID of a specific comment to get. If not supplied, will return back all comments for this repository. - .PARAMETER Uri - Uri for the repository. - The OwnerName and RepositoryName will be extracted from here instead of needing to provide - them individually. + .PARAMETER IssueNumber + 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 @@ -140,7 +48,7 @@ function Get-GitHubRepositoryComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubRepositoryComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Sort created -Direction asc -Since 2011-04-14T16:00:49Z + Get-GitHubComment-OwnerName Powershell -RepositoryName PowerShellForGitHub Get the comments for the PowerShell\PowerShellForGitHub project. #> @@ -149,25 +57,45 @@ function Get-GitHubRepositoryComment 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')] + [Parameter(Mandatory, ParameterSetName='RepositoryElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='CommentElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='RepositoryElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='CommentElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [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] $IssueNumber, + + [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, - [DateTime] $Since, - [string] $AccessToken, [switch] $NoStatus @@ -187,116 +115,42 @@ function Get-GitHubRepositoryComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'IssueNumber' = $IssueNumber - 'Sort' = $Sort - 'Direction' = $Direction - 'Since' = $SinceFormattedTime + 'ProvidedIssue' = $PSBoundParameters.ContainsKey('IssueNumber') + 'ProvidedComment' = $PSBoundParameters.ContainsKey('CommentID') } - $getParams = @( - "sort=$Sort", - "direction=$Direction", - "since=$SinceFormattedTime" - ) - - $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments`?" + ($getParams -join '&') - 'Method' = 'Get' - 'Description' = "Getting comments for $RepositoryName" - 'AccessToken' = $AccessToken - 'TelemetryEventName' = $MyInvocation.MyCommand.Name - 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + if ($PSBoundParameters.ContainsKey('CommentID')) + { + $uriFragment = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentId" + $description = "Getting comment $CommentID for $RepositoryName" } - - return Invoke-GHRestMethod @params -} - -function Get-GitHubComment -{ -<# - .DESCRIPTION - Get a single comment for a given repository in 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 CommentID - The ID of the comment to get. - - .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 -CommentID 1 - - Get a single comment from 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, - - [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' = $CommentID + elseif ($PSBoundParameters.ContainsKey('IssueNumber')) + { + $uriFragment = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$SinceFormattedTime" + $description = "Getting comments for issue $IssueNumber in $RepositoryName" + } + else + { + $getParams = @( + "sort=$Sort", + "direction=$Direction", + "since=$SinceFormattedTime" + ) + + $uriFragment = "repos/$OwnerName/$RepositoryName/issues/comments`?" + ($getParams -join '&') + $description = "Getting comments for $RepositoryName" } $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentId" - 'Method' = 'Get' - 'Description' = "Getting comment $CommentID for $RepositoryName" + 'UriFragment' = $uriFragment + 'Description' = $description 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethod @params + return Invoke-GHRestMethodMultipleResult @params } function New-GitHubComment @@ -377,7 +231,7 @@ function New-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'IssueNumber' = $IssueNumber + 'IssueNumber' = (Get-PiiSafeString -PlainText $IssueNumber) } $hashBody = @{ @@ -476,7 +330,7 @@ function Edit-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'CommentID' = $CommentID + 'CommentID' = (Get-PiiSafeString -PlainText $CommentID) } $hashBody = @{ @@ -569,7 +423,7 @@ function Remove-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'CommentID' = $CommentID + 'CommentID' = (Get-PiiSafeString -PlainText $CommentID) } $params = @{ diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 4b68da8b..81ff19a9 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -48,6 +48,7 @@ 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', 'Get-GitHubComment', + 'Get-GitHubCommentList', 'Get-GitHubConfiguration', 'Get-GitHubEmoji', 'Get-GitHubGitIgnore', From b5c74f12440b5ec0cbdc9f16a3c704006f71c6df Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 28 Nov 2018 16:30:47 -0800 Subject: [PATCH 09/20] Do not supply params --- GitHubComments.ps1 | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 6c3b94e0..3e1ff58e 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -54,7 +54,7 @@ function Get-GitHubComment #> [CmdletBinding( SupportsShouldProcess, - DefaultParametersetName='Elements')] + 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')] @@ -126,16 +126,33 @@ function Get-GitHubComment } elseif ($PSBoundParameters.ContainsKey('IssueNumber')) { - $uriFragment = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?since=$SinceFormattedTime" + $uriFragment = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?" + + if ($PSBoundParameters.ContainsKey('Since')) + { + $uriFragment += "since=$SinceFormattedTime" + } + $description = "Getting comments for issue $IssueNumber in $RepositoryName" } else { - $getParams = @( - "sort=$Sort", - "direction=$Direction", - "since=$SinceFormattedTime" - ) + $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" From f76cbf97bad3abc44ed7411abe3457f1a0412e0d Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 29 Nov 2018 09:40:56 -0800 Subject: [PATCH 10/20] PR comments --- GitHubComments.ps1 | 12 ++++++------ Tests/GitHubComments.tests.ps1 | 2 +- USAGE.md | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 3e1ff58e..10ff187e 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -210,7 +210,7 @@ function New-GitHubComment .EXAMPLE New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 -Body "Testing this API" - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + Creates a new Github comment in an issue for the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -269,11 +269,11 @@ function New-GitHubComment return Invoke-GHRestMethod @params } -function Edit-GitHubComment +function Set-GitHubComment { <# .DESCRIPTION - Edits an existing comment in an issue for the given repository + 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 @@ -307,9 +307,9 @@ function Edit-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Edit-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" + Set-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" - Edits an existing comment in an issue for the PowerShell\PowerShellForGitHub project. + Set an existing comment in an issue for the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -358,7 +358,7 @@ function Edit-GitHubComment 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentID" 'Body' = ($hashBody | ConvertTo-Json) 'Method' = 'Patch' - 'Description' = "Editing comment $CommentID for $RepositoryName" + 'Description' = "Set comment $CommentID for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index 713f99b9..14b4fd84 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -111,7 +111,7 @@ Describe 'Creating, modifying and deleting comments' { Context 'For editing a comment' { $newComment = New-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.number -Body $defaultCommentBody - $editedComment = Edit-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id -Body $defaultEditedCommentBody + $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 diff --git a/USAGE.md b/USAGE.md index 8f14c16e..0926d2bf 100644 --- a/USAGE.md +++ b/USAGE.md @@ -373,7 +373,7 @@ New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Iss #### Editing an existing comment ```powershell -Edit-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" +Set-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" ``` #### Removing a comment From fcf6633cee613d5a84969795572f6195646ab6ed Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 29 Nov 2018 10:36:54 -0800 Subject: [PATCH 11/20] Update tests and psd1 --- PowerShellForGitHub.psd1 | 5 +---- Tests/GitHubComments.tests.ps1 | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 81ff19a9..360b0d99 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -43,17 +43,14 @@ FunctionsToExport = @( 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', - 'Edit-GitHubComment', 'ConvertFrom-Markdown', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', 'Get-GitHubComment', - 'Get-GitHubCommentList', 'Get-GitHubConfiguration', 'Get-GitHubEmoji', 'Get-GitHubGitIgnore', 'Get-GitHubIssue', - 'Get-GitHubIssueComment', 'Get-GitHubIssueTimeline', 'Get-GitHubLabel', 'Get-GitHubLicense', @@ -65,7 +62,6 @@ 'Get-GitHubRepository', 'Get-GitHubRepositoryBranch', 'Get-GitHubRepositoryCollaborator', - 'Get-GitHubRepositoryComment', 'Get-GitHubRepositoryContributor', 'Get-GitHubRepositoryFork', 'Get-GitHubRepositoryLanguage', @@ -93,6 +89,7 @@ 'Reset-GitHubConfiguration', 'Restore-GitHubConfiguration', 'Set-GitHubAuthentication', + 'Set-GitHubComment', 'Set-GitHubConfiguration', 'Set-GitHubLabel', 'Set-GitHubRepositoryTopic', diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index 14b4fd84..ebad4c14 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -98,7 +98,7 @@ Describe 'Creating, modifying and deleting comments' { } Context 'For getting comments from an issue' { - $existingComments = @(Get-GitHubIssueComment -Uri $repo.svn_url -IssueNumber $issue.number) + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.number) It 'Should have the expected number of comments' { $existingComments.Count | Should be 1 @@ -123,7 +123,7 @@ Describe 'Creating, modifying and deleting comments' { } Context 'For getting comments from a repository and deleting them' { - $existingComments = @(Get-GitHubRepositoryComment -Uri $repo.svn_url) + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) It 'Should have the expected number of comments' { $existingComments.Count | Should be 2 @@ -133,7 +133,7 @@ Describe 'Creating, modifying and deleting comments' { Remove-GitHubComment -Uri $repo.svn_url -CommentID $comment.id } - $existingComments = @(Get-GitHubRepositoryComment -Uri $repo.svn_url) + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) It 'Should have no comments' { $existingComments.Count | Should be 0 From 7be85528a31303490b69fb4593e13d80b5bca175 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 29 Nov 2018 12:11:38 -0800 Subject: [PATCH 12/20] Support media types --- GitHubComments.ps1 | 119 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 10ff187e..ffeda102 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -37,6 +37,19 @@ function Get-GitHubComment .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 MediaTypeVersion + The version of the response to return. The default version of the API may change in the future. + If you're building an application and care about the stability of the API, be sure to request + a specific version. More info at https://developer.github.com/v3/media/. + .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. @@ -96,6 +109,11 @@ function Get-GitHubComment [ValidateSet('asc', 'desc')] [string] $Direction, + [ValidateSet('raw', 'text', 'html', 'full', '')] + [string] $MediaType, + + [string] $MediaTypeVersion = 'VERSION', + [string] $AccessToken, [switch] $NoStatus @@ -162,6 +180,7 @@ function Get-GitHubComment 'UriFragment' = $uriFragment 'Description' = $description 'AccessToken' = $AccessToken + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -197,6 +216,19 @@ function New-GitHubComment .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 MediaTypeVersion + The version of the response to return. The default version of the API may change in the future. + If you're building an application and care about the stability of the API, be sure to request + a specific version. More info at https://developer.github.com/v3/media/. + .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. @@ -234,6 +266,11 @@ function New-GitHubComment [Parameter(Mandatory)] [string] $Body, + [ValidateSet('raw', 'text', 'html', 'full', '')] + [string] $MediaType, + + [string] $MediaTypeVersion = 'VERSION', + [string] $AccessToken, [switch] $NoStatus @@ -261,6 +298,7 @@ function New-GitHubComment 'Method' = 'Post' 'Description' = "Creating comment under issue $IssueNumber for $RepositoryName" 'AccessToken' = $AccessToken + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -296,6 +334,19 @@ function Set-GitHubComment .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 MediaTypeVersion + The version of the response to return. The default version of the API may change in the future. + If you're building an application and care about the stability of the API, be sure to request + a specific version. More info at https://developer.github.com/v3/media/. + .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. @@ -333,6 +384,11 @@ function Set-GitHubComment [Parameter(Mandatory)] [string] $Body, + [ValidateSet('raw', 'text', 'html', 'full', '')] + [string] $MediaType, + + [string] $MediaTypeVersion = 'VERSION', + [string] $AccessToken, [switch] $NoStatus @@ -360,6 +416,7 @@ function Set-GitHubComment 'Method' = 'Patch' 'Description' = "Set comment $CommentID for $RepositoryName" 'AccessToken' = $AccessToken + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -454,4 +511,66 @@ function Remove-GitHubComment } return Invoke-GHRestMethod @params +} + +function Get-CommentAcceptHeader +{ +<# + .DESCRIPTION + Returns a formatted AcceptHeader based on the requested MediaType and MediaTypeVersion + + 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. + + .PARAMETER MediaTypeVersion + The version of the response to return. The default version of the API may change in the future. + If you're building an application and care about the stability of the API, be sure to request + a specific version. More info at https://developer.github.com/v3/media/. + + .EXAMPLE + Get-CommentAcceptHeader -MediaType raw MediaTypeVersion v3 + + Returns a formatted AcceptHeader for v3 of the response object +#> + [CmdletBinding()] + param( + [ValidateSet('raw', 'text', 'html', 'full', '')] + [string] $MediaType, + + [string] $MediaTypeVersion = 'VERSION' + ) + + $acceptHeaders = @() + + <# If the user has not specified a specific version, add the preview acceptHeader #> + if ('VERSION' -eq $MediaTypeVersion) + { + $acceptHeaders += 'application/vnd.github.squirrel-girl-preview' + } + + if ('raw' -eq $MediaType) + { + $acceptHeaders += "application/vnd.github.$MediaTypeVersion.raw+json" + } + elseif ('text' -eq $MediaType) + { + $acceptHeaders += "application/vnd.github.$MediaTypeVersion.text+json" + } + elseif ('html' -eq $MediaType) + { + $acceptHeaders += "application/vnd.github.$MediaTypeVersion.html+json" + } + elseif ('full' -eq $MediaType) + { + $acceptHeaders += "application/vnd.github.$MediaTypeVersion.full+json" + } + + return ($acceptHeaders -join ',') } \ No newline at end of file From 34b7fd1c96981c9e2bab67f443ed1efc14dc2c49 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 29 Nov 2018 12:17:23 -0800 Subject: [PATCH 13/20] End on an empty line --- GitHubComments.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index ffeda102..891ca442 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -573,4 +573,4 @@ function Get-CommentAcceptHeader } return ($acceptHeaders -join ',') -} \ No newline at end of file +} From 13388e0a8bb573bf106f7f43760ecb0504d7bdeb Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 29 Nov 2018 13:59:40 -0800 Subject: [PATCH 14/20] Set default value instead of empty string validation --- GitHubComments.ps1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 891ca442..ed189b46 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -109,8 +109,8 @@ function Get-GitHubComment [ValidateSet('asc', 'desc')] [string] $Direction, - [ValidateSet('raw', 'text', 'html', 'full', '')] - [string] $MediaType, + [ValidateSet('raw', 'text', 'html', 'full')] + [string] $MediaType ='raw', [string] $MediaTypeVersion = 'VERSION', @@ -266,8 +266,8 @@ function New-GitHubComment [Parameter(Mandatory)] [string] $Body, - [ValidateSet('raw', 'text', 'html', 'full', '')] - [string] $MediaType, + [ValidateSet('raw', 'text', 'html', 'full')] + [string] $MediaType ='raw', [string] $MediaTypeVersion = 'VERSION', @@ -384,8 +384,8 @@ function Set-GitHubComment [Parameter(Mandatory)] [string] $Body, - [ValidateSet('raw', 'text', 'html', 'full', '')] - [string] $MediaType, + [ValidateSet('raw', 'text', 'html', 'full')] + [string] $MediaType ='raw', [string] $MediaTypeVersion = 'VERSION', @@ -541,8 +541,8 @@ function Get-CommentAcceptHeader #> [CmdletBinding()] param( - [ValidateSet('raw', 'text', 'html', 'full', '')] - [string] $MediaType, + [ValidateSet('raw', 'text', 'html', 'full')] + [string] $MediaType ='raw', [string] $MediaTypeVersion = 'VERSION' ) From 348f12b1dfac594ed88c8cce3dcfa5e3d30aaa85 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 10:48:55 -0800 Subject: [PATCH 15/20] PR feedback --- GitHubComments.ps1 | 35 +++++----- PowerShellForGitHub.psd1 | 1 + Tests/GitHubComments.tests.ps1 | 114 +++++++++++++++++---------------- USAGE.md | 4 +- 4 files changed, 81 insertions(+), 73 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index ed189b46..a280b235 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -25,7 +25,7 @@ function Get-GitHubComment .PARAMETER CommentID The ID of a specific comment to get. If not supplied, will return back all comments for this repository. - .PARAMETER IssueNumber + .PARAMETER Issue Issue number to get comments for. If not supplied, will return back all comments for this repository. .PARAMETER Sort @@ -91,7 +91,7 @@ function Get-GitHubComment [Parameter(Mandatory, ParameterSetName='IssueUri')] [Parameter(Mandatory, ParameterSetName='IssueElements')] - [int] $IssueNumber, + [int] $Issue, [Parameter(ParameterSetName='RepositoryUri')] [Parameter(ParameterSetName='RepositoryElements')] @@ -133,7 +133,7 @@ function Get-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'ProvidedIssue' = $PSBoundParameters.ContainsKey('IssueNumber') + 'ProvidedIssue' = $PSBoundParameters.ContainsKey('Issue') 'ProvidedComment' = $PSBoundParameters.ContainsKey('CommentID') } @@ -142,16 +142,16 @@ function Get-GitHubComment $uriFragment = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentId" $description = "Getting comment $CommentID for $RepositoryName" } - elseif ($PSBoundParameters.ContainsKey('IssueNumber')) + elseif ($PSBoundParameters.ContainsKey('Issue')) { - $uriFragment = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments`?" + $uriFragment = "repos/$OwnerName/$RepositoryName/issues/$Issue/comments`?" if ($PSBoundParameters.ContainsKey('Since')) { $uriFragment += "since=$SinceFormattedTime" } - $description = "Getting comments for issue $IssueNumber in $RepositoryName" + $description = "Getting comments for issue $Issue in $RepositoryName" } else { @@ -210,7 +210,7 @@ function New-GitHubComment The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER IssueNumber + .PARAMETER Issue The number for the issue that the comment will be filed under. .PARAMETER Body @@ -240,7 +240,7 @@ function New-GitHubComment If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 -Body "Testing this API" + 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. #> @@ -261,7 +261,7 @@ function New-GitHubComment [string] $Uri, [Parameter(Mandatory)] - [string] $IssueNumber, + [string] $Issue, [Parameter(Mandatory)] [string] $Body, @@ -285,7 +285,7 @@ function New-GitHubComment $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'IssueNumber' = (Get-PiiSafeString -PlainText $IssueNumber) + 'Issue' = (Get-PiiSafeString -PlainText $Issue) } $hashBody = @{ @@ -293,10 +293,10 @@ function New-GitHubComment } $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$IssueNumber/comments" - 'Body' = ($hashBody | ConvertTo-Json) + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/comments" + 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Creating comment under issue $IssueNumber for $RepositoryName" + 'Description' = "Creating comment under issue $Issue for $RepositoryName" 'AccessToken' = $AccessToken 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -331,7 +331,7 @@ function Set-GitHubComment .PARAMETER CommentID The comment ID of the comment to edit. - .PARAMETER body + .PARAMETER Body The new contents of the comment. .PARAMETER MediaType @@ -360,7 +360,7 @@ function Set-GitHubComment .EXAMPLE Set-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1 -Body "Testing this API" - Set an existing comment in an issue for the PowerShell\PowerShellForGitHub project. + Update an existing comment in an issue for the PowerShell\PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -412,9 +412,9 @@ function Set-GitHubComment $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/comments/$CommentID" - 'Body' = ($hashBody | ConvertTo-Json) + 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Patch' - 'Description' = "Set comment $CommentID for $RepositoryName" + 'Description' = "Update comment $CommentID for $RepositoryName" 'AccessToken' = $AccessToken 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -467,6 +467,7 @@ function Remove-GitHubComment [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')] diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 360b0d99..23332119 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -103,6 +103,7 @@ ) AliasesToExport = @( + 'Delete-GitHubComment', 'Delete-GitHubLabel', 'Delete-GitHubRepository', 'Get-GitHubBranch', diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index ebad4c14..51aa4b05 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -70,78 +70,84 @@ if (-not $script:accessTokenConfigured) # 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 -Backup-GitHubConfiguration -Path $configFile -Reset-GitHubConfiguration - -# 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 -} +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 + Describe 'Creating, modifying and deleting comments' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - $issue = New-GitHubIssue -Uri $repo.svn_url -Title $defaultIssueTitle + $issue = New-GitHubIssue -Uri $repo.svn_url -Title $defaultIssueTitle - Context 'For creating a new comment' { - $newComment = New-GitHubComment -Uri $repo.svn_url -IssueNumber $issue.number -Body $defaultCommentBody - $existingComment = Get-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id + 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 + 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 -IssueNumber $issue.number) + 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 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 + 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 -IssueNumber $issue.number -Body $defaultCommentBody - $editedComment = Set-GitHubComment -Uri $repo.svn_url -CommentID $newComment.id -Body $defaultEditedCommentBody + 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 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 + 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) + 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 - } + 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 - } + foreach($comment in $existingComments) { + Remove-GitHubComment -Uri $repo.svn_url -CommentID $comment.id + } - $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) + $existingComments = @(Get-GitHubComment -Uri $repo.svn_url) - It 'Should have no comments' { - $existingComments.Count | Should be 0 + It 'Should have no comments' { + $existingComments.Count | Should be 0 + } } - } - Remove-GitHubRepository -Uri $repo.svn_url + Remove-GitHubRepository -Uri $repo.svn_url + } } - -# Restore the user's configuration to its pre-test state -Restore-GitHubConfiguration -Path $configFile +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 0926d2bf..cb4d8264 100644 --- a/USAGE.md +++ b/USAGE.md @@ -353,7 +353,7 @@ Get-GitHubCloneTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub #### Get comments from an issue ```powershell -Get-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 +Get-GitHubIssueComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Issue 1 ``` #### Get comments from a repository @@ -368,7 +368,7 @@ Get-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Com #### Adding a new comment to an issue ```powershell -New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -IssueNumber 1 -Body "Testing this API" +New-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Issue 1 -Body "Testing this API" ``` #### Editing an existing comment From 35978f18c23a7d77173ef09d0b068432e7762a9f Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 11:05:57 -0800 Subject: [PATCH 16/20] Remove media type version --- GitHubComments.ps1 | 50 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index a280b235..0f5ed63f 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +$script:MediaTypeVersion = 'v3' + function Get-GitHubComment { <# @@ -45,11 +47,6 @@ function Get-GitHubComment 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 MediaTypeVersion - The version of the response to return. The default version of the API may change in the future. - If you're building an application and care about the stability of the API, be sure to request - a specific version. More info at https://developer.github.com/v3/media/. - .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. @@ -112,8 +109,6 @@ function Get-GitHubComment [ValidateSet('raw', 'text', 'html', 'full')] [string] $MediaType ='raw', - [string] $MediaTypeVersion = 'VERSION', - [string] $AccessToken, [switch] $NoStatus @@ -180,7 +175,7 @@ function Get-GitHubComment 'UriFragment' = $uriFragment 'Description' = $description 'AccessToken' = $AccessToken - 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -224,11 +219,6 @@ function New-GitHubComment 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 MediaTypeVersion - The version of the response to return. The default version of the API may change in the future. - If you're building an application and care about the stability of the API, be sure to request - a specific version. More info at https://developer.github.com/v3/media/. - .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. @@ -269,8 +259,6 @@ function New-GitHubComment [ValidateSet('raw', 'text', 'html', 'full')] [string] $MediaType ='raw', - [string] $MediaTypeVersion = 'VERSION', - [string] $AccessToken, [switch] $NoStatus @@ -298,7 +286,7 @@ function New-GitHubComment 'Method' = 'Post' 'Description' = "Creating comment under issue $Issue for $RepositoryName" 'AccessToken' = $AccessToken - 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -342,11 +330,6 @@ function Set-GitHubComment 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 MediaTypeVersion - The version of the response to return. The default version of the API may change in the future. - If you're building an application and care about the stability of the API, be sure to request - a specific version. More info at https://developer.github.com/v3/media/. - .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. @@ -387,8 +370,6 @@ function Set-GitHubComment [ValidateSet('raw', 'text', 'html', 'full')] [string] $MediaType ='raw', - [string] $MediaTypeVersion = 'VERSION', - [string] $AccessToken, [switch] $NoStatus @@ -416,7 +397,7 @@ function Set-GitHubComment 'Method' = 'Patch' 'Description' = "Update comment $CommentID for $RepositoryName" 'AccessToken' = $AccessToken - 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType -MediaTypeVersion $MediaTypeVersion) + 'AcceptHeader' = (Get-CommentAcceptHeader -MediaType $MediaType) 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) @@ -518,7 +499,7 @@ function Get-CommentAcceptHeader { <# .DESCRIPTION - Returns a formatted AcceptHeader based on the requested MediaType and MediaTypeVersion + Returns a formatted AcceptHeader based on the requested MediaType The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -530,31 +511,18 @@ function Get-CommentAcceptHeader 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 MediaTypeVersion - The version of the response to return. The default version of the API may change in the future. - If you're building an application and care about the stability of the API, be sure to request - a specific version. More info at https://developer.github.com/v3/media/. - .EXAMPLE - Get-CommentAcceptHeader -MediaType raw MediaTypeVersion v3 + Get-CommentAcceptHeader -MediaType raw Returns a formatted AcceptHeader for v3 of the response object #> [CmdletBinding()] param( [ValidateSet('raw', 'text', 'html', 'full')] - [string] $MediaType ='raw', - - [string] $MediaTypeVersion = 'VERSION' + [string] $MediaType ='raw' ) - $acceptHeaders = @() - - <# If the user has not specified a specific version, add the preview acceptHeader #> - if ('VERSION' -eq $MediaTypeVersion) - { - $acceptHeaders += 'application/vnd.github.squirrel-girl-preview' - } + $acceptHeaders = @('application/vnd.github.squirrel-girl-preview') if ('raw' -eq $MediaType) { From 2014734e8cc12d6b5732dcaef3760f107eca407d Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 14:30:14 -0800 Subject: [PATCH 17/20] Move varibles and simplify some code --- GitHubComments.ps1 | 23 +++-------------------- GitHubCore.ps1 | 15 ++++++++++----- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 0f5ed63f..95148c50 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -1,8 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -$script:MediaTypeVersion = 'v3' - function Get-GitHubComment { <# @@ -522,24 +520,9 @@ function Get-CommentAcceptHeader [string] $MediaType ='raw' ) - $acceptHeaders = @('application/vnd.github.squirrel-girl-preview') - - if ('raw' -eq $MediaType) - { - $acceptHeaders += "application/vnd.github.$MediaTypeVersion.raw+json" - } - elseif ('text' -eq $MediaType) - { - $acceptHeaders += "application/vnd.github.$MediaTypeVersion.text+json" - } - elseif ('html' -eq $MediaType) - { - $acceptHeaders += "application/vnd.github.$MediaTypeVersion.html+json" - } - elseif ('full' -eq $MediaType) - { - $acceptHeaders += "application/vnd.github.$MediaTypeVersion.full+json" - } + $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 a6f89483..88a68d1a 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 + } function Invoke-GHRestMethod { From b53b270cf27f39a6b1ef3a9acad2ae56ac7506ea Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 14:33:25 -0800 Subject: [PATCH 18/20] lower case mediaTypeVersion --- GitHubComments.ps1 | 2 +- GitHubCore.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 95148c50..2d9c8cbf 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -522,7 +522,7 @@ function Get-CommentAcceptHeader $acceptHeaders = @( 'application/vnd.github.squirrel-girl-preview', - "application/vnd.github.$MediaTypeVersion.$MediaType+json") + "application/vnd.github.$mediaTypeVersion.$MediaType+json") return ($acceptHeaders -join ',') } diff --git a/GitHubCore.ps1 b/GitHubCore.ps1 index 88a68d1a..7f0463cd 100644 --- a/GitHubCore.ps1 +++ b/GitHubCore.ps1 @@ -6,8 +6,8 @@ gitHubApiReposUrl = 'https://api.github.com/repos' gitHubApiOrgsUrl = 'https://api.github.com/orgs' defaultAcceptHeader = 'application/vnd.github.v3+json' - MediaTypeVersion = 'v3' - + mediaTypeVersion = 'v3' + }.GetEnumerator() | ForEach-Object { Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value } From 6cfa2d405e5dd9e543086a2eb899c0a2971d217d Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 14:43:16 -0800 Subject: [PATCH 19/20] Remove trailing whitespace --- GitHubComments.ps1 | 2 +- Tests/GitHubComments.tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index 2d9c8cbf..aff1deae 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -118,7 +118,7 @@ function Get-GitHubComment $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName - if ($null -ne $Since) + if ($null -ne $Since) { $SinceFormattedTime = $Since.ToUniversalTime().ToString('o') } diff --git a/Tests/GitHubComments.tests.ps1 b/Tests/GitHubComments.tests.ps1 index 51aa4b05..c7ec7a34 100644 --- a/Tests/GitHubComments.tests.ps1 +++ b/Tests/GitHubComments.tests.ps1 @@ -146,7 +146,7 @@ try Remove-GitHubRepository -Uri $repo.svn_url } } -finally +finally { # Restore the user's configuration to its pre-test state Restore-GitHubConfiguration -Path $configFile From 1133da3589a477a700590a8103209b4d66d4c9df Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 30 Nov 2018 14:55:26 -0800 Subject: [PATCH 20/20] Commit unsaved usage file --- USAGE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 56734a28..0495d294 100644 --- a/USAGE.md +++ b/USAGE.md @@ -376,10 +376,11 @@ New-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -As #### Remove assignee from an issue ```powershell Remove-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -Assignees $assignees -Issue 1 +``` ---------- -### Comments### Comments +### Comments #### Get comments from an issue ```powershell