-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexclude.go
More file actions
132 lines (117 loc) · 3.77 KB
/
Copy pathexclude.go
File metadata and controls
132 lines (117 loc) · 3.77 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package repomap
import (
"fmt"
"path/filepath"
"github.com/flanksource/commons/collections"
)
type ExcludeConfig struct {
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
Authors []string `json:"authors,omitempty" yaml:"authors,omitempty"`
Commits []string `json:"commits,omitempty" yaml:"commits,omitempty"`
CommitTypes []string `json:"commit_types,omitempty" yaml:"commit_types,omitempty"`
Resources []ResourceFilter `json:"resources,omitempty" yaml:"resources,omitempty"`
Rules []ExcludeRule `json:"rules,omitempty" yaml:"rules,omitempty"`
}
type ResourceFilter struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
When string `json:"when,omitempty" yaml:"when,omitempty"`
}
type ExcludeRule struct {
When string `json:"when" yaml:"when"`
}
type Preset struct {
Exclude ExcludeConfig `json:"exclude,omitempty" yaml:"exclude,omitempty"`
}
func (e ExcludeConfig) Merge(other ExcludeConfig) ExcludeConfig {
return ExcludeConfig{
Files: append(sliceCopy(e.Files), other.Files...),
Authors: append(sliceCopy(e.Authors), other.Authors...),
Commits: append(sliceCopy(e.Commits), other.Commits...),
CommitTypes: append(sliceCopy(e.CommitTypes), other.CommitTypes...),
Resources: append(sliceCopy(e.Resources), other.Resources...),
Rules: append(sliceCopy(e.Rules), other.Rules...),
}
}
func (e ExcludeConfig) IsEmpty() bool {
return len(e.Files) == 0 && len(e.Authors) == 0 && len(e.Commits) == 0 &&
len(e.CommitTypes) == 0 && len(e.Resources) == 0 && len(e.Rules) == 0
}
func (e *ExcludeConfig) ResolvePresets(extends []string, presets map[string]Preset) {
for _, ext := range extends {
name := ext
if len(name) > 7 && name[:7] == "preset:" {
name = name[7:]
}
if preset, ok := presets[name]; ok {
*e = preset.Exclude.Merge(*e)
}
}
}
func MatchesAuthor(author Author, patterns []string) (bool, string) {
for _, pattern := range patterns {
if author.Matches(pattern) {
return true, fmt.Sprintf("author matches '%s'", pattern)
}
}
return false, ""
}
func MatchesCommitMessage(subject string, patterns []string) (bool, string) {
if len(patterns) == 0 {
return false, ""
}
matched, _ := collections.MatchItem(subject, patterns...)
if matched {
return true, fmt.Sprintf("commit message matches '%v'", patterns)
}
return false, ""
}
func MatchesCommitType(commitType string, patterns []string) (bool, string) {
if len(patterns) == 0 {
return false, ""
}
matched, _ := collections.MatchItem(commitType, patterns...)
if matched {
return true, fmt.Sprintf("commit type '%s' matches '%v'", commitType, patterns)
}
return false, ""
}
func MatchesFile(file string, patterns []string) (bool, string) {
if len(patterns) == 0 {
return false, ""
}
if matched, _ := collections.MatchItem(file, patterns...); matched {
return true, fmt.Sprintf("file '%s' matches patterns", file)
}
if matched, _ := collections.MatchItem(filepath.Base(file), patterns...); matched {
return true, fmt.Sprintf("file '%s' matches patterns (basename)", file)
}
return false, ""
}
func MatchesResourceFields(kind, name, namespace string, f ResourceFilter) bool {
if f.Kind != "" {
if matched, _ := collections.MatchItem(kind, f.Kind); !matched {
return false
}
}
if f.Name != "" {
if matched, _ := collections.MatchItem(name, f.Name); !matched {
return false
}
}
if f.Namespace != "" {
if matched, _ := collections.MatchItem(namespace, f.Namespace); !matched {
return false
}
}
return true
}
func sliceCopy[T any](s []T) []T {
if s == nil {
return nil
}
out := make([]T, len(s))
copy(out, s)
return out
}