/
config.go
199 lines (171 loc) · 4.33 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
package prchecker
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
"time"
"gopkg.in/yaml.v2"
)
const (
defaultConfigFilePath = "config.yml"
// enviroment parameters
defaultEnvConfigFile = "GITHUB_PR_CONFIG_FILE"
defaultEnvAPIToken = "GITHUB_PR_API_TOKEN"
defaultEnvAPITokenKMS = "GITHUB_PR_API_TOKEN_KMS"
)
var (
envFileName string
envAPIToken string
envAPITokenKMS string
)
func init() {
envFileName = os.Getenv(defaultEnvConfigFile)
envAPIToken = os.Getenv(defaultEnvAPIToken)
envAPITokenKMS = os.Getenv(defaultEnvAPITokenKMS)
}
// Config contains settings.
type Config struct {
RepositoryList map[string]*Repository `yaml:"repository"`
APIToken string `yaml:"api_token"`
APITokenKMS string `yaml:"api_token_kms"`
BotID int64 `yaml:"bot_id"`
WebHookSecret string `yaml:"webhook_secret"`
// If `AddComment` is true, comment is created each push event.
// If `AddComment` is false, comment is edited each push event.
AddComment bool `yaml:"add_comment"`
Timeout time.Duration `yaml:"timeout"`
}
// NewConfig creates Config from yaml file and environment paramter.
func NewConfig() (Config, error) {
var conf Config
var err error
switch {
case isEnvironmentConfigExists():
conf, err = loadConfigFromFile(envFileName)
case isDefaultConfigExists():
conf, err = loadConfigFromFile(defaultConfigFilePath)
}
if err != nil {
return conf, err
}
err = conf.Init()
return conf, err
}
// Init initializes config.
func (c *Config) Init() error {
// get api token through KMS
if !c.HasAPIToken() {
var err error
switch {
case c.APITokenKMS != "":
c.APIToken, err = DecryptKMS(c.APITokenKMS)
case envAPITokenKMS != "":
c.APIToken, err = DecryptKMS(c.APITokenKMS)
}
if err != nil {
return err
}
}
// initialize repository list and file's regexp
if c.RepositoryList == nil {
c.RepositoryList = make(map[string]*Repository)
}
for _, r := range c.RepositoryList {
r.Init()
}
return nil
}
// HasAPIToken checks if api token is exists or not.
func (c Config) HasAPIToken() bool {
return c.GetAPIToken() != ""
}
// GetAPIToken gets api token for GitHub.
func (c Config) GetAPIToken() string {
if c.APIToken != "" {
return c.APIToken
}
return envAPIToken
}
// GetRepository gets Repository setting to manage pull request.
func (c Config) GetRepository(name string) *Repository {
if c.RepositoryList == nil {
return nil
}
r, ok := c.RepositoryList[name]
if !ok {
return nil
}
return r
}
func loadConfigFromFile(path string) (Config, error) {
conf := Config{}
buf, err := ioutil.ReadFile(path)
if err != nil {
return conf, err
}
err = yaml.Unmarshal(buf, &conf)
return conf, err
}
func isEnvironmentConfigExists() bool {
return envFileName != "" && isFileExists(envFileName)
}
func isDefaultConfigExists() bool {
return isFileExists(defaultConfigFilePath)
}
func isFileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// Repository has settings to check changed files.
type Repository struct {
Files []*File
}
// Init initializes all of Files.
func (r *Repository) Init() {
for _, f := range r.Files {
f.Init()
}
}
// File has rules to check changed file and manage comment/assignees/reviwers of pull request.
type File struct {
once sync.Once `yaml:"-"`
Name string `yaml:"name"`
Regexp []string `yaml:"regexp"`
regexp []*regexp.Regexp `yaml:"-"`
Comment string `yaml:"comment"`
Assignees []string `yaml:"assignees"`
Reviewers []string `yaml:"reviewers"`
ShowFiles bool `yaml:"show_files"`
}
// Init initializes regexp rules.
func (f *File) Init() {
f.once.Do(func() {
fmt.Printf("[Init] %s\n", f.Name)
list := make([]*regexp.Regexp, len(f.Regexp))
for i, r := range f.Regexp {
list[i] = regexp.MustCompile(r)
}
f.regexp = list
})
}
// Match checks file path matches the regexp rule or not..
func (f *File) Match(path string) bool {
f.Init()
for _, r := range f.regexp {
isMatch := r.MatchString(path)
if isMatch {
return true
}
}
return false
}
// GetComment gets comment.
func (f *File) GetComment(files []string) string {
if f.ShowFiles {
return fmt.Sprintf("[%s]\n%s\n- %s", f.Name, f.Comment, strings.Join(files, "\n- "))
}
return fmt.Sprintf("[%s]\n%s", f.Name, f.Comment)
}