diff --git a/pkg/api/controller.go b/pkg/api/controller.go index cd2f42948cc..01dfd341329 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -3091,9 +3091,10 @@ func (c *Controller) InternalCreateBranchProtectionRule(w http.ResponseWriter, r } } if rules == nil { - rules = &graveler.BranchProtectionRules{ - BranchPatternToBlockedActions: make(map[string]*graveler.BranchProtectionBlockedActions), - } + rules = &graveler.BranchProtectionRules{} + } + if rules.BranchPatternToBlockedActions == nil { + rules.BranchPatternToBlockedActions = make(map[string]*graveler.BranchProtectionBlockedActions) } blockedActions := &graveler.BranchProtectionBlockedActions{ Value: []graveler.BranchProtectionBlockedAction{graveler.BranchProtectionBlockedAction_STAGING_WRITE, graveler.BranchProtectionBlockedAction_COMMIT}, diff --git a/pkg/graveler/branch/protection_manager_test.go b/pkg/graveler/branch/protection_manager_test.go index 8b02871cdf7..bbd0f2d8ed0 100644 --- a/pkg/graveler/branch/protection_manager_test.go +++ b/pkg/graveler/branch/protection_manager_test.go @@ -28,8 +28,9 @@ func TestSetAndGet(t *testing.T) { ctx := context.Background() bpm := prepareTest(t, ctx) rules, eTag, err := bpm.GetRules(ctx, repository) - if !errors.Is(err, graveler.ErrNotFound) { - t.Fatalf("expected ErrNotFound, got %v", err) + testutil.Must(t, err) + if len(rules.BranchPatternToBlockedActions) != 0 { + t.Fatalf("expected no rules, got %d rules", len(rules.BranchPatternToBlockedActions)) } testutil.Must(t, bpm.SetRulesIf(ctx, repository, &graveler.BranchProtectionRules{ BranchPatternToBlockedActions: map[string]*graveler.BranchProtectionBlockedActions{ @@ -90,8 +91,9 @@ func TestDelete(t *testing.T) { testutil.Must(t, bpm.Delete(ctx, repository, "main*")) rules, _, err = bpm.GetRules(ctx, repository) - if !errors.Is(err, graveler.ErrNotFound) { - t.Fatalf("expected ErrNotFound after delete, got %v", err) + testutil.Must(t, err) + if len(rules.BranchPatternToBlockedActions) != 0 { + t.Fatalf("expected no rules, got %d rules", len(rules.BranchPatternToBlockedActions)) } } diff --git a/pkg/graveler/mock/graveler.go b/pkg/graveler/mock/graveler.go index f8cbe8e4298..b20fee272ff 100644 --- a/pkg/graveler/mock/graveler.go +++ b/pkg/graveler/mock/graveler.go @@ -2916,15 +2916,29 @@ func (mr *MockProtectedBranchesManagerMockRecorder) IsBlocked(ctx, repository, b } // SetRules mocks base method. -func (m *MockProtectedBranchesManager) SetRulesIf(ctx context.Context, repository *graveler.RepositoryRecord, rules *graveler.BranchProtectionRules, ifMatchETag *string) error { +func (m *MockProtectedBranchesManager) SetRules(ctx context.Context, repository *graveler.RepositoryRecord, rules *graveler.BranchProtectionRules) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetRulesIf", ctx, repository, rules, ifMatchETag) + ret := m.ctrl.Call(m, "SetRules", ctx, repository, rules) ret0, _ := ret[0].(error) return ret0 } // SetRules indicates an expected call of SetRules. -func (mr *MockProtectedBranchesManagerMockRecorder) SetRules(ctx, repository, rules, ifMatchETag interface{}) *gomock.Call { +func (mr *MockProtectedBranchesManagerMockRecorder) SetRules(ctx, repository, rules interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRules", reflect.TypeOf((*MockProtectedBranchesManager)(nil).SetRules), ctx, repository, rules) +} + +// SetRulesIf mocks base method. +func (m *MockProtectedBranchesManager) SetRulesIf(ctx context.Context, repository *graveler.RepositoryRecord, rules *graveler.BranchProtectionRules, ifMatchETag string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetRulesIf", ctx, repository, rules, ifMatchETag) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetRulesIf indicates an expected call of SetRulesIf. +func (mr *MockProtectedBranchesManagerMockRecorder) SetRulesIf(ctx, repository, rules, ifMatchETag interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRulesIf", reflect.TypeOf((*MockProtectedBranchesManager)(nil).SetRulesIf), ctx, repository, rules, ifMatchETag) }