diff --git a/models/models_methods_test.go b/models/models_methods_test.go index 5d0afbdc5..19cdcc548 100644 --- a/models/models_methods_test.go +++ b/models/models_methods_test.go @@ -62,6 +62,12 @@ trigger_map: workflow: check - tag: '*' workflow: deploy +- type: pull_request + pull_request_label: label + pull_request_comment: comment + commit_message: message + changed_files: file + workflow: deploy workflows: wip-deploy: @@ -527,6 +533,10 @@ trigger_map: workflow: test - pull_request_target_branch: '*' pull_request_label: "[workflow: check]" + changed_files: + regex: '^ios\/.*' + pull_request_comment: comment + commit_message: message workflow: check - tag: '*' workflow: deploy diff --git a/models/trigger_map_item.go b/models/trigger_map_item.go index 9afa3d3f3..5aad26c44 100644 --- a/models/trigger_map_item.go +++ b/models/trigger_map_item.go @@ -44,7 +44,9 @@ type TriggerMapItemModel struct { WorkflowID string `json:"workflow,omitempty" yaml:"workflow,omitempty"` // Code Push Item conditions - PushBranch interface{} `json:"push_branch,omitempty" yaml:"push_branch,omitempty"` + PushBranch interface{} `json:"push_branch,omitempty" yaml:"push_branch,omitempty"` + + // Code Push and Pull Request Item conditions CommitMessage interface{} `json:"commit_message,omitempty" yaml:"commit_message,omitempty"` ChangedFiles interface{} `json:"changed_files,omitempty" yaml:"changed_files,omitempty"` @@ -56,6 +58,7 @@ type TriggerMapItemModel struct { PullRequestTargetBranch interface{} `json:"pull_request_target_branch,omitempty" yaml:"pull_request_target_branch,omitempty"` DraftPullRequestEnabled *bool `json:"draft_pull_request_enabled,omitempty" yaml:"draft_pull_request_enabled,omitempty"` PullRequestLabel interface{} `json:"pull_request_label,omitempty" yaml:"pull_request_label,omitempty"` + PullRequestComment interface{} `json:"pull_request_comment,omitempty" yaml:"pull_request_comment,omitempty"` // Deprecated properties Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"` @@ -159,6 +162,15 @@ func (item TriggerMapItemModel) Normalized(idx int) (TriggerMapItemModel, error) item.CommitMessage = value } + mapInterface, ok = item.PullRequestComment.(map[interface{}]interface{}) + if ok { + value, err := castInterfaceKeysToStringKeys(idx, "pull_request_comment", mapInterface) + if err != nil { + return TriggerMapItemModel{}, err + } + item.PullRequestComment = value + } + mapInterface, ok = item.ChangedFiles.(map[interface{}]interface{}) if ok { value, err := castInterfaceKeysToStringKeys(idx, "changed_files", mapInterface) @@ -331,6 +343,9 @@ func (item TriggerMapItemModel) validateType(idx int) error { if err := item.validateNoPullRequestConditionsSet(idx, field); err != nil { return err } + if err := item.validateNoCodePushOrPullRequestCommonConditionsSet(idx, field); err != nil { + return err + } return nil } @@ -362,18 +377,25 @@ func (item TriggerMapItemModel) validateConditionValues(idx int) error { if err := validateStringOrRegexType(idx, "pull_request_label", item.PullRequestLabel); err != nil { return err } + if err := validateStringOrRegexType(idx, "pull_request_comment", item.PullRequestComment); err != nil { + return err + } return nil } -func (item TriggerMapItemModel) validateNoCodePushConditionsSet(idx int, field string) error { - if isStringLiteralOrRegexSet(item.PushBranch) { - return fmt.Errorf("trigger item #%d: both %s and push_branch defined", idx+1, field) - } +func (item TriggerMapItemModel) validateNoCodePushOrPullRequestCommonConditionsSet(idx int, field string) error { if isStringLiteralOrRegexSet(item.CommitMessage) { - return fmt.Errorf("trigger item #%d: both %s and commit_message defined", idx+1, field) + return fmt.Errorf("trigger item #%d: both %s and commit_message defined", idx+1, field) } if isStringLiteralOrRegexSet(item.ChangedFiles) { - return fmt.Errorf("trigger item #%d: both %s and changed_files defined", idx+1, field) + return fmt.Errorf("trigger item #%d: both %s and changed_files defined", idx+1, field) + } + return nil +} + +func (item TriggerMapItemModel) validateNoCodePushConditionsSet(idx int, field string) error { + if isStringLiteralOrRegexSet(item.PushBranch) { + return fmt.Errorf("trigger item #%d: both %s and push_branch defined", idx+1, field) } return nil } @@ -399,6 +421,9 @@ func (item TriggerMapItemModel) validateNoPullRequestConditionsSet(idx int, fiel if isStringLiteralOrRegexSet(item.PullRequestLabel) { return fmt.Errorf("trigger item #%d: both %s and pull_request_label defined", idx+1, field) } + if isStringLiteralOrRegexSet(item.PullRequestComment) { + return fmt.Errorf("trigger item #%d: both %s and pull_request_comment defined", idx+1, field) + } return nil } diff --git a/models/trigger_map_item_test.go b/models/trigger_map_item_test.go index d0add2833..1e851ec11 100644 --- a/models/trigger_map_item_test.go +++ b/models/trigger_map_item_test.go @@ -442,6 +442,28 @@ func TestTriggerMapItemModel_String(t *testing.T) { }, want: "push_branch: master & tag: 0.9.0 & pull_request_source_branch: develop & pull_request_target_branch: master & pattern: * & is_pull_request_allowed: true", }, + { + name: "pr event - all conditions", + triggerMapItem: TriggerMapItemModel{ + Type: "pull_request", + WorkflowID: "ci", + PullRequestComment: "my comment", + PullRequestLabel: "my label", + CommitMessage: "my commit", + ChangedFiles: "my file", + }, + want: "commit_message: my commit & changed_files: my file & pull_request_label: my label & pull_request_comment: my comment", + }, + { + name: "push event - all conditions", + triggerMapItem: TriggerMapItemModel{ + Type: "push", + WorkflowID: "ci", + CommitMessage: "my commit", + ChangedFiles: "my file", + }, + want: "commit_message: my commit & changed_files: my file", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -725,6 +747,46 @@ func TestTriggerMapItemModel_Validate_TagPushItem(t *testing.T) { workflows: []string{"primary"}, wantErr: "trigger item #1: no type or relevant trigger condition defined", }, + { + name: "it fails when not tag related condition provided - commit message", + triggerMapItem: TriggerMapItemModel{ + Tag: "1.0", + CommitMessage: "msg", + WorkflowID: "primary", + }, + workflows: []string{"primary"}, + wantErr: "trigger item #1: both tag and commit_message defined", + }, + { + name: "it fails when not tag related condition provided - PR comment", + triggerMapItem: TriggerMapItemModel{ + Tag: "1.0", + PullRequestComment: "msg", + WorkflowID: "primary", + }, + workflows: []string{"primary"}, + wantErr: "trigger item #1: both tag and pull_request_comment defined", + }, + { + name: "it fails when not tag related condition provided - PR label", + triggerMapItem: TriggerMapItemModel{ + Tag: "1.0", + PullRequestLabel: "label", + WorkflowID: "primary", + }, + workflows: []string{"primary"}, + wantErr: "trigger item #1: both tag and pull_request_label defined", + }, + { + name: "it fails when not tag related condition provided - changed files", + triggerMapItem: TriggerMapItemModel{ + Tag: "1.0", + ChangedFiles: "file", + WorkflowID: "primary", + }, + workflows: []string{"primary"}, + wantErr: "trigger item #1: both tag and changed_files defined", + }, { name: "it fails for invalid code-push trigger item - missing pipeline & workflow", triggerMapItem: TriggerMapItemModel{ @@ -814,6 +876,33 @@ func TestTriggerMapItemModel_Validate_PullRequestItem(t *testing.T) { pipelines: []string{"primary"}, wantErr: "trigger item #1: no type or relevant trigger condition defined", }, + { + name: "type is required, when no pull_request_source_branch defined (pull_request_comment)", + triggerMapItem: TriggerMapItemModel{ + PullRequestComment: "comment", + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + wantErr: "trigger item #1: no type or relevant trigger condition defined", + }, + { + name: "type is required, when no pull_request_source_branch defined (commit_message)", + triggerMapItem: TriggerMapItemModel{ + CommitMessage: "commit", + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + wantErr: "trigger item #1: no type or relevant trigger condition defined", + }, + { + name: "type is required, when no pull_request_source_branch defined (changed_files)", + triggerMapItem: TriggerMapItemModel{ + ChangedFiles: "my file", + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + wantErr: "trigger item #1: no type or relevant trigger condition defined", + }, { name: "type is required, when no pull_request_source_branch defined (draft_pull_request_enabled)", triggerMapItem: TriggerMapItemModel{ @@ -869,6 +958,39 @@ func TestTriggerMapItemModel_Validate_PullRequestItem(t *testing.T) { }, pipelines: []string{"primary"}, }, + { + name: "commit_message can be a regex", + triggerMapItem: TriggerMapItemModel{ + Type: PullRequestType, + CommitMessage: map[string]string{ + "regex": "commit msg", + }, + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + }, + { + name: "changed_files can be a regex", + triggerMapItem: TriggerMapItemModel{ + Type: PullRequestType, + ChangedFiles: map[string]string{ + "regex": "files", + }, + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + }, + { + name: "pull_request_comment can be a regex", + triggerMapItem: TriggerMapItemModel{ + Type: PullRequestType, + PullRequestComment: map[string]string{ + "regex": "CI", + }, + PipelineID: "primary", + }, + pipelines: []string{"primary"}, + }, { name: "it fails for mixed type trigger item (pull_request_source_branch + tag)", triggerMapItem: TriggerMapItemModel{