Skip to content

Commit

Permalink
Enhance some project member cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Oct 26, 2024
1 parent c2db8a0 commit d5967cd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '1.120.1'
ModuleVersion = '1.120.2'

RequiredModules = @('powershell-yaml')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -283,6 +284,7 @@
'Remove-GitlabGroupMember'

'Get-GitlabProjectMember'
'Set-GitlabProjectMember'
'Add-GitlabProjectMember'
'Remove-GitlabProjectMember'

Expand Down
78 changes: 66 additions & 12 deletions src/GitlabCli/Members.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '.',
Expand All @@ -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
Expand Down

0 comments on commit d5967cd

Please sign in to comment.