Skip to content

Commit

Permalink
Add display name for profiles (#2724)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Mar 20, 2024
1 parent c225496 commit 36cb6b9
Show file tree
Hide file tree
Showing 12 changed files with 952 additions and 847 deletions.
15 changes: 15 additions & 0 deletions database/migrations/000033_profile_display_name.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 profiles DROP COLUMN display_name;
17 changes: 17 additions & 0 deletions database/migrations/000033_profile_display_name.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- 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.

-- Adds a `display_name` column to the `profiles` table. The display name defaults
-- to the profile's `name` column, but can be overridden by the user.
ALTER TABLE profiles ADD COLUMN display_name TEXT NOT NULL DEFAULT '';
8 changes: 5 additions & 3 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ INSERT INTO profiles (
alert,
name,
provider_id,
subscription_id
) VALUES ($1, $2, $3, $4, $5, sqlc.arg(provider_id), sqlc.narg(subscription_id)) RETURNING *;
subscription_id,
display_name
) VALUES ($1, $2, $3, $4, $5, sqlc.arg(provider_id), sqlc.narg(subscription_id), sqlc.arg(display_name)) RETURNING *;

-- name: UpdateProfile :one
UPDATE profiles SET
remediate = $3,
alert = $4,
updated_at = NOW()
updated_at = NOW(),
display_name = sqlc.arg(display_name)
WHERE id = $1 AND project_id = $2 RETURNING *;

-- name: CreateProfileForEntity :one
Expand Down
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.

14 changes: 13 additions & 1 deletion internal/controlplane/handlers_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,10 @@ func (s *Server) PatchProfile(ctx context.Context, ppr *minderv1.PatchProfileReq
return nil, status.Errorf(codes.InvalidArgument, "cannot patch profile from bundle")
}

params := db.UpdateProfileParams{ID: profileID, ProjectID: entityCtx.Project.ID}
params := db.UpdateProfileParams{
ID: profileID,
ProjectID: entityCtx.Project.ID,
}

// we check the pointers explicitly because the zero value of a string is valid
// value that means "use default" and we want to distinguish that from "not set in the patch"
Expand All @@ -583,6 +586,15 @@ func (s *Server) PatchProfile(ctx context.Context, ppr *minderv1.PatchProfileReq
params.Alert = oldProfile.Alert
}

// if the display name is set in the patch, use it, otherwise use the old display name or the name
if patch.GetDisplayName() != "" {
params.DisplayName = patch.GetDisplayName()
} else if oldProfile.DisplayName != "" {
params.DisplayName = oldProfile.DisplayName
} else {
params.DisplayName = oldProfile.Name
}

// Update top-level profile db object
_, err = s.store.UpdateProfile(ctx, params)
if err != nil {
Expand Down
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.

45 changes: 31 additions & 14 deletions internal/db/profiles.sql.go

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

11 changes: 9 additions & 2 deletions internal/engine/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,16 @@ func MergeDatabaseListIntoProfiles(ppl []db.ListProfilesByProjectIDRow) map[stri
if _, ok := profiles[p.Name]; !ok {
profileID := p.ID.String()
project := p.ProjectID.String()

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

profiles[p.Name] = &pb.Profile{
Id: &profileID,
Name: p.Name,
Id: &profileID,
Name: p.Name,
DisplayName: displayName,
Context: &pb.Context{
Provider: &p.Provider,
Project: &project,
Expand Down
22 changes: 18 additions & 4 deletions internal/profiles/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,18 @@ func (p *profileService) CreateSubscriptionProfile(

qtx := p.store.GetQuerierWithTransaction(tx)

displayName := profile.GetDisplayName()
// if empty use the name
if displayName == "" {
displayName = profile.GetName()
}

params := db.CreateProfileParams{
Provider: provider.Name,
ProviderID: provider.ID,
ProjectID: projectID,
Name: profile.GetName(),
DisplayName: displayName,
Remediate: db.ValidateRemediateType(profile.GetRemediate()),
Alert: db.ValidateAlertType(profile.GetAlert()),
SubscriptionID: uuid.NullUUID{UUID: subscriptionID, Valid: subscriptionID != uuid.Nil},
Expand Down Expand Up @@ -277,12 +284,19 @@ func (p *profileService) UpdateSubscriptionProfile(
return nil, status.Errorf(codes.Internal, "error fetching profile to be updated: %v", err)
}

displayName := profile.GetDisplayName()
// if empty use the name
if displayName == "" {
displayName = profile.GetName()
}

// Update top-level profile db object
updatedProfile, err := qtx.UpdateProfile(ctx, db.UpdateProfileParams{
ProjectID: projectID,
ID: oldDBProfile.ID,
Remediate: db.ValidateRemediateType(profile.GetRemediate()),
Alert: db.ValidateAlertType(profile.GetAlert()),
ProjectID: projectID,
ID: oldDBProfile.ID,
DisplayName: displayName,
Remediate: db.ValidateRemediateType(profile.GetRemediate()),
Alert: db.ValidateAlertType(profile.GetAlert()),
})
if err != nil {
return nil, status.Errorf(codes.Internal, "error updating profile: %v", err)
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 36cb6b9

Please sign in to comment.