diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index dcb5f54..ced5ffb 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -251,7 +251,7 @@ function New-GitHubComment [string] $Uri, [Parameter(Mandatory)] - [string] $Issue, + [int] $Issue, [Parameter(Mandatory)] [string] $Body, diff --git a/GitHubIssues.ps1 b/GitHubIssues.ps1 index 62767cb..61fd71d 100644 --- a/GitHubIssues.ps1 +++ b/GitHubIssues.ps1 @@ -128,7 +128,7 @@ function Get-GitHubIssue [ValidateSet('all', 'ownedAndMember')] [string] $RepositoryType = 'all', - [string] $Issue, + [int] $Issue, [switch] $IgnorePullRequests, @@ -375,8 +375,7 @@ function Get-GitHubIssueTimeline [string] $Uri, [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [string] $Issue, + [int] $Issue, [string] $AccessToken, diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index b66be94..9488f2e 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -29,6 +29,12 @@ function Get-GitHubLabel Name of the specific label to be retieved. If not supplied, all labels will be retrieved. Emoji and codes are supported. For more information, see here: https://www.webpagefx.com/tools/emoji-cheat-sheet/ + .PARAMETER Issue + If provided, will return all of the labels for this particular issue. + + .PARAMETER Milestone + If provided, will return all of the labels for this particular milestone. + .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. @@ -55,20 +61,37 @@ function Get-GitHubLabel 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='Elements')] + [Parameter(Mandatory, ParameterSetName='NameElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='MilestoneElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='NameElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='MilestoneElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='NameUri')] + [Parameter(Mandatory, ParameterSetName='IssueUri')] + [Parameter(Mandatory, ParameterSetName='MilestoneUri')] [string] $Uri, + [Parameter(Mandatory, ParameterSetName='NameUri')] + [Parameter(Mandatory, ParameterSetName='NameElements')] [Alias('LabelName')] [string] $Name, + [Parameter(Mandatory, ParameterSetName='IssueUri')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [int] $Issue, + + [Parameter(Mandatory, ParameterSetName='MilestoneUri')] + [Parameter(Mandatory, ParameterSetName='MilestoneElements')] + [int] $Milestone, + [string] $AccessToken, [switch] $NoStatus @@ -85,9 +108,36 @@ function Get-GitHubLabel 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } + $uriFragment = [String]::Empty + $description = [String]::Empty + + if ($PSBoundParameters.ContainsKey('Issue')) + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels" + $description = "Getting labels for Issue $Issue in $RepositoryName" + } + elseif ($PSBoundParameters.ContainsKey('Milestone')) + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/milestones/$Milestone/labels" + $description = "Getting labels for Milestone $Milestone in $RepositoryName" + } + else + { + $uriFragment = "repos/$OwnerName/$RepositoryName/labels/$Name" + + if ($PSBoundParameters.ContainsKey('Name')) + { + $description = "Getting label $Name for $RepositoryName" + } + else + { + $description = "Getting labels for $RepositoryName" + } + } + $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/labels/$Name" - 'Description' = "Getting all labels for $RepositoryName" + 'UriFragment' = $uriFragment + 'Description' = $description 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -284,6 +334,7 @@ function Remove-GitHubLabel [string] $Uri, [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] [Alias('LabelName')] [string] $Name, @@ -565,6 +616,314 @@ function Set-GitHubLabel } } +function Add-GitHubIssueLabel +{ +<# + .DESCRIPTION + Adds a label to an issue in the given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Issue + Issue number to add the label to. + + .PARAMETER Name + Array of label names to add to the issue + + .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 + Add-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -Name $labels + + Adds labels to an issue in the 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(Mandatory, ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(Mandatory, ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [int] $Issue, + + [Parameter(Mandatory)] + [Alias('LabelName')] + [string[]] $Name, + + [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) + 'LabelCount' = $Name.Count + } + + $hashBody = @{ + 'labels' = $Name + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/labels" + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = 'Post' + 'Description' = "Adding labels to issue $Issue in $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Set-GitHubIssueLabel +{ +<# + .DESCRIPTION + Replaces labels on an issue in the given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Issue + Issue number to replace the labels. + + .PARAMETER LabelName + Array of label names that will be set on the issue. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Set-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $labels + + Replaces labels on an issue in the 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] $Issue, + + [Parameter(Mandatory)] + [Alias('LabelName')] + [string[]] $Name, + + [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) + 'LabelCount' = $Name.Count + } + + $hashBody = @{ + 'labels' = $Name + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/labels" + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = 'Put' + 'Description' = "Replacing labels to issue $Issue in $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Remove-GitHubIssueLabel +{ +<# + .DESCRIPTION + Deletes a label from an issue in the given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Issue + Issue number to remove the label from. + + .PARAMETER Name + Name of the label to be deleted. If not provided, will delete all labels on the issue. + Emoji and codes are supported. For more information, see here: https://www.webpagefx.com/tools/emoji-cheat-sheet/ + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Remove-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Issue 1 + + Removes the label called "TestLabel" from issue 1 in the 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.")] + [Alias('Delete-GitHubLabel')] + param( + [Parameter(Mandatory, ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(Mandatory, ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter(Mandatory, ParameterSetName='Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [int] $Issue, + + [ValidateNotNullOrEmpty()] + [Alias('LabelName')] + [string] $Name, + + [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) + } + + $description = [String]::Empty + + if ($PSBoundParameters.ContainsKey('Name')) + { + $description = "Deleting label $Name from issue $Issue in $RepositoryName" + } + else + { + $description = "Deleting all labels from issue $Issue in $RepositoryName" + } + + $params = @{ + 'UriFragment' = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels/$Name" + 'Method' = 'Delete' + 'Description' = $description + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + # A set of labels that a project might want to initially populate their repository with # Used by Set-GitHubLabel when no Label list is provided by the user. # This list exists to support v0.1.0 users. diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 772ecd6..982e05f 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -44,6 +44,7 @@ # Functions to export from this module FunctionsToExport = @( + 'Add-GitHubIssueLabel', 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-Markdown', @@ -93,6 +94,7 @@ 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', 'Remove-GitHubComment', + 'Remove-GitHubIssueLabel', 'Remove-GitHubLabel', 'Remove-GitHubMilestone', 'Remove-GitHubRepository', @@ -101,6 +103,7 @@ 'Set-GitHubAuthentication', 'Set-GitHubComment', 'Set-GitHubConfiguration', + 'Set-GitHubIssueLabel', 'Set-GitHubLabel', 'Set-GitHubMilestone', 'Set-GitHubRepositoryTopic', diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 5f7a3df..aaa8ed9 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -58,8 +58,8 @@ function Initialize-AppVeyor Initialize-AppVeyor -$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured -if (-not $script:accessTokenConfigured) +$accessTokenConfigured = Test-GitHubAuthenticationConfigured +if (-not $accessTokenConfigured) { $message = @( 'GitHub API Token not defined, some of the tests will be skipped.', @@ -70,208 +70,300 @@ 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 - -$script:defaultLabels = @( - @{ - 'name' = 'pri:lowest' - 'color' = '4285F4' - }, - @{ - 'name' = 'pri:low' - 'color' = '4285F4' - }, - @{ - 'name' = 'pri:medium' - 'color' = '4285F4' - }, - @{ - 'name' = 'pri:high' - 'color' = '4285F4' - }, - @{ - 'name' = 'pri:highest' - 'color' = '4285F4' - }, - @{ - 'name' = 'bug' - 'color' = 'fc2929' - }, - @{ - 'name' = 'duplicate' - 'color' = 'cccccc' - }, - @{ - 'name' = 'enhancement' - 'color' = '121459' - }, - @{ - 'name' = 'up for grabs' - 'color' = '159818' - }, - @{ - 'name' = 'question' - 'color' = 'cc317c' - }, - @{ - 'name' = 'discussion' - 'color' = 'fe9a3d' - }, - @{ - 'name' = 'wontfix' - 'color' = 'dcb39c' - }, - @{ - 'name' = 'in progress' - 'color' = 'f0d218' - }, - @{ - 'name' = 'ready' - 'color' = '145912' - } -) - -if ($script:accessTokenConfigured) +try { - Describe 'Getting labels from repository' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels + Backup-GitHubConfiguration -Path $configFile + Reset-GitHubConfiguration - Context 'When querying for all labels' { - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName + $defaultLabels = @( + @{ + 'name' = 'pri:lowest' + 'color' = '4285F4' + }, + @{ + 'name' = 'pri:low' + 'color' = '4285F4' + }, + @{ + 'name' = 'pri:medium' + 'color' = '4285F4' + }, + @{ + 'name' = 'pri:high' + 'color' = '4285F4' + }, + @{ + 'name' = 'pri:highest' + 'color' = '4285F4' + }, + @{ + 'name' = 'bug' + 'color' = 'fc2929' + }, + @{ + 'name' = 'duplicate' + 'color' = 'cccccc' + }, + @{ + 'name' = 'enhancement' + 'color' = '121459' + }, + @{ + 'name' = 'up for grabs' + 'color' = '159818' + }, + @{ + 'name' = 'question' + 'color' = 'cc317c' + }, + @{ + 'name' = 'discussion' + 'color' = 'fe9a3d' + }, + @{ + 'name' = 'wontfix' + 'color' = 'dcb39c' + }, + @{ + 'name' = 'in progress' + 'color' = 'f0d218' + }, + @{ + 'name' = 'ready' + 'color' = '145912' + } + ) + + if ($accessTokenConfigured) + { + Describe 'Getting labels from repository' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels + + Context 'When querying for all labels' { + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName + + It 'Should return expected number of labels' { + $($labels).Count | Should be $:defaultLabels.Count + } + } + + Context 'When querying for specific label' { + $label = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name bug + + It 'Should return expected label' { + $label.name | Should be "bug" + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Creating new label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + + $labelName = [Guid]::NewGuid().Guid + New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + $label = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + + It 'New label should be created' { + $label.name | Should be $labelName + } + + AfterEach { + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Removing label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels + + $labelName = [Guid]::NewGuid().Guid + New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName + + It 'Should return increased number of labels' { + $($labels).Count | Should be ($defaultLabels.Count + 1) + } + + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName It 'Should return expected number of labels' { - $($labels).Count | Should be $script:defaultLabels.Count - } - } - - Context 'When querying for specific label' { - $label = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name bug - - It 'Should return expected label' { - $label.name | Should be "bug" - } - } - - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } - - Describe 'Creating new label' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName - - $labelName = [Guid]::NewGuid().Guid - New-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB - $label = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName - - It 'New label should be created' { - $label.name | Should be $labelName - } - - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName - } - - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } - - Describe 'Removing label' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels - - $labelName = [Guid]::NewGuid().Guid - New-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName - - It 'Should return increased number of labels' { - $($labels).Count | Should be ($script:defaultLabels.Count + 1) - } - - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName - - It 'Should return expected number of labels' { - $($labels).Count | Should be $script:defaultLabels.Count - } - - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } - - Describe 'Updating label' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName - - $labelName = [Guid]::NewGuid().Guid - - Context 'Updating label color' { - New-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB - Update-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -NewName $labelName -Color AAAAAA - $label = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName - - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName + $($labels).Count | Should be $defaultLabels.Count } - It 'Label should have different color' { - $label.color | Should be AAAAAA - } + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Context 'Updating label name' { - $newLabelName = $labelName + "2" - New-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB - Update-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -NewName $newLabelName -Color BBBBBB - $label = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $newLabelName + Describe 'Updating label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $newLabelName + $labelName = [Guid]::NewGuid().Guid + + Context 'Updating label color' { + New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + Update-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -NewName $labelName -Color AAAAAA + $label = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + + AfterEach { + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + } + + It 'Label should have different color' { + $label.color | Should be AAAAAA + } } - It 'Label should have different color' { - $label | Should not be $null - $label.color | Should be BBBBBB + Context 'Updating label name' { + $newLabelName = $labelName + "2" + New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + Update-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -NewName $newLabelName -Color BBBBBB + $label = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $newLabelName + + AfterEach { + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $newLabelName + } + + It 'Label should have different color' { + $label | Should not be $null + $label.color | Should be BBBBBB + } } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } + Describe 'Applying set of labels on repository' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName - Describe 'Applying set of labels on repository' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName + $labelName = [Guid]::NewGuid().Guid + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels - $labelName = [Guid]::NewGuid().Guid - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels + # Add new label + New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName - # Add new label - New-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName + # Change color of existing label + Update-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "bug" -NewName "bug" -Color BBBBBB - # Change color of existing label - Update-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -NewName "bug" -Color BBBBBB + # Remove one of approved labels" + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "discussion" - # Remove one of approved labels" - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" + It 'Should return increased number of labels' { + $($labels).Count | Should be ($defaultLabels.Count + 1) + } - It 'Should return increased number of labels' { - $($labels).Count | Should be ($script:defaultLabels.Count + 1) + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName + + It 'Should return expected number of labels' { + $($labels).Count | Should be $defaultLabels.Count + $bugLabel = $labels | Where-Object {$_.name -eq "bug"} + $bugLabel.color | Should be "fc2929" + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName + Describe 'Adding labels to an issue'{ + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels - It 'Should return expected number of labels' { - $($labels).Count | Should be $script:defaultLabels.Count - $bugLabel = $labels | Where-Object {$_.name -eq "bug"} - $bugLabel.color | Should be "fc2929" + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title $issueName + + Context 'Adding labels to an issue' { + $labelsToAdd = @('pri:lowest', 'pri:low', 'pri:medium', 'pri:high', 'pri:highest', 'bug', 'duplicate', + 'enhancement', 'up for grabs', 'question', 'discussion', 'wontfix', 'in progress', 'ready') + $addedLabels = @(Add-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) + + It 'Should return the number of labels that were just added' { + $addedLabels.Count | Should be $defaultLabels.Count + } + + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should return the number of labels that were just added from querying the issue again' { + $labelIssues.Count | Should be $defaultLabels.Count + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + Describe 'Removing labels on an issue'{ + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title $issueName + + $labelsToAdd = @('pri:lowest', 'pri:low', 'pri:medium', 'pri:high', 'pri:highest', 'bug', 'duplicate', + 'enhancement', 'up for grabs', 'question', 'discussion', 'wontfix', 'in progress', 'ready') + Add-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd + + Context 'For removing individual issues'{ + Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number + Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number + Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have removed three labels from the issue' { + $labelIssues.Count | Should be ($defaultLabels.Count - 3) + } + } + + Context 'For removing all issues'{ + Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have removed all labels from the issue' { + $labelIssues.Count | Should be 0 + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName + Describe 'Replacing labels on an issue'{ + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title $issueName + + $labelsToAdd = @('pri:lowest', 'pri:low', 'pri:medium', 'pri:high', 'pri:highest', 'bug', 'duplicate', + 'enhancement', 'up for grabs', 'question', 'discussion', 'wontfix', 'in progress', 'ready') + + Add-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName 'pri:medium' + + $addedLabels = @(Set-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) + + It 'Should return the issue with 14 labels' { + $addedLabels.Count | Should be $labelsToAdd.Count + } + + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have 14 labels after querying the issue' { + $labelIssues.Count | Should be $defaultLabels.Count + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } } } - -# 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 +} diff --git a/USAGE.md b/USAGE.md index 0435de0..28fd75e 100644 --- a/USAGE.md +++ b/USAGE.md @@ -13,8 +13,12 @@ * [Quering Team and Organization Membership](#querying-team-and-organization-membership) * [Labels](#labels) * [Getting Labels for a Repository](#getting-labels-for-a-repository) + * [Getting Labels for an issue](#getting-labels-for-an-issue) + * [Getting Labels for a milestone](#getting-labels-for-a-milestone) * [Adding a New Label to a Repository](#adding-a-new-label-to-a-repository) * [Removing a Label From a Repository](#removing-a-label-from-a-repository) + * [Adding Labels to an Issue](#adding-labels-to-an-issue) + * [Removing a Label From an Issue](#removing-a-label-from-an-issue) * [Updating a Label With a New Name and Color](#updating-a-label-with-a-new-name-and-color) * [Bulk Updating Labels in a Repository](#bulk-updating-labels-in-a-repository) * [Users](#users) @@ -281,6 +285,16 @@ $teamMembers = Get-GitHubTeamMembers -OrganizationName 'OrganizationName' -TeamN $labels = Get-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration ``` +#### Getting Labels for an Issue +```powershell +$labels = Get-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration -Issue 1 +``` + +#### Getting Labels for a Milestone +```powershell +$labels = Get-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration -Milestone 1 +``` + #### Adding a New Label to a Repository ```powershell New-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration -Name TestLabel -Color BBBBBB @@ -291,6 +305,17 @@ New-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration Remove-GitHubLabel -OwnerName Powershell -RepositoryName desiredstateconfiguration -Name TestLabel ``` +#### Adding Labels to an Issue +```powershell +$labelNames = @{'bug', 'discussion') +Add-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue 1 -LabelName $labelNames +``` + +#### Removing a Label From an Issue +```powershell +Remove-GitHubIssueLabel -OwnerName Powershell -RepositoryName desiredstateconfiguration -Name TestLabel -Issue 1 +``` + #### Updating a Label With a New Name and Color ```powershell Update-GitHubLabel -OwnerName Powershell -RepositoryName DesiredStateConfiguration -Name TestLabel -NewName NewTestLabel -Color BBBB00