forked from kubernetes-retired/contrib
/
repo-updates.go
232 lines (199 loc) · 6.07 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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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"
"os"
"os/exec"
"path/filepath"
"strings"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/yaml"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
const (
ownerFilename = "OWNERS" // file which contains owners and assignees
// RepoFeatureName is how mungers should indicate this is required
RepoFeatureName = "gitrepos"
)
type assignmentConfig struct {
Assignees []string `json:assignees yaml:assignees`
Owners []string `json:owners yaml:owners`
}
// RepoInfo provides information about users in OWNERS files in a git repo
type RepoInfo struct {
enabled bool
kubernetesDir string
assignees map[string]sets.String
owners map[string]sets.String
}
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
}
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
}
if filename != ownerFilename {
return nil
}
file, err := os.Open(path)
if err != nil {
glog.Errorf("%v", err)
return nil
}
defer file.Close()
c := &assignmentConfig{}
if err := yaml.NewYAMLToJSONDecoder(file).Decode(c); err != nil {
glog.Errorf("%v", err)
return nil
}
path, err = filepath.Rel(o.kubernetesDir, path)
if err != nil {
return err
}
path = filepath.Dir(path)
o.assignees[path] = sets.NewString(c.Assignees...)
o.owners[path] = sets.NewString(c.Assignees...)
return nil
}
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.assignees = map[string]sets.String{}
//o.owners = map[string]sets.String{}
err = filepath.Walk(o.kubernetesDir, o.walkFunc)
if err != nil {
glog.Errorf("Got error %v", err)
}
glog.Infof("Loaded config from %s:%s", o.kubernetesDir, sha)
glog.V(5).Infof("assignees: %v", o.assignees)
//glog.V(5).Infof("owners: %v", o.owners)
return nil
}
// Initialize will initialize the munger
func (o *RepoInfo) Initialize() error {
o.enabled = true
if len(o.kubernetesDir) == 0 {
glog.Fatalf("--kubernetes-dir is required with selected munger(s)")
}
finfo, err := os.Stat(o.kubernetesDir)
if err != nil {
return fmt.Errorf("Unable to stat --kubernetes-dir: %v", err)
}
if !finfo.IsDir() {
return fmt.Errorf("--kubernetes-dir is not a git directory")
}
return o.updateRepoUsers()
}
// 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.kubernetesDir, "kubernetes-dir", "./gitrepos/kubernetes", "Path to git checkout of kubernetes tree")
}
// GitCommand will execute the git command with the `args`
func (o *RepoInfo) GitCommand(args []string) ([]byte, error) {
cmd := exec.Command("git", args...)
cmd.Dir = o.kubernetesDir
return cmd.CombinedOutput()
}
func peopleForPath(path string, people map[string]sets.String, leafOnly bool) sets.String {
d := filepath.Dir(path)
out := sets.NewString()
for {
s, ok := people[d]
if ok {
out = out.Union(s)
if leafOnly {
break
}
}
if d == "" {
break
}
d, _ = filepath.Split(d)
d = strings.TrimSuffix(d, "/")
}
return out
}
// LeafAssignees returns a set of users who are the closest assginees 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) LeafAssignees(path string) sets.String {
return peopleForPath(path, o.assignees, true)
}
// Assignees returns a set of users who are assignees anywhere along the path to the
// requested file. 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) Assignees(path string) sets.String {
return peopleForPath(path, o.assignees, false)
}
// LeafOwners returns a set of users who are the closest owners to the
// requested file. If pkgOWNERS has user1 and pkg/util/OWNERS has user2 this
// will only return user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) LeafOwners(path string) sets.String {
return peopleForPath(path, o.owners, true)
}
// Assignees returns a set of users who are owners anywhere along the path to the
// requested file. 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) Owners(path string) sets.String {
return peopleForPath(path, o.owners, false)
}
// TestFakeRepo returns a repo with the specified values. It should only be used in
// _test.go
func TestFakeRepo(assignees, owners map[string]sets.String) *RepoInfo {
info := RepoInfo{
enabled: true,
assignees: assignees,
owners: owners,
}
return &info
}