Skip to content

Commit

Permalink
Very basic milestone support
Browse files Browse the repository at this point in the history
Can get by id and update an MR's milestone
  • Loading branch information
chris-peterson committed Dec 19, 2024
1 parent 0590e37 commit 6d2f952
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 28 deletions.
22 changes: 9 additions & 13 deletions src/GitlabCli/Branches.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@ function Get-GitlabProtectedBranchAccessLevel {
function Get-GitlabBranch {
[CmdletBinding(DefaultParameterSetName="ByProjectId")]
param (
[Parameter(ParameterSetName="ByProjectId", Mandatory=$false, ValueFromPipelineByPropertyName)]
[Parameter(ParameterSetName="ByRef", Mandatory=$false, ValueFromPipelineByPropertyName)]
[Parameter(ParameterSetName="ByProjectId", ValueFromPipelineByPropertyName)]
[Parameter(ParameterSetName="ByRef", ValueFromPipelineByPropertyName)]
[string]
$ProjectId = '.',

[Parameter(ParameterSetName="ByProjectId",Mandatory=$false)]
[Parameter(ParameterSetName="ByProjectId")]
[string]
$Search,

[Parameter(ParameterSetName="ByRef", Mandatory=$true)]
[Parameter(ParameterSetName="ByRef", Mandatory, Position=0)]
[Alias("Branch")]
[string]
$Ref,

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

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

$Project = Get-GitlabProject -ProjectId $ProjectId
Expand All @@ -48,7 +44,7 @@ function Get-GitlabBranch {
SiteUrl = $SiteUrl
}

switch($PSCmdlet.ParameterSetName) {
switch ($PSCmdlet.ParameterSetName) {
ByProjectId {
if($Search) {
$GitlabApiArguments.Query["search"] = $Search
Expand All @@ -58,11 +54,11 @@ function Get-GitlabBranch {
$GitlabApiArguments.Path += "/$($Ref)"
}
default {
throw "Parameterset $($PSCmdlet.ParameterSetName) is not implemented"
throw "$($PSCmdlet.ParameterSetName) is not implemented"
}
}

Invoke-GitlabApi @GitlabApiArguments -WhatIf:$WhatIf
Invoke-GitlabApi @GitlabApiArguments
| New-WrapperObject 'Gitlab.Branch'
| Add-Member -MemberType 'NoteProperty' -Name 'ProjectId' -Value $Project.Id -PassThru
| Sort-Object -Descending LastUpdated
Expand Down
38 changes: 38 additions & 0 deletions src/GitlabCli/Formats.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,44 @@
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>GitlabMilestone</Name>
<ViewSelectedBy>
<TypeName>Gitlab.Milestone</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
<Label>UpdatedAt</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Id</PropertyName>
<Alignment>Right</Alignment>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Title</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>DueDate</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>UpdatedAtSortable</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>GitlabMergeRequestApprovalRule</Name>
<ViewSelectedBy>
Expand Down
9 changes: 7 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '1.121.0'
ModuleVersion = '1.122.0'

RequiredModules = @('powershell-yaml')

Expand Down Expand Up @@ -27,7 +27,8 @@
ExternalModuleDependencies = @('powershell-yaml')
ReleaseNotes =
@'
* feature: Set build timeout for projects
* feature: implement milestones
* enhance: add options for issues/MRs
'@
}
}
Expand Down Expand Up @@ -62,6 +63,7 @@
'Jobs.psm1'
'Members.psm1'
'MergeRequests.psm1'
'Milestones.psm1'
'Notes.psm1'
'PersonalAccessTokens.psm1'
'Pipelines.psm1'
Expand Down Expand Up @@ -192,6 +194,9 @@
'New-GitlabMergeRequestApprovalRule'
'Remove-GitlabMergeRequestApprovalRule'

# Milestones
'Get-GitlabMilestone'

# PATs
'Get-GitlabPersonalAccessToken'
'New-GitlabPersonalAccessToken'
Expand Down
7 changes: 7 additions & 0 deletions src/GitlabCli/Issues.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function New-GitlabIssue {
[switch]
$MarkTodoAsRead,

[Parameter()]
[switch]
$Follow,

[Parameter()]
[string]
$SiteUrl
Expand All @@ -131,6 +135,9 @@ function New-GitlabIssue {
$Todo = Get-GitlabTodo -SiteUrl $SiteUrl | Where-Object TargetUrl -eq $Issue.WebUrl
Clear-GitlabTodo -TodoId $Todo.Id -SiteUrl $SiteUrl | Out-Null
}
if ($Follow) {
Start-Process $Issue.WebUrl
}
$Issue
}
}
Expand Down
48 changes: 37 additions & 11 deletions src/GitlabCli/MergeRequests.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ function Add-GitlabMergeRequestChangeSummary {
function New-GitlabMergeRequest {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position=0)]
[Parameter(Position=0, ValueFromPipelineByPropertyName)]
[string]
$ProjectId,
$ProjectId = '.',

[Parameter(Position=1)]
[string]
Expand All @@ -243,6 +243,15 @@ function New-GitlabMergeRequest {
[string]
$Title,

[Parameter()]
[string]
$MilestoneId,

[Parameter()]
[Alias('NoTodo')]
[switch]
$MarkTodoAsRead,

[Parameter()]
[switch]
$Follow,
Expand Down Expand Up @@ -270,20 +279,30 @@ function New-GitlabMergeRequest {

$Me = Get-GitlabCurrentUser

$Body = @{
source_branch = $SourceBranch
target_branch = $TargetBranch
remove_source_branch = 'true'
assignee_id = $Me.Id
title = $Title
}
if ($MilestoneId) {
$Body.milestone_id = $MilestoneId
}

$Request = @{
Method = 'POST'
Path = "projects/$($Project.Id)/merge_requests"
Body = @{
source_branch = $SourceBranch
target_branch = $TargetBranch
remove_source_branch = 'true'
assignee_id = $Me.Id
title = $Title
}
# https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
Path = "projects/$($Project.Id)/merge_requests"
Body = $Body
}

if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "create merge request ($($Request | ConvertTo-Json))")) {
if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "create merge request ($($Body | ConvertTo-Json))")) {
$MergeRequest = Invoke-GitlabApi @Request -SiteUrl $SiteUrl | New-WrapperObject 'Gitlab.MergeRequest'
if ($MarkTodoAsRead) {
$Todo = Get-GitlabTodo -SiteUrl $SiteUrl | Where-Object TargetUrl -eq $MergeRequest.WebUrl
Clear-GitlabTodo -TodoId $Todo.Id -SiteUrl $SiteUrl | Out-Null
}
if ($Follow) {
Start-Process $MergeRequest.WebUrl
}
Expand Down Expand Up @@ -436,6 +455,10 @@ function Update-GitlabMergeRequest {
[switch]
$Reopen,

[Parameter()]
[string]
$MilestoneId,

[Parameter()]
[string]
$SiteUrl
Expand Down Expand Up @@ -476,6 +499,9 @@ function Update-GitlabMergeRequest {
} elseif ($UnsetReviewers) {
$Request.reviewer_ids = @()
}
if ($MilestoneId) {
$Request.milestone_id = $MilestoneId
}

if ($PSCmdlet.ShouldProcess("MR $MergeRequestId in $($Project.PathWithNamespace)", "update $($Request | ConvertTo-Json)")) {
# https://docs.gitlab.com/ee/api/merge_requests.html#update-mr
Expand Down
62 changes: 62 additions & 0 deletions src/GitlabCli/Milestones.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function Get-GitlabMilestone {
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByGroup')]
param (

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ByGroup')]
[string]
$GroupId,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ByProject')]
[string]
$ProjectId,

[Parameter(ParameterSetName = 'ByGroup')]
[Parameter(ParameterSetName = 'ByProject')]
[Alias('Id')]
[string]
$MilestoneId,

[Parameter()]
[string]
$SiteUrl
)

if ($MilestoneId) {
$Data = Invoke-GitlabGraphQL -Query @"
{
milestone(id: "gid://gitlab/Milestone/$MilestoneId") {
groupMilestone,
group {
id
}
projectMilestone,
project {
id
}
}
}
"@
if ($Data.Milestone.groupMilestone) {
$GroupId = $Data.Milestone.group.id -split '/' | Select-Object -Last 1
}
elseif ($Data.Milestone.projectMilestone) {
$ProjectId = $Data.Milestone.project.id -split '/' | Select-Object -Last 1
}

}

$Resource = ''
if ($GroupId) {
$Resource = "groups/$GroupId/milestones"
} elseif ($ProjectId) {
$Resource = "projects/$ProjectId/milestones"
}

$Request = @{
HttpMethod = 'GET'
Path = $Resource
SiteUrl = $SiteUrl
}

Invoke-GitlabApi @Request | New-WrapperObject 'Gitlab.Milestone'
}
9 changes: 9 additions & 0 deletions src/GitlabCli/Types.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Gitlab.Milestone</Name>
<Members>
<ScriptProperty>
<Name>UpdatedAtSortable</Name>
<GetScriptBlock>$this.UpdatedAt.ToString('yyyy-MM-dd HH:mm')</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Gitlab.MergeRequestApprovalRule</Name>
<Members>
Expand Down
3 changes: 1 addition & 2 deletions src/GitlabCli/_Init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ $global:GitlabConsoleColors = @{
White = '1;37'
Yellow = '1;33'
}
# https://docs.gitlab.com/ee/api/#id-vs-iid
# TL;DR; it's a mess and we have to special-case specific entity types
$global:GitlabIdentityPropertyNameExemptions=@{
'Gitlab.AuditEvent' = 'Id'
'Gitlab.AccessToken' = 'Id'
Expand All @@ -33,6 +31,7 @@ $global:GitlabIdentityPropertyNameExemptions=@{
'Gitlab.Job' = 'Id'
'Gitlab.Member' = 'Id'
'Gitlab.MergeRequestApprovalRule' = 'Id'
'Gitlab.Milestone' = 'Iid'
'Gitlab.NewPersonalAccessToken' = 'Id'
'Gitlab.Note' = 'Id'
'Gitlab.PersonalAccessToken' = 'Id'
Expand Down

0 comments on commit 6d2f952

Please sign in to comment.