podman

Форк
0
133 строки · 3.5 Кб
1
package validate
2

3
import (
4
	"sort"
5
	"strings"
6
	"sync"
7

8
	"github.com/vbatts/git-validation/git"
9
)
10

11
var (
12
	// RegisteredRules are the avaible validation to perform on git commits
13
	RegisteredRules  = []Rule{}
14
	registerRuleLock = sync.Mutex{}
15
)
16

17
// RegisterRule includes the Rule in the avaible set to use
18
func RegisterRule(vr Rule) {
19
	registerRuleLock.Lock()
20
	defer registerRuleLock.Unlock()
21
	RegisteredRules = append(RegisteredRules, vr)
22
}
23

24
// Rule will operate over a provided git.CommitEntry, and return a result.
25
type Rule struct {
26
	Name        string // short name for reference in in the `-run=...` flag
27
	Value       string // value to configure for the rule (i.e. a regexp to check for in the commit message)
28
	Description string // longer Description for readability
29
	Run         func(Rule, git.CommitEntry) Result
30
	Default     bool // whether the registered rule is run by default
31
}
32

33
// Commit processes the given rules on the provided commit, and returns the result set.
34
func Commit(c git.CommitEntry, rules []Rule) Results {
35
	results := Results{}
36
	for _, r := range rules {
37
		results = append(results, r.Run(r, c))
38
	}
39
	return results
40
}
41

42
// Result is the result for a single validation of a commit.
43
type Result struct {
44
	CommitEntry git.CommitEntry
45
	Pass        bool
46
	Msg         string
47
}
48

49
// Results is a set of results. This is type makes it easy for the following function.
50
type Results []Result
51

52
// PassFail gives a quick over/under of passes and failures of the results in this set
53
func (vr Results) PassFail() (pass int, fail int) {
54
	for _, res := range vr {
55
		if res.Pass {
56
			pass++
57
		} else {
58
			fail++
59
		}
60
	}
61
	return pass, fail
62
}
63

64
// SanitizeFilters takes a comma delimited list and returns the trimmend and
65
// split (on ",") items in the list
66
func SanitizeFilters(filtStr string) (filters []string) {
67
	for _, item := range strings.Split(filtStr, ",") {
68
		filters = append(filters, strings.TrimSpace(item))
69
	}
70
	return
71
}
72

73
// FilterRules takes a set of rules and a list of short names to include, and
74
// returns the reduced set.  The comparison is case insensitive.
75
//
76
// Some `includes` rules have values assigned to them.
77
// i.e. -run "dco,message_regexp='^JIRA-[0-9]+ [A-Z].*$'"
78
func FilterRules(rules []Rule, includes []string) []Rule {
79
	ret := []Rule{}
80

81
	for _, r := range rules {
82
		for i := range includes {
83
			if strings.Contains(includes[i], "=") {
84
				chunks := strings.SplitN(includes[i], "=", 2)
85
				if strings.EqualFold(r.Name, chunks[0]) {
86
					// for these rules, the Name won't be unique per se. There may be
87
					// multiple "regexp=" with different values. We'll need to set the
88
					// .Value = chunk[1] and ensure r is dup'ed so they don't clobber
89
					// each other.
90
					newR := Rule(r)
91
					newR.Value = chunks[1]
92
					ret = append(ret, newR)
93
				}
94
			} else {
95
				if strings.EqualFold(r.Name, includes[i]) {
96
					ret = append(ret, r)
97
				}
98
			}
99
		}
100
	}
101

102
	return ret
103
}
104

105
// StringsSliceEqual compares two string arrays for equality
106
func StringsSliceEqual(a, b []string) bool {
107
	if !sort.StringsAreSorted(a) {
108
		sort.Strings(a)
109
	}
110
	if !sort.StringsAreSorted(b) {
111
		sort.Strings(b)
112
	}
113
	for i := range b {
114
		if !StringsSliceContains(a, b[i]) {
115
			return false
116
		}
117
	}
118
	for i := range a {
119
		if !StringsSliceContains(b, a[i]) {
120
			return false
121
		}
122
	}
123
	return true
124
}
125

126
// StringsSliceContains checks for the presence of a word in string array
127
func StringsSliceContains(a []string, b string) bool {
128
	if !sort.StringsAreSorted(a) {
129
		sort.Strings(a)
130
	}
131
	i := sort.SearchStrings(a, b)
132
	return i < len(a) && a[i] == b
133
}
134

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.