-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
maintainers.go
147 lines (132 loc) · 3.46 KB
/
maintainers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/git"
gitv2 "k8s.io/test-infra/prow/git/v2"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/repoowners"
)
// Maintainer is the struct representing the info about a maintainer.
type Maintainer struct {
Name string
GitHub string
Company string
Projects []string
}
// Maintainers represents a list of maintainers.
type Maintainers []Maintainer
// Encode outputs the receiving maintainers list in YAML format.
func (m Maintainers) Encode() (string, error) {
var b bytes.Buffer
enc := yaml.NewEncoder(&b)
err := enc.Encode(m)
if err != nil {
return "", err
}
return b.String(), nil
}
func getMaintainers(ghClient github.Client, gitClient *git.Client, opts *Options) (Maintainers, error) {
// Understand on which repository(ies) to act
repos := []Repository{}
if opts.repo == "" {
var err error
repos, err = getRepositories(ghClient, opts.org)
if err != nil {
return nil, err
}
} else {
repos = append(repos, Repository{
Org: opts.org,
Repo: opts.repo,
Branch: opts.branch,
})
}
// todo > how to change these 3 for every repo?
enableMDYAMLFunc := func(org, repo string) bool {
return false
}
skipCollaboratorsFunc := func(org, repo string) bool {
return false
}
ownersDirBlacklistFunc := func() config.OwnersDirBlacklist {
return config.OwnersDirBlacklist{}
}
// Get OWNERS client
ownersClient := repoowners.NewClient(gitv2.ClientFactoryFrom(gitClient), ghClient, enableMDYAMLFunc, skipCollaboratorsFunc, ownersDirBlacklistFunc)
// Get approvers from OWNER files for every repo
maintainers := map[string][]string{}
for _, v := range repos {
approvers, err := getApprovers(ownersClient, v.Org, v.Repo, v.Branch, opts.dedupe)
if err != nil {
logrus.WithField("organization", v.Org).WithField("repository", v.Repo).Error(err)
}
maintainers = mergeApprovers(maintainers, approvers, opts.sort)
}
// Read local database
var persons map[string]Person
if opts.personsSupport {
file, err := os.Open(opts.personsFile)
defer file.Close()
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &persons)
if err != nil {
return nil, err
}
}
// Create maintainers list
res := Maintainers{}
for handle, projects := range maintainers {
host := strings.TrimSuffix(fmt.Sprintf("https://%s", opts.github.Host), "/")
// Get the person name (or fallback to the handle)
// Get the person company (or fallback to UNKNOWN)
name := handle
company := "UNKNOWN"
person, ok := persons[handle]
if ok {
if len(person.Name) > 0 {
name = person.Name
}
if len(person.Company) > 0 {
company = person.Company
}
}
m := Maintainer{
Name: name,
GitHub: fmt.Sprintf("%s/%s", host, handle),
Company: company,
Projects: []string{},
}
for _, p := range projects {
parts := strings.Split(p, "/")
if len(parts) > 2 {
repoparts := strings.Split(parts[1], ":")
parts[1] = repoparts[0]
parts[2] = fmt.Sprintf("tree/%s/%s", repoparts[1], parts[2])
}
p = strings.Join(parts[:], "/")
m.Projects = append(m.Projects, fmt.Sprintf("%s/%s", host, p))
}
res = append(res, m)
}
if opts.sort {
sort.Slice(res, func(i, j int) bool {
return res[i].Name < res[j].Name
})
}
return res, nil
}