diff --git a/bcs-services/bcs-bscp/cmd/data-service/service/app_template_variable.go b/bcs-services/bcs-bscp/cmd/data-service/service/app_template_variable.go index e0e4d4cad3..7b8b6a4881 100644 --- a/bcs-services/bcs-bscp/cmd/data-service/service/app_template_variable.go +++ b/bcs-services/bcs-bscp/cmd/data-service/service/app_template_variable.go @@ -15,6 +15,7 @@ package service import ( "bytes" "context" + "sort" "time" "bscp.io/pkg/dal/table" @@ -245,6 +246,9 @@ func (s *Service) getVariableReferences(kt *kit.Kit, tmplRevisions []*table.Temp } allVariables = tools.RemoveDuplicateStrings(allVariables) + // Sort in ascending order + sort.Strings(allVariables) + refs := make([]*pbatv.AppTemplateVariableReference, len(allVariables)) for idx, v := range allVariables { ref := &pbatv.AppTemplateVariableReference{ @@ -334,12 +338,32 @@ func (s *Service) ListAppTmplVariables(ctx context.Context, req *pbds.ListAppTmp // for unset variable, just return its name, other fields keep empty finalVar = append(finalVar, &pbtv.TemplateVariableSpec{Name: name}) } + finalVar = sortVariables(finalVar) return &pbds.ListAppTmplVariablesResp{ Details: finalVar, }, nil } +func sortVariables(vars []*pbtv.TemplateVariableSpec) []*pbtv.TemplateVariableSpec { + // Define a custom sorting function that sorts by the name field in ascending order. + sortByName := func(i, j int) bool { + return vars[i].Name < vars[j].Name + } + sort.Slice(vars, sortByName) + + // put the variables whose value is empty in front + var varsNoVal, varWithVal []*pbtv.TemplateVariableSpec + for _, v := range vars { + if v.DefaultVal == "" { + varsNoVal = append(varsNoVal, v) + } else { + varWithVal = append(varWithVal, v) + } + } + return append(varsNoVal, varWithVal...) +} + // ListReleasedAppTmplVariables get app template variable references. func (s *Service) ListReleasedAppTmplVariables(ctx context.Context, req *pbds.ListReleasedAppTmplVariablesReq) ( *pbds.ListReleasedAppTmplVariablesResp, error) { diff --git a/bcs-services/bcs-bscp/pkg/dal/dao/released_app_template_variable.go b/bcs-services/bcs-bscp/pkg/dal/dao/released_app_template_variable.go index 60dd6fa301..3c2c0c49cd 100644 --- a/bcs-services/bcs-bscp/pkg/dal/dao/released_app_template_variable.go +++ b/bcs-services/bcs-bscp/pkg/dal/dao/released_app_template_variable.go @@ -13,6 +13,8 @@ package dao import ( + "sort" + "bscp.io/pkg/dal/gen" "bscp.io/pkg/dal/table" "bscp.io/pkg/kit" @@ -74,5 +76,13 @@ func (dao *releasedAppTemplateVariableDao) ListVariables(kit *kit.Kit, bizID, ap if len(appVars) == 0 { return []*table.TemplateVariableSpec{}, nil } - return appVars[0].Spec.Variables, nil + vars := appVars[0].Spec.Variables + + // Define a custom sorting function that sorts by the name field in ascending order. + sortByName := func(i, j int) bool { + return vars[i].Name < vars[j].Name + } + sort.Slice(vars, sortByName) + + return vars, nil } diff --git a/bcs-services/bcs-bscp/pkg/tmplprocess/tmplprocessor.go b/bcs-services/bcs-bscp/pkg/tmplprocess/tmplprocessor.go index b4a3470524..a66de958a4 100644 --- a/bcs-services/bcs-bscp/pkg/tmplprocess/tmplprocessor.go +++ b/bcs-services/bcs-bscp/pkg/tmplprocess/tmplprocessor.go @@ -16,6 +16,7 @@ package tmplprocess import ( "fmt" "regexp" + "sort" "strings" ) @@ -53,6 +54,9 @@ func (p *processor) ExtractVariables(template []byte) []string { varNames = append(varNames, name) } + // Sort in ascending order + sort.Strings(varNames) + return varNames }