This repository has been archived by the owner on Jul 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
owners_tree_provider.go
94 lines (75 loc) · 1.76 KB
/
owners_tree_provider.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
package main
import (
"io/ioutil"
"path"
"regexp"
)
type DirEntry struct {
Path string
DirOwners []string
FileOwners map[string][]string
Parent *DirEntry
SubDirs []*DirEntry
}
type OwnersTreeProvider struct {
RootPath string
Excludes []*regexp.Regexp
ownersFileProcessor *OwnersFileProcessor
}
func (o *OwnersTreeProvider) GetFileTree() (entries *DirEntry, err error) {
return o.walkDir("", nil)
}
func (o *OwnersTreeProvider) isExcluded(filePath string) bool {
for _, re := range o.Excludes {
if re.MatchString(filePath) {
return true
}
}
return false
}
func (o *OwnersTreeProvider) walkDir(filePath string, parent *DirEntry) (entry *DirEntry, err error) {
dirEntries, err := ioutil.ReadDir(path.Join(o.RootPath, filePath))
if err != nil {
return
}
entry = &DirEntry{
Path: filePath,
Parent: parent,
}
var subDirs []string // Queue for subDirs to walk.
hasOwners := false
for _, ent := range dirEntries {
if o.isExcluded(filePath) {
continue
}
if ent.IsDir() {
subDirs = append(subDirs, ent.Name())
}
if ent.Name() == "OWNERS" {
hasOwners = true
}
}
if !hasOwners {
// If there is no OWNERS file for this dir, we should inherit the parent dir
// owners.
if parent != nil {
entry.DirOwners = parent.DirOwners
}
} else {
ownersContent, err := o.ownersFileProcessor.getOwnersForFile(path.Join(filePath, "OWNERS"))
if err != nil {
return nil, err
}
entry.DirOwners = ownersContent.dirOwners
entry.FileOwners = ownersContent.fileOwners
}
// Parse subdirectories
for _, subdir := range subDirs {
subdirEntry, err := o.walkDir(path.Join(filePath, subdir), entry)
if err != nil {
return nil, err
}
entry.SubDirs = append(entry.SubDirs, subdirEntry)
}
return
}