-
Notifications
You must be signed in to change notification settings - Fork 3
/
mq.go
91 lines (70 loc) · 1.89 KB
/
mq.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
package main
import (
"errors"
"github.com/opensourceways/community-robot-lib/config"
"github.com/opensourceways/community-robot-lib/kafka"
"github.com/opensourceways/community-robot-lib/mq"
"github.com/sirupsen/logrus"
)
func initMQ(agent config.ConfigAgent) error {
cfg := &configuration{}
_, c := agent.GetConfig()
if v, ok := c.(*configuration); ok {
cfg = v
}
tlsConfig, err := cfg.Config.Broker.TLSConfig.TLSConfig()
if err != nil {
return err
}
err = kafka.Init(
mq.Addresses(cfg.Config.Broker.Addresses...),
mq.SetTLSConfig(tlsConfig),
mq.Log(logrus.WithField("module", "broker")),
mq.ErrorHandler(errorHandler()),
)
if err != nil {
return err
}
return kafka.Connect()
}
func handleGiteeMessage(d *dispatcher) mq.Handler {
return func(event mq.Event) error {
return d.HandlerMsg(event)
}
}
func parseWebHookInfoFromMsg(msg *mq.Message) (eventType, uuid string, payload []byte, err error) {
if msg == nil {
err = errors.New("get a nil msg from broker")
return
}
if ua := msg.Header["User-Agent"]; ua != "Robot-Github-Access" {
err = errors.New("unexpect github message: Missing User-Agent Header")
return
}
if eventType = msg.Header["X-GitHub-Event"]; eventType == "" {
err = errors.New("unexpect github message: Missing X-Gitee-Event Header")
return
}
if uuid = msg.Header["X-GitHub-Delivery"]; uuid == "" {
err = errors.New("unexpect github message: Missing X-Gitee-Timestamp Header")
return
}
if payload = msg.Body; len(payload) == 0 {
err = errors.New("unexpect github message: The payload is empty")
}
return
}
func errorHandler() mq.Handler {
return func(event mq.Event) error {
l := logrus.WithFields(logrus.Fields{
"msg error handle": "default handler",
})
l.Errorf(
"the %s message handler occur error: %v, extra info that: %v",
event.Message().MessageKey(),
event.Error(),
event.Extra(),
)
return nil
}
}