From fee39f4054c6023fef68ebe0228cda5ef444fb5c Mon Sep 17 00:00:00 2001 From: "Chang, Hui-Tang" Date: Tue, 22 Oct 2024 13:13:34 +0800 Subject: [PATCH] feat: standardize the tag naming convention (#767) Because - We didn't apply any naming convention on tags This commit - Standardizes the tag naming convention to match the following criteria: - All user-defined tags will be converted to lowercase. - All system/reserved tags will be capitalized. --- pkg/repository/transpiler.go | 3 ++- pkg/service/convert.go | 12 +++++++++--- pkg/service/pipeline.go | 27 ++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/pkg/repository/transpiler.go b/pkg/repository/transpiler.go index 0a05e9853..e4e904f17 100644 --- a/pkg/repository/transpiler.go +++ b/pkg/repository/transpiler.go @@ -2,6 +2,7 @@ package repository import ( "fmt" + "strings" "time" "github.com/iancoleman/strcase" @@ -218,7 +219,7 @@ func (t *transpiler) transpileComparisonCallExpr(e *expr.Expr, op interface{}) ( vars = append(vars, con.Vars...) case "tag": sql = "tag.tag_name = ?" - vars = append(vars, con.Vars...) + vars = append(vars, strings.ToLower(con.Vars[0].(string))) default: sql = fmt.Sprintf("%s = ?", ident.SQL) vars = append(vars, con.Vars...) diff --git a/pkg/service/convert.go b/pkg/service/convert.go index 023a3840a..8d58ad2c3 100644 --- a/pkg/service/convert.go +++ b/pkg/service/convert.go @@ -23,6 +23,8 @@ import ( "github.com/redis/go-redis/v9" "go.uber.org/zap" "golang.org/x/image/draw" + "golang.org/x/text/cases" + "golang.org/x/text/language" "google.golang.org/grpc/codes" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/structpb" @@ -558,9 +560,13 @@ func (c *converter) ConvertPipelineToPB(ctx context.Context, dbPipelineOrigin *d pbSharing.ShareCode.Code = dbPipeline.ShareCode } - tags := []string{} - for _, t := range dbPipeline.Tags { - tags = append(tags, t.TagName) + tags := make([]string, len(dbPipeline.Tags)) + for i, tag := range dbPipeline.TagNames() { + if slices.Contains(preserveTags, tag) { + tags[i] = cases.Title(language.English).String(tag) + } else { + tags[i] = tag + } } var pbRecipe *structpb.Struct diff --git a/pkg/service/pipeline.go b/pkg/service/pipeline.go index c3655b1ec..f7c2f7435 100644 --- a/pkg/service/pipeline.go +++ b/pkg/service/pipeline.go @@ -185,6 +185,25 @@ func (s *service) CreateNamespacePipeline(ctx context.Context, ns resource.Names if err != nil { return nil, err } + toCreatedTags := pbPipeline.GetTags() + toBeCreatedTagNames := make([]string, 0, len(toCreatedTags)) + for _, tag := range toCreatedTags { + tag = strings.ToLower(tag) + if !slices.Contains(preserveTags, tag) { + toBeCreatedTagNames = append(toBeCreatedTagNames, tag) + } + } + + if len(toBeCreatedTagNames) > 0 { + err = s.repository.CreatePipelineTags(ctx, dbCreatedPipeline.UID, toBeCreatedTagNames) + if err != nil { + return nil, err + } + dbCreatedPipeline, err = s.repository.GetNamespacePipelineByID(ctx, ownerPermalink, dbPipeline.ID, false, true) + if err != nil { + return nil, err + } + } pipeline, err := s.converter.ConvertPipelineToPB(ctx, dbCreatedPipeline, pipelinepb.Pipeline_VIEW_FULL, false, true) if err != nil { @@ -397,8 +416,14 @@ func (s *service) UpdateNamespacePipelineByID(ctx context.Context, ns resource.N } toUpdTags := toUpdPipeline.GetTags() - + for i := range toUpdTags { + toUpdTags[i] = strings.ToLower(toUpdTags[i]) + } currentTags := existingPipeline.TagNames() + for i := range currentTags { + currentTags[i] = strings.ToLower(currentTags[i]) + } + toBeCreatedTagNames := make([]string, 0, len(toUpdTags)) for _, tag := range toUpdTags { if !slices.Contains(currentTags, tag) && !slices.Contains(preserveTags, tag) {