Skip to content

Commit

Permalink
Implement Add-GitlabGroupToProject
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Jul 31, 2024
1 parent edad03c commit 232be28
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@{
ModuleVersion = '1.111.0'
ModuleVersion = '1.112.0'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE'
ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab'
ReleaseNotes =
@'
Rename Get-GitlabRunnerJobs to Get-GitlabRunnerJob (powershell naming convention)
new cmdlet: Add-GitlabGroupToProject
'@
}
}
Expand Down Expand Up @@ -104,23 +104,24 @@ Rename Get-GitlabRunnerJobs to Get-GitlabRunnerJob (powershell naming convention
'Remove-GitlabGroupShareLink'

# Projects
'Get-GitlabProject'
'Add-GitlabGroupToProject'
'ConvertTo-GitlabTriggerYaml'
'New-GitlabProject'
'Update-GitlabProject'
'Move-GitlabProject'
'Rename-GitlabProject'
'Rename-GitlabProjectDefaultBranch'
'Copy-GitlabProject'
'Get-GitlabProject'
'Get-GitlabProjectHook'
'Get-GitlabProjectVariable'
'Invoke-GitlabProjectArchival'
'Invoke-GitlabProjectUnarchival'
'Get-GitlabProjectVariable'
'Set-GitlabProjectVariable'
'Remove-GitlabProjectVariable'
'Get-GitlabProjectHook'
'Move-GitlabProject'
'New-GitlabProject'
'New-GitlabProjectHook'
'Remove-GitlabProjectHook'
'Remove-GitlabProjectVariable'
'Rename-GitlabProject'
'Rename-GitlabProjectDefaultBranch'
'Set-GitlabProjectVariable'
'Update-GitlabProject'
'Update-GitlabProjectHook'
'New-GitlabProjectHook'

# Repository Files
'Get-GitlabRepositoryFile'
Expand Down Expand Up @@ -274,6 +275,7 @@ Rename Get-GitlabRunnerJobs to Get-GitlabRunnerJob (powershell naming convention
'Remove-GitlabProtectedBranch'
'Review-GitlabMergeRequest'
'Revoke-GitlabGroupAccessToken'
'Share-GitlabProjectWithGroup'
'Transfer-GitlabGroup'
'Transfer-GitlabProject'
'Unarchive-GitlabProject'
Expand Down
1 change: 1 addition & 0 deletions src/GitlabCli/Members.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function Get-GitlabMemberAccessLevel {
Developer = 30
Maintainer = 40
Owner = 50
Admin = 60
}

if ($AccessLevel) {
Expand Down
40 changes: 40 additions & 0 deletions src/GitlabCli/Projects.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,43 @@ function Get-GitlabProjectEvent {
-Query $Query -MaxPages $MaxPages -SiteUrl $SiteUrl -WhatIf:$WhatIf |
New-WrapperObject 'Gitlab.Event'
}

function Add-GitlabGroupToProject {
[CmdletBinding(SupportsShouldProcess)]
[Alias('Share-GitlabProjectWithGroup')]
param (
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$ProjectId,

[Parameter(ValueFromPipelineByPropertyName)]
[string]
$GroupId,

[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Access')]
[string]
[ValidateSet('guest', 'reporter', 'developer', 'maintainer', 'owner')]
$GroupAccess
)

$AccessLiteral = Get-GitlabMemberAccessLevel $GroupAccess
$Project = Get-GitlabProject $ProjectId
$Group = Get-GitlabGroup $GroupId

# https://docs.gitlab.com/ee/api/projects.html#share-project-with-group
$Request = @{
Method = 'POST'
Path = "projects/$($ProjectId)/share"
Body = @{
group_id = $Group.Id
group_access = $AccessLiteral
}
}

if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "share project with group ($($Request | ConvertTo-Json))")) {
if (Invoke-GitlabApi @Request | Out-Null) {
Write-Host "Successfully shared $($Project.PathWithNamespace) with $($Group.FullPath)"
}
}
}

0 comments on commit 232be28

Please sign in to comment.