forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign-unassign-handler.go
173 lines (144 loc) · 5.55 KB
/
assign-unassign-handler.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mungers
import (
"bytes"
"fmt"
"k8s.io/contrib/mungegithub/features"
"k8s.io/contrib/mungegithub/github"
"k8s.io/kubernetes/pkg/util/sets"
githubapi "github.com/google/go-github/github"
"github.com/spf13/cobra"
c "k8s.io/contrib/mungegithub/mungers/matchers/comment"
)
const (
assignCommand = "assign"
unassignCommand = "unassign"
invalidReviewer = "ASSIGN_NOTIFIER"
)
// AssignUnassignHandler will
// - will assign a github user to a PR if they comment "/assign"
// - will unassign a github user to a PR if they comment "/unassign"
type AssignUnassignHandler struct {
features *features.Features
}
func init() {
dh := &AssignUnassignHandler{}
RegisterMungerOrDie(dh)
}
// Name is the name usable in --pr-mungers
func (AssignUnassignHandler) Name() string { return "assign-unassign-handler" }
// RequiredFeatures is a slice of 'features' that must be provided
func (AssignUnassignHandler) RequiredFeatures() []string { return []string{} }
// Initialize will initialize the munger
func (h *AssignUnassignHandler) Initialize(config *github.Config, features *features.Features) error {
h.features = features
return nil
}
// EachLoop is called at the start of every munge loop
func (AssignUnassignHandler) EachLoop() error { return nil }
// AddFlags will add any request flags to the cobra `cmd`
func (h AssignUnassignHandler) AddFlags(cmd *cobra.Command, config *github.Config) {
}
// Munge is the workhorse the will actually make updates to the PR
func (h AssignUnassignHandler) Munge(obj *github.MungeObject) {
if !obj.IsPR() {
return
}
comments, ok := obj.ListComments()
if !ok {
return
}
fileList, ok := obj.ListFiles()
if !ok {
return
}
//get ALL (not just leaf) the people that could potentially own the file based on the blunderbuss.go implementation
potentialOwners, _ := getPotentialOwners(*obj.Issue.User.Login, h.features, fileList, false)
toAssign, toUnassign := h.getAssigneesAndUnassignees(obj, comments, fileList, potentialOwners)
for _, username := range toAssign.List() {
obj.AssignPR(username)
}
obj.UnassignPR(toUnassign.List()...)
}
// getAssigneesAndUnassignees checks to see when someone comments "/assign" or "/unassign"
// returns two sets.String
// 1. github handles to be assigned
// 2. github handles to be unassigned
// Note* Could possibly assign directly in the function call, but easier to test if function returns toAssign, toUnassign
func (h *AssignUnassignHandler) getAssigneesAndUnassignees(obj *github.MungeObject, comments []*githubapi.IssueComment, fileList []*githubapi.CommitFile, potentialOwners weightMap) (toAssign, toUnassign sets.String) {
toAssign = sets.String{}
toUnassign = sets.String{}
assignComments := c.FilterComments(comments, c.CommandName(assignCommand))
unassignComments := c.FilterComments(comments, c.CommandName(unassignCommand))
invalidUsers := sets.String{}
//collect all the people that should be assigned
for _, cmt := range assignComments {
if isValidReviewer(potentialOwners, cmt.User) {
obj.DeleteComment(cmt)
toAssign.Insert(*cmt.User.Login)
} else {
// build the set of people who asked to be assigned but aren't in reviewers
// use the @ as a prefix so github notifies invalid users
invalidUsers.Insert("@" + *cmt.User.Login)
}
}
// collect all the people that should be unassigned
for _, cmt := range unassignComments {
if isAssignee(obj.Issue.Assignees, cmt.User) {
obj.DeleteComment(cmt)
toUnassign.Insert(*cmt.User.Login)
}
}
// Create a notification if someone tried to self assign, but could not because they weren't in the owners files
if invalidUsers.Len() != 0 {
previousNotifications := c.FilterComments(comments, c.MungerNotificationName(invalidReviewer))
if assignComments.Empty() || (!previousNotifications.Empty() && previousNotifications.GetLast().CreatedAt.After(*assignComments.GetLast().CreatedAt)) {
// if there were no assign comments, no need to notify
// if the last notification happened after the last assign comment, no need to notify again
return toAssign, toUnassign
}
if !previousNotifications.Empty() {
for _, c := range previousNotifications {
obj.DeleteComment(c)
}
}
context := bytes.NewBufferString("The following people cannot be assigned because they are not in the OWNERS files\n")
for user := range invalidUsers {
context.WriteString(fmt.Sprintf("- %s\n", user))
}
context.WriteString("\n")
c.Notification{Name: invalidReviewer, Arguments: "", Context: context.String()}.Post(obj)
}
return toAssign, toUnassign
}
func isValidReviewer(potentialOwners weightMap, commenter *githubapi.User) bool {
if commenter == nil || commenter.Login == nil {
return false
}
if _, ok := potentialOwners[*commenter.Login]; ok {
return true
}
return false
}
func isAssignee(assignees []*githubapi.User, someUser *githubapi.User) bool {
for _, assignee := range assignees {
if assignee.Login == nil || someUser.Login == nil {
continue
}
if *assignee.Login == *someUser.Login && *someUser.ID == *assignee.ID {
return true
}
}
return false
}