forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owner.go
176 lines (156 loc) · 4.29 KB
/
owner.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
/*
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 testowner
import (
"encoding/csv"
"errors"
"io"
"math/rand"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/golang/glog"
)
var tagRegex = regexp.MustCompile(`\[.*?\]|\{.*?\}`)
var whiteSpaceRegex = regexp.MustCompile(`\s+`)
// Turn a test name into a canonical form (without tags, lowercase, etc.)
func normalize(name string) string {
tagLess := tagRegex.ReplaceAll([]byte(name), []byte(""))
squeezed := whiteSpaceRegex.ReplaceAll(tagLess, []byte(" "))
return strings.ToLower(strings.TrimSpace(string(squeezed)))
}
// OwnerList uses a map to get owners for a given test name.
type OwnerList struct {
mapping map[string]string
rng *rand.Rand
}
// TestOwner returns the owner for a test, an owner from default if present,
// or else the empty string if none is found.
func (o *OwnerList) TestOwner(testName string) string {
name := normalize(testName)
// exact mapping
owner, _ := o.mapping[name]
// glob matching
if owner == "" {
keys := []string{}
for k := range o.mapping {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if match, _ := filepath.Match(k, name); match {
owner = o.mapping[k]
break
}
}
}
// falls into default
if owner == "" {
owner, _ = o.mapping["default"]
}
if strings.Contains(owner, "/") {
ownerSet := strings.Split(owner, "/")
owner = ownerSet[o.rng.Intn(len(ownerSet))]
}
return owner
}
// NewOwnerList constructs an OwnerList given a mapping from test names to test owners.
func NewOwnerList(mapping map[string]string) *OwnerList {
list := OwnerList{}
list.rng = rand.New(rand.NewSource(time.Now().UnixNano()))
list.mapping = make(map[string]string)
for input, output := range mapping {
list.mapping[normalize(input)] = output
}
return &list
}
// NewOwnerListFromCsv constructs an OwnerList given a CSV file that includes
// 'owner' and 'test name' columns.
func NewOwnerListFromCsv(r io.Reader) (*OwnerList, error) {
reader := csv.NewReader(r)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
mapping := make(map[string]string)
ownerCol := -1
nameCol := -1
for _, record := range records {
if ownerCol == -1 || nameCol == -1 {
for col, val := range record {
switch strings.ToLower(val) {
case "owner":
ownerCol = col
case "name":
nameCol = col
}
}
} else {
mapping[record[nameCol]] = record[ownerCol]
}
}
if len(mapping) == 0 {
return nil, errors.New("no mappings found in test owners CSV")
}
return NewOwnerList(mapping), nil
}
// ReloadingOwnerList maps test names to owners, reloading the mapping when the
// underlying file is changed.
type ReloadingOwnerList struct {
path string
mtime time.Time
ownerList *OwnerList
}
// NewReloadingOwnerList creates a ReloadingOwnerList given a path to a CSV
// file containing owner mapping information.
func NewReloadingOwnerList(path string) (*ReloadingOwnerList, error) {
ownerList := &ReloadingOwnerList{path: path}
err := ownerList.reload()
if err != nil {
return nil, err
}
return ownerList, nil
}
// TestOwner returns the owner for a test, or the empty string if none is found.
func (o *ReloadingOwnerList) TestOwner(testName string) string {
err := o.reload()
if err != nil {
glog.Errorf("Unable to reload test owners at %s: %v", o.path, err)
// Process using the previous data.
}
return o.ownerList.TestOwner(testName)
}
func (o *ReloadingOwnerList) reload() error {
info, err := os.Stat(o.path)
if err != nil {
return err
}
if info.ModTime() == o.mtime {
return nil
}
file, err := os.Open(o.path)
if err != nil {
return err
}
defer file.Close()
ownerList, err := NewOwnerListFromCsv(file)
if err != nil {
return err
}
o.ownerList = ownerList
o.mtime = info.ModTime()
return nil
}