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

Handle fancy wildcard branch patterns #49

Merged
merged 2 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions local/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ func (s *step) UnmarshalJSON(data []byte) error {
branches = b
}

// Handle various types of branch vs branches
if branches != nil {
// Sometimes branches are space delimited, sometimes comma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BK backend doesn't support branches: "foo, bar" - is that what this bit is for? Or is this something else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, not sure where I got that from, but will ditch it.

splitBranches := func(s string) []string {
if strings.Contains(",", s) {
return strings.Split(s, ",")
}
return strings.Fields(s)
}

// Handle various types of branch vs branches
switch b := branches.(type) {
case []interface{}:
for _, bi := range b {
s.Branches = append(s.Branches, strings.Split(bi.(string), ",")...)
s.Branches = append(s.Branches, splitBranches(bi.(string))...)
}
case string:
s.Branches = append(s.Branches, strings.Split(b, ",")...)
s.Branches = append(s.Branches, splitBranches(b)...)
default:
log.Printf("Branches is unhandled type %T", branches)
}
Expand All @@ -76,8 +84,29 @@ func (s step) MatchBranch(branch string) bool {
return true
}
for _, b := range s.Branches {
if b == branch {
return true
expected := true

// Handle negation at the start
for strings.HasPrefix(b, `!`) {
expected = !expected
b = strings.TrimPrefix(b, `!`)
}

// Handle trailing star matches
trailingStarMatch := strings.HasSuffix(b, `*`)
b = strings.TrimRight(b, `*`)

// Handle leading star matches
leadingStarMatch := strings.HasPrefix(b, `*`)
b = strings.TrimLeft(b, `*`)

switch {
case trailingStarMatch:
return strings.HasPrefix(branch, b) == expected
case leadingStarMatch:
return strings.HasSuffix(branch, b) == expected
default:
return (b == branch) == expected
}
}
return false
Expand Down
60 changes: 60 additions & 0 deletions local/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ func TestStepParsingCommandSteps(t *testing.T) {
},
},
},
{
`{
"command": "echo foo",
"branches": "!foo"
}`,
step{
Command: &commandStep{
Commands: []string{`echo foo`},
},
Branches: []string{`!foo`},
},
},
{
`{
"command": "echo foo",
"branches": "master stable/*"
}`,
step{
Command: &commandStep{
Commands: []string{`echo foo`},
},
Branches: []string{`master`, `stable/*`},
},
},
} {
t.Run("", func(t *testing.T) {
var step step
Expand Down Expand Up @@ -284,3 +308,39 @@ func TestPipelineParsing(t *testing.T) {
})
}
}

func TestMatchBranch(t *testing.T) {
for _, tc := range []struct {
Actual string
Branches []string
Expected bool
}{
{`foo`, []string{}, true},
{`foo`, []string{`foo`}, true},
{`foo`, []string{`llamas`}, false},
{`foo`, []string{`!foo`}, false},
{`foo`, []string{`!!foo`}, true},
{`foo`, []string{`foo*`}, true},
{`foo`, []string{`*foo`}, true},
{`foo`, []string{`!fo*`}, false},
{`foo`, []string{`!*oo`}, false},
{`foo`, []string{`!!foo*`}, true},
{`foo`, []string{`f*`}, true},
{`foo`, []string{`*oo`}, true},
{`foo`, []string{`l*`}, false},
{`foo`, []string{`*amas`}, false},
{`foo`, []string{`foo`, `l*`}, true},
} {
t.Run("", func(t *testing.T) {
s := step{
Branches: tc.Branches,
}

if result := s.MatchBranch(tc.Actual); result != tc.Expected {
t.Errorf("Expected %v for %v matches %v, got %v",
tc.Expected, tc.Actual, tc.Branches, result)
}
})
}

}