From 0f9190aa26a5a88c41c5601710614da94b277dde Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 3 Dec 2018 14:47:44 -0800 Subject: [PATCH 01/17] Support more labels API --- GitHubLabels.ps1 | 171 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 167 insertions(+), 4 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index b66be948..78638cd8 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. @@ -69,6 +75,10 @@ function Get-GitHubLabel [Alias('LabelName')] [string] $Name, + [int] $Issue, + + [int] $Milestone, + [string] $AccessToken, [switch] $NoStatus @@ -85,9 +95,33 @@ function Get-GitHubLabel 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } + 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/$Issue/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 @@ -251,6 +285,9 @@ function Remove-GitHubLabel Name of the label to be deleted. Emoji and codes are supported. For more information, see here: https://www.webpagefx.com/tools/emoji-cheat-sheet/ + .PARAMETER Issue + Issue number to delete the label from. If not provided the label will be deleted from the entire repository. + .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. @@ -287,6 +324,8 @@ function Remove-GitHubLabel [Alias('LabelName')] [string] $Name, + [int] $Issue, + [string] $AccessToken, [switch] $NoStatus @@ -303,10 +342,21 @@ function Remove-GitHubLabel 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } + if ($PSBoundParameters.ContainsKey('Issue')) + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels/$Name" + $description = "Deleting label $Name from issue $Issue in $RepositoryName" + } + else + { + $uriFragment = "repos/$OwnerName/$RepositoryName/labels/$Name" + $description = "Deleting label $Name from $RepositoryName" + } + $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/labels/$Name" + 'UriFragment' = $uriFragment 'Method' = 'Delete' - 'Description' = "Deleting label $Name from $RepositoryName" + 'Description' = $description 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -565,6 +615,119 @@ function Set-GitHubLabel } } +function Add-GitHubLabel +{ +<# + .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 Labels + Array of label names to add to the issue + + .PARAMETER Replace + If supplied, will replace all of the labels on the issue with the provided labels. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + New-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Color BBBBBB + + Creates a new, grey-colored label called "TestLabel" 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, + + [int] $Issue, + + [string[]] $Labels, + + [switch] $Replace, + + [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' = $Labels.Count + } + + $hashBody = @{ + 'labels' = $Labels + } + + if ($Replace) + { + $method = 'Put' + } + else + { + $method = 'Post' + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/labels" + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = $method + 'Description' = "Adding label $Name 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 +} + + # 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. From 59e4f596bdb20714d74638f090271ad9f449f493 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 3 Dec 2018 16:24:17 -0800 Subject: [PATCH 02/17] Add tests --- GitHubLabels.ps1 | 25 +++++++++++++---- PowerShellForGitHub.psd1 | 1 + Tests/GitHubLabels.tests.ps1 | 54 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 78638cd8..48208e13 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -61,22 +61,35 @@ 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, @@ -676,8 +689,10 @@ function Add-GitHubLabel ParameterSetName='Uri')] [string] $Uri, + [Parameter(Mandatory)] [int] $Issue, + [Parameter(Mandatory)] [string[]] $Labels, [switch] $Replace, diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index fb44f43f..644839b6 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -42,6 +42,7 @@ # Functions to export from this module FunctionsToExport = @( + 'Add-GitHubLabel', 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-Markdown', diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 5f7a3dff..3b746535 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -271,6 +271,60 @@ if ($script:accessTokenConfigured) $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } + + Describe 'Adding and removing labels to an issue'{ + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels + + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $script: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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -Label $labelsToAdd) + + It 'Should return the expected number of labels' { + $addedLabels.Count | Should be $script:defaultLabels.Count + } + + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be $script:defaultLabels.Count + } + } + + Context 'Removing labels from an issue' { + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) + } + } + + Context 'Replacing labels on 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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -Label $labelsToAdd -Replace) + + It 'Should return the expected number of labels' { + $addedLabels.Count | Should be $script:defaultLabels.Count + } + + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be $script:defaultLabels.Count + } + } + + } } # Restore the user's configuration to its pre-test state From 670f39c7f9f5aa11b99bbf8bcb396c82c8605bdf Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 4 Dec 2018 11:19:05 -0800 Subject: [PATCH 03/17] Few more tweaks --- GitHubLabels.ps1 | 14 +++++++------- Tests/GitHubLabels.tests.ps1 | 15 ++++++++------- USAGE.md | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 48208e13..538ab6c6 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -115,7 +115,7 @@ function Get-GitHubLabel } elseif ($PSBoundParameters.ContainsKey('Milestone')) { - $uriFragment = "/repos/$OwnerName/$RepositoryName/milestones/$Issue/labels" + $uriFragment = "/repos/$OwnerName/$RepositoryName/milestones/$Milestone/labels" $description = "Getting labels for Milestone $Milestone in $RepositoryName" } else @@ -652,7 +652,7 @@ function Add-GitHubLabel .PARAMETER Issue Issue number to add the label to. - .PARAMETER Labels + .PARAMETER LabelName Array of label names to add to the issue .PARAMETER Replace @@ -669,9 +669,9 @@ function Add-GitHubLabel If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - New-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Color BBBBBB + Add-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $labels - Creates a new, grey-colored label called "TestLabel" in the PowerShellForGitHub project. + Adds labels to an issue in the PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -693,7 +693,7 @@ function Add-GitHubLabel [int] $Issue, [Parameter(Mandatory)] - [string[]] $Labels, + [string[]] $LabelName, [switch] $Replace, @@ -711,11 +711,11 @@ function Add-GitHubLabel $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'labelCount' = $Labels.Count + 'labelCount' = $LabelName.Count } $hashBody = @{ - 'labels' = $Labels + 'labels' = $LabelName } if ($Replace) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 3b746535..e22d4b83 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -283,14 +283,14 @@ if ($script:accessTokenConfigured) 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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -Label $labelsToAdd) + $addedLabels = @(Add-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) It 'Should return the expected number of labels' { $addedLabels.Count | Should be $script:defaultLabels.Count } - + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - + It 'Should have added the expected number of labels' { $labelIssues.Count | Should be $script:defaultLabels.Count } @@ -311,19 +311,20 @@ if ($script:accessTokenConfigured) $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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -Label $labelsToAdd -Replace) + $addedLabels = @(Add-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd -Replace) It 'Should return the expected number of labels' { $addedLabels.Count | Should be $script:defaultLabels.Count } - + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - + It 'Should have added the expected number of labels' { $labelIssues.Count | Should be $script:defaultLabels.Count } } - + + $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } } diff --git a/USAGE.md b/USAGE.md index 0495d294..52607765 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) @@ -270,6 +274,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 @@ -280,6 +294,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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue 1 -LabelName $labelNames +``` + +#### Removing a Label From an Issue +```powershell +Remove-GitHubLabel -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 From aa3fa3f00ad4003b977692db2e44863cc3128cb1 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Thu, 6 Dec 2018 11:07:24 -0800 Subject: [PATCH 04/17] PR Revisions --- GitHubComments.ps1 | 2 +- GitHubIssues.ps1 | 5 +- GitHubLabels.ps1 | 160 ++++++++++++++++++++++++++++++----- Tests/Config/Settings.ps1 | 4 +- Tests/GitHubLabels.tests.ps1 | 44 +++++----- 5 files changed, 165 insertions(+), 50 deletions(-) diff --git a/GitHubComments.ps1 b/GitHubComments.ps1 index aff1deae..6f87890f 100644 --- a/GitHubComments.ps1 +++ b/GitHubComments.ps1 @@ -249,7 +249,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 62767cbc..61fd71da 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 538ab6c6..0a423e3b 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -299,7 +299,10 @@ function Remove-GitHubLabel Emoji and codes are supported. For more information, see here: https://www.webpagefx.com/tools/emoji-cheat-sheet/ .PARAMETER Issue - Issue number to delete the label from. If not provided the label will be deleted from the entire repository. + Issue number to remove the label from. If not provided the label will be deleted from the entire repository. + + .PARAMETER RemoveAll + If supplied, will remove all labels on an issue. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -318,27 +321,38 @@ function Remove-GitHubLabel #> [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.")] [Alias('Delete-GitHubLabel')] param( - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='RepositoryElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='RepositoryElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='RepositoryUri')] + [Parameter(Mandatory, ParameterSetName='IssueUri')] [string] $Uri, - [Parameter(Mandatory)] + [Parameter(Mandatory, ParameterSetName='RepositoryElements')] + [Parameter(Mandatory, ParameterSetName='RepositoryUri')] + [Parameter(ParameterSetName='IssueElements')] + [Parameter(ParameterSetName='IssueUri')] + [ValidateNotNullOrEmpty()] [Alias('LabelName')] [string] $Name, + [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='IssueUri')] [int] $Issue, + [Parameter(ParameterSetName='IssueElements')] + [Parameter(ParameterSetName='IssueUri')] + [switch] $RemoveAll, + [string] $AccessToken, [switch] $NoStatus @@ -357,8 +371,16 @@ function Remove-GitHubLabel if ($PSBoundParameters.ContainsKey('Issue')) { - $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels/$Name" - $description = "Deleting label $Name from issue $Issue in $RepositoryName" + if ($RemoveAll) + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels" + $description = "Deleting all labels from issue $Issue in $RepositoryName" + } + else + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels/$Name" + $description = "Deleting label $Name from issue $Issue in $RepositoryName" + } } else { @@ -693,7 +715,8 @@ function Add-GitHubLabel [int] $Issue, [Parameter(Mandatory)] - [string[]] $LabelName, + [Alias('LabelName')] + [string[]] $Name, [switch] $Replace, @@ -711,27 +734,121 @@ function Add-GitHubLabel $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'labelCount' = $LabelName.Count + 'labelCount' = $Name.Count } $hashBody = @{ - 'labels' = $LabelName + 'labels' = $Name } - if ($Replace) - { - $method = 'Put' + $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) } - else - { - $method = 'Post' + + 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 add the label to. + + .PARAMETER LabelName + 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-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $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(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, + + [switch] $Replace, + + [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' = $method - 'Description' = "Adding label $Name to issue $Issue in $RepositoryName" + 'Method' = 'Put' + 'Description' = "Replacing labels to issue $Issue in $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -742,7 +859,6 @@ function Add-GitHubLabel 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/Tests/Config/Settings.ps1 b/Tests/Config/Settings.ps1 index 701eb7e4..a250e88e 100644 --- a/Tests/Config/Settings.ps1 +++ b/Tests/Config/Settings.ps1 @@ -2,7 +2,7 @@ # Licensed under the MIT License. # The account that the tests will be running under -$script:ownerName = 'PowerShellForGitHubTeam' +$script:ownerName = 'jorivtest' # The organization that the tests will be running under -$script:organizationName = 'PowerShellForGitHubTeamTestOrg' \ No newline at end of file +$script:organizationName = 'jorivorg' \ No newline at end of file diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index e22d4b83..0201a93f 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -272,7 +272,7 @@ if ($script:accessTokenConfigured) $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } - Describe 'Adding and removing labels to an issue'{ + Describe 'Adding labels to an issue'{ $repositoryName = [Guid]::NewGuid().Guid $null = New-GitHubRepository -RepositoryName $repositoryName Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels @@ -295,37 +295,37 @@ if ($script:accessTokenConfigured) $labelIssues.Count | Should be $script:defaultLabels.Count } } + } + Describe 'Removing labels on an issue'{ - Context 'Removing labels from an issue' { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number + Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) - } + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) } + } - Context 'Replacing labels on 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') + Describe 'Replacing labels on 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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd -Replace) + $addedLabels = @(Add-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd -Replace) - It 'Should return the expected number of labels' { - $addedLabels.Count | Should be $script:defaultLabels.Count - } + It 'Should return the expected number of labels' { + $addedLabels.Count | Should be $script:defaultLabels.Count + } - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be $script:defaultLabels.Count - } + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be $script:defaultLabels.Count } - - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } + + $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } # Restore the user's configuration to its pre-test state From d7f456ebbcec89cb79622e57b0a306318ac1172c Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 14:04:37 -0800 Subject: [PATCH 05/17] Seperate functions --- GitHubLabels.ps1 | 174 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 122 insertions(+), 52 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 0a423e3b..4f611843 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -108,6 +108,9 @@ 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" @@ -298,12 +301,6 @@ function Remove-GitHubLabel Name of the label to be deleted. Emoji and codes are supported. For more information, see here: https://www.webpagefx.com/tools/emoji-cheat-sheet/ - .PARAMETER Issue - Issue number to remove the label from. If not provided the label will be deleted from the entire repository. - - .PARAMETER RemoveAll - If supplied, will remove all labels on an 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. @@ -321,38 +318,25 @@ function Remove-GitHubLabel #> [CmdletBinding( SupportsShouldProcess, - DefaultParametersetName='RepositoryElements')] + 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='RepositoryElements')] - [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='Elements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='RepositoryElements')] - [Parameter(Mandatory, ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='Elements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='RepositoryUri')] - [Parameter(Mandatory, ParameterSetName='IssueUri')] + [Parameter(Mandatory, ParameterSetName='Uri')] [string] $Uri, - [Parameter(Mandatory, ParameterSetName='RepositoryElements')] - [Parameter(Mandatory, ParameterSetName='RepositoryUri')] - [Parameter(ParameterSetName='IssueElements')] - [Parameter(ParameterSetName='IssueUri')] + [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='Uri')] [ValidateNotNullOrEmpty()] [Alias('LabelName')] [string] $Name, - [Parameter(Mandatory, ParameterSetName='IssueElements')] - [Parameter(Mandatory, ParameterSetName='IssueUri')] - [int] $Issue, - - [Parameter(ParameterSetName='IssueElements')] - [Parameter(ParameterSetName='IssueUri')] - [switch] $RemoveAll, - [string] $AccessToken, [switch] $NoStatus @@ -369,29 +353,10 @@ function Remove-GitHubLabel 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($PSBoundParameters.ContainsKey('Issue')) - { - if ($RemoveAll) - { - $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels" - $description = "Deleting all labels from issue $Issue in $RepositoryName" - } - else - { - $uriFragment = "/repos/$OwnerName/$RepositoryName/issues/$Issue/labels/$Name" - $description = "Deleting label $Name from issue $Issue in $RepositoryName" - } - } - else - { - $uriFragment = "repos/$OwnerName/$RepositoryName/labels/$Name" - $description = "Deleting label $Name from $RepositoryName" - } - $params = @{ - 'UriFragment' = $uriFragment + 'UriFragment' = "repos/$OwnerName/$RepositoryName/labels/$Name" 'Method' = 'Delete' - 'Description' = $description + 'Description' = "Deleting label $Name from $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -650,7 +615,7 @@ function Set-GitHubLabel } } -function Add-GitHubLabel +function Add-GitHubIssueLabel { <# .DESCRIPTION @@ -674,12 +639,9 @@ function Add-GitHubLabel .PARAMETER Issue Issue number to add the label to. - .PARAMETER LabelName + .PARAMETER Name Array of label names to add to the issue - .PARAMETER Replace - If supplied, will replace all of the labels on the issue with the provided labels. - .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. @@ -691,7 +653,7 @@ function Add-GitHubLabel If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Add-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $labels + Add-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -Name $labels Adds labels to an issue in the PowerShellForGitHub project. #> @@ -859,6 +821,114 @@ function Set-GitHubIssueLabel 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-GitHubLabel -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, + + [switch] $RemoveAll, + + [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. From ea28d4514380a07c675edb03f4ac14c83b7ad124 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 15:30:39 -0800 Subject: [PATCH 06/17] Fix tests --- PowerShellForGitHub.psd1 | 4 ++- Tests/GitHubLabels.tests.ps1 | 54 ++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 644839b6..37dd1bf1 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -42,7 +42,7 @@ # Functions to export from this module FunctionsToExport = @( - 'Add-GitHubLabel', + 'Add-GitHubIssueLabel', 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-Markdown', @@ -89,6 +89,7 @@ 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', 'Remove-GitHubComment', + 'Remove-GitHubIssueLabel', 'Remove-GitHubLabel', 'Remove-GitHubRepository', 'Reset-GitHubConfiguration', @@ -96,6 +97,7 @@ 'Set-GitHubAuthentication', 'Set-GitHubComment', 'Set-GitHubConfiguration', + 'Set-GitHubIssueLabel', 'Set-GitHubLabel', 'Set-GitHubRepositoryTopic', 'Split-GitHubUri', diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 0201a93f..5b2b93ba 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -283,7 +283,7 @@ if ($script:accessTokenConfigured) 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-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) + $addedLabels = @(Add-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) It 'Should return the expected number of labels' { $addedLabels.Count | Should be $script:defaultLabels.Count @@ -295,27 +295,59 @@ if ($script:accessTokenConfigured) $labelIssues.Count | Should be $script:defaultLabels.Count } } + + $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } Describe 'Removing labels on an issue'{ + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repositoryName -Title $issueName - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) + $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 $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd + + Context 'For removing individual issues'{ + Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number + Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number + Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) + } } + + Context 'For removing all issues'{ + Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be 0 + } + } + + $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 $script: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') - $addedLabels = @(Add-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd -Replace) + Add-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName 'pri:medium' + + $addedLabels = @(Set-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) It 'Should return the expected number of labels' { - $addedLabels.Count | Should be $script:defaultLabels.Count + $addedLabels.Count | Should be $labelsToAdd.Count } $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number @@ -323,9 +355,9 @@ if ($script:accessTokenConfigured) It 'Should have added the expected number of labels' { $labelIssues.Count | Should be $script:defaultLabels.Count } + + $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } - - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName } # Restore the user's configuration to its pre-test state From df359e5bad97018706f582b8279fae60ebce5041 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 15:44:38 -0800 Subject: [PATCH 07/17] Update usage and tweak tests --- Tests/GitHubLabels.tests.ps1 | 455 ++++++++++++++++++----------------- USAGE.md | 4 +- 2 files changed, 232 insertions(+), 227 deletions(-) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 5b2b93ba..93c50b0b 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,295 +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 + + $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' + } + ) - Context 'When querying for all labels' { - $labels = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName + if ($script:accessTokenConfigured) + { + Describe 'Getting labels from repository' { + $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 + 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 $script:ownerName -RepositoryName $repositoryName -Name bug + 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" + It 'Should return expected label' { + $label.name | Should be "bug" + } } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } + Describe 'Creating new label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -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 - $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 + } - It 'New label should be created' { - $label.name | Should be $labelName - } + AfterEach { + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + } - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } + Describe 'Removing label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName + Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels - 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 $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName - $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 ($defaultLabels.Count + 1) + } - It 'Should return increased number of labels' { - $($labels).Count | Should be ($script:defaultLabels.Count + 1) - } + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName + $labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName - 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 $defaultLabels.Count + } - It 'Should return expected number of labels' { - $($labels).Count | Should be $script:defaultLabels.Count + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName - } + Describe 'Updating label' { + $repositoryName = [Guid]::NewGuid().Guid + $null = New-GitHubRepository -RepositoryName $repositoryName - Describe 'Updating label' { - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName + $labelName = [Guid]::NewGuid().Guid - $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 - 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 $ownerName -RepositoryName $repositoryName -Name $labelName + } - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $labelName + It 'Label should have different color' { + $label.color | Should be AAAAAA + } } - It 'Label should have different color' { - $label.color | Should be AAAAAA - } - } + 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 - 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 + AfterEach { + Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $newLabelName + } - AfterEach { - Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name $newLabelName + It 'Label should have different color' { + $label | Should not be $null + $label.color | Should be BBBBBB + } } - 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 - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels - $labels = Get-GitHubLabel -OwnerName $script: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" + } - 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" + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - $null = Remove-GitHubRepository -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 - Describe 'Adding labels to an issue'{ - $repositoryName = [Guid]::NewGuid().Guid - $null = New-GitHubRepository -RepositoryName $repositoryName - Set-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $script:defaultLabels + $issueName = [Guid]::NewGuid().Guid + $issue = New-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title $issueName - $issueName = [Guid]::NewGuid().Guid - $issue = New-GitHubIssue -OwnerName $script: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) - 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 $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) - - It 'Should return the expected number of labels' { - $addedLabels.Count | Should be $script:defaultLabels.Count - } + It 'Should return the expected number of labels' { + $addedLabels.Count | Should be $defaultLabels.Count + } - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be $script:defaultLabels.Count + It 'Should have added the expected number of labels' { + $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 - $null = Remove-GitHubRepository -OwnerName $script: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 $script: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 $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd - - Context 'For removing individual issues'{ - Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number - Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number - Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be ($script:defaultLabels.Count - 3) + $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 added the expected number of labels' { + $labelIssues.Count | Should be ($defaultLabels.Count - 3) + } } - } - Context 'For removing all issues'{ - Remove-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + 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 added the expected number of labels' { - $labelIssues.Count | Should be 0 + It 'Should have added the expected number of labels' { + $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 - 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 - $issueName = [Guid]::NewGuid().Guid - $issue = New-GitHubIssue -OwnerName $script: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') - $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' - Add-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName 'pri:medium' + $addedLabels = @(Set-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) - $addedLabels = @(Set-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) + It 'Should return the expected number of labels' { + $addedLabels.Count | Should be $labelsToAdd.Count + } - It 'Should return the expected number of labels' { - $addedLabels.Count | Should be $labelsToAdd.Count - } + $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - $labelIssues = Get-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue $issue.number + It 'Should have added the expected number of labels' { + $labelIssues.Count | Should be $defaultLabels.Count + } - It 'Should have added the expected number of labels' { - $labelIssues.Count | Should be $script:defaultLabels.Count + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - - $null = Remove-GitHubRepository -OwnerName $script: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 52607765..76dc476d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -297,12 +297,12 @@ Remove-GitHubLabel -OwnerName Powershell -RepositoryName desiredstateconfigurati #### Adding Labels to an Issue ```powershell $labelNames = @{'bug', 'discussion') -Add-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue 1 -LabelName $labelNames +Add-GitHubIssueLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Issue 1 -LabelName $labelNames ``` #### Removing a Label From an Issue ```powershell -Remove-GitHubLabel -OwnerName Powershell -RepositoryName desiredstateconfiguration -Name TestLabel -Issue 1 +Remove-GitHubIssueLabel -OwnerName Powershell -RepositoryName desiredstateconfiguration -Name TestLabel -Issue 1 ``` #### Updating a Label With a New Name and Color From 91dac047e7dc87a5bb0b29aaf4a97008c3a035ab Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 15:46:47 -0800 Subject: [PATCH 08/17] Fix trailing white space --- Tests/GitHubLabels.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 93c50b0b..251cc9c8 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -70,7 +70,7 @@ if (-not $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 -try +try { Backup-GitHubConfiguration -Path $configFile Reset-GitHubConfiguration From 1315cbc71d07ee430230e8117932b9fd29286d7f Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 15:51:29 -0800 Subject: [PATCH 09/17] Fix settings --- Tests/Config/Settings.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Config/Settings.ps1 b/Tests/Config/Settings.ps1 index a250e88e..701eb7e4 100644 --- a/Tests/Config/Settings.ps1 +++ b/Tests/Config/Settings.ps1 @@ -2,7 +2,7 @@ # Licensed under the MIT License. # The account that the tests will be running under -$script:ownerName = 'jorivtest' +$script:ownerName = 'PowerShellForGitHubTeam' # The organization that the tests will be running under -$script:organizationName = 'jorivorg' \ No newline at end of file +$script:organizationName = 'PowerShellForGitHubTeamTestOrg' \ No newline at end of file From 755257c21b9bc775fbb81e60c0509318cc90a715 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 15:55:09 -0800 Subject: [PATCH 10/17] Fix comments --- GitHubLabels.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 4f611843..83aec24a 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -653,7 +653,7 @@ function Add-GitHubIssueLabel If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Add-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -Name $labels + Add-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -Name $labels Adds labels to an issue in the PowerShellForGitHub project. #> @@ -756,9 +756,9 @@ function Set-GitHubIssueLabel If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Add-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $labels + Set-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Issue 1 -LabelName $labels - Adds labels to an issue in the PowerShellForGitHub project. + Replaces labels on an issue in the PowerShellForGitHub project. #> [CmdletBinding( SupportsShouldProcess, @@ -860,7 +860,7 @@ function Remove-GitHubIssueLabel If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Remove-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Issue 1 + Remove-GitHubIssueLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Issue 1 Removes the label called "TestLabel" from issue 1 in the PowerShellForGitHub project. #> From d992f1549f41e432b24b9b8091969f131de260b5 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 16:03:15 -0800 Subject: [PATCH 11/17] Undo changes --- GitHubLabels.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 83aec24a..8c3975fb 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -322,17 +322,18 @@ function Remove-GitHubLabel [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')] + [Parameter(ParameterSetName='Elements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(ParameterSetName='Elements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='Uri')] + [Parameter( + Mandatory, + ParameterSetName='Uri')] [string] $Uri, - [Parameter(Mandatory, ParameterSetName='Elements')] - [Parameter(Mandatory, ParameterSetName='Uri')] + [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [Alias('LabelName')] [string] $Name, From 39078451b75d15b67581d53b0545b3da98a99ab4 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Mon, 10 Dec 2018 16:06:22 -0800 Subject: [PATCH 12/17] Fix doc error --- GitHubLabels.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 8c3975fb..6bf7b529 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -741,10 +741,10 @@ function Set-GitHubIssueLabel them individually. .PARAMETER Issue - Issue number to add the label to. + Issue number to replace the labels. .PARAMETER LabelName - Array of label names to add to the issue + 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 From fdc32ee4c82a47901de6119062067087b9e63e40 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 11 Dec 2018 10:08:28 -0800 Subject: [PATCH 13/17] Some more tweaks --- GitHubLabels.ps1 | 4 ++-- Tests/GitHubLabels.tests.ps1 | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index 6bf7b529..eab89648 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -663,10 +663,10 @@ function Add-GitHubIssueLabel 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')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='Elements')] [string] $RepositoryName, [Parameter( diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 251cc9c8..d6cb95cc 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -134,7 +134,7 @@ try } ) - if ($script:accessTokenConfigured) + if ($accessTokenConfigured) { Describe 'Getting labels from repository' { $repositoryName = [Guid]::NewGuid().Guid From edfa795e49c64c9222902939b88a43f5bf5ae42f Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 11 Dec 2018 13:21:48 -0800 Subject: [PATCH 14/17] More descriptive test names --- Tests/GitHubLabels.tests.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index d6cb95cc..3717d4af 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -287,13 +287,13 @@ try '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 expected number of labels' { + 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 have added the expected number of labels' { + It 'Should return the number of labels that were just added from querying the issue again' { $labelIssues.Count | Should be $defaultLabels.Count } } @@ -317,7 +317,7 @@ try Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { + It 'Should have removed three labels from the issue' { $labelIssues.Count | Should be ($defaultLabels.Count - 3) } } @@ -326,7 +326,7 @@ try Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { + It 'Should have removed all labels from the issue' { $labelIssues.Count | Should be 0 } } @@ -348,13 +348,13 @@ try $addedLabels = @(Set-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) - It 'Should return the expected number of labels' { + It 'Should should returned the issue with 14 labels' { $addedLabels.Count | Should be $labelsToAdd.Count } $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should have added the expected number of labels' { + It 'Should should have 14 labels after querying the issue' { $labelIssues.Count | Should be $defaultLabels.Count } From e06e502bfb81003e3046e59c3828c93b8d75b27f Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 11 Dec 2018 13:44:19 -0800 Subject: [PATCH 15/17] Fix sentence --- Tests/GitHubLabels.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index 3717d4af..f1c3471b 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -348,13 +348,13 @@ try $addedLabels = @(Set-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) - It 'Should should returned the issue with 14 labels' { + It 'Should returned the issue with 14 labels' { $addedLabels.Count | Should be $labelsToAdd.Count } $labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number - It 'Should should have 14 labels after querying the issue' { + It 'Should have 14 labels after querying the issue' { $labelIssues.Count | Should be $defaultLabels.Count } From 1d80c9fee2d58123d68001b2f32334f871d260f0 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 11 Dec 2018 13:51:27 -0800 Subject: [PATCH 16/17] Fix sentence again --- Tests/GitHubLabels.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GitHubLabels.tests.ps1 b/Tests/GitHubLabels.tests.ps1 index f1c3471b..aaa8ed9d 100644 --- a/Tests/GitHubLabels.tests.ps1 +++ b/Tests/GitHubLabels.tests.ps1 @@ -348,7 +348,7 @@ try $addedLabels = @(Set-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -LabelName $labelsToAdd) - It 'Should returned the issue with 14 labels' { + It 'Should return the issue with 14 labels' { $addedLabels.Count | Should be $labelsToAdd.Count } From 5a816cded3bbacffd4007789b38d9c7c3caba250 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 12 Dec 2018 09:38:58 -0800 Subject: [PATCH 17/17] PR comments --- GitHubLabels.ps1 | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/GitHubLabels.ps1 b/GitHubLabels.ps1 index eab89648..9488f2e5 100644 --- a/GitHubLabels.ps1 +++ b/GitHubLabels.ps1 @@ -61,9 +61,9 @@ 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(Mandatory,ParameterSetName='Elements')] - [Parameter(Mandatory,ParameterSetName='NameElements')] - [Parameter(Mandatory,ParameterSetName='IssueElements')] + [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='NameElements')] + [Parameter(Mandatory, ParameterSetName='IssueElements')] [Parameter(Mandatory, ParameterSetName='MilestoneElements')] [string] $OwnerName, @@ -681,8 +681,6 @@ function Add-GitHubIssueLabel [Alias('LabelName')] [string[]] $Name, - [switch] $Replace, - [string] $AccessToken, [switch] $NoStatus @@ -697,7 +695,7 @@ function Add-GitHubIssueLabel $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'labelCount' = $Name.Count + 'LabelCount' = $Name.Count } $hashBody = @{ @@ -784,8 +782,6 @@ function Set-GitHubIssueLabel [Alias('LabelName')] [string[]] $Name, - [switch] $Replace, - [string] $AccessToken, [switch] $NoStatus @@ -800,7 +796,7 @@ function Set-GitHubIssueLabel $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'labelCount' = $Name.Count + 'LabelCount' = $Name.Count } $hashBody = @{ @@ -887,8 +883,6 @@ function Remove-GitHubIssueLabel [Alias('LabelName')] [string] $Name, - [switch] $RemoveAll, - [string] $AccessToken, [switch] $NoStatus