forked from docker/gordon
/
review.go
208 lines (198 loc) · 5.72 KB
/
review.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
package gordon
import (
"bufio"
"code.google.com/p/go.codereview/patch"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
// ReviewPatch reads a git-formatted patch from `src`, and for each file affected by the patch
// it assign its Maintainers based on the current repository tree directories
// The list of Maintainers are generated when the MaintainerManager object is instantiated.
//
// The result is a map where the keys are the paths of files affected by the patch,
// and the values are the maintainers assigned to review that partiular file.
//
// There is no duplicate checks: the same maintainer may be present in multiple entries
// of the map, or even multiple times in the same entry if the MAINTAINERS file has
// duplicate lines.
func ReviewPatch(src io.Reader, maintainersDirMap *map[string][]*Maintainer) (reviewers map[string][]*Maintainer, err error) {
reviewers = make(map[string][]*Maintainer)
input, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
set, err := patch.Parse(input)
if err != nil {
return nil, err
}
for _, f := range set.File {
for _, target := range []string{f.Dst, f.Src} {
if target == "" {
continue
}
target = path.Clean(target)
if _, exists := reviewers[target]; exists {
continue
}
targetDir := "."
items := strings.Split(target, "/")
for i := 0; i < len(items)-1; i++ {
if targetDir == "." {
targetDir = items[i]
} else {
targetDir = path.Join(targetDir, items[i])
}
}
maintainers := (*maintainersDirMap)[targetDir]
if len(maintainers) == 0 {
parentPath := ""
for _, dir := range strings.Split(targetDir, "/") {
if parentPath == "" {
parentPath = "."
}
if len((*maintainersDirMap)[parentPath]) > 0 {
reviewers[target] = (*maintainersDirMap)[parentPath]
} else {
break
}
parentPath = path.Join(parentPath, dir)
}
} else {
reviewers[target] = maintainers
}
}
}
return reviewers, nil
}
// Currently not being used
func getMaintainers(target string) (maintainers []*Maintainer, err error) {
if _, err := os.Stat(target); err != nil {
return nil, err
}
target, err = filepath.Abs(target)
if err != nil {
return nil, err
}
if target == "/" {
return []*Maintainer{}, nil
}
defer func() {
if err == nil && (maintainers == nil || len(maintainers) == 0) {
maintainers, err = getMaintainers(path.Dir(target))
}
}()
if path.Base(target) == "MAINTAINERS" {
tpmf, err := TopMostMaintainerFile(path.Dir(target))
if err != nil {
return nil, err
}
if maintainers, exists := tpmf["."]; !exists || len(maintainers) == 0 {
return nil, fmt.Errorf("can't find the top-level maintainer to review MAINTAINERS change")
} else if lead := maintainers[0]; !lead.Active {
return nil, fmt.Errorf("can't review MAINTAINERS change: top-level maintainer %s is inactive", lead.FullName)
} else {
return []*Maintainer{lead}, nil
}
}
maintainerFile, err := LoadMaintainerFile(path.Dir(target))
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
if fileMaintainers, exists := maintainerFile[path.Base(target)]; exists {
return fileMaintainers, nil
} else if dirMaintainers, exists := maintainerFile["."]; exists {
return dirMaintainers, nil
}
return nil, nil
}
type MaintainerFile map[string][]*Maintainer
type Maintainer struct {
Username string
FullName string
Email string
Target string
Active bool
Lead bool
Raw string
}
// Currently not being used
func LoadMaintainerFile(dir string) (MaintainerFile, error) {
src, err := os.Open(path.Join(dir, "MAINTAINERS"))
if err != nil {
return nil, err
}
maintainers := make(MaintainerFile)
scanner := bufio.NewScanner(src)
for scanner.Scan() {
m := parseMaintainer(scanner.Text())
if m.Username == "" && m.Email == "" && m.FullName == "" {
return nil, fmt.Errorf("Incorrect maintainer format: %s", m.Raw)
}
if _, exists := maintainers[m.Target]; !exists {
maintainers[m.Target] = make([]*Maintainer, 0, 1)
}
maintainers[m.Target] = append(maintainers[m.Target], m)
}
return maintainers, nil
}
func parseMaintainer(line string) *Maintainer {
const (
commentIndex = 1
targetIndex = 3
fullnameIndex = 4
emailIndex = 5
usernameIndex = 7
)
re := regexp.MustCompile("^[ \t]*(#|)((?P<target>[^: ]*) *:|) *(?P<fullname>[a-zA-Z][^<]*) *<(?P<email>[^>]*)> *(\\(@(?P<username>[^\\)]+)\\)|).*$")
match := re.FindStringSubmatch(line)
return &Maintainer{
Active: match[commentIndex] == "",
Target: path.Base(path.Clean(match[targetIndex])),
Username: strings.Trim(match[usernameIndex], " \t"),
Email: strings.Trim(match[emailIndex], " \t"),
FullName: strings.Trim(match[fullnameIndex], " \t"),
Raw: line,
}
}
// Currently not being used
//
// TopMostMaintainerFile moves up the directory tree looking for a MAINTAINERS file,
// parses the top-most file it finds, and returns its contents.
// This is used to find the top-level maintainer of a project for certain
// privileged reviews, such as authorizing changes to a MAINTAINERS file.
func TopMostMaintainerFile(dir string) (MaintainerFile, error) {
if _, err := os.Stat(dir); err != nil {
return nil, err
}
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
if dir == "/" {
return make(MaintainerFile), nil
}
parent, err := TopMostMaintainerFile(path.Dir(dir))
if err != nil {
// Ignore recursive errors which might be caused by
// permission errors on parts of the filesystem, etc.
parent = make(MaintainerFile)
}
if len(parent) > 0 {
return parent, nil
}
current, err := LoadMaintainerFile(dir)
if os.IsNotExist(err) {
return make(MaintainerFile), nil
} else if err != nil {
return nil, err
}
return current, nil
}