Skip to content

Commit

Permalink
Workaround PATCH protected branch issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Dec 6, 2023
1 parent d9e2034 commit 83c47a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
57 changes: 35 additions & 22 deletions src/GitlabCli/Branches.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,41 @@ function Protect-GitlabBranch {
)

$Project = Get-GitlabProject -ProjectId $ProjectId

$PushAccessLevelLiteral = $(Get-GitlabProtectedBranchAccessLevel).$PushAccessLevel
$MergeAccessLevelLiteral = $(Get-GitlabProtectedBranchAccessLevel).$MergeAccessLevel
$UnprotectAccessLevelLiteral = $(Get-GitlabProtectedBranchAccessLevel).$UnprotectAccessLevel

if ($Project | Get-GitlabProtectedBranch | Where-Object Name -eq $Branch) {
# NOTE: the PATCH endpoint is crap (https://gitlab.com/gitlab-org/gitlab/-/issues/365520)
# $Request = @{
# allow_force_push = $AllowForcePush
# allowed_to_push = @($AllowedToPush | ConvertTo-SnakeCase) + @(@{access_level=$PushAccessLevelLiteral})
# allowed_to_merge = @($AllowedToMerge | ConvertTo-SnakeCase) + @(@{access_level=$MergeAccessLevelLiteral})
# allowed_to_unprotect = @($AllowedToUnprotect | ConvertTo-SnakeCase) + @(@{access_level=$UnprotectAccessLevelLiteral})
# code_owner_approval_required = $CodeOwnerApprovalRequired
# }
# if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace) ($Branch)", "update protected branch $($Request | ConvertTo-Json)")) {
# # https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
# Invoke-GitlabApi PATCH "projects/$($Project.Id)/protected_branches/$Branch" -Body $Request | New-WrapperObject 'Gitlab.ProtectedBranch'
# }
# as a workaround, remove protection
Remove-GitlabProtectedBranch -ProjectId $ProjectId -Branch $Branch -SiteUrl $SiteUrl -WhatIf:$WhatIfPreference | Out-Null
}

$Request = @{
push_access_level = $(Get-GitlabProtectedBranchAccessLevel).$PushAccessLevel
merge_access_level = $(Get-GitlabProtectedBranchAccessLevel).$MergeAccessLevel
unprotect_access_level = $(Get-GitlabProtectedBranchAccessLevel).$UnprotectAccessLevel
allow_force_push = $AllowForcePush
allowed_to_push = @($AllowedToPush | ConvertTo-SnakeCase)
allowed_to_merge = @($AllowedToMerge | ConvertTo-SnakeCase)
allowed_to_unprotect = @($AllowedToUnprotect | ConvertTo-SnakeCase)
name = $Branch
push_access_level = $PushAccessLevelLiteral
merge_access_level = $MergeAccessLevelLiteral
unprotect_access_level = $UnprotectAccessLevelLiteral
allow_force_push = $AllowForcePush
allowed_to_push = @($AllowedToPush | ConvertTo-SnakeCase)
allowed_to_merge = @($AllowedToMerge | ConvertTo-SnakeCase)
allowed_to_unprotect = @($AllowedToUnprotect | ConvertTo-SnakeCase)
code_owner_approval_required = $CodeOwnerApprovalRequired
}
if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace) ($Branch)", "protect branch $($Request | ConvertTo-Json)")) {
if ($Project | Get-GitlabProtectedBranch | Where-Object Name -eq $Branch) {
# https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
Invoke-GitlabApi PATCH "projects/$($Project.Id)/protected_branches/$Branch" -Body $Request | New-WrapperObject 'Gitlab.ProtectedBranch'
}
else {
$Request.name = $Branch
# https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches
Invoke-GitlabApi POST "projects/$($Project.Id)/protected_branches" -Body $Request | New-WrapperObject 'Gitlab.ProtectedBranch'
}
# https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches
Invoke-GitlabApi POST "projects/$($Project.Id)/protected_branches" -Body $Request | New-WrapperObject 'Gitlab.ProtectedBranch'
}
}

Expand All @@ -230,11 +244,10 @@ function UnProtect-GitlabBranch {
[Parameter()]
[string]
$SiteUrl
)

$Project = Get-GitlabProject -ProjectId $ProjectId

if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)/branches/$($Name)", "unprotect branch $($Name)")) {
)

$Project = Get-GitlabProject -ProjectId $ProjectId
if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)/branches/$($Name)", "unprotect branch $($Name)")) {
# https://docs.gitlab.com/ee/api/protected_branches.html#unprotect-repository-branches
Invoke-GitlabApi DELETE "projects/$($Project.Id)/protected_branches/$($Name)" -SiteUrl $SiteUrl
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@{
ModuleVersion = '1.106.0'
ModuleVersion = '1.106.1'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE'
ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab'
ReleaseNotes = 'Wrap releases'
ReleaseNotes = 'Fix for updating an existing protected branch'
}
}

Expand Down

0 comments on commit 83c47a0

Please sign in to comment.