forked from kubernetes-retired/contrib
/
user-lists.go
183 lines (160 loc) · 5.46 KB
/
user-lists.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
/*
Copyright 2015 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 mungers
// A simple binary for merging PR that match a criteria
// Usage:
// submit-queue -token=<github-access-token> -user-whitelist=<file> --jenkins-host=http://some.host [-min-pr-number=<number>] [-dry-run] [-once]
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
github_util "k8s.io/contrib/mungegithub/github"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
var (
_ = fmt.Print
)
// RefreshWhitelist updates the whitelist, re-getting the list of committers.
// called with sq.Lock() held!
func (sq *SubmitQueue) RefreshWhitelist() {
config := sq.githubConfig
info := map[string]userInfo{}
if sq.additionalUserWhitelist == nil {
users, err := loadWhitelist(sq.Whitelist)
if err != nil {
glog.Fatalf("error loading user whitelist: %v", err)
}
sq.additionalUserWhitelist = &users
}
if sq.committerList == nil {
committerList, err := loadWhitelist(sq.Committers)
if err != nil {
glog.Fatalf("error loading committers whitelist: %v", err)
}
sq.committerList = &committerList
}
// We must use the values on disk in case it has users which don't have
// explicit "pull" permission in the API
allUsers := sq.additionalUserWhitelist.Union(*sq.committerList)
if pushUsers, pullUsers, err := config.UsersWithAccess(); err != nil {
glog.Info("Falling back to static committers list.")
} else {
for _, user := range pullUsers {
allUsers.Insert(*user.Login)
info[*user.Login] = userInfo{
Login: *user.Login,
Access: "pull access",
AvatarURL: *user.AvatarURL,
}
}
for _, user := range pushUsers {
allUsers.Insert(*user.Login)
info[*user.Login] = userInfo{
Login: *user.Login,
Access: "push access",
AvatarURL: *user.AvatarURL,
}
}
}
// Anyone we got from a config file and not from the API we need to look up info
for _, login := range allUsers.List() {
if _, ok := info[login]; ok {
continue
}
user, err := config.GetUser(login)
if err != nil {
glog.Errorf("Unable to get user information about %q", login)
continue
}
info[login] = userInfo{
Login: *user.Login,
Access: "explicitly whitelisted",
AvatarURL: *user.AvatarURL,
}
}
sq.userWhitelist = &allUsers
sq.userInfo = info
return
}
func loadWhitelist(file string) (sets.String, error) {
result := sets.String{}
if len(file) == 0 {
return result, nil
}
fp, err := os.Open(file)
if err != nil {
return result, err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
current := scanner.Text()
if !strings.HasPrefix(current, "#") {
result.Insert(current)
}
}
return result, scanner.Err()
}
func writeWhitelist(fileName, header string, users sets.String) error {
items := append([]string{header}, users.List()...)
items = append(items, "")
return ioutil.WriteFile(fileName, []byte(strings.Join(items, "\n")), 0640)
}
func (sq *SubmitQueue) doGenCommitters(config *github_util.Config) error {
pushUsers, pullUsers, err := config.UsersWithAccess()
if err != nil {
glog.Fatalf("Unable to read committers from github: %v", err)
}
pushSet := sets.NewString()
for _, user := range pushUsers {
pushSet.Insert(*user.Login)
}
pullSet := sets.NewString()
for _, user := range pullUsers {
pullSet.Insert(*user.Login)
}
if err = writeWhitelist(sq.Committers, "# auto-generated by "+os.Args[0]+" gen-committers; manual additions should go in the whitelist", pushSet); err != nil {
glog.Fatalf("Unable to write committers: %v", err)
}
glog.Info("Successfully updated committers file.")
existingWhitelist, err := loadWhitelist(sq.Whitelist)
if err != nil {
glog.Fatalf("error loading whitelist; it will not be updated: %v", err)
}
neededInWhitelist := existingWhitelist.Union(pullSet)
neededInWhitelist = neededInWhitelist.Difference(pushSet)
if err = writeWhitelist(sq.Whitelist, "# auto-generated by "+os.Args[0]+" gen-committers; manual additions may be added by hand", neededInWhitelist); err != nil {
glog.Fatalf("Unable to write additional user whitelist: %v", err)
}
glog.Info("Successfully update whitelist file.")
return nil
}
func (sq *SubmitQueue) addWhitelistCommand(root *cobra.Command, config *github_util.Config) {
genCommitters := &cobra.Command{
Use: "gencommiters",
Short: "Generate the list of people with commit access",
RunE: func(_ *cobra.Command, _ []string) error {
if err := config.PreExecute(); err != nil {
return err
}
return sq.doGenCommitters(config)
},
}
root.PersistentFlags().StringVar(&sq.Whitelist, "user-whitelist", "./whitelist.txt", "Path to a whitelist file that contains users to auto-merge. Required.")
root.PersistentFlags().StringVar(&sq.Committers, "committers", "./committers.txt", "File in which the list of authorized committers is stored; only used if this list cannot be gotten at run time. (Merged with whitelist; separate so that it can be auto-generated)")
root.AddCommand(genCommitters)
}