Skip to content

Commit a6ceddb

Browse files
committed
Added support for Triggers attributes: BranchRegexInput, PRTargetBranch, CommentRegex, RuntimeEnvironment
1 parent b43f8a6 commit a6ceddb

File tree

5 files changed

+130
-42
lines changed

5 files changed

+130
-42
lines changed

client/pipeline.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,24 @@ type SpecTemplate struct {
3535
}
3636

3737
type Trigger struct {
38-
Name string `json:"name,omitempty"`
39-
Description string `json:"description,omitempty"`
40-
Type string `json:"type,omitempty"`
41-
Repo string `json:"repo,omitempty"`
42-
Events []string `json:"events,omitempty"`
43-
BranchRegex string `json:"branchRegex,omitempty"`
44-
ModifiedFilesGlob string `json:"modifiedFilesGlob,omitempty"`
45-
Provider string `json:"provider,omitempty"`
46-
Disabled bool `json:"disabled,omitempty"`
47-
PullRequestAllowForkEvents bool `json:"pullRequestAllowForkEvents,omitempty"`
48-
CommitStatusTitle string `json:"commitStatusTitle,omitempty"`
49-
Context string `json:"context,omitempty"`
50-
Contexts []string `json:"contexts,omitempty"`
51-
Variables []Variable `json:"variables,omitempty"`
38+
Name string `json:"name,omitempty"`
39+
Description string `json:"description,omitempty"`
40+
Type string `json:"type,omitempty"`
41+
Repo string `json:"repo,omitempty"`
42+
Events []string `json:"events,omitempty"`
43+
BranchRegex string `json:"branchRegex,omitempty"`
44+
BranchRegexInput string `json:"branchRegexInput,omitempty"`
45+
PullRequestTargetBranchRegex string `json:"pullRequestTargetBranchRegex,omitempty"`
46+
CommentRegex string `json:"commentRegex,omitempty"`
47+
ModifiedFilesGlob string `json:"modifiedFilesGlob,omitempty"`
48+
Provider string `json:"provider,omitempty"`
49+
Disabled bool `json:"disabled,omitempty"`
50+
PullRequestAllowForkEvents bool `json:"pullRequestAllowForkEvents,omitempty"`
51+
CommitStatusTitle string `json:"commitStatusTitle,omitempty"`
52+
Context string `json:"context,omitempty"`
53+
Contexts []string `json:"contexts,omitempty"`
54+
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
55+
Variables []Variable `json:"variables,omitempty"`
5256
}
5357

5458
type RuntimeEnvironment struct {

codefresh/resource_pipeline.go

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
910
)
1011

