Skip to content

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

fix(webhook): handle edge cases for `isSkipGeneratedSchemaFile` (#2335)

Joe Chenb25c30f8fee2
diff --git a/server/webhook.go b/server/webhook.go
index 10195ce240..794b49bc79 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -624,11 +624,13 @@ func isSkipGeneratedSchemaFile(repository *api.Repository, added string) bool {
"ENV_NAME",
"DB_NAME",
}
- schemafilePathRegex := repository.SchemaPathTemplate
+
+ // Escape "." characters to match literals instead of using it as a wildcard.
+ schemaFilePathRegex := strings.ReplaceAll(repository.SchemaPathTemplate, ".", `\.`)
for _, placeholder := range placeholderList {
- schemafilePathRegex = strings.ReplaceAll(schemafilePathRegex, fmt.Sprintf("{{%s}}", placeholder), fmt.Sprintf("(?P<%s>[a-zA-Z0-9+-=/_#?!$. ]+)", placeholder))
+ schemaFilePathRegex = strings.ReplaceAll(schemaFilePathRegex, fmt.Sprintf("{{%s}}", placeholder), fmt.Sprintf("(?P<%s>[a-zA-Z0-9+-=/_#?!$. ]+)", placeholder))
}
- myRegex, err := regexp.Compile(schemafilePathRegex)
+ myRegex, err := regexp.Compile(schemaFilePathRegex)
if err != nil {
log.Warn("Invalid schema path template.", zap.String("schema_path_template",
repository.SchemaPathTemplate),
diff --git a/server/webhook_test.go b/server/webhook_test.go
index 589b07aaba..b124838130 100644
--- a/server/webhook_test.go
+++ b/server/webhook_test.go
@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
+ "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/vcs/gitlab"
)
@@ -357,3 +358,31 @@ func TestParseBranchNameFromGitHubRefs(t *testing.T) {
}
}
}
+
+func TestIsSkipGeneratedSchemaFile(t *testing.T) {
+ tests := []struct {
+ repository *api.Repository
+ added string
+ want bool
+ }{
+ {
+ repository: &api.Repository{
+ SchemaPathTemplate: "{{ENV_NAME}}/.{{DB_NAME}}__LATEST.sql",
+ },
+ added: "Prod/.db__LATEST.sql",
+ want: true,
+ },
+ {
+ repository: &api.Repository{
+ SchemaPathTemplate: "{{ENV_NAME}}/.{{DB_NAME}}__LATEST.sql",
+ },
+ added: "Prod/Adb__LATEST.sql",
+ want: false,
+ },
+ }
+ for _, test := range tests {
+ t.Run(test.added, func(t *testing.T) {
+ assert.Equal(t, test.want, isSkipGeneratedSchemaFile(test.repository, test.added))
+ })
+ }
+}