-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifier.go
223 lines (186 loc) · 6.74 KB
/
notifier.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
package main
import (
"context"
"fmt"
"os"
"github.com/geckoboard/cake-bot/github"
"github.com/geckoboard/cake-bot/slack"
slackapi "github.com/slack-go/slack"
)
type Notifier interface {
ReviewRequested(context.Context, *github.Repository, *github.PullRequest, *github.User) error
Approved(context.Context, *github.Repository, *github.PullRequest, *github.Review) error
ChangesRequested(context.Context, *github.Repository, *github.PullRequest, *github.Review) error
RespondToSlackAction(context.Context, *slackapi.InteractionCallback, string) error
}
const (
maxTitleLength = 80
reviewingRequestStatusMsg = "reviewing"
unableToReviewStatusMsg = "unable"
)
var (
notificationChannel string
)
type SlackNotifier struct {
client *slackapi.Client
}
func NewSlackNotifier(client *slackapi.Client) *SlackNotifier {
targetChannel, ok := os.LookupEnv("SLACK_NOTIFICATION_CHANNEL")
if !ok {
notificationChannel = "#devs"
}
notificationChannel = targetChannel
return &SlackNotifier{client}
}
func (n *SlackNotifier) Approved(c context.Context, repo *github.Repository, pr *github.PullRequest, review *github.Review) error {
text := fmt.Sprintf(
"%s you have received a :cake: for %s",
buildLinkToUser(pr.User),
prLink(review.HTMLURL(), repo, pr),
)
messageBlocks := []slackapi.Block{buildTextMessageBlock(text)}
return n.notifyChannel(c, notificationChannel, messageBlocks)
}
func (n *SlackNotifier) ChangesRequested(c context.Context, repo *github.Repository, pr *github.PullRequest, review *github.Review) error {
text := fmt.Sprintf(
"%s you have received some feedback on %s",
buildLinkToUser(pr.User),
prLink(review.HTMLURL(), repo, pr),
)
messageBlocks := []slackapi.Block{buildTextMessageBlock(text)}
return n.notifyChannel(c, notificationChannel, messageBlocks)
}
func (n *SlackNotifier) ReviewRequested(c context.Context, repo *github.Repository, pr *github.PullRequest, reviewer *github.User) error {
text := fmt.Sprintf(
"%s you have been asked by %s to review %s",
buildLinkToUser(reviewer), buildUserName(pr.User),
prLink(pr.HTMLURL, repo, pr),
)
messageBlocks := []slackapi.Block{buildTextMessageBlock(text)}
// When a review is first requested, show some buttons for the reviewer to respond
buttonBlock := slackapi.NewActionBlock(
"reviewer_response",
slackapi.NewButtonBlockElement("", reviewingRequestStatusMsg, slackapi.NewTextBlockObject("plain_text", ":eyes: Looking", false, false)),
slackapi.NewButtonBlockElement("", unableToReviewStatusMsg, slackapi.NewTextBlockObject("plain_text", ":pray: Please reassign", false, false)),
)
messageBlocks = append(messageBlocks, buttonBlock)
err := n.notifyChannel(c, notificationChannel, messageBlocks)
if err != nil {
return err
}
presenceText := fmt.Sprintf(
"%s may be busy and unable to review %s",
buildLinkToUser(reviewer),
prLink(pr.HTMLURL, repo, pr),
)
return n.tryNotifyPresence(c, reviewer, pr.User, presenceText)
}
// Updates the original Slack message with a `context` block to show the status of the PR
// See https://api.slack.com/reference/block-kit/blocks#context for clarification
// The `response` string is the text to display in the context block.
func (n *SlackNotifier) RespondToSlackAction(c context.Context, payload *slackapi.InteractionCallback, response string) error {
// We need to read the original message blocks and construct a new message
// otherwise `update` will overwrite everything, which we don't want.
msg := payload.Message
// The new block to add to the message
contextBlock := slackapi.NewContextBlock(
"",
slackapi.NewTextBlockObject(slackapi.MarkdownType, response, false, false),
)
var newBlocks []slackapi.Block
for _, block := range msg.Blocks.BlockSet {
// Drop the buttons, we're done with them now.
if block.BlockType() == "actions" {
continue
}
newBlocks = append(newBlocks, block)
}
newBlocks = append(newBlocks, contextBlock)
msg.Blocks.BlockSet = newBlocks
_, _, _, err := n.client.UpdateMessageContext(c,
payload.Channel.ID,
payload.Message.Timestamp,
slackapi.MsgOptionBlocks(msg.Blocks.BlockSet...),
)
return err
}
func (n *SlackNotifier) tryNotifyPresence(c context.Context, ghReviewer *github.User, ghReviewee *github.User, text string) error {
reviewer := findSlackUser(ghReviewer)
if reviewer == nil {
return nil
}
presence := n.findSlackUserStatus(reviewer)
// presence may be one of 'active', 'away' or a custom status text
if presence == "away" {
if reviewee := findSlackUser(ghReviewee); reviewee != nil {
return n.notifyUserWithDM(c, reviewee.ID, text)
}
}
return nil
}
// Notifies a user with a direct message.
func (n *SlackNotifier) notifyUserWithDM(c context.Context, userID, text string) error {
channel, _, _, err := n.client.OpenConversation(&slackapi.OpenConversationParameters{
Users: []string{userID},
})
if err != nil {
return err
}
messageBlocks := []slackapi.Block{buildTextMessageBlock(text)}
return n.notifyChannel(c, channel.ID, messageBlocks)
}
// notifyChannel sends a message to a channel constructed from blocks
// Callers should pass the channel ID, not the channel name (e.g. "C1234567890")
func (n *SlackNotifier) notifyChannel(c context.Context, channel string, blocks []slackapi.Block) error {
params := slackapi.NewPostMessageParameters()
params.AsUser = true
params.EscapeText = false
_, _, err := n.client.PostMessageContext(
c,
channel,
slackapi.MsgOptionBlocks(blocks...),
slackapi.MsgOptionPostMessageParameters(params),
)
return err
}
// findSlackUserStatus returns the status of a Slack user.
// The status can be one of 'active', 'away' or a custom status text.
func (n *SlackNotifier) findSlackUserStatus(user *slackapi.User) string {
u, err := n.client.GetUserPresence(user.ID)
if err != nil {
return ""
}
return u.Presence
}
func buildLinkToUser(ghUser *github.User) string {
if user := findSlackUser(ghUser); user != nil {
return fmt.Sprintf("<@%s>", user.ID)
}
return ghUser.Login
}
func buildUserName(ghUser *github.User) interface{} {
if user := findSlackUser(ghUser); user != nil {
return user.Name
}
return ghUser.Login
}
func findSlackUser(ghUser *github.User) *slackapi.User {
return slack.Users.FindByGitHubUsername(ghUser.Login)
}
func prLink(url string, repo *github.Repository, pr *github.PullRequest) string {
title := pr.Title
if len(title) > maxTitleLength {
title = fmt.Sprintf("%s...", title[0:maxTitleLength])
}
return fmt.Sprintf("<%s|%s#%d> - %s", url, repo.Name, pr.Number, title)
}
// buildTextMessageBlock returns a single slackapi.Block text
// The block supports slack markdown formatting.
func buildTextMessageBlock(text string) slackapi.Block {
textBlock := slackapi.NewSectionBlock(
slackapi.NewTextBlockObject(slackapi.MarkdownType, text, false, false),
nil,
nil,
)
return textBlock
}