File size: 2,695 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package webhook

import (
	"context"
	"net/url"

	"github.com/target/goalert/config"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

const (
	DestTypeWebhook  = "builtin-webhook"
	FieldWebhookURL  = "webhook_url"
	ParamBody        = "body"
	ParamContentType = "content_type"
	FallbackIconURL  = "builtin://webhook"
)

func NewWebhookDest(url string) gadb.DestV1 {
	return gadb.NewDestV1(DestTypeWebhook, FieldWebhookURL, url)
}

var _ (nfydest.Provider) = (*Sender)(nil)

func (Sender) ID() string { return DestTypeWebhook }

func (Sender) TypeInfo(ctx context.Context) (*nfydest.TypeInfo, error) {
	cfg := config.FromContext(ctx)
	return &nfydest.TypeInfo{
		Type:                       DestTypeWebhook,
		Name:                       "Webhook",
		Enabled:                    cfg.Webhook.Enable,
		SupportsUserVerification:   true,
		SupportsOnCallNotify:       true,
		SupportsSignals:            true,
		SupportsStatusUpdates:      true,
		SupportsAlertNotifications: true,
		StatusUpdatesRequired:      true,
		RequiredFields: []nfydest.FieldConfig{{
			FieldID:            FieldWebhookURL,
			Label:              "Webhook URL",
			PlaceholderText:    "https://example.com",
			InputType:          "url",
			Hint:               "Webhook Documentation",
			HintURL:            "/docs#webhooks",
			SupportsValidation: true,
		}},
		DynamicParams: []nfydest.DynamicParamConfig{
			{
				ParamID: ParamBody,
				Label:   "Body",
				Hint:    "The body of the request.",
			},
			{
				ParamID:      ParamContentType,
				Label:        "Content Type",
				Hint:         "The content type (e.g., application/json).",
				DefaultValue: `"application/json"`, // Because this is an expression, it needs the double quotes.
			},
		},
	}, nil
}

func (s *Sender) ValidateField(ctx context.Context, fieldID, value string) error {
	cfg := config.FromContext(ctx)
	switch fieldID {
	case FieldWebhookURL:
		err := validate.AbsoluteURL(FieldWebhookURL, value)
		if err != nil {
			return err
		}
		if !cfg.ValidWebhookURL(value) {
			return validation.NewGenericError("url is not allowed by administator")
		}

		return nil
	}

	return validation.NewGenericError("unknown field ID")
}

func (s *Sender) DisplayInfo(ctx context.Context, args map[string]string) (*nfydest.DisplayInfo, error) {
	if args == nil {
		args = make(map[string]string)
	}

	u, err := url.Parse(args[FieldWebhookURL])
	if err != nil {
		return nil, validation.WrapError(err)
	}
	return &nfydest.DisplayInfo{
		IconURL:     FallbackIconURL,
		IconAltText: "Webhook",
		Text:        u.Hostname(),
	}, nil
}