Skip to content

Commit

Permalink
Add some functionality around issues and todos
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Oct 18, 2024
1 parent 19f3f3a commit f810841
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 36 deletions.
37 changes: 37 additions & 0 deletions src/GitlabCli/Formats.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,43 @@
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>GitlabTodo</Name>
<ViewSelectedBy>
<TypeName>Gitlab.Todo</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader/>
<TableColumnHeader/>
<TableColumnHeader/>
<TableColumnHeader/>
<TableColumnHeader/>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Id</PropertyName>
<Alignment>Right</Alignment>
</TableColumnItem>
<TableColumnItem>
<PropertyName>State</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Type</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Url</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>UpdatedAt</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>GitlabTopic</Name>
<ViewSelectedBy>
Expand Down
12 changes: 10 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '1.119.4'
ModuleVersion = '1.120.0'

RequiredModules = @('powershell-yaml')

Expand Down Expand Up @@ -27,7 +27,10 @@
ExternalModuleDependencies = @('powershell-yaml')
ReleaseNotes =
@'
* bugfix: get job cmdlet parameter group confusion
* feature: wrap TODO apis; allow clearing TODO as part of creation (https://github.com/chris-peterson/pwsh-gitlab/issues/56)
* enhance: better preview for issue cmdlets
* enhance: allow a project to have a trailing slash
* enhance: Get-GitlabProject no longer supports WhatIf
'@
}
}
Expand Down Expand Up @@ -72,6 +75,7 @@
'Releases.psm1'
'Runners.psm1'
'Search.psm1'
'Todos.psm1'
'Topics.psm1'
'Users.psm1'
'Utilities.psm1'
Expand Down Expand Up @@ -242,6 +246,10 @@
'Search-Gitlab'
'Search-GitlabProject'

# Todo
'Get-GitlabTodo'
'Clear-GitlabTodo'

# Topics
'Get-GitlabTopic'
'New-GitlabTopic'
Expand Down
58 changes: 32 additions & 26 deletions src/GitlabCli/Issues.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -92,39 +92,47 @@ function Get-GitlabIssue {
Sort-Object SortKey
}

# https://docs.gitlab.com/ee/api/issues.html#new-issue
function New-GitlabIssue {
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$false)]
[Parameter()]
[string]
$ProjectId = '.',

[Parameter(Position=0, Mandatory=$true)]
[Parameter(Position=0, Mandatory)]
[string]
$Title,

[Parameter(Position=1, Mandatory=$false)]
[Parameter(Position=1)]
[string]
$Description,

[Parameter(Mandatory=$false)]
[string]
$SiteUrl,

[Parameter()]
[Alias('NoTodo')]
[switch]
[Parameter(Mandatory=$false)]
$WhatIf
)
$MarkTodoAsRead,

$ProjectId = $(Get-GitlabProject -ProjectId $ProjectId).Id
[Parameter()]
[string]
$SiteUrl
)

Invoke-GitlabApi POST "projects/$ProjectId/issues" -Body @{
$Project = Get-GitlabProject -ProjectId $ProjectId
$Request = @{
title = $Title
description = $Description
assignee_id = $(Get-GitlabUser -Me).Id
} -SiteUrl $SiteUrl -WhatIf:$WhatIf |
New-WrapperObject 'Gitlab.Issue'
}

if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "Create new issue ($($Request | ConvertTo-Json))")) {
# https://docs.gitlab.com/ee/api/issues.html#new-issue
$Issue = Invoke-GitlabApi POST "projects/$($Project.Id)/issues" -Body $Request -SiteUrl $SiteUrl | New-WrapperObject 'Gitlab.Issue'
if ($MarkTodoAsRead) {
$Todo = Get-GitlabTodo -SiteUrl $SiteUrl | Where-Object TargetUrl -eq $Issue.WebUrl
Clear-GitlabTodo -TodoId $Todo.Id -SiteUrl $SiteUrl | Out-Null
}
$Issue
}
}

# https://docs.gitlab.com/ee/api/issues.html#edit-issue
Expand Down Expand Up @@ -281,24 +289,22 @@ function Open-GitlabIssue {
}

function Close-GitlabIssue {
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$ProjectId = '.',

[Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Parameter(Position=0, Mandatory, ValueFromPipelineByPropertyName)]
[string]
$IssueId,

[Parameter(Mandatory=$false)]
[Parameter()]
[string]
$SiteUrl,

[switch]
[Parameter(Mandatory=$false)]
$WhatIf
$SiteUrl
)

Update-GitlabIssue -ProjectId $ProjectId $IssueId -StateEvent 'close' -SiteUrl $SiteUrl -WhatIf:$WhatIf
if ($PSCmdlet.ShouldProcess("issue #$IssueId", "close")) {
Update-GitlabIssue -ProjectId $ProjectId -IssueId $IssueId -StateEvent 'close' -SiteUrl $SiteUrl
}
}
14 changes: 6 additions & 8 deletions src/GitlabCli/Projects.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ function Get-GitlabProject {

[Parameter()]
[string]
$SiteUrl,

[switch]
[Parameter()]
$WhatIf
$SiteUrl
)

