-
Notifications
You must be signed in to change notification settings - Fork 6
fix: branch protection for mergeOnly #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package gitlab | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/obcode/glabs/config" | ||
| gitlabapi "gitlab.com/gitlab-org/api/client-go/v2" | ||
| ) | ||
|
|
||
| func TestSyncConfiguredBranches_ReturnsErrorWhenProtectionFails(t *testing.T) { | ||
| client := newContractClient(t, func(w http.ResponseWriter, r *http.Request) { | ||
| switch { | ||
| case r.Method == http.MethodPatch && r.URL.Path == "/api/v4/projects/1": | ||
| _, _ = w.Write([]byte(`{"id":1,"name":"repo"}`)) | ||
| case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/api/v4/projects/1/protected_branches/main"): | ||
| _, _ = w.Write([]byte(`{"id":1,"name":"main","push_access_levels":[{"id":10,"access_level":40}],"merge_access_levels":[{"id":11,"access_level":40}],"unprotect_access_levels":[{"id":12,"access_level":40}]}`)) | ||
| case r.Method == http.MethodDelete && strings.Contains(r.URL.Path, "/api/v4/projects/1/protected_branches/main"): | ||
| w.WriteHeader(http.StatusNoContent) | ||
| case r.Method == http.MethodPost && strings.Contains(r.URL.Path, "/api/v4/projects/1/protected_branches"): | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| _, _ = w.Write([]byte(`{"message":"500 Internal Server Error"}`)) | ||
| default: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| } | ||
| }) | ||
|
|
||
| cfg := &config.AssignmentConfig{ | ||
| Branches: []config.BranchRule{{Name: "main", MergeOnly: true, Default: true}}, | ||
| } | ||
| project := &gitlabapi.Project{ID: 1, Name: "repo"} | ||
|
|
||
| err := client.syncConfiguredBranches(cfg, project, "main") | ||
| if err == nil { | ||
| t.Fatal("syncConfiguredBranches() expected error when branch protection fails, got nil") | ||
| } | ||
|
Comment on lines
+12
to
+37
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,15 @@ func (c *Client) protectSingleBranch( | |
| ) error { | ||
| existing, _, err := c.ProtectedBranches.GetProtectedBranch(project.ID, branch.Name) | ||
| if err == nil { | ||
| // GitLab can keep stale push permissions when updating existing rules to | ||
| // "No one" (merge-only). Recreate the rule to enforce the access levels. | ||
| if pushAccessLevel == gitlab.NoPermissions { | ||
| if err := c.recreateProtectedBranch(project, branch, pushAccessLevel, mergeAccessLevel); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
Comment on lines
+122
to
+129
|
||
|
|
||
| updateOpts := &gitlab.UpdateProtectedBranchOptions{ | ||
| AllowedToPush: replaceBranchPermissions(existing.PushAccessLevels, pushAccessLevel), | ||
| AllowedToMerge: replaceBranchPermissions(existing.MergeAccessLevels, mergeAccessLevel), | ||
|
|
@@ -149,6 +158,29 @@ func (c *Client) protectSingleBranch( | |
| return fmt.Errorf("error while trying to read protected branch %s: %w", branch.Name, err) | ||
| } | ||
|
|
||
| if err := c.recreateProtectedBranch(project, branch, pushAccessLevel, mergeAccessLevel); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
|
Comment on lines
+161
to
+165
|
||
| } | ||
|
|
||
| func (c *Client) recreateProtectedBranch( | ||
| project *gitlab.Project, | ||
| branch config.BranchRule, | ||
| pushAccessLevel gitlab.AccessLevelValue, | ||
| mergeAccessLevel gitlab.AccessLevelValue, | ||
| ) error { | ||
| _, err := c.ProtectedBranches.UnprotectRepositoryBranches(project.ID, branch.Name) | ||
| if err != nil && !isProtectedBranchNotFoundError(err) { | ||
| log.Debug().Err(err). | ||
| Str("name", project.Name). | ||
| Str("toURL", project.SSHURLToRepo). | ||
| Str("branch", branch.Name). | ||
| Msg("cannot unprotect branch") | ||
| return fmt.Errorf("error while trying to unprotect branch %s: %w", branch.Name, err) | ||
| } | ||
|
|
||
| opts := &gitlab.ProtectRepositoryBranchesOptions{ | ||
| Name: gitlab.Ptr(branch.Name), | ||
| PushAccessLevel: gitlab.Ptr(pushAccessLevel), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mock server currently only treats
PATCH /api/v4/projects/1as a success. Since this test’s goal is to reach the protection step, consider making this handler accept whatever HTTP methodProjects.EditProjectactually uses (or at least fail the test if an unexpected method hits this endpoint) so the test can’t silently pass/fail due to a mock mismatch.