-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhotfix.go
More file actions
93 lines (81 loc) · 2.36 KB
/
hotfix.go
File metadata and controls
93 lines (81 loc) · 2.36 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package glow
import (
"fmt"
"regexp"
"strings"
l "github.com/meinto/glow/logging"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// Hotfix definition
type hotfix struct {
version string
Branch
}
// NewHotfix creates a new hotfix definition
func NewHotfix(version string) (b Branch, err error) {
l.Log().Info(l.Fields{"version": version})
defer func() {
l.Log().
Info(l.Fields{"branch": b}).
Error(err)
}()
branchName := fmt.Sprintf(BRANCH_NAME_PREFIX+"hotfix/v%s", version)
b = NewBranch(branchName)
return hotfix{version, b}, nil
}
// HotfixFromBranch extracts a fix definition from branch name
func HotfixFromBranch(branchName string) (b Branch, err error) {
l.Log().Info(l.Fields{"branchName": branchName})
defer func() {
l.Log().
Info(l.Fields{"branch": b}).
Error(err)
}()
matched, err := regexp.Match(HOTFIX_BRANCH_PATTERN, []byte(branchName))
if !matched || err != nil {
return hotfix{}, errors.New("no valid hotfix branch")
}
b = NewBranch(branchName)
parts := strings.Split(branchName, "/")
if len(parts) < 1 {
return hotfix{}, errors.New("invalid branch name " + branchName)
}
version := parts[len(parts)-1]
version = strings.TrimPrefix(version, "v")
return hotfix{version, b}, nil
}
// CreationIsAllowedFrom returns wheter branch is allowed to be created
// from given this source branch
func (f hotfix) CreationIsAllowedFrom(sourceBranch Branch) bool {
mainBranch := viper.GetString("mainBranch")
if strings.Contains(sourceBranch.ShortBranchName(), mainBranch) {
return true
}
return false
}
// CanBeClosed checks if the branch name is a valid
func (f hotfix) CanBeClosed() bool {
return true
}
// CanBePublished checks if the branch can be published directly to production
func (f hotfix) CanBePublished() bool {
return true
}
// CloseBranches returns all branches which this branch have to be merged with
func (f hotfix) CloseBranches(availableBranches []Branch) []Branch {
branches := make([]Branch, 0)
for _, b := range availableBranches {
if strings.Contains(b.BranchName(), "/release/v") {
branches = append(branches, b)
}
}
devBranch := viper.GetString("devBranch")
branches = append(branches, NewBranch(devBranch))
return branches
}
// PublishBranch returns the publish branch if available
func (f hotfix) PublishBranch() Branch {
mainBranch := viper.GetString("mainBranch")
return NewBranch(mainBranch)
}