-
Notifications
You must be signed in to change notification settings - Fork 128
/
webhook_worker.go
71 lines (58 loc) · 1.7 KB
/
webhook_worker.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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package main
import (
"github.com/pkg/errors"
"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
)
type webhookWorker struct {
id int
p *Plugin
workQueue <-chan *webhookMessage
}
type webhookMessage struct {
InstanceID types.ID
Data []byte
}
func (ww webhookWorker) work() {
for msg := range ww.workQueue {
err := ww.process(msg)
if err != nil {
if errors.Is(err, errWebhookeventUnsupported) {
ww.p.debugf("WebhookWorker id: %d, error processing, err: %v", ww.id, err)
} else {
ww.p.errorf("WebhookWorker id: %d, error processing, err: %v", ww.id, err)
}
}
}
}
func (ww webhookWorker) process(msg *webhookMessage) (err error) {
defer func() {
if errors.Is(err, ErrWebhookIgnored) {
// ignore ErrWebhookIgnored - from here up it's a success
err = nil
}
}()
wh, err := ParseWebhook(msg.Data)
if err != nil {
return err
}
if _, _, err = wh.PostNotifications(ww.p, msg.InstanceID); err != nil {
ww.p.errorf("WebhookWorker id: %d, error posting notifications, err: %v", ww.id, err)
}
v := wh.(*webhook)
if err = v.JiraWebhook.expandIssue(ww.p, msg.InstanceID); err != nil {
return err
}
channelsSubscribed, err := ww.p.getChannelsSubscribed(v, msg.InstanceID)
if err != nil {
return err
}
botUserID := ww.p.getUserID()
for _, channelSubscribed := range channelsSubscribed {
if _, _, err1 := wh.PostToChannel(ww.p, msg.InstanceID, channelSubscribed.ChannelID, botUserID, channelSubscribed.Name); err1 != nil {
ww.p.errorf("WebhookWorker id: %d, error posting to channel, err: %v", ww.id, err1)
}
}
return nil
}