This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
/
config.go
242 lines (214 loc) · 6.57 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
SPDX-Copyright: Copyright (c) Brad Rydzewski, project contributors, Capital One Services, LLC
SPDX-License-Identifier: Apache-2.0
Copyright 2017 Brad Rydzewski, project contributors, Capital One Services, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
*/
package model
import (
"regexp"
"github.com/capitalone/checks-out/envvars"
"github.com/capitalone/checks-out/hjson"
"github.com/capitalone/checks-out/strings/rxserde"
"github.com/mspiegel/go-multierror"
"github.com/pelletier/go-toml"
)
type Config struct {
Approvals []*ApprovalPolicy `json:"approvals"`
Pattern rxserde.RegexSerde `json:"pattern"`
AntiPattern *rxserde.RegexSerde `json:"antipattern,omitempty"`
AntiTitle *rxserde.RegexSerde `json:"antititle,omitempty"`
Commit CommitConfig `json:"commit,omitempty"`
Maintainers MaintainersConfig `json:"maintainers,omitempty"`
Merge MergeConfig `json:"merge,omitempty"`
Feedback FeedbackConfig `json:"feedback,omitempty"`
Tag TagConfig `json:"tag,omitempty"`
Comment CommentConfig `json:"comment,omitempty"`
Deployment DeployConfig `json:"deploy,omitempty"`
Audit AuditConfig `json:"audit,omitempty"`
IsOld bool `json:"-"`
}
type CommitConfig struct {
Range CommitRange `json:"range"`
AntiRange CommitRange `json:"antirange"`
TagRange CommitRange `json:"tagrange"`
IgnoreUIMerge bool `json:"ignoreuimerge,omitempty"`
}
type MaintainersConfig struct {
Path string `json:"path"`
Type string `json:"type"`
}
type MergeConfig struct {
Enable bool `json:"enable"`
UpToDate bool `json:"uptodate"`
Method string `json:"method"`
Delete bool `json:"delete"`
}
type FeedbackConfig struct {
Types []FeedbackType `json:"types,omitempty"`
AuthorAffirm bool `json:"authoraffirm"`
}
type CommentConfig struct {
Enable bool `json:"enable"`
Targets []TargetConfig `json:"targets"`
}
type TargetConfig struct {
Target string `json:"target"`
Pattern *rxserde.RegexSerde `json:"pattern"`
Types []CommentMessage `json:"types"`
Names []string `json:"names"`
Url string `json:"-"`
}
type DeployConfig struct {
Enable bool `json:"enable"`
Path string `json:"path"`
DeploymentMap DeploymentConfigs `json:"-"`
}
const (
maintainers = "MAINTAINERS"
maintType = "text"
deployment = "DEPLOYMENTS"
)
func DefaultConfig() *Config {
c := new(Config)
c.Approvals = nil
c.Pattern = rxserde.RegexSerde{Regex: regexp.MustCompile(envvars.Env.Pattern.Default)}
c.Maintainers.Path = maintainers
c.Maintainers.Type = maintType
c.Deployment.Path = deployment
c.Commit = DefaultCommit()
c.Feedback = DefaultFeedback()
c.Merge = DefaultMerge()
c.Tag = DefaultTag()
c.Audit = DefaultAudit()
_ = c.Tag.Compile()
return c
}
func NonEmptyConfig() *Config {
c := DefaultConfig()
c.Approvals = []*ApprovalPolicy{DefaultApprovalPolicy()}
c.Approvals[0].Position = 1
return c
}
func (c *Config) Validate(caps *Capabilities) error {
var errs error
errs = multierror.Append(errs, validateCapabilities(c, caps))
errs = multierror.Append(errs, validateApprovals(c.Approvals))
errs = multierror.Append(errs, validateMaintainerConfig(&c.Maintainers))
return errs
}
func (c *Config) GetFeedbackConfig(policy *ApprovalPolicy) *FeedbackConfig {
if policy.Feedback == nil {
return &c.Feedback
}
return policy.Feedback
}
func (c *Config) GetMergeConfig(policy *ApprovalPolicy) *MergeConfig {
if policy.Merge == nil {
return &c.Merge
}
return policy.Merge
}
// ParseConfig parses a project configuration file.
func ParseConfig(data []byte, caps *Capabilities) (*Config, error) {
c := DefaultConfig()
err := hjson.Unmarshal(data, c)
if err != nil {
return nil, err
}
var errs error
errs = multierror.Append(errs, c.Tag.Compile())
if errs != nil {
return nil, errs
}
for i, policy := range c.Approvals {
setupPolicyDefaults(i, policy)
}
errs = multierror.Append(errs, c.Validate(caps))
if errs != nil {
return nil, errs
}
return c, nil
}
func setupPolicyDefaults(i int, policy *ApprovalPolicy) {
if policy.AuthorMatch == nil {
policy.AuthorMatch = new(MatcherHolder)
}
if policy.AntiMatch == nil {
policy.AntiMatch = new(MatcherHolder)
}
if policy.Scope == nil {
policy.Scope = DefaultApprovalScope()
}
if policy.Match.Matcher == nil {
policy.Match.Matcher = DefaultMatcher()
}
if policy.AntiMatch.Matcher == nil {
policy.AntiMatch.Matcher = DefaultMatcher()
}
if policy.AuthorMatch.Matcher == nil {
policy.AuthorMatch.Matcher = DefaultUniverseMatch()
}
if c, ok := policy.Match.Matcher.(ChangePolicy); ok {
c.ChangePolicy(policy)
}
policy.Position = i + 1
}
// ParseOldConfig parses a projects .lgtm file
func ParseOldConfig(data []byte) (*Config, error) {
c, err := parseOldConfigStr(string(data))
if err != nil {
return nil, err
}
// convert to a current config structure
// we don't map Team because it wasn't publically supported before
regex, err := regexp.Compile(c.Get("pattern").(string))
if err != nil {
return nil, err
}
c2 := NonEmptyConfig()
c2.Pattern = rxserde.RegexSerde{Regex: regex}
mm := &MaintainerMatch{
CommonMatch{
Approvals: int(c.Get("approvals").(int64)),
Self: !c.Get("self_approval_off").(bool),
},
}
c2.Approvals[0].Match.Matcher = mm
//clear out unused fields
c2.Tag = TagConfig{}
c2.Deployment = DeployConfig{}
c2.IsOld = true
c2.Maintainers.Type = "legacy"
return c2, nil
}
//all of these variables are for old file format support
var (
oldApprovals = envvars.Env.Old.Approvals
oldPattern = envvars.Env.Old.Pattern
oldSelfApprovalOff = envvars.Env.Old.SelfApprovalOff
)
// parseOldConfigStr parses a projects .lgtm file in string format.
func parseOldConfigStr(data string) (*toml.Tree, error) {
c, err := toml.Load(data)
if err != nil {
return nil, err
}
if !c.Has("approvals") {
c.Set("approvals", oldApprovals)
}
if !c.Has("pattern") {
c.Set("pattern", oldPattern)
}
if !c.Has("self_approval_off") {
c.Set("self_approval_off", oldSelfApprovalOff)
}
return c, nil
}