forked from kubernetes-retired/contrib
/
util.go
114 lines (89 loc) · 2.79 KB
/
util.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
/*
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 mungerutil
import (
"strings"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/google/go-github/github"
)
const (
// BotName is the name of merge-bot
BotName = "k8s-merge-robot"
)
// UserSet is a set a of users
type UserSet sets.String
// GetUsers returns a UserSet
func GetUsers(users ...*github.User) UserSet {
allUsers := sets.String{}
for _, user := range users {
if !IsValidUser(user) {
continue
}
allUsers.Insert(*user.Login)
}
return UserSet(allUsers)
}
// Has tells you if the users can be found in the set
func (u UserSet) Has(user ...*github.User) bool {
return len(u.intersection(GetUsers(user...))) != 0
}
// Mention adds @ to user in the list who don't have it yet
func (u UserSet) Mention() UserSet {
mentionedUsers := sets.NewString()
for _, user := range u.List() {
if !strings.HasPrefix(user, "@") {
mentionedUsers.Insert("@" + user)
} else {
mentionedUsers.Insert(user)
}
}
return UserSet(mentionedUsers)
}
// List makes a list from the set
func (u UserSet) List() []string {
return sets.String(u).List()
}
// Join joins each users into a single string
func (u UserSet) Join() string {
return strings.Join(u.List(), " ")
}
func (u UserSet) union(o UserSet) UserSet {
return UserSet(sets.String(u).Union(sets.String(o)))
}
func (u UserSet) intersection(o UserSet) UserSet {
return UserSet(sets.String(u).Intersection(sets.String(o)))
}
// IssueUsers tracks Users involved in a github Issue
type IssueUsers struct {
Assignees UserSet
Author UserSet // This will usually be one or zero
}
// GetIssueUsers creates a new IssueUsers object from an issue's fields
func GetIssueUsers(issue *github.Issue) *IssueUsers {
return &IssueUsers{
Assignees: GetUsers(issue.Assignees...).union(GetUsers(issue.Assignee)),
Author: GetUsers(issue.User),
}
}
// AllUsers return a list of unique users (both assignees and author)
func (u *IssueUsers) AllUsers() UserSet {
return u.Assignees.union(u.Author)
}
// IsValidUser returns true only if given user has valid github username.
func IsValidUser(u *github.User) bool {
return u != nil && u.Login != nil
}
// IsMungeBot returns true only if given user is this bot.
func IsMungeBot(u *github.User) bool {
return IsValidUser(u) && *u.Login == BotName
}