/
branch-configuration.go
44 lines (36 loc) · 1.12 KB
/
branch-configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package models
import (
"regexp"
"github.com/wufe/polo/pkg/logging"
)
type BranchConfigurationMatch struct {
SharedConfiguration `yaml:",inline"`
BranchConfiguration `yaml:",inline"`
Test string `yaml:"test"`
}
type BranchConfiguration struct {
Main bool `yaml:"main"`
Watch bool `yaml:"watch"`
}
type Branches []BranchConfigurationMatch
func (branches Branches) BranchIsBeingWatched(branch string, logger logging.Logger) bool {
foundBranch, ok := branches.findBranchConfiguration(branch, logger)
return ok && foundBranch.Watch
}
func (branches Branches) BranchIsMain(branch string, logger logging.Logger) bool {
foundBranch, ok := branches.findBranchConfiguration(branch, logger)
return ok && foundBranch.Main
}
func (branches Branches) findBranchConfiguration(name string, logger logging.Logger) (BranchConfigurationMatch, bool) {
for _, b := range branches {
test, err := regexp.Compile(b.Test)
if err != nil {
logger.Errorf("Could not compile branch test regexp: %s", err.Error())
continue
}
if test.MatchString(name) {
return b, true
}
}
return BranchConfigurationMatch{}, false
}