1112
func resourcePipeline() *schema.Resource {
@@ -127,9 +128,27 @@ func resourcePipeline() *schema.Resource {
127128
Optional: true,
128129
},
129130
"branch_regex": {
130-
Type: schema.TypeString,
131-
Optional: true,
132-
Default: "/.*/gi",
131+
Type: schema.TypeString,
132+
Optional: true,
133+
Default: "/.*/gi",
134+
ValidateFunc: validation.StringIsValidRegExp,
135+
},
136+
"branch_regex_input": {
137+
Type: schema.TypeString,
138+
Optional: true,
139+
Default: "regex",
140+
ValidateFunc: validation.StringInSlice([]string{"multiselect-exclude", "multiselect", "regex"}, false),
141+
},
142+
"pull_request_target_branch_regex": {
143+
Type: schema.TypeString,
144+
Optional: true,
145+
ValidateFunc: validation.StringIsValidRegExp,
146+
},
147+
"comment_regex": {
148+
Type: schema.TypeString,
149+
Optional: true,
150+
Default: "/.*/gi",
151+
ValidateFunc: validation.StringIsValidRegExp,
133152
},
134153
"modified_files_glob": {
135154
Type: schema.TypeString,
@@ -174,6 +193,30 @@ func resourcePipeline() *schema.Resource {
174193
Type: schema.TypeString,
175194
},
176195
},
196+
"runtime_environment": {
197+
Type: schema.TypeList,
198+
Optional: true,
199+
Elem: &schema.Resource{
200+
Schema: map[string]*schema.Schema{
201+
"name": {
202+
Type: schema.TypeString,
203+
Optional: true,
204+
},
205+
"memory": {
206+
Type: schema.TypeString,
207+
Optional: true,
208+
},
209+
"cpu": {
210+
Type: schema.TypeString,
211+
Optional: true,
212+
},
213+
"dind_storage": {
214+
Type: schema.TypeString,
215+
Optional: true,
216+
},
217+
},
218+
},
219+
},
177220
"variables": {
178221
Type: schema.TypeMap,
179222
Optional: true,
@@ -390,6 +433,9 @@ func flattenTriggers(triggers []cfClient.Trigger) []map[string]interface{} {
390433
m["contexts"] = trigger.Contexts
391434
m["repo"] = trigger.Repo
392435
m["branch_regex"] = trigger.BranchRegex
436+
m["branch_regex_input"] = trigger.BranchRegexInput
437+
m["pull_request_target_branch_regex"] = trigger.PullRequestTargetBranchRegex
438+
m["comment_regex"] = trigger.CommentRegex
393439
m["modified_files_glob"] = trigger.ModifiedFilesGlob
394440
m["disabled"] = trigger.Disabled
395441
m["pull_request_allow_fork_events"] = trigger.PullRequestAllowForkEvents
@@ -398,7 +444,9 @@ func flattenTriggers(triggers []cfClient.Trigger) []map[string]interface{} {
398444
m["type"] = trigger.Type
399445
m["events"] = trigger.Events
400446
m["variables"] = convertVariables(trigger.Variables)
401-
447+
if trigger.RuntimeEnvironment != (cfClient.RuntimeEnvironment{}) {
448+
m["runtime_environment"] = flattenSpecRuntimeEnvironment(trigger.RuntimeEnvironment)
449+
}
402450
res[i] = m
403451
}
404452
return res
@@ -460,19 +508,22 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
460508
events := d.Get(fmt.Sprintf("spec.0.trigger.%v.events", idx)).([]interface{})
461509
contexts := d.Get(fmt.Sprintf("spec.0.trigger.%v.contexts", idx)).([]interface{})
462510
codefreshTrigger := cfClient.Trigger{
463-
Name: d.Get(fmt.Sprintf("spec.0.trigger.%v.name", idx)).(string),
464-
Description: d.Get(fmt.Sprintf("spec.0.trigger.%v.description", idx)).(string),
465-
Type: d.Get(fmt.Sprintf("spec.0.trigger.%v.type", idx)).(string),
466-
Repo: d.Get(fmt.Sprintf("spec.0.trigger.%v.repo", idx)).(string),
467-
BranchRegex: d.Get(fmt.Sprintf("spec.0.trigger.%v.branch_regex", idx)).(string),
468-
ModifiedFilesGlob: d.Get(fmt.Sprintf("spec.0.trigger.%v.modified_files_glob", idx)).(string),
469-
Provider: d.Get(fmt.Sprintf("spec.0.trigger.%v.provider", idx)).(string),
470-
Disabled: d.Get(fmt.Sprintf("spec.0.trigger.%v.disabled", idx)).(bool),
471-
PullRequestAllowForkEvents: d.Get(fmt.Sprintf("spec.0.trigger.%v.pull_request_allow_fork_events", idx)).(bool),
472-
CommitStatusTitle: d.Get(fmt.Sprintf("spec.0.trigger.%v.commit_status_title", idx)).(string),
473-
Context: d.Get(fmt.Sprintf("spec.0.trigger.%v.context", idx)).(string),
474-
Contexts: convertStringArr(contexts),
475-
Events: convertStringArr(events),
511+
Name: d.Get(fmt.Sprintf("spec.0.trigger.%v.name", idx)).(string),
512+
Description: d.Get(fmt.Sprintf("spec.0.trigger.%v.description", idx)).(string),
513+
Type: d.Get(fmt.Sprintf("spec.0.trigger.%v.type", idx)).(string),
514+
Repo: d.Get(fmt.Sprintf("spec.0.trigger.%v.repo", idx)).(string),
515+
BranchRegex: d.Get(fmt.Sprintf("spec.0.trigger.%v.branch_regex", idx)).(string),
516+
BranchRegexInput: d.Get(fmt.Sprintf("spec.0.trigger.%v.branch_regex_input", idx)).(string),
517+
PullRequestTargetBranchRegex: d.Get(fmt.Sprintf("spec.0.trigger.%v.pull_request_target_branch_regex", idx)).(string),
518+
CommentRegex: d.Get(fmt.Sprintf("spec.0.trigger.%v.comment_regex", idx)).(string),
519+
ModifiedFilesGlob: d.Get(fmt.Sprintf("spec.0.trigger.%v.modified_files_glob", idx)).(string),
520+
Provider: d.Get(fmt.Sprintf("spec.0.trigger.%v.provider", idx)).(string),
521+
Disabled: d.Get(fmt.Sprintf("spec.0.trigger.%v.disabled", idx)).(bool),
522+
PullRequestAllowForkEvents: d.Get(fmt.Sprintf("spec.0.trigger.%v.pull_request_allow_fork_events", idx)).(bool),
523+
CommitStatusTitle: d.Get(fmt.Sprintf("spec.0.trigger.%v.commit_status_title", idx)).(string),
524+
Context: d.Get(fmt.Sprintf("spec.0.trigger.%v.context", idx)).(string),
525+
Contexts: convertStringArr(contexts),
526+
Events: convertStringArr(events),
476527
}
477528
variables := d.Get(fmt.Sprintf("spec.0.trigger.%v.variables", idx)).(map[string]interface{})
478529
codefreshTrigger.SetVariables(variables)

codefresh/resource_pipeline_test.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
206206
"master",
207207
"git",
208208
"commits",
209-
"git",
209+
"/master/gi",
210+
"multiselect",
211+
"/master/gi",
212+
"/PR comment/gi",
210213
"shared_context1",
214+
"git",
211215
"push.heads",
212216
"codefresh-contrib/react-sample-app",
213217
"tags",
@@ -222,6 +226,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
222226
Check: resource.ComposeTestCheckFunc(
223227
testAccCheckCodefreshPipelineExists(resourceName),
224228
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.#", "2"),
229+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/master/gi"),
230+
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"),
225233
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.name", "commits"),
226234
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.name", "tags"),
227235
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2"),
@@ -240,8 +248,12 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
240248
"master",
241249
"git",
242250
"commits",
243-
"git",
251+
"/release/gi",
252+
"multiselect-exclude",
253+
"/release/gi",
254+
"/PR comment2/gi",
244255
"shared_context1_update",
256+
"git",
245257
"push.heads",
246258
"codefresh-contrib/react-sample-app",
247259
"tags",
@@ -255,6 +267,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
255267
),
256268
Check: resource.ComposeTestCheckFunc(
257269
testAccCheckCodefreshPipelineExists(resourceName),
270+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex", "/release/gi"),
271+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.branch_regex_input", "multiselect-exclude"),
272+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.pull_request_target_branch_regex", "/release/gi"),
273+
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/PR comment2/gi"),
258274
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.variables.triggerTestVar", "triggerTestValue"),
259275
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2_update"),
260276
),
@@ -493,6 +509,10 @@ func testAccCodefreshPipelineBasicConfigTriggers(
493509
revision,
494510
context,
495511
trigger1Name,
512+
trigger1Regex,
513+
trigger1RegexInput,
514+
trigger1PrTargetBranchRegex,
515+
trigger1CommentRegex,
496516
trigger1Context,
497517
trigger1Contexts,
498518
trigger1Event,
@@ -517,16 +537,20 @@ resource "codefresh_pipeline" "test" {
517537
name = "%s"
518538
519539
spec {
520-
spec_template {
521-
repo = %q
522-
path = %q
523-
revision = %q
524-
context = %q
525-
}
526-
527-
trigger {
540+
spec_template {
541+
repo = %q
542+
path = %q
543+
revision = %q
544+
context = %q
545+
}
546+
547+
trigger {
528548
name = %q
529-
branch_regex = "/.*/gi"
549+
branch_regex = %q
550+
branch_regex_input = %q
551+
pull_request_target_branch_regex = %q
552+
comment_regex = %q
553+
530554
context = %q
531555
contexts = [
532556
%q
@@ -575,6 +599,10 @@ resource "codefresh_pipeline" "test" {
575599
revision,
576600
context,
577601
trigger1Name,
602+
trigger1Regex,
603+
trigger1RegexInput,
604+
trigger1PrTargetBranchRegex,
605+
trigger1CommentRegex,
578606
trigger1Context,
579607
trigger1Contexts,
580608
trigger1Event,

docs/resources/pipeline.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ resource "codefresh_pipeline" "test" {
126126
- `type` - (Optional) The trigger type. Default value - **git**.
127127
- `repo` - (Optional) The GitHub `account/repo_name`.
128128
- `branch_regex` - (Optional) A regular expression and will only trigger for branches that match this naming pattern.
129+
- `branch_regex_input` - (Optional) Flag to manage how the `branch_regex` field is interpreted. Possible values: "multiselect-exclude", "multiselect", "regex". Default: "regex"
130+
- `pull_request_target_branch_regex` - (Optional) A regular expression and will only trigger for pull requests to branches that match this naming pattern.
131+
- `comment_regex` - (Optional) A regular expression and will only trigger for pull requests where a comment matches this naming pattern.
129132
- `modified_files_glob` - (Optional) Allows to constrain the build and trigger it only if the modified files from the commit match this glob expression.
130133
- `events` - (Optional) A list of GitHub events for which a Pipeline is triggered. Default value - **push.heads**.
131134
- `provider` - (Optional) Default value - **github**.
@@ -135,6 +138,7 @@ resource "codefresh_pipeline" "test" {
135138
- `disabled` - (Optional) Boolean. If false, trigger will never be activated.
136139
- `pull_request_allow_fork_events` - (Optional) Boolean. If this trigger is also applicable to Git forks.
137140
- `contexts` - (Optional) A list of strings representing the contexts ([shared_configuration](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/shared-configuration/)) to be loaded when the trigger is executed
141+
- `runtime_environment` - (Optional) A collection of `runtime_environment` blocks as documented below.
138142
---
139143

140144
`runtime_environment` supports the following:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/client9/misspell v0.3.4
88
github.com/golangci/golangci-lint v1.27.0
99
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
10+
github.com/hashicorp/terraform-plugin-sdk v1.7.0
1011
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.0-rc.2.0.20200717132200-7435e2abc9d1
1112
github.com/imdario/mergo v0.3.9
1213
github.com/stretchr/objx v0.1.1

0 commit comments

Comments
 (0)