|
| 1 | +package meethelper |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/mattermost/mattermost-server/v6/model" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +type MeetHook struct { |
| 17 | + WebhookURL string |
| 18 | + AcceptedLevels []logrus.Level |
| 19 | + Timeout time.Duration |
| 20 | + Async bool |
| 21 | + FieldHeader string |
| 22 | + Model model.IncomingWebhookRequest |
| 23 | + |
| 24 | + hook *WebHook |
| 25 | +} |
| 26 | + |
| 27 | +var ErrTimeout = errors.New("Request timed out") |
| 28 | + |
| 29 | +func (sh *MeetHook) Fire(e *logrus.Entry) error { |
| 30 | + if sh.hook == nil { |
| 31 | + sh.hook = NewWebHook(sh.WebhookURL) |
| 32 | + } |
| 33 | + |
| 34 | + payload := sh.Model |
| 35 | + color := LevelColorMap[e.Level] |
| 36 | + |
| 37 | + attachment := model.SlackAttachment{} |
| 38 | + payload.Attachments = []*model.SlackAttachment{&attachment} |
| 39 | + |
| 40 | + if len(e.Data) > 0 { |
| 41 | + attachment.Text = sh.FieldHeader |
| 42 | + |
| 43 | + for k, v := range e.Data { |
| 44 | + field := &model.SlackAttachmentField{} |
| 45 | + |
| 46 | + field.Title = k |
| 47 | + if str, ok := v.(string); ok { |
| 48 | + field.Value = str |
| 49 | + } else { |
| 50 | + field.Value = fmt.Sprint(v) |
| 51 | + } |
| 52 | + |
| 53 | + if len(field.Value.(string)) <= 20 { |
| 54 | + field.Short = true |
| 55 | + } |
| 56 | + attachment.Fields = append(attachment.Fields, field) |
| 57 | + } |
| 58 | + attachment.Pretext = e.Message |
| 59 | + } else { |
| 60 | + attachment.Text = e.Message |
| 61 | + } |
| 62 | + attachment.Fallback = e.Message |
| 63 | + attachment.Color = color |
| 64 | + |
| 65 | + if sh.Async { |
| 66 | + go sh.postMessage(&payload) |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + return sh.postMessage(&payload) |
| 71 | +} |
| 72 | + |
| 73 | +func (sh *MeetHook) postMessage(payload *model.IncomingWebhookRequest) error { |
| 74 | + if sh.Timeout <= 0 { |
| 75 | + return sh.hook.PostWebhookMessage(payload) |
| 76 | + } |
| 77 | + |
| 78 | + ech := make(chan error, 1) |
| 79 | + go func(ch chan error) { |
| 80 | + ch <- nil |
| 81 | + ch <- sh.hook.PostWebhookMessage(payload) |
| 82 | + }(ech) |
| 83 | + <-ech |
| 84 | + |
| 85 | + select { |
| 86 | + case err := <-ech: |
| 87 | + return err |
| 88 | + case <-time.After(sh.Timeout): |
| 89 | + return ErrTimeout |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Levels sets which levels to sent to slack |
| 94 | +func (sh *MeetHook) Levels() []logrus.Level { |
| 95 | + if sh.AcceptedLevels == nil { |
| 96 | + return AllLevels |
| 97 | + } |
| 98 | + return sh.AcceptedLevels |
| 99 | +} |
| 100 | + |
| 101 | +var LevelColorMap = map[logrus.Level]string{ |
| 102 | + logrus.DebugLevel: "#9B30FF", |
| 103 | + logrus.InfoLevel: "good", |
| 104 | + logrus.WarnLevel: "warning", |
| 105 | + logrus.ErrorLevel: "danger", |
| 106 | + logrus.FatalLevel: "danger", |
| 107 | + logrus.PanicLevel: "danger", |
| 108 | +} |
| 109 | + |
| 110 | +// Supported log levels |
| 111 | +var AllLevels = []logrus.Level{ |
| 112 | + logrus.DebugLevel, |
| 113 | + logrus.InfoLevel, |
| 114 | + logrus.WarnLevel, |
| 115 | + logrus.ErrorLevel, |
| 116 | + logrus.FatalLevel, |
| 117 | + logrus.PanicLevel, |
| 118 | +} |
| 119 | + |
| 120 | +// LevelThreshold - Returns every logging level above and including the given parameter. |
| 121 | +func LevelThreshold(l logrus.Level) []logrus.Level { |
| 122 | + for i := range AllLevels { |
| 123 | + if AllLevels[i] == l { |
| 124 | + return AllLevels[i:] |
| 125 | + } |
| 126 | + } |
| 127 | + return []logrus.Level{} |
| 128 | +} |
| 129 | + |
| 130 | +type WebHook struct { |
| 131 | + hookURL string |
| 132 | +} |
| 133 | + |
| 134 | +func NewWebHook(hookURL string) *WebHook { |
| 135 | + return &WebHook{hookURL} |
| 136 | +} |
| 137 | + |
| 138 | +func (wh *WebHook) PostWebhookMessage(payload *model.IncomingWebhookRequest) error { |
| 139 | + |
| 140 | + values, err := json.Marshal(payload) |
| 141 | + if err != nil { |
| 142 | + fmt.Println("PostWebhookMessage: cannot marshal payload:", err) |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + req, err := http.NewRequest("POST", wh.hookURL, bytes.NewBuffer(values)) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("failed to create POST request: %v", err) |
| 149 | + } |
| 150 | + |
| 151 | + client := &http.Client{} |
| 152 | + |
| 153 | + req.Header.Set("Content-Type", "application/json") |
| 154 | + resp, err := client.Do(req) |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("failed POST request to %s: %v", wh.hookURL, err) |
| 157 | + } |
| 158 | + defer resp.Body.Close() |
| 159 | + |
| 160 | + if resp.StatusCode != 200 { |
| 161 | + t, _ := io.ReadAll(resp.Body) |
| 162 | + return errors.New(string(t)) |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
0 commit comments