$ProjectId = $ProjectId.TrimEnd('/')

$MaxPages = Get-GitlabMaxPages -MaxPages $MaxPages -All:$All
$Projects = @()
switch ($PSCmdlet.ParameterSetName) {
Expand All @@ -131,7 +129,7 @@ function Get-GitlabProject {
throw "Could not infer project based on current directory ($(Get-Location))"
}
}
$Projects = Invoke-GitlabApi GET "projects/$($ProjectId | ConvertTo-UrlEncoded)" -SiteUrl $SiteUrl -WhatIf:$WhatIf
$Projects = Invoke-GitlabApi GET "projects/$($ProjectId | ConvertTo-UrlEncoded)" -SiteUrl $SiteUrl
}
ByGroup {
$Group = Get-GitlabGroup $GroupId
Expand All @@ -141,7 +139,7 @@ function Get-GitlabProject {
if (-not $IncludeArchived) {
$Query['archived'] = 'false'
}
$Projects = Invoke-GitlabApi GET "groups/$($Group.Id)/projects" $Query -MaxPages $MaxPages -SiteUrl $SiteUrl -WhatIf:$WhatIf |
$Projects = Invoke-GitlabApi GET "groups/$($Group.Id)/projects" $Query -MaxPages $MaxPages -SiteUrl $SiteUrl |
Where-Object { $($_.path_with_namespace).StartsWith($Group.FullPath) } |
Sort-Object -Property 'Name'
}
Expand All @@ -160,7 +158,7 @@ function Get-GitlabProject {
ByTopics {
$Projects = Invoke-GitlabApi GET "projects" -Query @{
topic = $Topics -join ','
} -MaxPages $MaxPages -SiteUrl $SiteUrl -WhatIf:$WhatIf
} -MaxPages $MaxPages -SiteUrl $SiteUrl
}
ByUrl {
$Url -match "$($(Get-DefaultGitlabSite).Url)/(?<ProjectId>.*)" | Out-Null
Expand Down
64 changes: 64 additions & 0 deletions src/GitlabCli/Todos.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function Get-GitlabTodo {
[CmdletBinding()]
param (
[Parameter()]
[uint]
$MaxPages,

[switch]
[Parameter()]
$All,

[Parameter()]
[string]
$SiteUrl
)

# https://docs.gitlab.com/ee/api/todos.html#get-a-list-of-to-do-items
$Request = @{
HttpMethod = 'GET'
Path = 'todos'
MaxPages = Get-GitlabMaxPages -MaxPages:$MaxPages -All:$All
SiteUrl = $SiteUrl
}

Invoke-GitlabApi @Request | New-WrapperObject 'Gitlab.Todo'
}

function Clear-GitlabTodo {
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName='ById')]
param (
[Parameter(ParameterSetName='ById', Mandatory, Position=0)]
$TodoId,

[Parameter(ParameterSetName='All')]
[switch]
$All,

[Parameter()]
[string]
$SiteUrl
)
$Request = @{
HttpMethod = 'POST'
SiteUrl = $SiteUrl
}

switch ($PSCmdlet.ParameterSetName) {
All {
# https://docs.gitlab.com/ee/api/todos.html#mark-all-to-do-items-as-done
$Request.Path = "todos/mark_as_done"
if ($PSCmdlet.ShouldProcess("all todos", "$($Request | ConvertTo-Json)")) {
Invoke-GitlabApi @Request | Out-Null
Write-Host "All todos cleared"
}
}
ById {
# https://docs.gitlab.com/ee/api/todos.html#mark-a-to-do-item-as-done
$Request.Path = "todos/$TodoId/mark_as_done"
if ($PSCmdlet.ShouldProcess("todo #$TodoId", "$($Request | ConvertTo-Json)")) {
Invoke-GitlabApi @Request | New-WrapperObject 'Gitlab.Todo'
}
}
}
}
9 changes: 9 additions & 0 deletions src/GitlabCli/Types.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Gitlab.Todo</Name>
<Members>
<ScriptProperty>
<Name>Type</Name>
<GetScriptBlock>$this.TargetType</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Gitlab.Project</Name>
<Members>
Expand Down
1 change: 1 addition & 0 deletions src/GitlabCli/Utilities.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ function New-WrapperObject {

# aliases for common property names
Add-AliasedProperty -On $Wrapper -From 'Url' -To 'WebUrl'
Add-AliasedProperty -On $Wrapper -From 'Url' -To 'TargetUrl'

if ($DisplayType) {
$Wrapper.PSTypeNames.Insert(0, $DisplayType)
Expand Down
1 change: 1 addition & 0 deletions src/GitlabCli/_Init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ $global:GitlabIdentityPropertyNameExemptions=@{
'Gitlab.SearchResult.Blob' = ''
'Gitlab.SearchResult.MergeRequest' = ''
'Gitlab.SearchResult.Project' = ''
'Gitlab.Todo' = 'Id'
'Gitlab.Topic' = 'Id'
'Gitlab.User' = 'Id'
'Gitlab.UserMembership' = ''
Expand Down

0 comments on commit f810841

Please sign in to comment.