Skip to content

Commit

Permalink
Add some cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Jan 20, 2024
1 parent ae36e3c commit ef96e21
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
@{
ModuleVersion = '1.107.0'
ModuleVersion = '1.108.0'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE'
ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab'
ReleaseNotes = 'Get more resources by Url'
ReleaseNotes =
@'
New CmdLets:
* Remove-GitlabUserMembership (https://github.com/chris-peterson/pwsh-gitlab/issues/87)
* Block-GitlabUser, Unblock-GitlabUser
'@
}
}

GUID = '220fdbee-bea7-4951-9375-f6e76bd981b4'

Author = 'Chris Peterson'
CompanyName = 'Chris Peterson'
Copyright = '(c) 2021-2023'
Copyright = '(c) 2021-2024'

Description = 'Interact with GitLab via PowerShell'
PowerShellVersion = '7.1'
Expand Down Expand Up @@ -215,6 +220,8 @@
# User
'Get-GitlabUser'
'Get-GitlabCurrentUser'
'Block-GitlabUser'
'Unblock-GitlabUser'

# Events
'Get-GitlabUserEvent'
Expand All @@ -241,6 +248,7 @@
'Get-GitlabUserMembership'
'Add-GitlabUserMembership'
'Update-GitlabUserMembership'
'Remove-GitlabUserMembership'

# Utilities
'ConvertTo-PascalCase'
Expand Down
51 changes: 50 additions & 1 deletion src/GitlabCli/Members.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function Add-GitlabGroupMember {
}

function Remove-GitlabGroupMember {
[CmdletBinding(SupportsShouldProcess)]
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
Expand Down Expand Up @@ -304,6 +304,55 @@ function Get-GitlabUserMembership {
New-WrapperObject 'Gitlab.UserMembership'
}

function Remove-GitlabUserMembership {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
param(
[Parameter(Mandatory, Position=0, ValueFromPipelineByPropertyName)]
[string]
$Username,

[Parameter()]
$Group,

[Parameter()]
$Project,

[Parameter()]
[switch]
$RemoveAllAccess,

[Parameter]
[string]
$SiteUrl
)

$User = Get-GitlabUser -Username $Username -SiteUrl $SiteUrl

if ($Group) {
if ($PSCmdlet.ShouldProcess("$($Group -join ',' )", "remove $Username access from groups")) {
$Group | ForEach-Object {
$User | Remove-GitlabGroupMember -GroupId $_ -SiteUrl $SiteUrl
}
}
}
if ($Project) {
if ($PSCmdlet.ShouldProcess("$($Project -join ',' )", "remove $Username access from project ")) {
$Project | ForEach-Object {
$User | Remove-GitlabProjectMember -ProjectId $_ -SiteUrl $SiteUrl
}
}
}
if ($RemoveAllAccess) {
$CurrentAccess = $User | Get-GitlabUserMembership
$Request = @{
Group = $CurrentAccess | Where-Object Sourcetype -eq 'Namespace' | Select-Object -ExpandProperty SourceId
Project = $CurrentAccess | Where-Object Sourcetype -eq 'Project' | Select-Object -ExpandProperty SourceId
SiteUrl = $SiteUrl
}
$User | Remove-GitlabUserMembership @Request
}
}

# https://docs.gitlab.com/ee/api/members.html#add-a-member-to-a-group-or-project
function Add-GitlabUserMembership {
[CmdletBinding()]
Expand Down
58 changes: 58 additions & 0 deletions src/GitlabCli/Users.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,64 @@ function Get-GitlabUser {
Invoke-GitlabApi @Parameters | New-WrapperObject 'Gitlab.User'
}

function Block-GitlabUser {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param (
[Parameter(Position=0, Mandatory, ValueFromPipelineByPropertyName)]
[Alias('Id')]
[Alias('Username')]
[Alias('EmailAddress')]
[string]
$UserId,

[Parameter()]
[string]
$SiteUrl
)
$User = Get-GitlabUser $UserId

if ($PSCmdlet.ShouldProcess("$($User.Username)", "block user")) {
$Parameters = @{
# https://docs.gitlab.com/ee/api/users.html#block-user
Method = 'POST'
Path = "users/$($User.Id)/block"
SiteUrl = $SiteUrl
}
if (Invoke-GitlabApi @Parameters) {
Write-Host "$($User.Username) blocked"
}
}
}

function Unblock-GitlabUser {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
param (
[Parameter(Position=0, Mandatory, ValueFromPipelineByPropertyName)]
[Alias('Id')]
[Alias('Username')]
[Alias('EmailAddress')]
[string]
$UserId,

[Parameter()]
[string]
$SiteUrl
)
$User = Get-GitlabUser $UserId

if ($PSCmdlet.ShouldProcess("$($User.Username)", "unblock user")) {
$Parameters = @{
# https://docs.gitlab.com/ee/api/users.html#unblock-user
Method = 'POST'
Path = "users/$($User.Id)/unblock"
SiteUrl = $SiteUrl
}
if (Invoke-GitlabApi @Parameters) {
Write-Host "$($User.Username) unblocked"
}
}
}

# https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events
function Get-GitlabUserEvent {
[CmdletBinding(DefaultParameterSetName='ByUserId')]
Expand Down

0 comments on commit ef96e21

Please sign in to comment.