Skip to content

Commit

Permalink
Add "public" metadata section in project's metadata and use it for di…
Browse files Browse the repository at this point in the history
…splay name (#2702)

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Mar 18, 2024
1 parent 8a0bc3f commit a5e688f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
17 changes: 16 additions & 1 deletion internal/controlplane/handlers_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/google/uuid"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -57,10 +58,24 @@ func (s *Server) ListProjects(
if err != nil {
return nil, status.Errorf(codes.Internal, "error getting project: %v", err)
}

var description, displayName string
meta, err := projects.ParseMetadata(&project)
// ignore error if we can't parse the metadata. This information is not critical... yet.
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to parse metadata")
description = ""
displayName = project.Name
} else {
description = meta.Public.Description
displayName = meta.Public.DisplayName
}

resp.Projects = append(resp.Projects, &minderv1.Project{
ProjectId: project.ID.String(),
Name: project.Name,
Description: "",
Description: description,
DisplayName: displayName,
CreatedAt: timestamppb.New(project.CreatedAt),
UpdatedAt: timestamppb.New(project.UpdatedAt),
})
Expand Down
5 changes: 3 additions & 2 deletions internal/projects/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ProvisionSelfEnrolledProject(
projectName string,
userSub string,
) (outproj *pb.Project, projerr error) {
projectmeta := NewSelfEnrolledMetadata()
projectmeta := NewSelfEnrolledMetadata(projectName)

jsonmeta, err := json.Marshal(&projectmeta)
if err != nil {
Expand Down Expand Up @@ -76,7 +76,8 @@ func ProvisionSelfEnrolledProject(
prj := pb.Project{
ProjectId: project.ID.String(),
Name: project.Name,
Description: projectmeta.Description,
Description: projectmeta.Public.Description,
DisplayName: projectmeta.Public.DisplayName,
CreatedAt: timestamppb.New(project.CreatedAt),
UpdatedAt: timestamppb.New(project.UpdatedAt),
}
Expand Down
42 changes: 40 additions & 2 deletions internal/projects/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
// Package projects contains utilities for working with projects.
package projects

import (
"encoding/json"

"github.com/stacklok/minder/internal/db"
)

const (
// MinderMetadataVersion is the version of the metadata format.
MinderMetadataVersion = "v1alpha1"
Expand All @@ -25,16 +31,48 @@ const (
type Metadata struct {
Version string `json:"version"`
SelfEnrolled bool `json:"self_enrolled"`
Description string `json:"description"`

// This will be deprecated in favor of PublicMetadataV1.
Description string `json:"description"`

// TODO: Add more metadata fields here.
// e.g. vendor-specific fields

// Public is a field that is meant to be read by other systems.
// It will be exposed to the public, e.g. via a UI.
Public PublicMetadataV1 `json:"public"`
}

// PublicMetadataV1 contains public metadata relevant for a project.
type PublicMetadataV1 struct {
Description string `json:"description"`
DisplayName string `json:"display_name"`
}

// NewSelfEnrolledMetadata returns a new Metadata object with the SelfEnrolled field set to true.
func NewSelfEnrolledMetadata() Metadata {
func NewSelfEnrolledMetadata(projectName string) Metadata {
return Metadata{
Version: MinderMetadataVersion,
SelfEnrolled: true,
// These will be editable by the user.
Public: PublicMetadataV1{
Description: "A self-enrolled project.",
DisplayName: projectName,
},
}
}

// ParseMetadata parses the given JSON data into a Metadata object.
func ParseMetadata(proj *db.Project) (*Metadata, error) {
var meta Metadata
if err := json.Unmarshal(proj.Metadata, &meta); err != nil {
return nil, err
}

// default the display name to the project name if it's not set
if meta.Public.DisplayName == "" {
meta.Public.DisplayName = proj.Name
}

return &meta, nil
}

0 comments on commit a5e688f

Please sign in to comment.