forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
77 lines (58 loc) · 1.44 KB
/
label.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
package main
import (
"k8s.io/apimachinery/pkg/util/sets"
)
func updatePRLabel(c ghclient, pr iPRInfo, keep ...string) error {
_, err := updateAndReturnRemovedLabels(c, pr, keep...)
return err
}
func updateAndReturnRemovedLabels(c ghclient, pr iPRInfo, keep ...string) ([]string, error) {
l := labelUpdating{
c: c,
pr: pr,
}
mr := multiError()
if err := l.addLabels(keep...); err != nil {
mr.AddError(err)
}
all := sets.NewString(labelApproved, labelLGTM, labelCanReview, labelRequestChange)
toRemove := all.Delete(keep...).UnsortedList()
removed, err := l.removeLabels(toRemove)
if err != nil {
mr.AddError(err)
}
return removed, mr.Err()
}
type labelUpdating struct {
c ghclient
pr iPRInfo
}
func (l labelUpdating) addLabels(labels ...string) error {
pr := l.pr
toAdd := filterSlice(labels, pr.hasLabel)
if len(toAdd) == 0 {
return nil
}
org, repo := pr.getOrgAndRepo()
return l.c.AddMultiPRLabel(org, repo, pr.getNumber(), labels)
}
func (l labelUpdating) removeLabels(labels []string) ([]string, error) {
pr := l.pr
toRemove := filterSlice(labels, func(l string) bool {
return !pr.hasLabel(l)
})
if len(toRemove) == 0 {
return nil, nil
}
org, repo := pr.getOrgAndRepo()
return toRemove, l.c.RemovePRLabels(org, repo, pr.getNumber(), toRemove)
}
func filterSlice(s []string, filter func(string) bool) []string {
v := []string{}
for _, l := range s {
if !filter(l) {
v = append(v, l)
}
}
return v
}