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.
This commit is contained in:
Simone Gotti 2019-10-14 16:51:54 +02:00
parent cfa2db77e0
commit fa4b41ab74
6 changed files with 323 additions and 287 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}
})
}
}

View File

@ -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

311
services/types/when_test.go Normal file
View File

@ -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)
}
})
}
}