generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 144
/
plugin.go
527 lines (452 loc) · 18.1 KB
/
plugin.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package main
import (
"net/http"
"os"
"path/filepath"
"time"
"github.com/mattermost/mattermost-plugin-playbooks/server/api"
"github.com/mattermost/mattermost-plugin-playbooks/server/app"
"github.com/mattermost/mattermost-plugin-playbooks/server/bot"
"github.com/mattermost/mattermost-plugin-playbooks/server/command"
"github.com/mattermost/mattermost-plugin-playbooks/server/config"
"github.com/mattermost/mattermost-plugin-playbooks/server/enterprise"
"github.com/mattermost/mattermost-plugin-playbooks/server/metrics"
"github.com/mattermost/mattermost-plugin-playbooks/server/scheduler"
"github.com/mattermost/mattermost-plugin-playbooks/server/sqlstore"
"github.com/mattermost/mattermost-plugin-playbooks/server/telemetry"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/mattermost/mattermost-server/v6/shared/i18n"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
pluginapi "github.com/mattermost/mattermost-plugin-api"
"github.com/mattermost/mattermost-plugin-api/cluster"
)
const (
updateMetricsTaskFrequency = 15 * time.Minute
metricsExposePort = ":9093"
// Topic represents a start of a thread. In playbooks we support 2 types of topics:
// status topic - indicating the start of the thread below status update and
// task topic - indicating the start of the thread below task(checklist item)
TopicTypeStatus = "status"
TopicTypeTask = "task"
// Collection is a group of topics and their corresponding threads.
// In Playbooks we support a single type of collection - a run
CollectionTypeRun = "run"
)
const (
rudderDataplaneURL = "https://pdat.matterlytics.com"
rudderWriteKey = "1ag0Mv7LPf5uJNhcnKomqg0ENFd"
)
type TelemetryClient interface {
app.PlaybookRunTelemetry
app.PlaybookTelemetry
app.GenericTelemetry
bot.Telemetry
app.UserInfoTelemetry
app.ChannelActionTelemetry
app.CategoryTelemetry
Enable() error
Disable() error
}
// Plugin implements the interface expected by the Mattermost server to communicate between the
// server and plugin processes.
type Plugin struct {
plugin.MattermostPlugin
handler *api.Handler
config *config.ServiceImpl
playbookRunService app.PlaybookRunService
playbookService app.PlaybookService
permissions *app.PermissionsService
channelActionService app.ChannelActionService
categoryService app.CategoryService
bot *bot.Bot
pluginAPI *pluginapi.Client
userInfoStore app.UserInfoStore
telemetryClient TelemetryClient
licenseChecker app.LicenseChecker
metricsService *metrics.Metrics
}
type StatusRecorder struct {
http.ResponseWriter
Status int
}
func (r *StatusRecorder) WriteHeader(status int) {
r.Status = status
r.ResponseWriter.WriteHeader(status)
}
// ServeHTTP routes incoming HTTP requests to the plugin's REST API.
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
p.handler.ServeHTTP(w, r)
}
// OnActivate Called when this plugin is activated.
func (p *Plugin) OnActivate() error {
bundlePath, err := p.API.GetBundlePath()
if err != nil {
return errors.Wrapf(err, "unable to get bundle path")
}
if err := i18n.TranslationsPreInit(filepath.Join(bundlePath, "assets/i18n")); err != nil {
return errors.Wrapf(err, "unable to load translation files")
}
p.metricsService = p.newMetricsInstance()
pluginAPIClient := pluginapi.NewClient(p.API, p.Driver)
p.pluginAPI = pluginAPIClient
p.config = config.NewConfigService(pluginAPIClient, manifest)
logger := logrus.StandardLogger()
pluginapi.ConfigureLogrus(logger, pluginAPIClient)
botID, err := pluginAPIClient.Bot.EnsureBot(&model.Bot{
Username: "playbooks",
DisplayName: "Playbooks",
Description: "Playbooks bot.",
OwnerId: "playbooks",
},
pluginapi.ProfileImagePath("assets/plugin_icon.png"),
)
if err != nil {
return errors.Wrapf(err, "failed to ensure bot")
}
err = p.config.UpdateConfiguration(func(c *config.Configuration) {
c.BotUserID = botID
})
if err != nil {
return errors.Wrapf(err, "failed save bot to config")
}
if rudderDataplaneURL == "" || rudderWriteKey == "" {
logrus.Warn("Rudder credentials are not set. Disabling analytics.")
p.telemetryClient = &telemetry.NoopTelemetry{}
} else {
diagnosticID := pluginAPIClient.System.GetDiagnosticID()
serverVersion := pluginAPIClient.System.GetServerVersion()
p.telemetryClient, err = telemetry.NewRudder(rudderDataplaneURL, rudderWriteKey, diagnosticID, manifest.Version, serverVersion)
if err != nil {
return errors.Wrapf(err, "failed init telemetry client")
}
}
toggleTelemetry := func() {
diagnosticsFlag := pluginAPIClient.Configuration.GetConfig().LogSettings.EnableDiagnostics
telemetryEnabled := diagnosticsFlag != nil && *diagnosticsFlag
if telemetryEnabled {
if err = p.telemetryClient.Enable(); err != nil {
logrus.WithError(err).Error("Telemetry could not be enabled")
}
return
}
if err = p.telemetryClient.Disable(); err != nil {
logrus.WithError(err).Error("Telemetry could not be disabled")
}
}
toggleTelemetry()
p.config.RegisterConfigChangeListener(toggleTelemetry)
apiClient := sqlstore.NewClient(pluginAPIClient)
p.bot = bot.New(pluginAPIClient, p.config.GetConfiguration().BotUserID, p.config, p.telemetryClient)
scheduler := cluster.GetJobOnceScheduler(p.API)
sqlStore, err := sqlstore.New(apiClient, scheduler)
if err != nil {
return errors.Wrapf(err, "failed creating the SQL store")
}
playbookRunStore := sqlstore.NewPlaybookRunStore(apiClient, sqlStore)
playbookStore := sqlstore.NewPlaybookStore(apiClient, sqlStore)
statsStore := sqlstore.NewStatsStore(apiClient, sqlStore)
p.userInfoStore = sqlstore.NewUserInfoStore(sqlStore)
channelActionStore := sqlstore.NewChannelActionStore(apiClient, sqlStore)
categoryStore := sqlstore.NewCategoryStore(apiClient, sqlStore)
p.handler = api.NewHandler(pluginAPIClient, p.config)
p.playbookService = app.NewPlaybookService(playbookStore, p.bot, p.telemetryClient, pluginAPIClient, p.metricsService)
keywordsThreadIgnorer := app.NewKeywordsThreadIgnorer()
p.channelActionService = app.NewChannelActionsService(pluginAPIClient, p.bot, p.config, channelActionStore, p.playbookService, keywordsThreadIgnorer, p.telemetryClient)
p.categoryService = app.NewCategoryService(categoryStore, pluginAPIClient, p.telemetryClient)
p.licenseChecker = enterprise.NewLicenseChecker(pluginAPIClient)
p.playbookRunService = app.NewPlaybookRunService(
pluginAPIClient,
playbookRunStore,
p.bot,
p.config,
scheduler,
p.telemetryClient,
p.telemetryClient,
p.API,
p.playbookService,
p.channelActionService,
p.licenseChecker,
p.metricsService,
)
if err = scheduler.SetCallback(p.playbookRunService.HandleReminder); err != nil {
logrus.WithError(err).Error("JobOnceScheduler could not add the playbookRunService's HandleReminder")
}
if err = scheduler.Start(); err != nil {
logrus.WithError(err).Error("JobOnceScheduler could not start")
}
// Migrations use the scheduler, so they have to be run after playbookRunService and scheduler have started
mutex, err := cluster.NewMutex(p.API, "IR_dbMutex")
if err != nil {
return errors.Wrapf(err, "failed creating cluster mutex")
}
mutex.Lock()
if err = sqlStore.RunMigrations(); err != nil {
mutex.Unlock()
return errors.Wrapf(err, "failed to run migrations")
}
mutex.Unlock()
p.permissions = app.NewPermissionsService(p.playbookService, p.playbookRunService, pluginAPIClient, p.config, p.licenseChecker)
// register collections and topics.
// TODO bump the minimum server version
if err := p.API.RegisterCollectionAndTopic(CollectionTypeRun, TopicTypeStatus); err != nil {
logrus.WithError(err).Warnf("failed to register collection - %s and topic - %s", CollectionTypeRun, TopicTypeStatus)
}
if err := p.API.RegisterCollectionAndTopic(CollectionTypeRun, TopicTypeTask); err != nil {
logrus.WithError(err).Warnf("failed to register collection - %s and topic - %s", CollectionTypeRun, TopicTypeTask)
}
api.NewGraphQLHandler(
p.handler.APIRouter,
p.playbookService,
p.playbookRunService,
p.categoryService,
pluginAPIClient,
p.config,
p.permissions,
playbookStore,
playbookRunStore,
p.licenseChecker,
)
api.NewPlaybookHandler(
p.handler.APIRouter,
p.playbookService,
pluginAPIClient,
p.config,
p.permissions,
)
api.NewPlaybookRunHandler(
p.handler.APIRouter,
p.playbookRunService,
p.playbookService,
p.permissions,
p.licenseChecker,
pluginAPIClient,
p.bot,
p.config,
)
api.NewStatsHandler(p.handler.APIRouter, pluginAPIClient, statsStore, p.playbookService, p.permissions, p.licenseChecker)
api.NewBotHandler(p.handler.APIRouter, pluginAPIClient, p.bot, p.config, p.playbookRunService, p.userInfoStore)
api.NewTelemetryHandler(p.handler.APIRouter, p.playbookRunService, pluginAPIClient, p.telemetryClient, p.playbookService, p.telemetryClient, p.telemetryClient, p.telemetryClient, p.permissions)
api.NewSignalHandler(p.handler.APIRouter, pluginAPIClient, p.playbookRunService, p.playbookService, keywordsThreadIgnorer)
api.NewSettingsHandler(p.handler.APIRouter, pluginAPIClient, p.config)
api.NewActionsHandler(p.handler.APIRouter, p.channelActionService, p.pluginAPI, p.permissions)
api.NewCategoryHandler(p.handler.APIRouter, pluginAPIClient, p.categoryService, p.playbookService, p.playbookRunService)
isTestingEnabled := false
flag := p.API.GetConfig().ServiceSettings.EnableTesting
if flag != nil {
isTestingEnabled = *flag
}
if err = command.RegisterCommands(p.API.RegisterCommand, isTestingEnabled); err != nil {
return errors.Wrapf(err, "failed register commands")
}
enableMetrics := p.API.GetConfig().MetricsSettings.Enable
if enableMetrics != nil && *enableMetrics {
// run metrics server to expose data
p.runMetricsServer()
// run metrics updater recurring task
p.runMetricsUpdaterTask(playbookStore, playbookRunStore, updateMetricsTaskFrequency)
// set error counter middleware handler
p.handler.APIRouter.Use(p.getErrorCounterHandler())
}
// prevent a recursive OnConfigurationChange
go func() {
// Remove the prepackaged old versions of the plugin
_ = pluginAPIClient.Plugin.Remove("com.mattermost.plugin-incident-response")
_ = pluginAPIClient.Plugin.Remove("com.mattermost.plugin-incident-management")
}()
return nil
}
// OnConfigurationChange handles any change in the configuration.
func (p *Plugin) OnConfigurationChange() error {
if p.config == nil {
return nil
}
return p.config.OnConfigurationChange()
}
// ExecuteCommand executes a command that has been previously registered via the RegisterCommand.
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
runner := command.NewCommandRunner(c, args, pluginapi.NewClient(p.API, p.Driver), p.bot,
p.playbookRunService, p.playbookService, p.config, p.userInfoStore, p.telemetryClient, p.permissions)
if err := runner.Execute(); err != nil {
return nil, model.NewAppError("Playbooks.ExecuteCommand", "app.command.execute.error", nil, err.Error(), http.StatusInternalServerError)
}
return &model.CommandResponse{}, nil
}
func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
actorID := ""
if actor != nil && actor.Id != channelMember.UserId {
actorID = actor.Id
}
p.channelActionService.UserHasJoinedChannel(channelMember.UserId, channelMember.ChannelId, actorID)
}
func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
p.channelActionService.MessageHasBeenPosted(post)
p.playbookRunService.MessageHasBeenPosted(post)
}
func (p *Plugin) newMetricsInstance() *metrics.Metrics {
// Init metrics
instanceInfo := metrics.InstanceInfo{
Version: manifest.Version,
InstallationID: os.Getenv("MM_CLOUD_INSTALLATION_ID"),
}
return metrics.NewMetrics(instanceInfo)
}
func (p *Plugin) runMetricsServer() {
logrus.WithField("port", metricsExposePort).Info("Starting Playbooks metrics server")
metricServer := metrics.NewMetricsServer(metricsExposePort, p.metricsService)
// Run server to expose metrics
go func() {
err := metricServer.Run()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.WithError(err).Error("Metrics server could not be started")
}
}()
}
func (p *Plugin) runMetricsUpdaterTask(playbookStore app.PlaybookStore, playbookRunStore app.PlaybookRunStore, updateMetricsTaskFrequency time.Duration) {
metricsUpdater := func() {
if playbooksActiveTotal, err := playbookStore.GetPlaybooksActiveTotal(); err == nil {
p.metricsService.ObservePlaybooksActiveTotal(playbooksActiveTotal)
} else {
logrus.WithError(err).Error("error updating metrics, playbooks_active_total")
}
if runsActiveTotal, err := playbookRunStore.GetRunsActiveTotal(); err == nil {
p.metricsService.ObserveRunsActiveTotal(runsActiveTotal)
} else {
logrus.WithError(err).Error("error updating metrics, runs_active_total")
}
if remindersOverdueTotal, err := playbookRunStore.GetOverdueUpdateRunsTotal(); err == nil {
p.metricsService.ObserveRemindersOutstandingTotal(remindersOverdueTotal)
} else {
logrus.WithError(err).Error("error updating metrics, reminders_outstanding_total")
}
if retrosOverdueTotal, err := playbookRunStore.GetOverdueRetroRunsTotal(); err == nil {
p.metricsService.ObserveRetrosOutstandingTotal(retrosOverdueTotal)
} else {
logrus.WithError(err).Error("error updating metrics, retros_outstanding_total")
}
if followersActiveTotal, err := playbookRunStore.GetFollowersActiveTotal(); err == nil {
p.metricsService.ObserveFollowersActiveTotal(followersActiveTotal)
} else {
logrus.WithError(err).Error("error updating metrics, followers_active_total")
}
if participantsActiveTotal, err := playbookRunStore.GetParticipantsActiveTotal(); err == nil {
p.metricsService.ObserveParticipantsActiveTotal(participantsActiveTotal)
} else {
logrus.WithError(err).Error("error updating metrics, participants_active_total")
}
}
scheduler.CreateRecurringTask("metricsUpdater", metricsUpdater, updateMetricsTaskFrequency)
}
func (p *Plugin) getErrorCounterHandler() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recorder := &StatusRecorder{
ResponseWriter: w,
Status: 200,
}
next.ServeHTTP(recorder, r)
if recorder.Status < 200 || recorder.Status > 299 {
p.metricsService.IncrementErrorsCount(1)
}
})
}
}
func (p *Plugin) UserHasPermissionToCollection(c *plugin.Context, userID string, collectionType, collectionID string, permission *model.Permission) (bool, error) {
if collectionType != CollectionTypeRun {
return false, errors.Errorf("collection %s is not registered by playbooks", collectionType)
}
run, err := p.playbookRunService.GetPlaybookRun(collectionID)
if err != nil {
return false, errors.Wrapf(err, "No run with id - %s", collectionID)
}
return p.permissions.HasPermissionsToRun(userID, run, permission), nil
}
func (p *Plugin) GetAllCollectionIDsForUser(c *plugin.Context, userID, collectionType string) ([]string, error) {
if collectionType != CollectionTypeRun {
return nil, errors.Errorf("collection %s is not registered by playbooks", collectionType)
}
ids, err := p.playbookRunService.GetPlaybookRunIDsForUser(userID)
if err != nil {
return nil, err
}
return ids, nil
}
func (p *Plugin) GetAllUserIdsForCollection(c *plugin.Context, collectionType, collectionID string) ([]string, error) {
if collectionType != CollectionTypeRun {
return nil, errors.Errorf("collection %s is not registered by playbooks", collectionType)
}
run, err := p.playbookRunService.GetPlaybookRun(collectionID)
if err != nil {
return nil, errors.Wrapf(err, "No run with id - %s", collectionID)
}
followers, err := p.playbookRunService.GetFollowers(collectionID)
if err != nil {
return nil, errors.Wrapf(err, "can't get followers for run - %s", collectionID)
}
return mergeSlice(run.ParticipantIDs, followers), nil
}
func (p *Plugin) GetCollectionMetadataByIds(c *plugin.Context, collectionType string, collectionIDs []string) (map[string]*model.CollectionMetadata, error) {
if collectionType != CollectionTypeRun {
return nil, errors.Errorf("collection %s is not registered by playbooks", collectionType)
}
runsMetadata := map[string]*model.CollectionMetadata{}
runs, err := p.playbookRunService.GetRunMetadataByIDs(collectionIDs)
if err != nil {
return nil, errors.Wrap(err, "can't get playbook run metadata by ids")
}
for _, run := range runs {
runsMetadata[run.ID] = &model.CollectionMetadata{
Id: run.ID,
CollectionType: CollectionTypeRun,
TeamId: run.TeamID,
Name: run.Name,
RelativeURL: app.GetRunDetailsRelativeURL(run.ID),
}
}
return runsMetadata, nil
}
func (p *Plugin) GetTopicMetadataByIds(c *plugin.Context, topicType string, topicIDs []string) (map[string]*model.TopicMetadata, error) {
topicsMetadata := map[string]*model.TopicMetadata{}
var getTopicMetadataByIDs func(topicIDs []string) ([]app.TopicMetadata, error)
switch topicType {
case TopicTypeStatus:
getTopicMetadataByIDs = p.playbookRunService.GetStatusMetadataByIDs
case TopicTypeTask:
getTopicMetadataByIDs = p.playbookRunService.GetTaskMetadataByIDs
default:
return map[string]*model.TopicMetadata{}, errors.Errorf("topic type %s is not registered by playbooks", topicType)
}
topics, err := getTopicMetadataByIDs(topicIDs)
if err != nil {
return nil, errors.Wrap(err, "can't get metadata by topic ids")
}
for _, topic := range topics {
topicsMetadata[topic.ID] = &model.TopicMetadata{
Id: topic.ID,
TopicType: topicType,
CollectionType: CollectionTypeRun,
TeamId: topic.TeamID,
CollectionId: topic.RunID,
}
}
return topicsMetadata, nil
}
func (p *Plugin) OnDeactivate() error {
logrus.Info("Shutting down store..")
return p.pluginAPI.Store.Close()
}
func mergeSlice(a, b []string) []string {
m := make(map[string]struct{}, len(a)+len(b))
for _, elem := range a {
m[elem] = struct{}{}
}
for _, elem := range b {
m[elem] = struct{}{}
}
merged := make([]string, 0, len(m))
for key := range m {
merged = append(merged, key)
}
return merged
}