diff --git a/app/services/rules/create.go b/app/services/rules/create.go index 0e38c7a24..bf56f5791 100644 --- a/app/services/rules/create.go +++ b/app/services/rules/create.go @@ -32,6 +32,8 @@ import ( "github.com/rs/zerolog/log" ) +const ruleScopeRepo = int64(0) + type CreateInput struct { Type types.RuleType `json:"type"` State enum.RuleState `json:"state"` @@ -94,6 +96,14 @@ func (s *Service) Create(ctx context.Context, return nil, usererror.BadRequestf("invalid rule definition: %s", err.Error()) } + scope := ruleScopeRepo + if parentType == enum.RuleParentSpace { + ids, err := s.spaceStore.GetAncestorIDs(ctx, parentID) + if err != nil { + return nil, fmt.Errorf("failed to get ancestor IDs: %w", err) + } + scope = int64(len(ids)) + } now := time.Now().UnixMilli() rule := &types.Rule{ CreatedBy: principal.ID, @@ -105,6 +115,7 @@ func (s *Service) Create(ctx context.Context, Description: in.Description, Pattern: in.Pattern.JSON(), Definition: in.Definition, + Scope: scope, CreatedByInfo: types.PrincipalInfo{}, } diff --git a/app/store/database/migrate/postgres/0088_alter_rules_add_scope.down.sql b/app/store/database/migrate/postgres/0088_alter_rules_add_scope.down.sql new file mode 100644 index 000000000..e16075d4c --- /dev/null +++ b/app/store/database/migrate/postgres/0088_alter_rules_add_scope.down.sql @@ -0,0 +1 @@ +ALTER TABLE rules DROP COLUMN rule_scope; \ No newline at end of file diff --git a/app/store/database/migrate/postgres/0088_alter_rules_add_scope.up.sql b/app/store/database/migrate/postgres/0088_alter_rules_add_scope.up.sql new file mode 100644 index 000000000..b60471dfd --- /dev/null +++ b/app/store/database/migrate/postgres/0088_alter_rules_add_scope.up.sql @@ -0,0 +1 @@ +ALTER TABLE rules ADD COLUMN rule_scope INTEGER DEFAULT 0; \ No newline at end of file diff --git a/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.down.sql b/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.down.sql new file mode 100644 index 000000000..e16075d4c --- /dev/null +++ b/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.down.sql @@ -0,0 +1 @@ +ALTER TABLE rules DROP COLUMN rule_scope; \ No newline at end of file diff --git a/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.up.sql b/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.up.sql new file mode 100644 index 000000000..b60471dfd --- /dev/null +++ b/app/store/database/migrate/sqlite/0088_alter_rules_add_scope.up.sql @@ -0,0 +1 @@ +ALTER TABLE rules ADD COLUMN rule_scope INTEGER DEFAULT 0; \ No newline at end of file diff --git a/app/store/database/rule.go b/app/store/database/rule.go index d95fa5904..eb73e7dae 100644 --- a/app/store/database/rule.go +++ b/app/store/database/rule.go @@ -72,6 +72,8 @@ type rule struct { Pattern string `db:"rule_pattern"` Definition string `db:"rule_definition"` + + Scope int64 `db:"rule_scope"` } const ( @@ -88,7 +90,8 @@ const ( ,rule_type ,rule_state ,rule_pattern - ,rule_definition` + ,rule_definition + ,rule_scope` ruleSelectBase = ` SELECT` + ruleColumns + ` @@ -165,6 +168,7 @@ func (s *RuleStore) Create(ctx context.Context, rule *types.Rule) error { ,rule_state ,rule_pattern ,rule_definition + ,rule_scope ) values ( :rule_version ,:rule_created_by @@ -178,6 +182,7 @@ func (s *RuleStore) Create(ctx context.Context, rule *types.Rule) error { ,:rule_state ,:rule_pattern ,:rule_definition + ,:rule_scope ) RETURNING rule_id` db := dbtx.GetAccessor(ctx, s.db) @@ -459,6 +464,7 @@ func (s *RuleStore) mapToRule( State: in.State, Pattern: json.RawMessage(in.Pattern), Definition: json.RawMessage(in.Definition), + Scope: in.Scope, } createdBy, err := s.pCache.Get(ctx, in.CreatedBy) @@ -499,6 +505,7 @@ func mapToInternalRule(in *types.Rule) rule { State: in.State, Pattern: string(in.Pattern), Definition: string(in.Definition), + Scope: in.Scope, } } diff --git a/types/rule.go b/types/rule.go index 547eac244..55f87f043 100644 --- a/types/rule.go +++ b/types/rule.go @@ -48,6 +48,9 @@ type Rule struct { Users map[int64]*PrincipalInfo `json:"users"` UserGroups map[int64]*UserGroupInfo `json:"user_groups"` + + // scope 0 indicates repo; scope > 0 indicates space depth level + Scope int64 `json:"scope"` } // TODO [CODE-1363]: remove after identifier migration.