forked from fatedier/freebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
141 lines (124 loc) · 3.38 KB
/
event.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package freebot
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/fatedier/freebot/pkg/client"
"github.com/fatedier/freebot/pkg/client/githubapp"
"github.com/fatedier/freebot/pkg/event"
"github.com/fatedier/freebot/pkg/httputil"
"github.com/fatedier/freebot/pkg/log"
"github.com/fatedier/freebot/plugin"
"github.com/google/go-github/github"
)
var (
ErrEventPayload = httputil.NewHttpError(400, "error event payload")
ErrNoSupportEvent = httputil.NewHttpError(400, "no support event")
ErrNoOwnerRepo = httputil.NewHttpError(400, "event no owner and repo info")
ErrNoPlugins = httputil.NewHttpError(400, "no correspond plugins")
ErrNoInstallation = httputil.NewHttpError(400, "no installation")
)
type EventHandler struct {
requireInstallation bool
// key is owner/repo
plugins map[string][]plugin.Plugin
mu sync.RWMutex
}
func NewEventHandler(requireInstallation bool, plugins map[string][]plugin.Plugin) *EventHandler {
return &EventHandler{
requireInstallation: requireInstallation,
plugins: plugins,
}
}
func (eh *EventHandler) UpdatePlugins(plugins map[string][]plugin.Plugin) {
eh.mu.Lock()
defer eh.mu.Unlock()
eh.plugins = plugins
}
func (eh *EventHandler) HandleEvent(ctx context.Context, evType string, content string) (err error) {
var (
payload interface{}
owner string
repo string
)
// parse content
switch evType {
case event.EvIssueComment:
v := &github.IssueCommentEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvPullRequest:
v := &github.PullRequestEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvPullRequestReview:
v := &github.PullRequestReviewEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvPullRequestReviewComment:
v := &github.PullRequestReviewCommentEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvCheckRun:
v := &github.CheckRunEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvCheckSuite:
v := &github.CheckSuiteEvent{}
err = json.Unmarshal([]byte(content), &v)
payload = v
case event.EvPing:
return nil
default:
return ErrNoSupportEvent
}
if err != nil {
return ErrEventPayload
}
// get owner and repo name
if v, ok := payload.(client.GetRepoInterface); ok {
owner = v.GetRepo().GetOwner().GetLogin()
repo = v.GetRepo().GetName()
} else {
return ErrNoOwnerRepo
}
if eh.requireInstallation {
if v, ok := payload.(client.GetInstallationInterface); ok && v.GetInstallation() != nil && v.GetInstallation().ID != nil {
ctx = githubapp.WithInstallID(ctx, int(*v.GetInstallation().ID))
} else {
return ErrNoInstallation
}
}
// get plugins
eh.mu.RLock()
plugins, ok := eh.plugins[owner+"/"+repo]
eh.mu.RUnlock()
if !ok {
return ErrNoPlugins
}
// handle event by plugins
var (
notSupport bool
partialErr error
)
object := client.NewObject(payload)
for _, p := range plugins {
notSupport, partialErr = p.HandleEvent(&event.EventContext{
Ctx: ctx,
Type: evType,
Owner: owner,
Repo: repo,
Object: object,
})
if notSupport {
log.Debug("[%s/%s] plugin [%s] not support", owner, repo, p.Name())
continue
}
log.Info("[%s/%s] plugin: [%s] event: [%v]", owner, repo, p.Name(), evType)
if partialErr != nil {
err = fmt.Errorf("%v;[%s] %v", err, p.Name(), partialErr)
}
}
return err
}