-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
233 lines (196 loc) · 4.99 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package seabird_webhook_receiver
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
"gopkg.in/go-playground/webhooks.v5/github"
"github.com/seabird-chat/seabird-go"
"github.com/seabird-chat/seabird-go/pb"
)
var mainBranches = []string{
"refs/heads/main",
"refs/heads/master",
}
var prActions = []string{
"opened",
"closed",
"reopened",
"merged",
}
type Server struct {
logger zerolog.Logger
seabird *seabird.Client
github *github.Webhook
targetChannelId string
}
type Config struct {
Logger zerolog.Logger
SeabirdHost string
SeabirdToken string
SeabirdChannel string
GithubToken string
}
func NewServer(config Config) (*Server, error) {
github, err := github.New(github.Options.Secret(config.GithubToken))
if err != nil {
return nil, err
}
seabird, err := seabird.NewClient(config.SeabirdHost, config.SeabirdToken)
if err != nil {
return nil, err
}
return &Server{
logger: config.Logger,
seabird: seabird,
github: github,
targetChannelId: config.SeabirdChannel,
}, nil
}
func (s *Server) sendSeabirdMessagef(format string, args ...interface{}) error {
_, err := s.seabird.Inner.SendMessage(context.TODO(), &pb.SendMessageRequest{
ChannelId: s.targetChannelId,
Text: fmt.Sprintf(format, args...),
Tags: map[string]string{
"url/skip": "1",
},
})
return err
}
func (s *Server) handleGithubWebhook(w http.ResponseWriter, r *http.Request) {
payload, err := s.github.Parse(r,
github.PingEvent,
github.IssuesEvent,
github.PullRequestEvent,
github.PushEvent)
if err != nil {
s.logger.Error().Err(err).Msg("Got error when handling webhook")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.logger.Info().Msgf("Got %T payload", payload)
//fmt.Printf("%+v\n", payload)
switch event := payload.(type) {
case github.PingPayload:
err = s.sendSeabirdMessagef("Callback added by %s", event.Sender.Login)
case github.IssuesPayload:
err = s.sendSeabirdMessagef(
"[%s] %s %s issue %q: %s",
event.Repository.FullName,
event.Sender.Login,
event.Action,
event.Issue.Title,
event.Issue.HTMLURL,
)
case github.PullRequestPayload:
if !contains(prActions, event.Action) {
s.logger.Info().Msgf("Skipping pull request event of type %s for %q", event.Action, event.PullRequest.Title)
break
}
action := event.Action
if action == "closed" && event.PullRequest.Merged {
action = "merged"
}
err = s.sendSeabirdMessagef(
"[%s] %s %s pull request #%d: %q (%s...%s) %s",
event.Repository.FullName,
event.Sender.Login,
action,
event.PullRequest.Number,
event.PullRequest.Title,
event.PullRequest.Base.Ref,
event.PullRequest.Head.Ref,
event.PullRequest.HTMLURL,
)
case github.PushPayload:
action := "pushed"
if event.Deleted && !event.Created {
action = "deleted"
} else if event.Forced {
action = "force pushed"
}
if strings.HasPrefix(event.Ref, "refs/tags/") {
split := strings.SplitN(event.Ref, "/", 3)
tag := "<unknown>"
if len(split) == 3 {
tag = split[2]
}
err = s.sendSeabirdMessagef(
"[%s] %s %s tag %s: %s",
event.Repository.FullName,
event.Pusher.Name,
action,
tag,
event.Compare,
)
} else {
if !contains(mainBranches, event.Ref) {
s.logger.Info().Msgf("Skipping push event for ref %s", event.Ref)
break
}
split := strings.SplitN(event.Ref, "/", 3)
branch := "<unknown>"
if len(split) == 3 {
branch = split[2]
}
err = s.sendSeabirdMessagef(
"[%s] %s %s %d commit(s) to %s: %s",
event.Repository.FullName,
event.Pusher.Name,
action,
len(event.Commits),
branch,
event.Compare,
)
for _, commit := range event.Commits {
err = s.sendSeabirdMessagef(
"[%s] [%s] %s %s: %s",
event.Repository.FullName,
branch,
commit.ID[:8],
commit.Author.Username,
commit.Message,
)
}
}
}
if err != nil {
s.logger.Error().Err(err).Msg("Got error when sending notification")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) runHttp() error {
r := chi.NewRouter()
// The recommended middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// TODO: write custom Logger to work with zerolog
r.Post("/webhooks/github", s.handleGithubWebhook)
return http.ListenAndServe(":3000", r)
}
func (s *Server) runSeabird() error {
stream, err := s.seabird.StreamEvents(map[string]*pb.CommandMetadata{})
if err != nil {
return err
}
for event := range stream.C {
s.logger.Debug().Msgf("Got seabird event: %T", event.Inner)
}
return errors.New("seabird stream ended")
}
func (s *Server) Run(ctx context.Context) error {
// TODO: use the ctx Luke
group, _ := errgroup.WithContext(ctx)
group.Go(s.runHttp)
group.Go(s.runSeabird)
return group.Wait()
}