Skip to content

Commit 6eeaf1c

Browse files
feat: introduce regex2 to support perl regex (#40)
1 parent b4ad609 commit 6eeaf1c

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

codefresh/resource_pipeline.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func resourcePipeline() *schema.Resource {
142142
Type: schema.TypeString,
143143
Optional: true,
144144
Default: "/.*/gi",
145-
ValidateFunc: validation.StringIsValidRegExp,
145+
ValidateFunc: stringIsValidRe2RegExp,
146146
},
147147
"branch_regex_input": {
148148
Type: schema.TypeString,
@@ -153,13 +153,13 @@ func resourcePipeline() *schema.Resource {
153153
"pull_request_target_branch_regex": {
154154
Type: schema.TypeString,
155155
Optional: true,
156-
ValidateFunc: validation.StringIsValidRegExp,
156+
ValidateFunc: stringIsValidRe2RegExp,
157157
},
158158
"comment_regex": {
159159
Type: schema.TypeString,
160160
Optional: true,
161161
Default: "/.*/gi",
162-
ValidateFunc: validation.StringIsValidRegExp,
162+
ValidateFunc: stringIsValidRe2RegExp,
163163
},
164164
"modified_files_glob": {
165165
Type: schema.TypeString,
@@ -260,7 +260,7 @@ func resourcePipeline() *schema.Resource {
260260
"branch_name": {
261261
Type: schema.TypeString,
262262
Optional: true,
263-
ValidateFunc: validation.StringIsValidRegExp,
263+
ValidateFunc: stringIsValidRe2RegExp,
264264
ConflictsWith: []string{"spec.0.termination_policy.0.on_create_branch.0.ignore_branch"},
265265
},
266266
"ignore_trigger": {

codefresh/resource_pipeline_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
206206
"master",
207207
"git",
208208
"commits",
209-
"/master/gi",
209+
"/^(?!(master)$).*/gi",
210210
"multiselect",
211-
"/master/gi",
212-
"/PR comment/gi",
211+
"/^(?!(master)$).*/gi",
212+
"/^PR comment$/gi",
213213
"shared_context1",
214214
"git",
215215
"push.heads",
@@ -226,10 +226,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
226226
Check: resource.ComposeTestCheckFunc(
227227
testAccCheckCodefreshPipelineExists(resourceName),
228228
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.#", "2"),
229-
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/master/gi"),
229+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/^(?!(master)$).*/gi"),
230230
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex_input", "multiselect"),
231-
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.pull_request_target_branch_regex", "/master/gi"),
232-
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/PR comment/gi"),
231+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.pull_request_target_branch_regex", "/^(?!(master)$).*/gi"),
232+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/^PR comment$/gi"),
233233
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.name", "commits"),
234234
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.name", "tags"),
235235
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2"),

codefresh/utils.go

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"regexp"
77

88
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
9+
"github.com/dlclark/regexp2"
910
"github.com/ghodss/yaml"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
)
@@ -97,3 +98,21 @@ func suppressEquivalentYamlDiffs(k, old, new string, d *schema.ResourceData) boo
9798

9899
return normalizedOld == normalizedNew
99100
}
101+
102+
// This function has the same structure of StringIsValidRegExp from the terraform plugin SDK
103+
// https://github.com/hashicorp/terraform-plugin-sdk/blob/695f0c7b92e26444786b8963e00c665f1b4ef400/helper/validation/strings.go#L225
104+
// It has been modified to use the library https://github.com/dlclark/regexp2 instead of the standard regex golang package
105+
// in order to support complex regular expressions including perl regex syntax
106+
func stringIsValidRe2RegExp(i interface{}, k string) (warnings []string, errors []error) {
107+
v, ok := i.(string)
108+
if !ok {
109+
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
110+
return warnings, errors
111+
}
112+
113+
if _, err := regexp2.Compile(v, regexp2.RE2); err != nil {
114+
errors = append(errors, fmt.Errorf("%q: %s", k, err))
115+
}
116+
117+
return warnings, errors
118+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/bflad/tfproviderdocs v0.6.0
77
github.com/bflad/tfproviderlint v0.14.0
88
github.com/client9/misspell v0.3.4
9+
github.com/dlclark/regexp2 v1.4.0
910
github.com/ghodss/yaml v1.0.0
1011
github.com/golangci/golangci-lint v1.27.0
1112
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
7474
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7575
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
7676
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
77+
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
78+
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
7779
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
7880
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
7981
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=

0 commit comments

Comments
 (0)