Skip to content

Commit

Permalink
feat: standardize the tag naming convention (#767)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
donch1989 authored and jvallesm committed Oct 29, 2024
1 parent dcae037 commit fee39f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/repository/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"fmt"
"strings"
"time"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -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...)
Expand Down
12 changes: 9 additions & 3 deletions pkg/service/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion pkg/service/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fee39f4

Please sign in to comment.