-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
578 lines (498 loc) · 19.1 KB
/
bot.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package main
import (
"context"
"fmt"
"math/rand"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/Clever/kayvee-go/logger"
"github.com/Clever/pickabot/github"
"github.com/Clever/pickabot/slackapi"
whoswho "github.com/Clever/who-is-who/go-client"
"github.com/slack-go/slack"
lev "github.com/texttheater/golang-levenshtein/levenshtein"
)
// generate mocks of dependencies for use during testing
//go:generate sh -c "bin/mockgen -package main -source slackapi/SlackService.go SlackAPIService,SlackRTMService > slack_service_mock_test.go"
//go:generate sh -c "bin/mockgen -package main -source github/client.go AppClientIface > github_mock_test.go"
// Bot is the encapsulation of the logic to respond to Slack messages, by calling out to external services
type Bot struct {
Name string
Logger logger.KayveeLogger
DevMode bool
GithubClient github.AppClientIface
GithubOrgName string
SlackAPIService slackapi.SlackAPIService
SlackRTMService slackapi.SlackRTMService
// TODO: Move all picking logic to a separate struct{}
UserFlair map[string]string
TeamToTeamMembers map[string][]whoswho.User
TeamOverrides []Override
RandomSource rand.Source
WhoIsWhoClient whoIsWhoClientIface
LastCacheRefresh time.Time
}
const teamMatcher = `#?(eng)?[- ]?([a-zA-Z-]+)`
const individualMatcher = `<@([a-zA-Z0-9-]+)>`
var botMessageRegex = regexp.MustCompile(`^<@(.+?)> (.*)`)
var pickTeamRegex = regexp.MustCompile(`^\s*(pick\ and\ assign|pick|assign)\s*[a]?[n]?\s*` + teamMatcher)
var pickIndividualRegex = regexp.MustCompile(`^\s*(pick\ and\ assign|pick|assign)\s*[a]?[n]?\s*` + individualMatcher)
var listTeamRegex = regexp.MustCompile(`^\s*who is\s*[ai]?[n]?\s*` + teamMatcher)
var overrideTeamRegex = regexp.MustCompile(`^\s*<@(.+?)> is\s*(not)?\s*[a]?[n]? ` + teamMatcher)
var overrideTeamRegex2 = regexp.MustCompile(`^\s*(add|remove)\s+<@(.+?)>\s+(to|from)\s+` + teamMatcher)
var addFlairRegex = regexp.MustCompile(`^\s*add flair (.*)`)
var removeFlairRegex = regexp.MustCompile(`^\s*remove flair`)
var setAssigneeRegex = regexp.MustCompile(`.*assign.*`)
var helpRegex = regexp.MustCompile(`^\s*help`)
var refreshCacheRegex = regexp.MustCompile(`^\s*refresh`)
const didNotUnderstand = "Sorry, I didn't understand that"
const couldNotFindTeam = "Sorry, I couldn't find a team with that name"
const pickUserProblem = "Sorry, I ran into an issue picking a user. Check my logs for more details :sleuth_or_spy:"
const helpMessage = "_Pika-pi!_\n\nI can do the following:\n\n" +
"`@pickabot pick a <team>` - picks a user from that team\n" +
"`@pickabot assign a <team> for <Github PR URL(s)>` - assigns a user from that team to the Github PR(s)\n" +
"`@pickabot who is <team>` - lists users who belong to that team\n" +
"`@pickabot add @user to <team>` - adds user to team\n" +
"`@pickabot remove @user from <team>` - removes user from team\n" +
"`@pickabot add flair :emoji:` - set flair that appears when you're picked\n" +
"`@pickabot remove flair` - remove your flair\n" +
"`@pickabot refresh` - refreshes the user/team cache\n"
// Override denotes a team override where as user should (not) be included on a team
type Override struct {
User whoswho.User
Team string
Include bool // true = added, false = removed
}
var teamOverridesLock = &sync.Mutex{}
var userFlairLock = &sync.Mutex{}
// DecodeMessage takes a message from the Slack loop and responds appropriately
func (bot *Bot) DecodeMessage(ev *slack.MessageEvent) {
if ev == nil {
return
}
result := botMessageRegex.FindStringSubmatch(ev.Text)
if len(result) > 1 {
info, err := bot.SlackAPIService.GetUserInfo(result[1])
if err != nil {
bot.Logger.ErrorD("listen-error", logger.M{"error": err.Error(), "event-text": ev.Text})
return
}
bot.Logger.InfoD("listening", logger.M{"message": "Saw message for", "data": info.Name, "my-name": bot.Name})
if info.Name == bot.Name {
message := result[2]
message = strings.Trim(message, " ")
bot.Logger.InfoD("listening", logger.M{"message": message})
if message == "" {
return
}
// Help
helpMatch := helpRegex.FindStringSubmatch(message)
if len(helpMatch) > 0 {
bot.Logger.Info("help match")
// TODO: Print help
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(helpMessage, ev.Channel))
return
}
// Refresh user/team cache
refreshMatch := refreshCacheRegex.FindStringSubmatch(message)
if len(refreshMatch) > 0 {
bot.Logger.Info("refresh cache match")
teams, overrides, userFlair, err := buildTeams(bot.WhoIsWhoClient)
if err != nil {
bot.Logger.CriticalD("user cache refresh failed", logger.M{"error": err})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage("user cache refresh failed", ev.Channel))
} else {
bot.TeamToTeamMembers = teams
bot.TeamOverrides = overrides
bot.UserFlair = userFlair
bot.LastCacheRefresh = time.Now()
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage("refreshed user cache", ev.Channel))
}
return
}
// Override team
overrideMatch := overrideTeamRegex.FindStringSubmatch(message)
if len(overrideMatch) > 4 {
userID := overrideMatch[1]
addOrRemove := overrideMatch[2] != "not"
teamName := overrideMatch[4]
bot.setTeamOverride(ev, userID, teamName, addOrRemove)
return
}
// Override team (alternate matcher)
overrideMatch2 := overrideTeamRegex2.FindStringSubmatch(message)
if len(overrideMatch2) > 5 {
userID := overrideMatch2[2]
addOrRemove := overrideMatch2[1] == "add"
teamName := overrideMatch2[5]
bot.setTeamOverride(ev, userID, teamName, addOrRemove)
return
}
// List team members
listTeamMatch := listTeamRegex.FindStringSubmatch(message)
if len(listTeamMatch) > 2 {
teamName := listTeamMatch[2]
bot.listTeamMembers(ev, teamName)
return
}
// Add flair
addFlairMatch := addFlairRegex.FindStringSubmatch(message)
if len(addFlairMatch) > 1 {
flair := addFlairMatch[1]
bot.addFlair(ev, flair)
return
}
// Remove flair
removeFlairMatch := removeFlairRegex.FindStringSubmatch(message)
if len(removeFlairMatch) > 0 {
bot.removeFlair(ev)
return
}
// Determine if doing PR assignment
setAssigneeMatch := setAssigneeRegex.FindStringSubmatch(message)
setAssignee := len(setAssigneeMatch) > 0
// Check if picking an individual
// TODO: must come before team because team regex also matches individual regex
individualMatch := pickIndividualRegex.FindStringSubmatch(message)
if len(individualMatch) > 2 {
individualName := individualMatch[2]
bot.pickIndividual(ev, individualName, setAssignee)
return
}
// Pick a team member
teamMatch := pickTeamRegex.FindStringSubmatch(message)
if len(teamMatch) > 3 {
teamName := teamMatch[3]
bot.pickTeamMember(ev, teamName, setAssignee)
return
}
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(didNotUnderstand, ev.Channel))
}
}
}
// Returns all teams among teams that appear in who-is-who and all overrides
func (bot *Bot) knownTeams() []string {
teamsSet := map[string]struct{}{}
for team := range bot.TeamToTeamMembers {
teamsSet[team] = struct{}{}
}
// TeamToTeamMembers only contains "official" teams as known by who-is-who
// But overrides can use any team name
teamOverridesLock.Lock()
defer teamOverridesLock.Unlock()
for _, override := range bot.TeamOverrides {
teamsSet[override.Team] = struct{}{}
}
teams := make([]string, 0, len(teamsSet))
for team := range teamsSet {
teams = append(teams, team)
}
return teams
}
// findMatchingTeam allops smarter lookup of team name
// ex. "eng-team-name", "team-name", "team-namm" (slight misspelling)
func (bot *Bot) findMatchingTeam(s string) (string, error) {
s = strings.TrimPrefix(s, "eng-")
teams := bot.knownTeams()
possibles := []string{}
for _, t := range teams {
if s == t {
return t, nil
}
if lev.DistanceForStrings([]rune(s), []rune(t), lev.DefaultOptions) < 2 {
possibles = append(possibles, t)
}
}
if len(possibles) == 1 {
return possibles[0], nil
} else if len(possibles) > 1 {
return "", fmt.Errorf("multiple possible matches: %s", strings.Join(possibles, ", "))
}
return "", fmt.Errorf("no team with that name was found")
}
func (bot *Bot) setTeamOverrideInWhoIsWho(slackID, team string, include bool, until time.Time) {
// If user is in WIW update it,
user, err := bot.WhoIsWhoClient.UserBySlackID(slackID)
if err != nil {
bot.Logger.ErrorD("set-team-override-wiw-user-by-slack", logger.M{"user": slackID, "error": err.Error()})
return
}
o := whoswho.PickabotTeamOverride{
Team: team,
Include: include,
Until: until.Unix(),
}
// Remove any existing override for current team
foundIdx := -1
for idx, override := range user.Pickabot.TeamOverrides {
if override.Team == team {
foundIdx = idx
}
}
if foundIdx > -1 {
// remove current entry
user.Pickabot.TeamOverrides = append(user.Pickabot.TeamOverrides[:foundIdx], user.Pickabot.TeamOverrides[foundIdx+1:]...)
}
// Add override
user.Pickabot.TeamOverrides = append(user.Pickabot.TeamOverrides, o)
_, err = bot.WhoIsWhoClient.UpsertUser("pickabot", user)
if err != nil {
bot.Logger.ErrorD("set-team-override-wiw-upsert-user", logger.M{"user": slackID, "error": err.Error()})
return
}
}
func (bot *Bot) setTeamOverride(ev *slack.MessageEvent, userID, teamName string, addOrRemove bool) {
bot.Logger.InfoD("set-team-override", logger.M{"user": userID, "team": teamName, "add-or-remove": addOrRemove})
actualTeamName, err := bot.findMatchingTeam(teamName)
if err != nil {
bot.Logger.ErrorD("find-matching-team-error", logger.M{"error": err.Error(), "event-text": ev.Text})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(couldNotFindTeam, ev.Channel))
return
}
teamOverridesLock.Lock()
defer teamOverridesLock.Unlock()
// Remove user override for the current team, if already present
foundIdx := -1
for idx, o := range bot.TeamOverrides {
if o.User.SlackID == userID && o.Team == actualTeamName {
foundIdx = idx
break
}
}
if foundIdx > -1 {
// remove current entry
bot.TeamOverrides = append(bot.TeamOverrides[:foundIdx], bot.TeamOverrides[foundIdx+1:]...)
}
bot.TeamOverrides = append(bot.TeamOverrides, Override{
User: whoswho.User{SlackID: userID},
Team: actualTeamName,
Include: addOrRemove,
})
bot.setTeamOverrideInWhoIsWho(userID, actualTeamName, addOrRemove, time.Time{})
if addOrRemove {
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(
fmt.Sprintf("Added <@%s> to team %s! Remember to update https://github.com/orgs/Clever/teams/eng-%s/edit/review_assignment too!", userID, actualTeamName, actualTeamName),
ev.Channel))
} else {
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(
fmt.Sprintf("Removed <@%s> from team %s! Remember to update https://github.com/orgs/Clever/teams/eng-%s/edit/review_assignment too!", userID, actualTeamName, actualTeamName),
ev.Channel))
}
}
func (bot *Bot) updateFlairInWhoIsWho(slackID, flair string) {
// If user is in WIW update it,
user, err := bot.WhoIsWhoClient.UserBySlackID(slackID)
if err != nil {
bot.Logger.ErrorD("add-flair-wiw-user-by-slack", logger.M{"user": slackID, "flair": flair, "error": err.Error()})
return
}
user.Pickabot.Flair = flair
_, err = bot.WhoIsWhoClient.UpsertUser("pickabot", user)
if err != nil {
bot.Logger.ErrorD("add-flair-wiw-upsert-user", logger.M{"user": slackID, "flair": flair, "error": err.Error()})
return
}
}
func (bot *Bot) addFlair(ev *slack.MessageEvent, flair string) {
bot.Logger.InfoD("add-flair", logger.M{"user": ev.User, "flair": flair})
userFlairLock.Lock()
defer userFlairLock.Unlock()
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(fmt.Sprintf("<@%s>, I like your style!", ev.User), ev.Channel))
bot.UserFlair[ev.User] = flair
bot.updateFlairInWhoIsWho(ev.User, flair)
}
func (bot *Bot) removeFlair(ev *slack.MessageEvent) {
bot.Logger.InfoD("remove-flair", logger.M{"user": ev.User})
userFlairLock.Lock()
defer userFlairLock.Unlock()
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage("OK, so you don't like flair.", ev.Channel))
delete(bot.UserFlair, ev.User)
bot.updateFlairInWhoIsWho(ev.User, "")
}
func (bot *Bot) pickTeamMember(ev *slack.MessageEvent, teamName string, setAssignee bool) {
currentUser := whoswho.User{SlackID: ev.User}
bot.Logger.InfoD("pick-team-member", logger.M{"team": teamName, "omit-user": currentUser.SlackID})
actualTeamName, err := bot.findMatchingTeam(teamName)
if err != nil {
bot.Logger.ErrorD("find-matching-team-error", logger.M{"error": err.Error(), "event-text": ev.Text})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(couldNotFindTeam, ev.Channel))
return
}
teamMembers := bot.buildTeam(actualTeamName)
user, err := pickUser(teamMembers, ¤tUser, bot.RandomSource)
if err != nil {
bot.Logger.ErrorD("pick-user-error", logger.M{"error": err.Error(), "event-text": ev.Text})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(pickUserProblem, ev.Channel))
return
}
// Add flair
flair := bot.UserFlair[user.SlackID]
if flair != "" {
flair = " " + flair
}
text := fmt.Sprintf("I choose you: <@%s>%s", user.SlackID, flair)
if setAssignee {
err = bot.setAssignee(ev, user)
if err != nil {
text = fmt.Sprintf("Error setting <@%s>%s as pull-request reviewer: %s", user.SlackID, flair, err.Error())
} else {
text = fmt.Sprintf("Set <@%s>%s as pull-request reviewer", user.SlackID, flair)
}
}
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(text, ev.Channel))
return
}
func (bot *Bot) pickIndividual(ev *slack.MessageEvent, individualSlackID string, setAssignee bool) {
bot.Logger.InfoD("pick-individual", logger.M{"slack ID": individualSlackID})
user, err := bot.WhoIsWhoClient.UserBySlackID(individualSlackID)
if err != nil {
bot.Logger.ErrorD("pick-user-error", logger.M{"error": err.Error(), "event-text": ev.Text})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(pickUserProblem, ev.Channel))
return
}
// Add flair
flair := bot.UserFlair[user.SlackID]
if flair != "" {
flair = " " + flair
}
text := fmt.Sprintf("I choose you: <@%s>%s", user.SlackID, flair)
if setAssignee {
err = bot.setAssignee(ev, user)
if err != nil {
text = fmt.Sprintf("Error setting <@%s>%s as pull-request reviewer: %s", user.SlackID, flair, err.Error())
} else {
text = fmt.Sprintf("Set <@%s>%s as pull-request reviewer", user.SlackID, flair)
}
}
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(text, ev.Channel))
return
}
func (bot *Bot) setAssignee(ev *slack.MessageEvent, user whoswho.User) error {
var err error
if user.Github == "" {
// try to fetch the user from SlackID
user, err = bot.WhoIsWhoClient.UserBySlackID(user.SlackID)
// error if there is still no valid account associated
if user.Github == "" {
bot.Logger.ErrorD("set-assignee-error", logger.M{
"error": fmt.Sprintf("no valid Github account for %s", user.Email),
"event-text": ev.Text,
"user-pickabot-config": user.Pickabot,
"user-slack": user.Slack,
"user-slack-id": user.SlackID,
})
return fmt.Errorf("no github account for slack user <@%s>", user.SlackID)
}
// bubble up who-is-who-error
if err != nil {
bot.Logger.ErrorD("set-assignee-wiw-error", logger.M{
"error": err.Error(),
"event-text": ev.Text,
})
return fmt.Errorf("error fetching <@%s> from who-is-who. Please manually assign instead", user.SlackID)
}
}
var reposWithAssigneeSet []string
var reposWithReviewerSet []string
prs := parseMessageForPRs(bot.GithubOrgName, ev.Text)
for _, pr := range prs {
var err error
// the dev bot shouldn't hit the API
if bot.DevMode {
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(fmt.Sprintf("would have assigned %s to %s", user.Github, pr.Repo), ev.Channel))
} else {
_, _, err = bot.GithubClient.AddAssignees(context.Background(), pr.Owner, pr.Repo, pr.PRNumber, []string{user.Github})
if err != nil {
bot.Logger.ErrorD("set-assignee-failure-warning", logger.M{"warning": err.Error(), "event-text": ev.Text, "repo": pr.Repo, "user": user.Github})
} else {
reposWithAssigneeSet = append(reposWithAssigneeSet, pr.Repo)
}
_, _, err = bot.GithubClient.AddReviewers(context.Background(), pr.Owner, pr.Repo, pr.PRNumber, []string{user.Github})
if err != nil {
bot.Logger.ErrorD("set-reviewer-failure-warning", logger.M{"warning": err.Error(), "event-text": ev.Text, "repo": pr.Repo, "user": user.Github})
} else {
reposWithReviewerSet = append(reposWithReviewerSet, pr.Repo)
}
}
}
if len(reposWithAssigneeSet) > 0 || len(reposWithReviewerSet) > 0 {
bot.Logger.InfoD("set-assignee-success", logger.M{
"assigned-repos": reposWithAssigneeSet,
"reviewing-repos": reposWithReviewerSet,
"event-text": ev.Text,
"user": user.Github,
})
}
return nil
}
func (bot *Bot) buildTeam(teamName string) []whoswho.User {
teamOverridesLock.Lock()
defer teamOverridesLock.Unlock()
teamMembers := bot.TeamToTeamMembers[teamName]
finalTeam := []whoswho.User{}
// Remove some members
for _, user := range teamMembers {
includeUser := true
for _, override := range bot.TeamOverrides {
if user.SlackID == override.User.SlackID && teamName == override.Team && !override.Include {
// user has been removed
includeUser = false
break
}
}
if includeUser {
finalTeam = append(finalTeam, user)
}
}
// Add some members
for _, override := range bot.TeamOverrides {
if teamName == override.Team && override.Include {
finalTeam = append(finalTeam, override.User)
}
}
// De-dupe
dedupedTeam := []whoswho.User{}
for _, ft := range finalTeam {
alreadyInTeam := false
for _, dt := range dedupedTeam {
if dt.SlackID == ft.SlackID {
alreadyInTeam = true
break
}
}
if !alreadyInTeam {
dedupedTeam = append(dedupedTeam, ft)
}
}
return dedupedTeam
}
func (bot *Bot) listTeamMembers(ev *slack.MessageEvent, teamName string) {
bot.Logger.DebugD("list-team-members", logger.M{"team": teamName, "current-user": ev.User})
actualTeamName, err := bot.findMatchingTeam(teamName)
if err != nil {
bot.Logger.ErrorD("find-matching-team-error", logger.M{"error": err.Error(), "event-text": ev.Text})
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(couldNotFindTeam, ev.Channel))
return
}
teamMembers := bot.buildTeam(actualTeamName)
usernames := []string{}
for _, t := range teamMembers {
info, err := bot.SlackAPIService.GetUserInfo(t.SlackID)
if err != nil {
bot.Logger.ErrorD("slack-api-error", logger.M{"error": err.Error(), "event-text": ev.Text, "failed-user": t.SlackID})
return
}
// Add flair
flair := bot.UserFlair[t.SlackID]
if flair != "" {
flair = " " + flair
}
usernames = append(usernames, info.Name+flair)
}
sort.Strings(usernames)
bot.SlackRTMService.SendMessage(bot.SlackRTMService.NewOutgoingMessage(fmt.Sprintf("Team %s has the following members: %s", actualTeamName, strings.Join(usernames, ", ")), ev.Channel))
return
}