diff --git a/codefresh/resource_pipeline.go b/codefresh/resource_pipeline.go index 86f7af1..100b1b6 100644 --- a/codefresh/resource_pipeline.go +++ b/codefresh/resource_pipeline.go @@ -142,7 +142,7 @@ func resourcePipeline() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "/.*/gi", - ValidateFunc: validation.StringIsValidRegExp, + ValidateFunc: stringIsValidRe2RegExp, }, "branch_regex_input": { Type: schema.TypeString, @@ -153,13 +153,13 @@ func resourcePipeline() *schema.Resource { "pull_request_target_branch_regex": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringIsValidRegExp, + ValidateFunc: stringIsValidRe2RegExp, }, "comment_regex": { Type: schema.TypeString, Optional: true, Default: "/.*/gi", - ValidateFunc: validation.StringIsValidRegExp, + ValidateFunc: stringIsValidRe2RegExp, }, "modified_files_glob": { Type: schema.TypeString, @@ -260,7 +260,7 @@ func resourcePipeline() *schema.Resource { "branch_name": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringIsValidRegExp, + ValidateFunc: stringIsValidRe2RegExp, ConflictsWith: []string{"spec.0.termination_policy.0.on_create_branch.0.ignore_branch"}, }, "ignore_trigger": { diff --git a/codefresh/resource_pipeline_test.go b/codefresh/resource_pipeline_test.go index a08ca82..0229710 100644 --- a/codefresh/resource_pipeline_test.go +++ b/codefresh/resource_pipeline_test.go @@ -206,10 +206,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) { "master", "git", "commits", - "/master/gi", + "/^(?!(master)$).*/gi", "multiselect", - "/master/gi", - "/PR comment/gi", + "/^(?!(master)$).*/gi", + "/^PR comment$/gi", "shared_context1", "git", "push.heads", @@ -226,10 +226,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckCodefreshPipelineExists(resourceName), resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.#", "2"), - resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/master/gi"), + resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/^(?!(master)$).*/gi"), resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex_input", "multiselect"), - resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.pull_request_target_branch_regex", "/master/gi"), - resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/PR comment/gi"), + resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.pull_request_target_branch_regex", "/^(?!(master)$).*/gi"), + resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/^PR comment$/gi"), resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.name", "commits"), resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.name", "tags"), resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2"), diff --git a/codefresh/utils.go b/codefresh/utils.go index bd75e77..91c2094 100644 --- a/codefresh/utils.go +++ b/codefresh/utils.go @@ -6,6 +6,7 @@ import ( "regexp" cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" + "github.com/dlclark/regexp2" "github.com/ghodss/yaml" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -97,3 +98,21 @@ func suppressEquivalentYamlDiffs(k, old, new string, d *schema.ResourceData) boo return normalizedOld == normalizedNew } + +// This function has the same structure of StringIsValidRegExp from the terraform plugin SDK +// https://github.com/hashicorp/terraform-plugin-sdk/blob/695f0c7b92e26444786b8963e00c665f1b4ef400/helper/validation/strings.go#L225 +// It has been modified to use the library https://github.com/dlclark/regexp2 instead of the standard regex golang package +// in order to support complex regular expressions including perl regex syntax +func stringIsValidRe2RegExp(i interface{}, k string) (warnings []string, errors []error) { + v, ok := i.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected type of %s to be string", k)) + return warnings, errors + } + + if _, err := regexp2.Compile(v, regexp2.RE2); err != nil { + errors = append(errors, fmt.Errorf("%q: %s", k, err)) + } + + return warnings, errors +} diff --git a/go.mod b/go.mod index d3f90f2..d243ede 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/bflad/tfproviderdocs v0.6.0 github.com/bflad/tfproviderlint v0.14.0 github.com/client9/misspell v0.3.4 + github.com/dlclark/regexp2 v1.4.0 github.com/ghodss/yaml v1.0.0 github.com/golangci/golangci-lint v1.27.0 github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect diff --git a/go.sum b/go.sum index 030134c..1f23cf9 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=