-
Notifications
You must be signed in to change notification settings - Fork 7
/
event_handler.go
115 lines (107 loc) · 2.81 KB
/
event_handler.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
package webui
import (
"fmt"
"strings"
"time"
"github.com/Masterminds/goutils"
"github.com/jenkins-x/go-scm/scm"
"github.com/sirupsen/logrus"
)
type EventHandler struct {
Store *Store
Logger *logrus.Logger
}
func (h *EventHandler) HandleWebhook(webhook scm.Webhook) error {
log := h.Logger.
WithField("repo", webhook.Repository().FullName).
WithField("kind", webhook.Kind())
event := convertWebhookToEvent(webhook)
if event == nil {
log.Trace("Ignoring webhook event")
return nil
}
log.Debug("Handling webhook event")
event.Kind = string(webhook.Kind())
event.Owner = webhook.Repository().Namespace
event.Repository = webhook.Repository().Name
if event.Branch == "" {
event.Branch = webhook.Repository().Branch
}
if event.Time.IsZero() {
event.Time = time.Now()
}
return h.Store.AddEvent(*event)
}
func convertWebhookToEvent(webhook scm.Webhook) *Event {
switch event := webhook.(type) {
case *scm.PingHook:
return &Event{
GUID: event.GUID,
Sender: event.Sender.Login,
}
case *scm.PushHook:
ref := event.Ref
ref = strings.TrimPrefix(ref, "refs/heads/")
ref = strings.TrimPrefix(ref, "refs/tags/")
e := Event{
GUID: event.GUID,
Details: ref,
Sender: event.Sender.Login,
Branch: ref,
}
if event.Deleted {
e.Action = scm.ActionDelete.String()
}
if event.Created {
e.Action = scm.ActionCreate.String()
}
return &e
case *scm.PullRequestHook:
var details string
switch event.Action {
case scm.ActionLabel, scm.ActionUnlabel:
details = fmt.Sprintf("%s: %s", event.Action.String(), event.Label.Name)
case scm.ActionAssigned, scm.ActionUnassigned:
var assignees []string
for _, assignee := range event.PullRequest.Assignees {
assignees = append(assignees, assignee.Login)
}
details = fmt.Sprintf("%s. Assignees: %s", event.Action.String(), strings.Join(assignees, ", "))
default:
details = event.Action.String()
}
return &Event{
GUID: event.GUID,
Action: event.Action.String(),
Details: details,
Sender: event.Sender.Login,
Branch: fmt.Sprintf("PR-%d", event.PullRequest.Number),
URL: event.PullRequest.Link,
}
case *scm.PullRequestCommentHook:
comment, _ := goutils.Abbreviate(event.Comment.Body, 50)
return &Event{
GUID: event.GUID,
Action: event.Action.String(),
Details: comment,
Sender: event.Sender.Login,
Branch: fmt.Sprintf("PR-%d", event.PullRequest.Number),
URL: event.Comment.Link,
}
case *scm.IssueCommentHook:
comment, _ := goutils.Abbreviate(event.Comment.Body, 50)
e := Event{
GUID: event.GUID,
Action: event.Action.String(),
Details: comment,
Sender: event.Sender.Login,
URL: event.Comment.Link,
}
if event.Issue.PullRequest != nil {
e.Branch = fmt.Sprintf("PR-%d", event.Issue.Number)
}
return &e
default:
return nil
}
}