forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
approver_helper.go
135 lines (115 loc) · 3.48 KB
/
approver_helper.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
package main
import (
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/opensourceways/robot-gitee-review-trigger/approvers"
)
type approverHelper struct {
currentApprovers []string
assignees []string
filenames []string
prNumber int32
numberOfApprovers int
repoOwner approvers.Repo
prAuthor string
allowSelfApprove bool
log *logrus.Entry
}
func (ah approverHelper) suggestApprovers() []string {
currentApprovers1 := sets.NewString(ah.currentApprovers...)
assignees1 := sets.NewString(ah.assignees...)
prAuthor := ah.prAuthor
dontAllowSelfApprove := !ah.allowSelfApprove
if dontAllowSelfApprove {
if currentApprovers1.Has(prAuthor) {
currentApprovers1.Delete(prAuthor)
}
if assignees1.Has(prAuthor) {
assignees1.Delete(prAuthor)
}
}
owner := approvers.NewOwners(ah.log, ah.filenames, ah.repoOwner, int64(ah.prNumber))
if ah.numberOfApprovers == 1 {
ap := approvers.NewApprovers(owner)
ap.AddAssignee(assignees1.UnsortedList()...)
ap.AddApprover(currentApprovers1.UnsortedList()...)
return ap.GetCCs()
}
approversAndAssignees := currentApprovers1.Union(assignees1)
randomizedApprovers := owner.GetShuffledApprovers()
leafReverseMap := approvers.GetReverseMap(owner.GetLeafApprovers())
if dontAllowSelfApprove {
if _, ok := leafReverseMap[prAuthor]; ok {
delete(leafReverseMap, prAuthor)
randomizedApprovers = removeSliceElement(randomizedApprovers, prAuthor)
}
}
suggested := ah.keepCoveringApprovers(
owner, leafReverseMap, approversAndAssignees, randomizedApprovers,
)
approversAndSuggested := currentApprovers1.Union(suggested)
everyone := approversAndSuggested.Union(assignees1)
fullReverseMap := approvers.GetReverseMap(owner.GetApprovers())
if dontAllowSelfApprove {
if _, ok := fullReverseMap[prAuthor]; ok {
delete(fullReverseMap, prAuthor)
}
}
keepAssignees := ah.keepCoveringApprovers(
owner, fullReverseMap, approversAndSuggested, everyone.UnsortedList(),
)
return suggested.Union(keepAssignees).List()
}
func removeSliceElement(v []string, target string) []string {
for i, item := range v {
if item == target {
n := len(v) - 1
v[i] = v[n]
return v[:n]
}
}
return v
}
func (ah approverHelper) keepCoveringApprovers(owner approvers.Owners, reverseMap map[string]sets.String, knownApprovers sets.String, potentialApprovers []string) sets.String {
numberOfApprovers := ah.numberOfApprovers
f := func(ap *approvers.StaticApprovers) sets.String {
excludedApprovers := sets.String{}
unapproved := sets.String{}
files := ap.GetFilesApprovers()
for f, v := range files {
if len(v) < numberOfApprovers {
unapproved.Insert(f)
for k := range v {
excludedApprovers.Insert(k)
}
}
}
if len(unapproved) == 0 {
return sets.NewString()
}
candidates := []string{}
for _, item := range potentialApprovers {
if !excludedApprovers.Has(item) {
candidates = append(candidates, item)
}
}
keptApprovers := sets.NewString()
for suggestedApprover := range owner.GetSuggestedApprovers(reverseMap, candidates) {
if reverseMap[suggestedApprover].Intersection(unapproved).Len() != 0 {
keptApprovers.Insert(suggestedApprover)
}
}
return keptApprovers
}
ap := approvers.NewStaticApprovers(owner, knownApprovers.UnsortedList())
r := sets.NewString()
for i := 0; i < numberOfApprovers; i++ {
v := f(ap)
if len(v) == 0 {
break
}
r = r.Union(v)
ap.AddApprover(v.UnsortedList()...)
}
return r
}