fix: [CODE-2846]: Add scope info to rule (#3045)

* Add scope info to rule
This commit is contained in:
Darko Draskovic 2024-11-25 17:43:21 +00:00 committed by Harness
parent d114c23f01
commit c79fe0f829
7 changed files with 26 additions and 1 deletions

View File

@ -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{},
}

View File

@ -0,0 +1 @@
ALTER TABLE rules DROP COLUMN rule_scope;

View File

@ -0,0 +1 @@
ALTER TABLE rules ADD COLUMN rule_scope INTEGER DEFAULT 0;

View File

@ -0,0 +1 @@
ALTER TABLE rules DROP COLUMN rule_scope;

View File

@ -0,0 +1 @@
ALTER TABLE rules ADD COLUMN rule_scope INTEGER DEFAULT 0;

View File

@ -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,
}
}

View File

@ -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.