From d5967cdf4fbade9fb96b9b3e3edb236e451d6ecc Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Fri, 25 Oct 2024 18:00:10 -0700 Subject: [PATCH] Enhance some project member cmdlets --- src/GitlabCli/GitlabCli.psd1 | 4 +- src/GitlabCli/Members.psm1 | 78 ++++++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/GitlabCli/GitlabCli.psd1 b/src/GitlabCli/GitlabCli.psd1 index de80667..09cc1fa 100644 --- a/src/GitlabCli/GitlabCli.psd1 +++ b/src/GitlabCli/GitlabCli.psd1 @@ -1,5 +1,5 @@ @{ - ModuleVersion = '1.120.1' + ModuleVersion = '1.120.2' RequiredModules = @('powershell-yaml') @@ -28,6 +28,7 @@ ReleaseNotes = @' * feature: wrap TODO apis; allow clearing TODO as part of creation (https://github.com/chris-peterson/pwsh-gitlab/issues/56) +* feature: Set-GitlabProjectMember * enhance: better preview for issue cmdlets * enhance: allow a project to have a trailing slash * enhance: Get-GitlabProject no longer supports WhatIf @@ -283,6 +284,7 @@ 'Remove-GitlabGroupMember' 'Get-GitlabProjectMember' + 'Set-GitlabProjectMember' 'Add-GitlabProjectMember' 'Remove-GitlabProjectMember' diff --git a/src/GitlabCli/Members.psm1 b/src/GitlabCli/Members.psm1 index 0e125fb..9ed45ff 100644 --- a/src/GitlabCli/Members.psm1 +++ b/src/GitlabCli/Members.psm1 @@ -201,9 +201,9 @@ function Get-GitlabProjectMember { Sort-Object -Property $(Get-GitlabMembershipSortKey) } -# https://docs.gitlab.com/ee/api/members.html#add-a-member-to-a-group-or-project -function Add-GitlabProjectMember { - param ( +function Set-GitlabProjectMember { + [CmdletBinding(SupportsShouldProcess)] + param( [Parameter(Mandatory=$false)] [string] $ProjectId = '.', @@ -220,22 +220,76 @@ function Add-GitlabProjectMember { [Parameter(Mandatory=$false)] [string] - $SiteUrl, + $SiteUrl + ) - [switch] + $Existing = $Null + try { + $Existing = Get-GitlabProjectMember -ProjectId @ProjectId -UserId $UserId -SiteUrl $SiteUrl + } + catch { + Write-Verbose "User '$UserId' is not a member of '$ProjectId'" + } + + if ($Existing) { + # https://docs.gitlab.com/ee/api/members.html#edit-a-member-of-a-group-or-project + $Request = @{ + HttpMethod = 'PUT' + Path = "projects/$($Existing.ProjectId)/members/$($Existing.Id)" + Body = @{ + access_level = Get-GitlabMemberAccessLevel $AccessLevel + } + SiteUrl = $SiteUrl + } + if ($PSCmdlet.ShouldProcess("Project '$ProjectId'", "update '$($Existing.Name)' membership to '$AccessLevel'")) { + Invoke-GitlabApi @Request | New-WrapperObject 'Gitlab.Member' + } + } else { + if ($PSCmdlet.ShouldProcess("Project '$ProjectId'", "add '$UserId' as '$AccessLevel'")) { + Add-GitlabProjectMember -ProjectId $ProjectId -UserId $UserId -AccessLevel $AccessLevel -SiteUrl $SiteUrl + } + } +} + +function Add-GitlabProjectMember { + [CmdletBinding(SupportsShouldProcess)] + param ( [Parameter(Mandatory=$false)] - $WhatIf + [string] + $ProjectId = '.', + + [Parameter(Position=0, Mandatory=$true)] + [Alias('Username')] + [string] + $UserId, + + [Parameter(Position=1, Mandatory=$true)] + [ValidateSet('guest', 'reporter', 'developer', 'maintainer', 'owner')] + [string] + $AccessLevel, + + [Parameter(Mandatory=$false)] + [string] + $SiteUrl ) $User = Get-GitlabUser -UserId $UserId -SiteUrl $SiteUrl - $Project = Get-GitlabProject -ProjectId $ProjectId -SiteUrl $SiteUrl -WhatIf:$false + $Project = Get-GitlabProject -ProjectId $ProjectId -SiteUrl $SiteUrl - $Query = @{ - user_id = $User.Id - access_level = Get-GitlabMemberAccessLevel $AccessLevel + $Request = @{ + # https://docs.gitlab.com/ee/api/members.html#add-a-member-to-a-group-or-project + HttpMethod = 'POST' + Path = "projects/$($Project.Id)/members" + Body = @{ + user_id = $User.Id + access_level = Get-GitlabMemberAccessLevel $AccessLevel + } + SiteUrl = $SiteUrl + } + + if ($PSCmdlet.ShouldProcess($Project.PathWithNamespace, "grant '$($User.Username)' $AccessLevel membership")) { + Invoke-GitlabApi @Request | New-WrapperObject 'Gitlab.Member' } - Invoke-GitlabApi POST "projects/$($Project.Id)/members" -Query $Query -SiteUrl $SiteUrl -WhatIf:$WhatIf | - New-WrapperObject 'Gitlab.Member' } # https://docs.gitlab.com/ee/api/members.html#remove-a-member-from-a-group-or-project