Skip to content

Commit

Permalink
Add display names to rule types (#2824)
Browse files Browse the repository at this point in the history
This adds a display name to rule types which defaults to the rule type
name.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Mar 27, 2024
1 parent 6186d55 commit 809687a
Show file tree
Hide file tree
Showing 13 changed files with 1,148 additions and 1,060 deletions.
15 changes: 15 additions & 0 deletions database/migrations/000039_ruletype_displayname.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TABLE rule_type DROP COLUMN display_name;
15 changes: 15 additions & 0 deletions database/migrations/000039_ruletype_displayname.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TABLE rule_type ADD COLUMN display_name TEXT NOT NULL DEFAULT '';
8 changes: 5 additions & 3 deletions database/query/rule_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ INSERT INTO rule_type (
definition,
severity_value,
provider_id,
subscription_id
subscription_id,
display_name
) VALUES (
$1,
$2,
Expand All @@ -18,7 +19,8 @@ INSERT INTO rule_type (
sqlc.arg(definition)::jsonb,
sqlc.arg(severity_value),
sqlc.arg(provider_id),
sqlc.narg(subscription_id)
sqlc.narg(subscription_id),
sqlc.arg(display_name)
) RETURNING *;

-- name: ListRuleTypesByProviderAndProject :many
Expand All @@ -35,6 +37,6 @@ DELETE FROM rule_type WHERE id = $1;

-- name: UpdateRuleType :one
UPDATE rule_type
SET description = $2, definition = sqlc.arg(definition)::jsonb, severity_value = sqlc.arg(severity_value)
SET description = $2, definition = sqlc.arg(definition)::jsonb, severity_value = sqlc.arg(severity_value), display_name = sqlc.arg(display_name)
WHERE id = $1
RETURNING *;
1 change: 1 addition & 0 deletions docs/docs/ref/proto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions internal/db/rule_types.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions internal/engine/rule_type_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,15 @@ func RuleTypePBFromDB(rt *db.RuleType) (*minderv1.RuleType, error) {
seval = minderv1.Severity_VALUE_UNKNOWN
}

displayName := rt.DisplayName
if displayName == "" {
displayName = rt.Name
}

return &minderv1.RuleType{
Id: &id,
Name: rt.Name,
Id: &id,
Name: rt.Name,
DisplayName: displayName,
Context: &minderv1.Context{
Provider: &rt.Provider,
Project: &project,
Expand Down
4 changes: 4 additions & 0 deletions internal/ruletypes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ func (_ *ruleTypeService) CreateRuleType(
return nil, err
}

ruleType = ruleType.WithDefaultDisplayName()
newDBRecord, err := qtx.CreateRuleType(ctx, db.CreateRuleTypeParams{
Name: ruleTypeName,
DisplayName: ruleType.GetDisplayName(),
Provider: provider.Name,
ProviderID: provider.ID,
ProjectID: projectID,
Expand Down Expand Up @@ -214,11 +216,13 @@ func (_ *ruleTypeService) UpdateRuleType(
return nil, err
}

ruleType = ruleType.WithDefaultDisplayName()
updatedRuleType, err := qtx.UpdateRuleType(ctx, db.UpdateRuleTypeParams{
ID: oldRuleType.ID,
Description: ruleType.GetDescription(),
Definition: serializedRule,
SeverityValue: *severity,
DisplayName: ruleType.GetDisplayName(),
})
if err != nil {
return nil, fmt.Errorf("failed to update rule type: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/ruletypes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func TestRuleTypeService(t *testing.T) {
require.Equal(t, scenario.RuleType.Id, res.Id)
require.Equal(t, scenario.RuleType.Description, res.Description)
require.Equal(t, scenario.RuleType.Name, res.Name)
// By default this should be the name
require.Equal(t, scenario.RuleType.Name, res.DisplayName)
require.Equal(t, scenario.RuleType.Severity.Value, res.Severity.Value)
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/openapi/minder/v1/minder.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 809687a

Please sign in to comment.