Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: create issue error when DBNameTemplate include regex string (#1295)

Cluas9d8549053340
diff --git a/api/project.go b/api/project.go
index 49c9ceb03f..d6422bdfc6 100644
--- a/api/project.go
+++ b/api/project.go
@@ -252,6 +252,17 @@ func ValidateProjectDBNameTemplate(template string) error {
// FormatTemplate formats the template.
func FormatTemplate(template string, tokens map[string]string) (string, error) {
+ keys, _ := getTemplateTokens(template)
+ for _, key := range keys {
+ if _, ok := tokens[key]; !ok {
+ return "", fmt.Errorf("token %q not found", key)
+ }
+ template = strings.ReplaceAll(template, key, tokens[key])
+ }
+ return template, nil
+}
+
+func buildTemplateExpr(template string, tokens map[string]string) (string, error) {
keys, fixed := getTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
@@ -264,7 +275,6 @@ func FormatTemplate(template string, tokens map[string]string) (string, error) {
if quoteKey != key {
template = strings.Replace(template, key, quoteKey, 1)
}
-
}
return template, nil
}
@@ -291,7 +301,7 @@ func GetBaseDatabaseName(databaseName, dbNameTemplate, labelsJSON string) (strin
}
labelMap["{{DB_NAME}}"] = "(?P<NAME>.+)"
- expr, err := FormatTemplate(dbNameTemplate, labelMap)
+ expr, err := buildTemplateExpr(dbNameTemplate, labelMap)
if err != nil {
return "", fmt.Errorf("FormatTemplate(%q, %+v) failed with error: %v", dbNameTemplate, labelMap, err)
}
diff --git a/api/project_test.go b/api/project_test.go
index 1e9f91d056..14eadace8c 100644
--- a/api/project_test.go
+++ b/api/project_test.go
@@ -197,7 +197,7 @@ func TestFormatTemplate(t *testing.T) {
"{{DB_NAME}}": "db1",
"{{LOCATION}}": "us-central1",
},
- "db1_hello_\\$us-central1",
+ "db1_hello_$us-central1",
"",
},
}
@@ -213,6 +213,56 @@ func TestFormatTemplate(t *testing.T) {
}
}
+func TestBuildTemplateExpr(t *testing.T) {
+ tests := []struct {
+ name string
+ template string
+ tokens map[string]string
+ want string
+ errPart string
+ }{
+ {
+ "valid",
+ "{{DB_NAME}}_hello-{{LOCATION}}",
+ map[string]string{
+ "{{DB_NAME}}": "db1",
+ "{{LOCATION}}": "us-central1",
+ },
+ "db1_hello-us-central1",
+ "",
+ },
+ {
+ "tokenNotFound",
+ "{{DB_NAME}}_hello_{{LOCATION}}",
+ map[string]string{
+ "{{DB_NAME}}": "db1",
+ },
+ "",
+ "not found",
+ },
+ {
+ "template with regex meta",
+ "{{DB_NAME}}_hello_${{LOCATION}}",
+ map[string]string{
+ "{{DB_NAME}}": "db1",
+ "{{LOCATION}}": "us-central1",
+ },
+ "db1_hello_\\$us-central1",
+ "",
+ },
+ }
+
+ for _, test := range tests {
+ got, err := buildTemplateExpr(test.template, test.tokens)
+ if test.errPart == "" {
+ require.NoError(t, err)
+ } else {
+ require.Contains(t, err.Error(), test.errPart)
+ }
+ require.Equal(t, got, test.want)
+ }
+}
+
func TestGetBaseDatabaseName(t *testing.T) {
tests := []struct {
name string
diff --git a/server/issue.go b/server/issue.go
index de17c6b231..70cb118fea 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -692,9 +692,6 @@ func (s *Server) getPipelineCreate(ctx context.Context, issueCreate *api.IssueCr
}
baseDBName := d.DatabaseName
- if err != nil {
- return nil, fmt.Errorf("api.GetBaseDatabaseName(%q, %q) failed, error: %v", d.DatabaseName, project.DBNameTemplate, err)
- }
deployments, matrix, err := s.getTenantDatabaseMatrix(ctx, issueCreate.ProjectID, project.DBNameTemplate, dbList, baseDBName)
if err != nil {
return nil, err