forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo-updates.go
354 lines (308 loc) · 10.7 KB
/
repo-updates.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Copyright 2016 The Kubernetes Authors.
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 features
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"k8s.io/contrib/mungegithub/github"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/yaml"
parseYaml "github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
const (
ownerFilename = "OWNERS" // file which contains approvers and reviewers
// RepoFeatureName is how mungers should indicate this is required
RepoFeatureName = "gitrepos"
// Github's api uses "" (empty) string as basedir by convention but it's clearer to use "/"
baseDirConvention = ""
)
type assignmentConfig struct {
Assignees []string `json:assignees yaml:assignees`
Approvers []string `json:approvers yaml:approvers`
Reviewers []string `json:reviewers yaml:reviewers`
}
// RepoInfo provides information about users in OWNERS files in a git repo
type RepoInfo struct {
BaseDir string
EnableMdYaml bool
UseReviewers bool
enabled bool
projectDir string
approvers map[string]sets.String
reviewers map[string]sets.String
config *github.Config
}
func init() {
RegisterFeature(&RepoInfo{})
}
// Name is just going to return the name mungers use to request this feature
func (o *RepoInfo) Name() string {
return RepoFeatureName
}
// by default, github's api doesn't root the project directory at "/" and instead uses the empty string for the base dir
// of the project. And the built-in dir function returns "." for empty strings, so for consistency, we use this
// canonicalize to get the directories of files in a consistent format with NO "/" at the root (a/b/c/ -> a/b/c)
func canonicalize(path string) string {
if path == "." {
return baseDirConvention
}
return strings.TrimSuffix(path, "/")
}
func (o *RepoInfo) walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
glog.Errorf("%v", err)
return nil
}
filename := filepath.Base(path)
if info.Mode().IsDir() {
switch filename {
case ".git":
return filepath.SkipDir
case "_output":
return filepath.SkipDir
}
}
if !info.Mode().IsRegular() {
return nil
}
c := &assignmentConfig{}
// '.md' files may contain assignees at the top of the file in a yaml header
// Flag guarded because this is only enabled in some repos
if o.EnableMdYaml && filename != ownerFilename && strings.HasSuffix(filename, "md") {
// Parse the yaml header from the file if it exists and marshal into the config
if err := decodeAssignmentConfig(path, c); err != nil {
glog.Errorf("%v", err)
return err
}
// Set assignees for this file using the relative path if they were found
path, err = filepath.Rel(o.projectDir, path)
if err != nil {
glog.Errorf("Unable to find relative path between %q and %q: %v", o.projectDir, path, err)
return err
}
o.approvers[path] = sets.NewString(c.Approvers...)
o.approvers[path].Insert(c.Assignees...)
o.reviewers[path] = sets.NewString(c.Reviewers...)
return nil
}
if filename != ownerFilename {
return nil
}
file, err := os.Open(path)
if err != nil {
glog.Errorf("%v", err)
return nil
}
defer file.Close()
if err := yaml.NewYAMLToJSONDecoder(file).Decode(c); err != nil {
glog.Errorf("%v", err)
return nil
}
path, err = filepath.Rel(o.projectDir, path)
path = filepath.Dir(path)
if err != nil {
glog.Errorf("Unable to find relative path between %q and %q: %v", o.projectDir, path, err)
return err
}
path = canonicalize(path)
o.approvers[path] = sets.NewString(c.Approvers...)
o.approvers[path].Insert(c.Assignees...)
o.reviewers[path] = sets.NewString(c.Reviewers...)
return nil
}
// decodeAssignmentConfig will parse the yaml header if it exists and unmarshal it into an assignmentConfig.
// If no yaml header is found, do nothing
// Returns an error if the file cannot be read or the yaml header is found but cannot be unmarshalled
var mdStructuredHeaderRegex = regexp.MustCompile("^---\n(.|\n)*\n---")
func decodeAssignmentConfig(path string, config *assignmentConfig) error {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Parse the yaml header from the top of the file. Will return an empty string if regex does not match.
meta := mdStructuredHeaderRegex.FindString(string(fileBytes))
// Unmarshal the yaml header into the config
return parseYaml.Unmarshal([]byte(meta), &config)
}
func (o *RepoInfo) updateRepoUsers() error {
out, err := o.GitCommand([]string{"pull"})
if err != nil {
glog.Errorf("Unable to run git pull:\n%s\n%v", string(out), err)
return err
}
out, err = o.GitCommand([]string{"rev-parse", "HEAD"})
if err != nil {
glog.Errorf("Unable get sha of HEAD:\n%s\n%v", string(out), err)
return err
}
sha := out
o.approvers = map[string]sets.String{}
o.reviewers = map[string]sets.String{}
err = filepath.Walk(o.projectDir, o.walkFunc)
if err != nil {
glog.Errorf("Got error %v", err)
}
glog.Infof("Loaded config from %s:%s", o.projectDir, sha)
glog.V(5).Infof("approvers: %v", o.approvers)
glog.V(5).Infof("reviewers: %v", o.reviewers)
return nil
}
// Initialize will initialize the munger
func (o *RepoInfo) Initialize(config *github.Config) error {
o.enabled = true
o.config = config
o.projectDir = path.Join(o.BaseDir, o.config.Project)
if len(o.BaseDir) == 0 {
glog.Fatalf("--repo-dir is required with selected munger(s)")
}
finfo, err := os.Stat(o.BaseDir)
if err != nil {
return fmt.Errorf("Unable to stat --repo-dir: %v", err)
}
if !finfo.IsDir() {
return fmt.Errorf("--repo-dir is not a directory")
}
// check if the cloned dir already exists, if yes, cleanup.
if _, err := os.Stat(o.projectDir); !os.IsNotExist(err) {
if err := o.cleanUp(o.projectDir); err != nil {
return fmt.Errorf("Unable to remove old clone directory at %v: %v", o.projectDir, err)
}
}
if cloneUrl, err := o.cloneRepo(); err != nil {
return fmt.Errorf("Unable to clone %v: %v", cloneUrl, err)
}
return o.updateRepoUsers()
}
func (o *RepoInfo) cleanUp(path string) error {
return os.RemoveAll(path)
}
func (o *RepoInfo) cloneRepo() (string, error) {
cloneUrl := fmt.Sprintf("https://github.com/%s/%s.git", o.config.Org, o.config.Project)
output, err := o.gitCommandDir([]string{"clone", cloneUrl, o.projectDir}, o.BaseDir)
if err != nil {
glog.Errorf("Failed to clone github repo: %s", output)
}
return cloneUrl, err
}
// EachLoop is called at the start of every munge loop
func (o *RepoInfo) EachLoop() error {
if !o.enabled {
return nil
}
_, err := o.GitCommand([]string{"remote", "update"})
if err != nil {
glog.Errorf("Unable to git remote update: %v", err)
}
return o.updateRepoUsers()
}
// AddFlags will add any request flags to the cobra `cmd`
func (o *RepoInfo) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.BaseDir, "repo-dir", "", "Path to perform checkout of repository")
cmd.Flags().BoolVar(&o.EnableMdYaml, "enable-md-yaml", false, "If true, look for assignees in md yaml headers.")
cmd.Flags().BoolVar(&o.UseReviewers, "use-reviewers", false, "Use \"reviewers\" rather than \"approvers\" for review")
}
// GitCommand will execute the git command with the `args` within the project directory.
func (o *RepoInfo) GitCommand(args []string) ([]byte, error) {
return o.gitCommandDir(args, o.projectDir)
}
// GitCommandDir will execute the git command with the `args` within the 'dir' directory.
func (o *RepoInfo) gitCommandDir(args []string, cmdDir string) ([]byte, error) {
cmd := exec.Command("git", args...)
cmd.Dir = cmdDir
return cmd.CombinedOutput()
}
// FindOwnersForPath returns the OWNERS file path further down the tree for a file
func (o *RepoInfo) FindOwnersForPath(path string) string {
d := path
for {
_, ok := o.approvers[d]
if ok {
return d
}
if d == baseDirConvention {
break
}
d = filepath.Dir(d)
d = canonicalize(d)
}
return ""
}
// peopleForPath returns a set of users who are assignees to the
// requested file. The path variable should be a full path to a filename
// and not directory as the final directory will be discounted if enableMdYaml is true
// leafOnly indicates whether only the OWNERS deepest in the tree (closest to the file)
// should be returned or if all OWNERS in filepath should be returned
func peopleForPath(path string, people map[string]sets.String, leafOnly bool, enableMdYaml bool) sets.String {
d := path
if !enableMdYaml {
// if path is a directory, this will remove the leaf directory, and returns "." for topmost dir
d = filepath.Dir(d)
d = canonicalize(path)
}
out := sets.NewString()
for {
s, ok := people[d]
if ok {
out = out.Union(s)
if leafOnly && out.Len() > 0 {
break
}
}
if d == baseDirConvention {
break
}
d = filepath.Dir(d)
d = canonicalize(d)
}
return out
}
// LeafApprovers returns a set of users who are the closest approvers to the
// requested file. If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will only return user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) LeafApprovers(path string) sets.String {
return peopleForPath(path, o.approvers, true, o.EnableMdYaml)
}
// Approvers returns ALL of the users who are approvers for the
// requested file (including approvers in parent dirs' OWNERS).
// If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will return both user1 and user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) Approvers(path string) sets.String {
return peopleForPath(path, o.approvers, false, o.EnableMdYaml)
}
// LeafReviewers returns a set of users who are the closest reviewers to the
// requested file. If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will only return user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) LeafReviewers(path string) sets.String {
if !o.UseReviewers {
return o.LeafApprovers(path)
}
return peopleForPath(path, o.reviewers, true, o.EnableMdYaml)
}
// Reviewers returns ALL of the users who are reviewers for the
// requested file (including reviewers in parent dirs' OWNERS).
// If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will return both user1 and user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) Reviewers(path string) sets.String {
if !o.UseReviewers {
return o.Approvers(path)
}
return peopleForPath(path, o.reviewers, false, o.EnableMdYaml)
}