Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for issue labels #59

Merged
merged 18 commits into from
Dec 13, 2018
Merged
2 changes: 1 addition & 1 deletion GitHubComments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function New-GitHubComment
[string] $Uri,

[Parameter(Mandatory)]
[string] $Issue,
[int] $Issue,

[Parameter(Mandatory)]
[string] $Body,
Expand Down
5 changes: 2 additions & 3 deletions GitHubIssues.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function Get-GitHubIssue
[ValidateSet('all', 'ownedAndMember')]
[string] $RepositoryType = 'all',

[string] $Issue,
[int] $Issue,

[switch] $IgnorePullRequests,

Expand Down Expand Up @@ -375,8 +375,7 @@ function Get-GitHubIssueTimeline
[string] $Uri,

[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $Issue,
[int] $Issue,

[string] $AccessToken,

Expand Down
264 changes: 225 additions & 39 deletions GitHubLabels.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -298,9 +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 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.
Expand All @@ -322,23 +322,21 @@ 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(ParameterSetName='Elements')]
[Parameter(Mandatory, ParameterSetName='Elements')]
[string] $OwnerName,

[Parameter(ParameterSetName='Elements')]
[Parameter(Mandatory, ParameterSetName='Elements')]
[string] $RepositoryName,

[Parameter(
Mandatory,
ParameterSetName='Uri')]
[Parameter(Mandatory, ParameterSetName='Uri')]
[string] $Uri,

[Parameter(Mandatory)]
[Parameter(Mandatory, ParameterSetName='Elements')]
[Parameter(Mandatory, ParameterSetName='Uri')]
[ValidateNotNullOrEmpty()]
[Alias('LabelName')]
[string] $Name,

[int] $Issue,

[string] $AccessToken,

[switch] $NoStatus
Expand All @@ -355,21 +353,10 @@ 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' = $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
Expand Down Expand Up @@ -628,7 +615,7 @@ function Set-GitHubLabel
}
}

function Add-GitHubLabel
function Add-GitHubIssueLabel
{
<#
.DESCRIPTION
Expand All @@ -652,11 +639,111 @@ 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.

.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 -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(ParameterSetName='Elements')]
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved
[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,
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved

[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
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved
}

$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 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
Expand Down Expand Up @@ -693,7 +780,8 @@ function Add-GitHubLabel
[int] $Issue,

[Parameter(Mandatory)]
[string[]] $LabelName,
[Alias('LabelName')]
[string[]] $Name,

[switch] $Replace,
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -711,27 +799,126 @@ function Add-GitHubLabel
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'labelCount' = $LabelName.Count
'labelCount' = $Name.Count
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved
}

$hashBody = @{
'labels' = $LabelName
'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-GitHubLabel -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Name TestLabel -Issue 1
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved

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,
joseartrivera marked this conversation as resolved.
Show resolved Hide resolved

[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)
}

if ($Replace)
$description = [String]::Empty

if ($PSBoundParameters.ContainsKey('Name'))
{
$method = 'Put'
$description = "Deleting label $Name from issue $Issue in $RepositoryName"
}
else
{
$method = 'Post'
$description = "Deleting all labels from issue $Issue in $RepositoryName"
}

$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/labels"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = $method
'Description' = "Adding label $Name to issue $Issue in $RepositoryName"
'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
Expand All @@ -742,7 +929,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.
Expand Down
Loading