From fa4b41ab74796f09949b11485b6ad3ddc41975d7 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 14 Oct 2019 16:51:54 +0200 Subject: [PATCH] when: match only the current ref type Only match the current ref type, ie: don't match a branch when the ref type is a tag or pull request. Ref is always matched because it's not related to a specific ref type. --- internal/runconfig/runconfig.go | 5 +- internal/runconfig/runconfig_test.go | 2 +- internal/services/gateway/action/run.go | 6 +- services/configstore/types/types_test.go | 278 -------------------- services/types/when.go | 8 +- services/types/when_test.go | 311 +++++++++++++++++++++++ 6 files changed, 323 insertions(+), 287 deletions(-) delete mode 100644 services/configstore/types/types_test.go create mode 100644 services/types/when_test.go diff --git a/internal/runconfig/runconfig.go b/internal/runconfig/runconfig.go index 5a23b13..f77101a 100644 --- a/internal/runconfig/runconfig.go +++ b/internal/runconfig/runconfig.go @@ -19,6 +19,7 @@ import ( "strings" "agola.io/agola/internal/config" + itypes "agola.io/agola/internal/services/types" "agola.io/agola/internal/util" rstypes "agola.io/agola/services/runservice/types" "agola.io/agola/services/types" @@ -195,13 +196,13 @@ fi // GenRunConfigTasks generates a run config tasks from a run in the config, expanding all the references to tasks // this functions assumes that the config is already checked for possible errors (i.e referenced task must exits) -func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, runName string, variables map[string]string, branch, tag, ref string) map[string]*rstypes.RunConfigTask { +func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, runName string, variables map[string]string, refType itypes.RunRefType, branch, tag, ref string) map[string]*rstypes.RunConfigTask { cr := c.Run(runName) rcts := map[string]*rstypes.RunConfigTask{} for _, ct := range cr.Tasks { - include := types.MatchWhen(ct.When.ToWhen(), branch, tag, ref) + include := types.MatchWhen(ct.When.ToWhen(), refType, branch, tag, ref) steps := make(rstypes.Steps, len(ct.Steps)) for i, cpts := range ct.Steps { diff --git a/internal/runconfig/runconfig_test.go b/internal/runconfig/runconfig_test.go index 00705d3..be856f1 100644 --- a/internal/runconfig/runconfig_test.go +++ b/internal/runconfig/runconfig_test.go @@ -1010,7 +1010,7 @@ func TestGenRunConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - out := GenRunConfigTasks(uuid, tt.in, "run01", tt.variables, "", "", "") + out := GenRunConfigTasks(uuid, tt.in, "run01", tt.variables, "", "", "", "") if diff := cmp.Diff(tt.out, out); diff != "" { t.Error(diff) diff --git a/internal/services/gateway/action/run.go b/internal/services/gateway/action/run.go index 3fee31d..e4527ef 100644 --- a/internal/services/gateway/action/run.go +++ b/internal/services/gateway/action/run.go @@ -493,12 +493,12 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e continue } - if match := types.MatchWhen(run.When.ToWhen(), req.Branch, req.Tag, req.Ref); !match { + if match := types.MatchWhen(run.When.ToWhen(), req.RefType, req.Branch, req.Tag, req.Ref); !match { h.log.Debugf("skipping run since when condition doesn't match") continue } - rcts := runconfig.GenRunConfigTasks(util.DefaultUUIDGenerator{}, config, run.Name, variables, req.Branch, req.Tag, req.Ref) + rcts := runconfig.GenRunConfigTasks(util.DefaultUUIDGenerator{}, config, run.Name, variables, req.RefType, req.Branch, req.Tag, req.Ref) createRunReq := &rsapitypes.RunCreateRequest{ RunConfigTasks: rcts, @@ -560,7 +560,7 @@ func (h *ActionHandler) genRunVariables(ctx context.Context, req *CreateRunReque // find the value match var varval cstypes.VariableValue for _, varval = range pvar.Values { - match := types.MatchWhen(varval.When, req.Branch, req.Tag, req.Ref) + match := types.MatchWhen(varval.When, req.RefType, req.Branch, req.Tag, req.Ref) if !match { continue } diff --git a/services/configstore/types/types_test.go b/services/configstore/types/types_test.go deleted file mode 100644 index bd7c9ff..0000000 --- a/services/configstore/types/types_test.go +++ /dev/null @@ -1,278 +0,0 @@ -// Copyright 2019 Sorint.lab -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied -// See the License for the specific language governing permissions and -// limitations under the License. - -package types - -import ( - "testing" - - "agola.io/agola/services/types" -) - -func TestMatchWhen(t *testing.T) { - tests := []struct { - name string - when *types.When - branch string - tag string - ref string - out bool - }{ - { - name: "test no when, should always match", - when: nil, - out: true, - }, - { - name: "test branch when include with empty match value, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple}, - }, - }, - }, - branch: "master", - out: false, - }, - { - name: "test branch when include regexp with empty match value, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include with empty match value and empty provided branch, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple}, - }, - }, - }, - branch: "", - out: false, - }, - { - name: "test branch when include regexp with empty match value and empty provided branch, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp}, - }, - }, - }, - branch: "", - out: false, - }, - { - name: "test branch when include, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "master"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "master"}, - }, - }, - }, - branch: "branch01", - out: false, - }, - { - name: "test tag, ref when include, should not match since when is not nil and we have provided a branch and not a tag", - when: &types.When{ - Tag: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "master"}, - }, - }, - Ref: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "master"}, - }, - }, - }, - branch: "branch01", - out: false, - }, - { - name: "test branch when include regexp, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "master"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "master"}, - }, - }, - }, - branch: "branch01", - out: false, - }, - { - name: "test branch when include regexp, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - }, - }, - branch: "branch01", - out: false, - }, - { - name: "test branch when include regexp, exclude simple, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "maste"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include regexp, exclude simple, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeSimple, Match: "master"}, - }, - }, - }, - branch: "master", - out: false, - }, - { - name: "test branch when include regexp, exclude regexp, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "mb.*"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when include regexp, exclude regexp, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "ma.*"}, - }, - }, - }, - branch: "master", - out: false, - }, - { - name: "test branch when multiple include regexp, multiple exclude regexp, should match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - {Type: types.WhenConditionTypeRegExp, Match: "b.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "b.*"}, - {Type: types.WhenConditionTypeRegExp, Match: "c.*"}, - }, - }, - }, - branch: "master", - out: true, - }, - { - name: "test branch when multiple include regexp, multiple exclude regexp, should not match", - when: &types.When{ - Branch: &types.WhenConditions{ - Include: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "m.*"}, - {Type: types.WhenConditionTypeRegExp, Match: "b.*"}, - }, - Exclude: []types.WhenCondition{ - {Type: types.WhenConditionTypeRegExp, Match: "b.*"}, - {Type: types.WhenConditionTypeRegExp, Match: "ma.*"}, - }, - }, - }, - branch: "master", - out: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - out := types.MatchWhen(tt.when, tt.branch, tt.tag, tt.ref) - if tt.out != out { - t.Fatalf("expected match: %t, got: %t", tt.out, out) - } - }) - } -} diff --git a/services/types/when.go b/services/types/when.go index 4f87da9..109c3cd 100644 --- a/services/types/when.go +++ b/services/types/when.go @@ -16,6 +16,8 @@ package types import ( "regexp" + + itypes "agola.io/agola/internal/services/types" ) type When struct { @@ -41,12 +43,12 @@ type WhenCondition struct { Match string `json:"match,omitempty"` } -func MatchWhen(when *When, branch, tag, ref string) bool { +func MatchWhen(when *When, refType itypes.RunRefType, branch, tag, ref string) bool { include := true if when != nil { include = false // test only if branch is not empty, if empty mean that we are not in a branch - if when.Branch != nil && branch != "" { + if refType == itypes.RunRefTypeBranch && when.Branch != nil && branch != "" { // first check includes and override with excludes if matchCondition(when.Branch.Include, branch) { include = true @@ -56,7 +58,7 @@ func MatchWhen(when *When, branch, tag, ref string) bool { } } // test only if tag is not empty, if empty mean that we are not in a tag - if when.Tag != nil && tag != "" { + if refType == itypes.RunRefTypeBranch && when.Tag != nil && tag != "" { // first check includes and override with excludes if matchCondition(when.Tag.Include, tag) { include = true diff --git a/services/types/when_test.go b/services/types/when_test.go new file mode 100644 index 0000000..cb14fab --- /dev/null +++ b/services/types/when_test.go @@ -0,0 +1,311 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package types + +import ( + "testing" + + itypes "agola.io/agola/internal/services/types" +) + +func TestMatchWhen(t *testing.T) { + tests := []struct { + name string + when *When + refType itypes.RunRefType + branch string + tag string + ref string + out bool + }{ + { + name: "test no when, should always match", + when: nil, + out: true, + }, + { + name: "test branch when include with empty match value, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: false, + }, + { + name: "test branch when include regexp with empty match value, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include with empty match value and empty provided branch, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "", + out: false, + }, + { + name: "test branch when include regexp with empty match value and empty provided branch, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "", + out: false, + }, + { + name: "test branch when include, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "branch01", + out: false, + }, + { + name: "test tag, ref when include, should not match since when is not nil and we have provided a branch and not a tag", + when: &When{ + Tag: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + Ref: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "branch01", + out: false, + }, + { + name: "test branch when include regexp, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "branch01", + out: false, + }, + { + name: "test branch when include regexp, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "branch01", + out: false, + }, + { + name: "test branch when include regexp, exclude simple, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "maste"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include regexp, exclude simple, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: false, + }, + { + name: "test branch when include regexp, exclude regexp, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "mb.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when include regexp, exclude regexp, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "ma.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: false, + }, + { + name: "test branch when multiple include regexp, multiple exclude regexp, should match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + {Type: WhenConditionTypeRegExp, Match: "b.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "b.*"}, + {Type: WhenConditionTypeRegExp, Match: "c.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: true, + }, + { + name: "test branch when multiple include regexp, multiple exclude regexp, should not match", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "m.*"}, + {Type: WhenConditionTypeRegExp, Match: "b.*"}, + }, + Exclude: []WhenCondition{ + {Type: WhenConditionTypeRegExp, Match: "b.*"}, + {Type: WhenConditionTypeRegExp, Match: "ma.*"}, + }, + }, + }, + refType: itypes.RunRefTypeBranch, + branch: "master", + out: false, + }, + { + name: "test only matching reftype", + when: &When{ + Branch: &WhenConditions{ + Include: []WhenCondition{ + {Type: WhenConditionTypeSimple, Match: "master"}, + }, + }, + }, + refType: itypes.RunRefTypeTag, + // we provide also a value to branch (should not be done) + branch: "master", + tag: "master", + out: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := MatchWhen(tt.when, tt.refType, tt.branch, tt.tag, tt.ref) + if tt.out != out { + t.Fatalf("expected match: %t, got: %t", tt.out, out) + } + }) + } +}