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

added pull request comment #937

Merged
merged 8 commits into from
Apr 18, 2024
10 changes: 10 additions & 0 deletions models/models_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
39 changes: 32 additions & 7 deletions models/trigger_map_item.go
molnarm marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
molnarm marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("trigger item #%d: both %s and pull_request_comment defined", idx+1, field)
}
return nil
}

Expand Down
122 changes: 122 additions & 0 deletions models/trigger_map_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -869,6 +958,39 @@ func TestTriggerMapItemModel_Validate_PullRequestItem(t *testing.T) {
},
pipelines: []string{"primary"},
},
{
trapacska marked this conversation as resolved.
Show resolved Hide resolved
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{
Expand Down