Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/classes/public/Issues/GitHubLabel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class GitHubLabel : GitHubNode {
# The name of the label
# Example: "bug"
[string] $Name

# The repository where the label is
[string] $Repository

# The owner of the repository
[string] $Owner

# The description of the label
# Example: "Something isn't working"
[string] $Description

# The color of the label (hex code without #)
# Example: "d73a4a"
[string] $Color

# Whether this is a default label
# Example: true
[bool] $IsDefault

# The API URL of the label
# Example: "https://api.github.com/repos/octocat/Hello-World/labels/bug"
[string] $Url

# Constructor from API response
GitHubLabel(
[object] $data
) {
$this.ID = $data.id
$this.NodeID = $data.node_id
$this.Name = $data.name
$this.Description = $data.description
$this.Color = $data.color
$this.IsDefault = $data.default
$this.Url = $data.url

# Parse owner and repository from URL
# URL format: https://api.github.com/repos/{owner}/{repo}/labels/{name}
if ($data.url -match 'repos/([^/]+)/([^/]+)/labels') {
$this.Owner = $matches[1]
$this.Repository = $matches[2]
}
}
}
51 changes: 51 additions & 0 deletions src/formats/GitHubLabel.Format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>GitHubLabelTable</Name>
<ViewSelectedBy>
<TypeName>GitHubLabel</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Description</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Color</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>IsDefault</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>
if ($Host.UI.SupportsVirtualTerminal) {
$PSStyle.FormatHyperlink($_.Name, $_.Url)
} else {
$_.Name
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Description</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Color</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>IsDefault</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
70 changes: 70 additions & 0 deletions src/functions/private/Issues/Labels/Get-GitHubLabelByName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
filter Get-GitHubLabelByName {
<#
.SYNOPSIS
Get a label

.DESCRIPTION
Gets a label by name.

.EXAMPLE
Get-GitHubLabelByName -Owner 'octocat' -Repository 'hello-world' -Name 'bug' -Context $context

Gets the label with the name 'bug' for the repository 'hello-world' owned by 'octocat'.

.INPUTS
None

.OUTPUTS
GitHubLabel

.NOTES
[Get a label](https://docs.github.com/rest/issues/labels#get-a-label)
#>
[OutputType([GitHubLabel])]
[CmdletBinding()]
param(
# The account owner of the repository. The name is not case sensitive.
[Parameter(Mandatory)]
[string] $Owner,

# The name of the repository without the .git extension. The name is not case sensitive.
[Parameter(Mandatory)]
[string] $Repository,

# The name of the label.
[Parameter(Mandatory)]
[string] $Name,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter(Mandatory)]
[object] $Context
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}

process {
$apiParams = @{
Method = 'GET'
APIEndpoint = "/repos/$Owner/$Repository/labels/$([uri]::EscapeDataString($Name))"
Context = $Context
}

try {
Invoke-GitHubAPI @apiParams | ForEach-Object {
[GitHubLabel]::new($_.Response)
}
} catch {
Write-Error $_.Exception.Message
return
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
71 changes: 71 additions & 0 deletions src/functions/private/Issues/Labels/Get-GitHubLabelList.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
filter Get-GitHubLabelList {
<#
.SYNOPSIS
List labels for a repository

.DESCRIPTION
Lists all labels for a repository.

.EXAMPLE
Get-GitHubLabelList -Owner 'octocat' -Repository 'hello-world' -Context $context

Lists all labels for the repository 'hello-world' owned by 'octocat'.

.INPUTS
None

.OUTPUTS
GitHubLabel

.NOTES
[List labels for a repository](https://docs.github.com/rest/issues/labels#list-labels-for-a-repository)
#>
[OutputType([GitHubLabel])]
[CmdletBinding()]
param(
# The account owner of the repository. The name is not case sensitive.
[Parameter(Mandatory)]
[string] $Owner,

# The name of the repository without the .git extension. The name is not case sensitive.
[Parameter(Mandatory)]
[string] $Repository,

# The number of results per page (max 100).
[Parameter()]
[System.Nullable[int]] $PerPage,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter(Mandatory)]
[object] $Context
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}

process {
$apiParams = @{
Method = 'GET'
APIEndpoint = "/repos/$Owner/$Repository/labels"
Context = $Context
}

if ($PerPage) {
$apiParams['QueryParameters'] = @{
per_page = $PerPage
}
}

Invoke-GitHubAPI @apiParams | ForEach-Object {
[GitHubLabel]::new($_.Response)
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
81 changes: 81 additions & 0 deletions src/functions/public/Issues/Labels/Add-GitHubIssueLabel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
filter Add-GitHubIssueLabel {
<#
.SYNOPSIS
Add labels to an issue

.DESCRIPTION
Adds labels to an issue.

.EXAMPLE
Add-GitHubIssueLabel -Owner 'octocat' -Repository 'hello-world' -IssueNumber 1 -Label 'bug', 'enhancement'

Adds the labels 'bug' and 'enhancement' to issue #1 in the repository 'hello-world' owned by 'octocat'.

.INPUTS
None

.OUTPUTS
GitHubLabel

.LINK
https://psmodule.io/GitHub/Functions/Issues/Add-GitHubIssueLabel/

.NOTES
[Add labels to an issue](https://docs.github.com/rest/issues/labels#add-labels-to-an-issue)
#>
[OutputType([GitHubLabel])]
[CmdletBinding(SupportsShouldProcess)]
param(
# The account owner of the repository. The name is not case sensitive.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias('Organization', 'User')]
[string] $Owner,

# The name of the repository without the .git extension. The name is not case sensitive.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string] $Repository,

# The number that identifies the issue.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[int] $IssueNumber,

# The names of the labels to add to the issue.
[Parameter(Mandatory)]
[string[]] $Label,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
[object] $Context
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}

process {
if ($PSCmdlet.ShouldProcess("Issue #$IssueNumber in repository '$Owner/$Repository'", "Add labels: $($Label -join ', ')")) {
$body = @{
labels = $Label
}

$apiParams = @{
Method = 'POST'
APIEndpoint = "/repos/$Owner/$Repository/issues/$IssueNumber/labels"
Body = $body
Context = $Context
}

Invoke-GitHubAPI @apiParams | ForEach-Object {
[GitHubLabel]::new($_.Response)
}
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
Loading