This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
build_message.go
234 lines (199 loc) · 6.42 KB
/
build_message.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
package slacknotifier
import (
"fmt"
"strings"
"time"
"github.com/common-fate/common-fate/pkg/access"
"github.com/common-fate/common-fate/pkg/identity"
"github.com/common-fate/common-fate/pkg/notifiers"
"github.com/common-fate/common-fate/pkg/rule"
"github.com/common-fate/common-fate/pkg/types"
"github.com/slack-go/slack"
)
// titleCase turns STRING into String
func titleCase(s string) string {
if s == "" {
return ""
}
lower := strings.ToLower(s)
cap := strings.ToUpper(string(lower[0])) + lower[1:]
return cap
}
type RequestMessageOpts struct {
Request access.Request
RequestArguments []types.With
Rule rule.AccessRule
ReviewURLs notifiers.ReviewURLs
// optional field that will replace the default requestor email with a slack @mention
RequestorSlackID string
RequestorEmail string
WasReviewed bool
RequestReviewer *identity.User
//Optional field for a user or group to be tagged in the message.
MentionUser string
IsWebhook bool
}
/**
* BuildRequestReviewMessage builds a slack message for a request review based on the contextual RequestMessageOpts
*/
func BuildRequestReviewMessage(o RequestMessageOpts) (summary string, msg slack.Message) {
requestor := o.RequestorEmail
if o.RequestorSlackID != "" {
requestor = fmt.Sprintf("<@%s>", o.RequestorSlackID)
}
status := titleCase(string(o.Request.Status))
statusLower := strings.ToLower(status)
if o.IsWebhook && o.WasReviewed && o.Request.Status != access.PENDING {
summary = fmt.Sprintf("%s %s %s's request", o.RequestReviewer.Email, statusLower, o.RequestorEmail)
} else {
summary = fmt.Sprintf("New request for %s from %s", o.Rule.Name, o.RequestorEmail)
}
when := "ASAP"
if o.Request.RequestedTiming.StartTime != nil {
t := o.Request.RequestedTiming.StartTime
when = types.ExpiryString(*t)
}
requestDetails := []*slack.TextBlockObject{
{
Type: "mrkdwn",
Text: fmt.Sprintf("*When:*\n%s", when),
},
{
Type: "mrkdwn",
Text: fmt.Sprintf("*Duration:*\n%s", o.Request.RequestedTiming.Duration),
},
{
Type: "mrkdwn",
Text: fmt.Sprintf("*Status:*\n%s", status),
},
}
for _, v := range o.RequestArguments {
requestDetails = append(requestDetails, &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf("*%s:*\n%s", v.Title, v.Label),
})
}
// Only show the Request reason if it is not empty
if o.Request.Data.Reason != nil && len(*o.Request.Data.Reason) > 0 {
requestDetails = append(requestDetails, &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf("*Request Reason:*\n%s", *o.Request.Data.Reason),
})
}
//If a tagged user is specified then add it to the message.
if o.MentionUser != "" {
requestDetails = append(requestDetails, &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf("*Approver:*\n%s", o.MentionUser),
})
}
var richTextSummary string
if o.IsWebhook && o.WasReviewed && o.Request.Status != access.PENDING {
richTextSummary = fmt.Sprintf("*%s %s %s's request*", o.RequestReviewer.Email, statusLower, o.RequestorEmail)
} else {
richTextSummary = fmt.Sprintf("*<%s|New request for %s> from %s*", o.ReviewURLs.Review, o.Rule.Name, requestor)
}
msg = slack.NewBlockMessage(
slack.SectionBlock{
Type: slack.MBTSection,
Text: &slack.TextBlockObject{
Type: slack.MarkdownType,
Text: richTextSummary,
},
},
slack.SectionBlock{
Type: slack.MBTSection,
Fields: requestDetails,
},
)
if o.WasReviewed || o.Request.Status == access.CANCELLED {
t := time.Now()
when := types.ExpiryString(t)
text := fmt.Sprintf("*Reviewed by* %s at %s", o.RequestReviewer.Email, when)
if o.Request.Status == access.CANCELLED {
text = fmt.Sprintf("*Cancelled by* %s at %s", o.RequestorEmail, when)
}
reviewContextBlock := slack.NewContextBlock("", slack.TextBlockObject{
Type: slack.MarkdownType,
Text: text,
})
msg.Blocks.BlockSet = append(msg.Blocks.BlockSet, reviewContextBlock)
}
// If the request has just been sent (PENDING), then append Action Blocks
if o.Request.Status == access.PENDING {
msg.Blocks.BlockSet = append(msg.Blocks.BlockSet, slack.NewActionBlock("review_actions",
slack.ButtonBlockElement{
Type: slack.METButton,
Text: &slack.TextBlockObject{Type: slack.PlainTextType, Text: "Approve"},
Style: slack.StylePrimary,
ActionID: "approve",
Value: "approve",
URL: o.ReviewURLs.Approve,
},
slack.ButtonBlockElement{
Type: slack.METButton,
Text: &slack.TextBlockObject{Type: slack.PlainTextType, Text: "Close Request"},
Style: slack.StyleDanger,
ActionID: "deny",
Value: "deny",
URL: o.ReviewURLs.Deny,
},
))
}
return summary, msg
}
type RequestDetailMessageOpts struct {
Request access.Request
RequestArguments []types.With
Rule rule.AccessRule
// the message that renders in the header of the slack message
HeadingMessage string
}
// Builds a contextual request detail message, with an optional HeadingMessage to be rendered in the header, this is fired after a request has been reviewed
func BuildRequestDetailMessage(o RequestDetailMessageOpts) (summary string, msg slack.Message) {
summary = fmt.Sprintf("Request detail for %s", o.Rule.Name)
var expires time.Time
if o.Request.RequestedTiming.StartTime != nil {
expires = o.Request.RequestedTiming.StartTime.Add(o.Request.RequestedTiming.Duration)
} else {
expires = time.Now().Add(o.Request.RequestedTiming.Duration)
}
when := types.ExpiryString(expires)
requestDetails := []*slack.TextBlockObject{
{
Type: "mrkdwn",
Text: fmt.Sprintf("*Duration:*\n%s", o.Request.RequestedTiming.Duration),
},
{
Type: "mrkdwn",
Text: fmt.Sprintf("*Expires:*\n%s", when),
},
}
for _, v := range o.RequestArguments {
requestDetails = append(requestDetails, &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf("*%s:*\n%s", v.Title, v.Label),
})
}
// Only show the Request reason if it is not empty
if o.Request.Data.Reason != nil && len(*o.Request.Data.Reason) > 0 {
requestDetails = append(requestDetails, &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf("*Request Reason:*\n%s", *o.Request.Data.Reason),
})
}
msg = slack.NewBlockMessage(
slack.SectionBlock{
Type: slack.MBTSection,
Text: &slack.TextBlockObject{
Type: slack.MarkdownType,
Text: o.HeadingMessage,
},
},
slack.SectionBlock{
Type: slack.MBTSection,
Fields: requestDetails,
},
)
return summary, msg
}