161 lines
3.6 KiB
Go
161 lines
3.6 KiB
Go
package plugin
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
)
|
|
|
|
func (p *Plugin) InitApi() {
|
|
p.router = mux.NewRouter()
|
|
p.router.HandleFunc("/webhook", p.handleWebhook).Methods("POST")
|
|
p.router.HandleFunc("/dialog/submit", p.handleDialogSubmit).Methods("POST")
|
|
}
|
|
|
|
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
|
if !p.IsReady {
|
|
http.Error(w, "Plugin not ready", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
|
|
p.router.ServeHTTP(w, r)
|
|
}
|
|
|
|
func (p *Plugin) registerCommands() error {
|
|
command := &model.Command{
|
|
Trigger: "sentry",
|
|
DisplayName: "Sentry",
|
|
Description: "Manage Sentry alerts and integrations",
|
|
AutoComplete: true,
|
|
AutocompleteData: p.getAutocompleteData(),
|
|
}
|
|
|
|
if err := p.API.RegisterCommand(command); err != nil {
|
|
p.API.LogError("Failed to register Sentry command", "error", err.Error())
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) ExecuteCommand(
|
|
ctx *plugin.Context,
|
|
args *model.CommandArgs,
|
|
) (*model.CommandResponse, *model.AppError) {
|
|
|
|
split := strings.Fields(args.Command)
|
|
if len(split) < 2 {
|
|
// Отправляем help в DM
|
|
helpText := `**Sentry plugin commands**
|
|
|
|
• /sentry link <project_slug> — link project to channel
|
|
• /sentry unlink <project_slug> — unlink project
|
|
• /sentry list — list linked projects
|
|
• /sentry setup — setup project
|
|
• /sentry help — show help`
|
|
post := &model.Post{Message: helpText}
|
|
_ = p.sendDMToUser(args.UserId, post)
|
|
return &model.CommandResponse{}, nil
|
|
}
|
|
|
|
switch split[1] {
|
|
case "setup":
|
|
if len(split) == 2 {
|
|
return p.commandSetup(args)
|
|
}
|
|
|
|
return p.commandSetupHook(args, split[2])
|
|
|
|
case "link":
|
|
return p.commandLink(args, split)
|
|
case "unlink":
|
|
return p.commandUnlink(args, split)
|
|
case "list":
|
|
return p.commandList(args)
|
|
case "help":
|
|
fallthrough
|
|
default:
|
|
// Отправляем help в DM
|
|
helpText := `**Sentry plugin commands**
|
|
|
|
• /sentry link <project_slug> — link project to channel
|
|
• /sentry unlink <project_slug> — unlink project
|
|
• /sentry list — list linked projects
|
|
• /sentry setup — setup project
|
|
• /sentry help — show help`
|
|
post := &model.Post{Message: helpText}
|
|
_ = p.sendDMToUser(args.UserId, post)
|
|
return &model.CommandResponse{}, nil
|
|
}
|
|
|
|
}
|
|
|
|
func (p *Plugin) getAutocompleteData() *model.AutocompleteData {
|
|
// root: /sentry
|
|
sentry := model.NewAutocompleteData(
|
|
"sentry",
|
|
"",
|
|
"Sentry integration commands",
|
|
)
|
|
|
|
// /sentry setup [hook]
|
|
setup := model.NewAutocompleteData(
|
|
"setup",
|
|
"[hook]",
|
|
"Initial setup or configure a specific webhook",
|
|
)
|
|
|
|
help := model.NewAutocompleteData(
|
|
"help",
|
|
"",
|
|
"Show help for Sentry commands",
|
|
)
|
|
|
|
list := model.NewAutocompleteData(
|
|
"list",
|
|
"",
|
|
"List linked Sentry projects",
|
|
)
|
|
|
|
link := model.NewAutocompleteData(
|
|
"link",
|
|
"[project_slug]",
|
|
"Link a Sentry project to this channel",
|
|
)
|
|
|
|
unlink := model.NewAutocompleteData(
|
|
"unlink",
|
|
"[project_slug]",
|
|
"Unlink a Sentry project from this channel",
|
|
)
|
|
|
|
// register top-level commands
|
|
sentry.AddCommand(setup)
|
|
sentry.AddCommand(link)
|
|
sentry.AddCommand(unlink)
|
|
sentry.AddCommand(list)
|
|
sentry.AddCommand(help)
|
|
|
|
// /sentry setup <hook>
|
|
hooks := []struct {
|
|
name string
|
|
desc string
|
|
}{
|
|
{"event_alert", "Configure event alert webhook"},
|
|
{"metric_alert", "Configure metric alert webhook"},
|
|
{"issue", "Configure issue webhook"},
|
|
{"comment", "Configure comment webhook"},
|
|
}
|
|
|
|
for _, h := range hooks {
|
|
setup.AddCommand(
|
|
model.NewAutocompleteData(h.name, "", h.desc),
|
|
)
|
|
}
|
|
|
|
return sentry
|
|
}
|