File size: 2,896 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package slack

import (
	"context"
	"fmt"
	"sort"
	"strings"

	"github.com/slack-go/slack/slackutilsx"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/user"
	"github.com/target/goalert/util/log"
)

// onCallNotificationText will return text intended to be sent to Slack representing a ScheduleOnCallUsers notification.
//
// It gracefully degrades to excluding slack IDs when there is an error fetching the required information (e.g., team ID or
// auth subjects).
func (s *ChannelSender) onCallNotificationText(ctx context.Context, t notification.ScheduleOnCallUsers) string {
	if len(t.Users) == 0 {
		return renderOnCallNotificationMessage(t, nil)
	}

	teamID, err := s.TeamID(ctx)
	if err != nil {
		log.Log(ctx, fmt.Errorf("lookup team ID: %w", err))
		return renderOnCallNotificationMessage(t, nil)
	}

	userIDs := make([]string, len(t.Users))
	for i, u := range t.Users {
		userIDs[i] = u.ID
	}

	userSlackIDs := make(map[string]string, len(t.Users))
	err = s.cfg.UserStore.AuthSubjectsFunc(ctx, "slack:"+teamID, userIDs, func(sub user.AuthSubject) error {
		userSlackIDs[sub.UserID] = sub.SubjectID
		return nil
	})
	if err != nil {
		log.Log(ctx, fmt.Errorf("lookup auth subjects for slack: %w", err))
		// handled error by logging, continue on to render message with any included slack IDs
	}

	return renderOnCallNotificationMessage(t, userSlackIDs)
}

// renderOnCallNotificationMessage will render a message for Slack including links for the schedule and any users.
//
// If a user's ID is available in userSlackIDs, an `@` user mention will be used in place of a link to the GoAlert user's detail page.
func renderOnCallNotificationMessage(msg notification.ScheduleOnCallUsers, userSlackIDs map[string]string) string {
	suffix := fmt.Sprintf("on-call for <%s|%s>", slackutilsx.EscapeMessage(msg.ScheduleURL), slackutilsx.EscapeMessage(msg.ScheduleName))

	sort.Slice(msg.Users, func(i, j int) bool {
		ui, uj := msg.Users[i], msg.Users[j]
		if ui.Name == uj.Name {
			return ui.ID < uj.ID
		}

		return ui.Name < uj.Name
	})

	var userLinks []string
	for _, u := range msg.Users {
		var subjectID string
		if userSlackIDs != nil {
			subjectID = userSlackIDs[u.ID]
		}
		if subjectID == "" {
			// fallback to a link to the GoAlert user
			userLinks = append(userLinks, fmt.Sprintf("<%s|%s>", slackutilsx.EscapeMessage(u.URL), slackutilsx.EscapeMessage(u.Name)))
			continue
		}

		userLinks = append(userLinks, fmt.Sprintf("<@%s>", slackutilsx.EscapeMessage(subjectID)))
	}

	if len(userLinks) == 0 {
		return "No users are " + suffix
	}
	if len(userLinks) == 1 {
		return fmt.Sprintf("%s is %s", userLinks[0], suffix)
	}
	if len(userLinks) == 2 {
		return fmt.Sprintf("%s and %s are %s", userLinks[0], userLinks[1], suffix)
	}

	return fmt.Sprintf("%s, and %s are %s", strings.Join(userLinks[:len(userLinks)-1], ", "), userLinks[len(userLinks)-1], suffix)
}