File size: 4,509 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
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
package slack

import (
	"bytes"
	"context"
	"fmt"
	"strings"

	"github.com/google/uuid"
	"github.com/pkg/errors"
	"github.com/slack-go/slack"
	"github.com/target/goalert/config"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/user"
	"github.com/target/goalert/util/log"
)

// UserGroupSender processes on-call notifications by updating the members of a Slack user group.
type UserGroupSender struct {
	*ChannelSender
}

var _ nfydest.MessageSender = (*UserGroupSender)(nil)

// UserGroupSender returns a new UserGroupSender wrapping the given ChannelSender.
func (s *ChannelSender) UserGroupSender() *UserGroupSender {
	return &UserGroupSender{s}
}

// Send implements notification.Sender.
func (s *UserGroupSender) SendMessage(ctx context.Context, msg notification.Message) (*notification.SentMessage, error) {
	err := permission.LimitCheckAny(ctx, permission.User, permission.System)
	if err != nil {
		return nil, err
	}

	if msg.DestType() != DestTypeSlackUsergroup {
		return nil, errors.Errorf("unsupported destination type: %s", msg.DestType())
	}

	t, ok := msg.(notification.ScheduleOnCallUsers)
	if !ok {
		return nil, errors.Errorf("unsupported message type: %T", msg)
	}

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

	var userIDs []string
	for _, u := range t.Users {
		userIDs = append(userIDs, u.ID)
	}

	userSlackIDs := make(map[string]string, len(t.Users))
	err = s.cfg.UserStore.AuthSubjectsFunc(ctx, fmt.Sprintf("slack:%s", teamID), userIDs, func(sub user.AuthSubject) error {
		userSlackIDs[sub.UserID] = sub.SubjectID
		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("lookup user slack IDs: %w", err)
	}

	var slackUsers []string
	var missing []notification.User
	for _, u := range t.Users {
		slackID, ok := userSlackIDs[u.ID]
		if !ok {
			missing = append(missing, u)
			continue
		}

		slackUsers = append(slackUsers, slackID)
	}

	ugID := t.DestArg(FieldSlackUsergroupID)
	chanID := t.DestArg(FieldSlackChannelID)
	cfg := config.FromContext(ctx)

	var errorMsg, stateDetails string

	// If any users are missing, we need to abort and let the channel know.
	switch {
	case len(missing) > 0:
		// TODO: add link action button to invite missing users
		var buf bytes.Buffer
		err := userGroupErrorMissing.Execute(&buf, userGroupError{
			GroupID:      ugID,
			Missing:      missing,
			callbackFunc: cfg.CallbackURL,
		})
		if err != nil {
			return nil, fmt.Errorf("execute template: %w", err)
		}
		errorMsg = buf.String()
		stateDetails = "missing users, sent error to channel"

		// If no users are on-call, we need to abort and let the channel know.
		//
		// This is because we can't update the user group with no members.
	case len(slackUsers) == 0:
		var buf bytes.Buffer
		err := userGroupErrorEmpty.Execute(&buf, userGroupError{
			GroupID:      ugID,
			ScheduleID:   t.ScheduleID,
			ScheduleName: t.ScheduleName,
			callbackFunc: cfg.CallbackURL,
		})
		if err != nil {
			return nil, fmt.Errorf("execute template: %w", err)
		}
		errorMsg = buf.String()
		stateDetails = "empty user-group, sent error to channel"
	default:
		err = s.withClient(ctx, func(c *slack.Client) error {
			_, err := c.UpdateUserGroupMembersContext(ctx, ugID, strings.Join(slackUsers, ","))
			if err != nil {
				return fmt.Errorf("update user group '%s': %w", ugID, err)
			}

			return nil
		})
	}

	// If there was an error, we need to let the channel know.
	if err != nil {
		errID := uuid.New()
		log.Log(log.WithField(ctx, "SlackUGErrorID", errID), err)
		var buf bytes.Buffer
		err := userGroupErrorUpdate.Execute(&buf, userGroupError{
			ErrorID: errID,
			GroupID: ugID,
		})
		if err != nil {
			return nil, fmt.Errorf("execute template: %w", err)
		}
		errorMsg = buf.String()
		stateDetails = "failed to update user-group, sent error to channel and log"
	}

	// Only send to the channel if an error occurred
	if stateDetails == "" {
		return &notification.SentMessage{State: notification.StateDelivered}, nil
	}

	var ts string
	err = s.withClient(ctx, func(c *slack.Client) error {
		_, ts, err = c.PostMessageContext(ctx, chanID, slack.MsgOptionText(errorMsg, false))
		return err
	})
	if err != nil {
		return nil, fmt.Errorf("post message to channel '%s': %w", chanID, err)
	}

	return &notification.SentMessage{State: notification.StateDelivered, ExternalID: ts, StateDetails: stateDetails}, nil
}