-
Notifications
You must be signed in to change notification settings - Fork 0
/
repoowners.go
273 lines (238 loc) · 6.32 KB
/
repoowners.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package repoowners
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/nasa9084/go-repoowners/internal/pkg/git"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v2"
)
const (
DefaultOwnersFilename = "OWNERS"
DefaultAliasesFilename = "OWNERS_ALIASES"
)
var fs = &afero.Afero{Fs: afero.NewOsFs()}
var gc *git.Client
func init() {
c, err := git.NewClient()
if err != nil {
panic(err)
}
gc = c
}
// Owners holds Owners configuration for one repository.
type Owners struct {
// these are path: UsernameSet mapping
approvers map[string]UsernameSet
reviewers map[string]UsernameSet
requiredReviewers map[string]UsernameSet
// path: options mapping
options map[string]options
// aliasname: []username mapping
aliases map[string]UsernameSet
memoizedApprovers memo
memoizedReviewers memo
memoizedRequiredReviewers memo
// base is a base path of repository.
base string
}
func newOwners() Owners {
return Owners{
approvers: map[string]UsernameSet{},
reviewers: map[string]UsernameSet{},
requiredReviewers: map[string]UsernameSet{},
options: map[string]options{},
aliases: map[string]UsernameSet{},
}
}
func LoadRemote(domain, org, repo, branch string) (Owners, error) {
r, err := gc.Clone(fmt.Sprintf("%s/%s/%s:%s", domain, org, repo, branch))
if err != nil {
return Owners{}, err
}
return LoadLocal(r.Dir)
}
func LoadLocal(basePath string) (Owners, error) {
o := newOwners()
o.base = basePath
if _, err := fs.Stat(filepath.Join(basePath, DefaultAliasesFilename)); err == nil {
f, err := fs.Open(filepath.Join(basePath, DefaultAliasesFilename))
if err != nil {
return Owners{}, err
}
defer f.Close()
ac, err := parseAliases(f)
if err != nil {
return Owners{}, err
}
for alias, list := range ac.Aliases {
o.aliases[alias] = newUsernameSet(list...)
}
}
if err := fs.Walk(o.base, o.walkFunc); err != nil {
return Owners{}, err
}
return o, nil
}
type memo struct {
sync.Map
}
func (m *memo) store(path string, set UsernameSet) {
m.Map.Store(path, set)
}
func (m memo) load(path string) UsernameSet {
val, ok := m.Map.Load(path)
if ok {
return val.(UsernameSet)
}
return nil
}
func (o *Owners) walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
fn := filepath.Base(path)
relPath, err := filepath.Rel(o.base, path)
if err != nil {
return err
}
relPathDir := filepath.Dir(relPath)
if info.Mode().IsDir() || !info.Mode().IsRegular() {
return nil
}
if fn != DefaultOwnersFilename {
return nil
}
f, err := fs.Open(path)
if err != nil {
return err
}
defer f.Close()
oc, err := parseOwners(f)
if err != nil {
return err
}
o.applyOwnersConfig(relPathDir, oc)
return nil
}
func (o *Owners) applyOwnersConfig(path string, oc ownersConfig) {
if len(oc.Approvers) > 0 {
o.approvers[path] = newUsernameSet(oc.Approvers...)
}
if len(oc.Reviewers) > 0 {
o.reviewers[path] = newUsernameSet(oc.Reviewers...)
}
if len(oc.RequiredReviewers) > 0 {
o.requiredReviewers[path] = newUsernameSet(oc.RequiredReviewers...)
}
o.options[path] = oc.Options
}
func (o Owners) entries(path string, mp map[string]UsernameSet, opts map[string]options) UsernameSet {
ret := UsernameSet{}
for {
us, ok := mp[path]
if ok {
ret = ret.Union(us)
}
if opts[path].NoInheritance {
break
}
if path == "" {
break
}
path = filepath.Dir(path)
if path == "." {
path = ""
}
path = strings.TrimSuffix(path, "/")
}
ret = o.expandAliases(ret)
return ret
}
func (o *Owners) expandAliases(usernames UsernameSet) UsernameSet {
usernames = usernames.Copy()
for _, username := range usernames.List() {
if expanded, ok := o.aliases[username]; ok {
usernames.Delete(username)
usernames = usernames.Union(expanded)
}
}
return usernames
}
// Approvers returns a set of approvers for given file path.
func (o *Owners) Approvers(path string) UsernameSet {
if approvers := o.memoizedApprovers.load(path); approvers != nil {
return approvers
}
approvers := o.entries(path, o.approvers, o.options)
o.memoizedApprovers.store(path, approvers)
return approvers
}
// IsApprover returns true if given user is an approver for given file path.
func (o *Owners) IsApprover(user, path string) bool {
approvers := o.Approvers(path)
return approvers.Has(user)
}
// Reviewers returns a set of reviewers for given file path.
func (o *Owners) Reviewers(path string) UsernameSet {
if reviewers := o.memoizedReviewers.load(path); reviewers != nil {
return reviewers
}
reviewers := o.entries(path, o.reviewers, o.options)
o.memoizedReviewers.store(path, reviewers)
return reviewers
}
// IsReviewer returns true if given user is a reviewer for given file path.
func (o *Owners) IsReviewer(user, path string) bool {
reviewers := o.Reviewers(path)
return reviewers.Has(user)
}
// RequiredReviewers returns a set of required reviewers for given file path.
func (o *Owners) RequiredReviewers(path string) UsernameSet {
if requiredReviewers := o.memoizedRequiredReviewers.load(path); requiredReviewers != nil {
return requiredReviewers
}
requiredReviewers := o.entries(path, o.requiredReviewers, o.options)
o.memoizedRequiredReviewers.store(path, requiredReviewers)
return requiredReviewers
}
// IsRequiredReviewer returns true if given user is a required reviewer for given path.
func (o *Owners) IsRequiredReviewer(user, path string) bool {
requiredReviewers := o.RequiredReviewers(path)
return requiredReviewers.Has(user)
}
type options struct {
NoInheritance bool `yaml:"no_inherit,omitempty"`
}
type ownersConfig struct {
Options options `yaml:",inline"`
Approvers []string `yaml:"approvers,omitempty"`
Reviewers []string `yaml:"reviewers,omitempty"`
RequiredReviewers []string `yaml:"required_reviewers,omitempty"`
}
type aliasesConfig struct {
Aliases map[string][]string `yaml:"aliases"`
}
func parseOwners(r io.Reader) (ownersConfig, error) {
var o ownersConfig
if err := yaml.NewDecoder(r).Decode(&o); err != nil {
if err == io.EOF {
return ownersConfig{}, nil
}
return ownersConfig{}, err
}
return o, nil
}
func parseAliases(r io.Reader) (aliasesConfig, error) {
var a aliasesConfig
if err := yaml.NewDecoder(r).Decode(&a); err != nil {
if err == io.EOF {
return aliasesConfig{}, nil
}
return aliasesConfig{}, err
}
return a, nil
}