-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
163 lines (126 loc) · 3.92 KB
/
config.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"regexp"
"strings"
libconfig "github.com/opensourceways/community-robot-lib/config"
)
type pullRequestMergeMethod string
const (
mergeMethodeMerge pullRequestMergeMethod = "merge"
mergeMethodSquash pullRequestMergeMethod = "squash"
)
type configuration struct {
ConfigItems []botConfig `json:"config_items,omitempty"`
}
func (c *configuration) configFor(org, repo string) *botConfig {
if c == nil {
return nil
}
items := c.ConfigItems
v := make([]libconfig.IPluginForRepo, len(items))
for i := range items {
v[i] = &items[i]
}
if i := libconfig.FindConfig(org, repo, v); i >= 0 {
return &items[i]
}
return nil
}
func (c *configuration) Validate() error {
if c == nil {
return nil
}
items := c.ConfigItems
for i := range items {
if err := items[i].validate(); err != nil {
return err
}
}
return nil
}
func (c *configuration) SetDefault() {
if c == nil {
return
}
Items := c.ConfigItems
for i := range Items {
Items[i].setDefault()
}
}
type botConfig struct {
libconfig.PluginForRepo
// LgtmCountsRequired specifies the number of lgtm label which will be need for the pr.
// When it is greater than 1, the lgtm label is composed of 'lgtm-login'.
// The default value is 1 which means the lgtm label is itself.
LgtmCountsRequired uint `json:"lgtm_counts_required,omitempty"`
// CheckPermissionBasedOnSigOwners means it should check the devepler's permission
// besed on the owners file in sig directory when the developer comment /lgtm or /approve
// command. The repository is 'tc' at present.
CheckPermissionBasedOnSigOwners bool `json:"check_permission_based_on_sig_owners,omitempty"`
// SigsDir is the directory of Sig. It must be set when CheckPermissionBasedOnSigOwners is true.
SigsDir string `json:"sigs_dir,omitempty"`
regSigDir regexp.Regexp `json:"-"`
// LabelsForMerge specifies the labels except approved and lgtm relevant labels
// that must be available to merge pr
LabelsForMerge []string `json:"labels_for_merge,omitempty"`
// MissingLabelsForMerge specifies the ones which a PR must not have to be merged.
MissingLabelsForMerge []string `json:"missing_labels_for_merge,omitempty"`
// MergeMethod is the method to merge PR.
// The default method of merge. Valid options are squash and merge.
MergeMethod pullRequestMergeMethod `json:"merge_method,omitempty"`
// UnableCheckingReviewerForPR is a switch used to check whether the pr has been set reviewers when it is open.
UnableCheckingReviewerForPR bool `json:"unable_checking_reviewer_for_pr,omitempty"`
// FreezeFile is the freeze branch of community config files
FreezeFile []freezeFile `json:"freeze_file,omitempty"`
}
func (c *botConfig) setDefault() {
if c.LgtmCountsRequired == 0 {
c.LgtmCountsRequired = 1
}
if c.MergeMethod == "" {
c.MergeMethod = mergeMethodeMerge
}
}
func (c *botConfig) validate() error {
if m := c.MergeMethod; m != mergeMethodeMerge && m != mergeMethodSquash {
return fmt.Errorf("unsupported merge method:%s", m)
}
if c.CheckPermissionBasedOnSigOwners {
if c.SigsDir == "" {
return fmt.Errorf("missing sigs_dir")
}
v, err := regexp.Compile(fmt.Sprintf(
`^%s/[-\w]+/`,
strings.TrimSuffix(c.SigsDir, "/"),
))
if err != nil {
return err
}
c.regSigDir = *v
}
if len(c.FreezeFile) > 0 {
for _, v := range c.FreezeFile {
return v.validate()
}
}
return c.PluginForRepo.Validate()
}
type freezeFile struct {
Owner string `json:"owner" required:"true"`
Repo string `json:"repo" required:"true"`
Branch string `json:"branch"`
Path string `json:"path" required:"true"`
}
func (f freezeFile) validate() error {
if f.Owner == "" {
return fmt.Errorf("the owner configuration item of freeze file can not empty")
}
if f.Repo == "" {
return fmt.Errorf("the repo configuration item of freeze file can not empty")
}
if f.Path == "" {
return fmt.Errorf("the path configuration item of freeze file can not empty")
}
return nil
}