-
Notifications
You must be signed in to change notification settings - Fork 231
/
mutation.go
293 lines (253 loc) · 8.78 KB
/
mutation.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
package graphqlapp
import (
context "context"
"database/sql"
"fmt"
"net/url"
"strings"
"time"
"github.com/target/goalert/assignment"
"github.com/target/goalert/config"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/notification/webhook"
"github.com/target/goalert/notificationchannel"
"github.com/target/goalert/permission"
"github.com/target/goalert/retry"
"github.com/target/goalert/schedule"
"github.com/target/goalert/user"
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/validation"
"github.com/target/goalert/validation/validate"
"github.com/pkg/errors"
)
type Mutation App
func (a *App) Mutation() graphql2.MutationResolver { return (*Mutation)(a) }
func (a *Mutation) SetFavorite(ctx context.Context, input graphql2.SetFavoriteInput) (bool, error) {
var err error
if input.Favorite {
err = a.FavoriteStore.Set(ctx, permission.UserID(ctx), input.Target)
} else {
err = a.FavoriteStore.Unset(ctx, permission.UserID(ctx), input.Target)
}
if err != nil {
return false, err
}
return true, nil
}
func (a *Mutation) LinkAccount(ctx context.Context, token string) (bool, error) {
err := a.AuthLinkStore.LinkAccount(ctx, token)
return err == nil, err
}
func (a *Mutation) SetScheduleOnCallNotificationRules(ctx context.Context, input graphql2.SetScheduleOnCallNotificationRulesInput) (bool, error) {
schedID, err := parseUUID("ScheduleID", input.ScheduleID)
if err != nil {
return false, err
}
err = withContextTx(ctx, a.DB, func(ctx context.Context, tx *sql.Tx) error {
rules := make([]schedule.OnCallNotificationRule, 0, len(input.Rules))
for i, r := range input.Rules {
err := validate.OneOf(fmt.Sprintf("Rules[%d].Target.Type", i), r.Target.Type, assignment.TargetTypeSlackChannel, assignment.TargetTypeSlackUserGroup, assignment.TargetTypeChanWebhook)
if err != nil {
return err
}
var nfyChan *notificationchannel.Channel
switch r.Target.Type {
case assignment.TargetTypeSlackUserGroup:
grpID, chanID, _ := strings.Cut(r.Target.ID, ":")
grp, err := a.SlackStore.UserGroup(ctx, grpID)
if err != nil {
return validation.WrapError(err)
}
ch, err := a.SlackStore.Channel(ctx, chanID)
if err != nil {
return validation.WrapError(err)
}
nfyChan = ¬ificationchannel.Channel{
Type: notificationchannel.TypeSlackUG,
Name: fmt.Sprintf("%s (%s)", grp.Handle, ch.Name),
Value: r.Target.ID,
}
case assignment.TargetTypeSlackChannel:
ch, err := a.SlackStore.Channel(ctx, r.Target.ID)
if err != nil {
return err
}
nfyChan = ¬ificationchannel.Channel{
Type: notificationchannel.TypeSlackChan,
Name: ch.Name,
Value: ch.ID,
}
case assignment.TargetTypeChanWebhook:
url, err := url.Parse(r.Target.ID)
if err != nil {
return validation.NewFieldError("Rules[%d].Target.ID", "Invalid URL format")
}
url.RawQuery = ""
if len(url.Path) > 15 {
url.Path = url.Path[:12] + "..."
}
cfg := config.FromContext(ctx)
if !cfg.ValidWebhookURL(r.Target.ID) {
return validation.NewFieldError("Rules[%d].Target.ID", "URL not allowed by administrator")
}
nfyChan = ¬ificationchannel.Channel{
Type: notificationchannel.TypeWebhook,
Name: webhook.MaskURLPass(url),
Value: r.Target.ID,
}
}
r.ChannelID, err = a.NCStore.MapToID(ctx, tx, nfyChan)
if err != nil {
return err
}
rules = append(rules, r.OnCallNotificationRule)
}
return a.ScheduleStore.SetOnCallNotificationRules(ctx, tx, schedID, rules)
})
return err == nil, err
}
func (a *Mutation) SetTemporarySchedule(ctx context.Context, input graphql2.SetTemporaryScheduleInput) (bool, error) {
schedID, err := parseUUID("ScheduleID", input.ScheduleID)
if err != nil {
return false, err
}
tmp := schedule.TemporarySchedule{
Start: input.Start,
End: input.End,
Shifts: input.Shifts,
}
var clearSet bool
if input.ClearStart != nil || input.ClearEnd != nil {
if input.ClearStart == nil {
return false, validation.NewFieldError("ClearStart", "must be set if ClearEnd is set")
}
if input.ClearEnd == nil {
return false, validation.NewFieldError("ClearEnd", "must be set if ClearStart is set")
}
clearSet = true
}
err = withContextTx(ctx, a.DB, func(ctx context.Context, tx *sql.Tx) error {
if clearSet {
return a.ScheduleStore.SetClearTemporarySchedule(ctx, tx, schedID, tmp, *input.ClearStart, *input.ClearEnd)
}
return a.ScheduleStore.SetTemporarySchedule(ctx, tx, schedID, tmp)
})
return err == nil, err
}
func (a *Mutation) ClearTemporarySchedules(ctx context.Context, input graphql2.ClearTemporarySchedulesInput) (bool, error) {
schedID, err := parseUUID("ScheduleID", input.ScheduleID)
if err != nil {
return false, err
}
err = withContextTx(ctx, a.DB, func(ctx context.Context, tx *sql.Tx) error {
return a.ScheduleStore.ClearTemporarySchedules(ctx, tx, schedID, input.Start, input.End)
})
return err == nil, err
}
func (a *Mutation) TestContactMethod(ctx context.Context, id string) (bool, error) {
err := a.NotificationStore.SendContactMethodTest(ctx, id)
if err != nil {
return false, err
}
return true, nil
}
func (a *Mutation) AddAuthSubject(ctx context.Context, input user.AuthSubject) (bool, error) {
err := a.UserStore.AddAuthSubjectTx(ctx, nil, &input)
if err != nil {
return false, err
}
return true, nil
}
func (a *Mutation) DeleteAuthSubject(ctx context.Context, input user.AuthSubject) (bool, error) {
err := a.UserStore.DeleteAuthSubjectTx(ctx, nil, &input)
if err != nil {
return false, err
}
return true, nil
}
func (a *Mutation) EndAllAuthSessionsByCurrentUser(ctx context.Context) (bool, error) {
err := a.AuthHandler.EndAllUserSessionsTx(ctx, nil)
if err != nil {
return false, err
}
return true, nil
}
func (a *Mutation) DeleteAll(ctx context.Context, input []assignment.RawTarget) (bool, error) {
// Retry because deleting frequently can cause a deadlock
// under heavy load.
err := retry.DoTemporaryError(func(int) error {
return a.tryDeleteAll(ctx, input)
},
retry.Log(ctx),
retry.Limit(5),
retry.FibBackoff(time.Second),
)
return err == nil, err
}
func (a *Mutation) tryDeleteAll(ctx context.Context, input []assignment.RawTarget) error {
tx, err := a.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer sqlutil.Rollback(ctx, "graphql: delete all", tx)
m := make(map[assignment.TargetType][]string)
for _, tgt := range input {
m[tgt.TargetType()] = append(m[tgt.TargetType()], tgt.TargetID())
}
order := []assignment.TargetType{
assignment.TargetTypeRotation,
assignment.TargetTypeUserOverride,
assignment.TargetTypeSchedule,
assignment.TargetTypeCalendarSubscription,
assignment.TargetTypeUser,
assignment.TargetTypeIntegrationKey,
assignment.TargetTypeHeartbeatMonitor,
assignment.TargetTypeService,
assignment.TargetTypeEscalationPolicy,
assignment.TargetTypeNotificationRule,
assignment.TargetTypeContactMethod,
assignment.TargetTypeUserSession,
}
for _, typ := range order {
ids := m[typ]
if len(ids) == 0 {
continue
}
switch typ {
case assignment.TargetTypeUserOverride:
err = errors.Wrap(a.OverrideStore.DeleteUserOverrideTx(ctx, tx, ids...), "delete user overrides")
case assignment.TargetTypeUser:
err = errors.Wrap(a.UserStore.DeleteManyTx(ctx, tx, ids), "delete users")
case assignment.TargetTypeService:
err = errors.Wrap(a.ServiceStore.DeleteManyTx(ctx, tx, ids), "delete services")
case assignment.TargetTypeEscalationPolicy:
err = errors.Wrap(a.PolicyStore.DeleteManyPoliciesTx(ctx, tx, ids), "delete escalation policies")
case assignment.TargetTypeIntegrationKey:
err = errors.Wrap(a.IntKeyStore.DeleteManyTx(ctx, tx, ids), "delete integration keys")
case assignment.TargetTypeSchedule:
err = errors.Wrap(a.ScheduleStore.DeleteManyTx(ctx, tx, ids), "delete schedules")
case assignment.TargetTypeCalendarSubscription:
err = errors.Wrap(a.CalSubStore.DeleteTx(ctx, tx, permission.UserID(ctx), ids...), "delete calendar subscriptions")
case assignment.TargetTypeRotation:
err = errors.Wrap(a.RotationStore.DeleteManyTx(ctx, tx, ids), "delete rotations")
case assignment.TargetTypeContactMethod:
err = errors.Wrap(a.CMStore.DeleteTx(ctx, tx, ids...), "delete contact methods")
case assignment.TargetTypeNotificationRule:
err = errors.Wrap(a.NRStore.DeleteTx(ctx, tx, ids...), "delete notification rules")
case assignment.TargetTypeHeartbeatMonitor:
err = errors.Wrap(a.HeartbeatStore.DeleteTx(ctx, tx, ids...), "delete heartbeat monitors")
case assignment.TargetTypeUserSession:
err = errors.Wrap(a.AuthHandler.EndUserSessionTx(ctx, tx, ids...), "end user sessions")
default:
return validation.NewFieldError("type", "unsupported type "+typ.String())
}
if err != nil {
return err
}
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}