Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce regex2 to support perl regex #40

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"),
Expand Down
19 changes: 19 additions & 0 deletions codefresh/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down