-
Notifications
You must be signed in to change notification settings - Fork 7
/
user.go
113 lines (98 loc) · 2.78 KB
/
user.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
package main
import (
"fmt"
"strings"
"github.com/slack-go/slack"
v1 "k8s.io/api/core/v1"
)
// Role is a type to represent a role of a user.
//
// A user can have multiple roles.
// But we currently assume any Admin users are also Developer users.
// So, a consumer of this type should be able to check if a user has an equal or higher role than
// Developer by checking if the user has the Developer role.
type Role string
const (
RoleDeveloper Role = "Developer"
RoleAdmin Role = "Admin"
)
type User struct {
SlackUserID string
SlackDisplayName string
GitHubUserName string
GitHubNodeID string
isDeveloper bool
isAdmin bool
}
func (u User) IsDeveloper() bool {
return u.isDeveloper
}
// IsAdmin returns a flag to indicate whether the user is an admin or not.
// An admin can force-unlock a project/phase using `@bot unlock` command,
// even if the project/phase is locked by other users.
func (u User) IsAdmin() bool {
return u.isAdmin
}
type UserList struct {
Items []User
github GitHub
slackClient *slack.Client
}
func (ul *UserList) Reload() {
ul.Items = []User{}
slackUsers, err := ul.slackClient.GetUsers()
if err != nil {
fmt.Println("[ERROR] Cannot load slack users")
return
}
githubUsers, err := ul.github.GetUsers()
if err != nil {
fmt.Println("[ERROR] Cannot load GitHub users")
return
}
cml := getConfigMapList("githubuser-mapping")
rolebindings := getConfigMapList("rolebinding")
userNamesInGroups := ul.createUserNamesInGroups(rolebindings)
for _, slackUser := range slackUsers {
if slackUser.IsBot || slackUser.Deleted {
continue
}
user := User{SlackUserID: slackUser.ID, SlackDisplayName: slackUser.Profile.DisplayName}
for _, cm := range cml.Items {
if cm.Data[user.SlackDisplayName] != "" {
user.GitHubUserName = cm.Data[user.SlackDisplayName]
break
}
}
user.GitHubNodeID = githubUsers[user.GitHubUserName]
_, user.isDeveloper = userNamesInGroups[RoleDeveloper][user.SlackDisplayName]
_, user.isAdmin = userNamesInGroups[RoleAdmin][user.SlackDisplayName]
ul.Items = append(ul.Items, user)
}
}
func (ul UserList) createUserNamesInGroups(rolebindings *v1.ConfigMapList) map[Role]map[string]struct{} {
userNamesInGroups := map[Role]map[string]struct{}{
RoleDeveloper: {},
RoleAdmin: {},
}
for _, rolebinding := range rolebindings.Items {
for group, users := range userNamesInGroups {
raw := rolebinding.Data[string(group)]
userNames := strings.Split(raw, "\n")
for _, userName := range userNames {
if userName != "" {
users[userName] = struct{}{}
}
}
}
}
return userNamesInGroups
}
func (ul UserList) FindBySlackUserID(slackUserID string) User {
for _, user := range ul.Items {
if user.SlackUserID == slackUserID {
return user
}
}
return User{}
}