-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.go
168 lines (139 loc) · 4.48 KB
/
routes.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/bugsnag/bugsnag-go"
"github.com/geckoboard/cake-bot/ctx"
"github.com/geckoboard/cake-bot/github"
"github.com/geckoboard/cake-bot/log"
"github.com/julienschmidt/httprouter"
slackapi "github.com/slack-go/slack"
)
func NewServer(notifier Notifier, validator WebhookValidator) http.Handler {
s := &Server{
Notifier: notifier,
WebhookValidator: validator,
}
r := httprouter.New()
r.GET("/", s.root)
r.POST("/github", s.githubWebhook)
r.POST("/slack/interact", s.handleSlackInteractionEvent)
return r
}
type Server struct {
Notifier Notifier
WebhookValidator WebhookValidator
}
func (s *Server) validateSignature(r *http.Request) error {
return s.WebhookValidator.ValidateSignature(r)
}
func (s *Server) root(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.Header().Add("Location", "https://github.com/geckoboard/cake-bot")
w.WriteHeader(http.StatusFound)
}
func (s *Server) githubWebhook(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
event := r.Header.Get("X-GitHub-Event")
l := logger.With(
"endpoint", "webhook",
"request_id", r.Header.Get("X-Request-ID"),
"github_delivery_id", r.Header.Get("X-GitHub-Delivery"),
"github_event", event,
)
if err := s.validateSignature(r); err != nil {
l.Error("at", "invalid_signature", "err", err)
w.WriteHeader(http.StatusUnauthorized)
return
}
switch event {
case github.PullRequestEvent:
s.handlePullRequestEvent(w, r, l)
case github.PullRequestReviewEvent:
s.handlePullRequestReviewEvent(w, r, l)
default:
l.Info("at", "ignore_event")
w.WriteHeader(http.StatusOK)
}
}
func (s *Server) handleSlackInteractionEvent(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var payload slackapi.InteractionCallback
err := json.Unmarshal([]byte(r.FormValue("payload")), &payload)
if err != nil {
fmt.Printf("Could not parse action response JSON: %v", err)
}
if len(payload.ActionCallback.BlockActions) == 0 {
fmt.Printf("No block actions found in payload")
w.WriteHeader(http.StatusNotFound)
}
var receivedAction = payload.ActionCallback.BlockActions[0]
switch receivedAction.Value {
case reviewingRequestStatusMsg:
err := s.Notifier.RespondToSlackAction(
context.Background(),
&payload,
fmt.Sprintf("%s is looking at the PR\n", payload.User.Name),
)
if err != nil {
fmt.Printf("Error updating message: %v\n", err)
}
case unableToReviewStatusMsg:
err := s.Notifier.RespondToSlackAction(
context.Background(),
&payload,
fmt.Sprintf("%s is unable to look at the PR right now, sorry!\n", payload.User.Name),
)
if err != nil {
fmt.Printf("Error updating message: %v\n", err)
}
default:
fmt.Printf("Unknown action value '%s'\n", receivedAction.Value)
}
}
func (s *Server) handlePullRequestEvent(w http.ResponseWriter, r *http.Request, l log.LeveledLogger) {
var webhook github.PullRequestWebhook
if err := json.NewDecoder(r.Body).Decode(&webhook); err != nil {
_ = bugsnag.Notify(err)
l.Error("at", "unmarshal_error", "err", err)
w.WriteHeader(http.StatusNotImplemented)
return
}
l = webhook.EnhanceLogger(l)
switch webhook.Action {
case "review_requested":
c := ctx.WithLogger(context.Background(), l)
_ = s.Notifier.ReviewRequested(c, webhook.Repository, webhook.PullRequest, webhook.RequestedReviewer)
w.WriteHeader(http.StatusOK)
default:
l.Info("at", "ignore_pull_request_action")
w.WriteHeader(http.StatusOK)
}
}
func (s *Server) handlePullRequestReviewEvent(w http.ResponseWriter, r *http.Request, l log.LeveledLogger) {
var webhook github.PullRequestReviewWebhook
if err := json.NewDecoder(r.Body).Decode(&webhook); err != nil {
_ = bugsnag.Notify(err)
l.Error("at", "unmarshal_error", "err", err)
w.WriteHeader(http.StatusNotImplemented)
return
}
l = webhook.EnhanceLogger(l)
if webhook.Action != "submitted" {
l.Info("at", "ignore_review_action")
w.WriteHeader(http.StatusOK)
return
}
if err := webhook.Validate(); err != nil {
l.Error("at", "payload_error", "err", err)
w.WriteHeader(http.StatusNotImplemented)
return
}
c := ctx.WithLogger(context.Background(), l)
if webhook.Review.IsApproved() {
_ = s.Notifier.Approved(c, webhook.Repository, webhook.PullRequest, webhook.Review)
} else if webhook.Review.User.ID != webhook.PullRequest.User.ID {
_ = s.Notifier.ChangesRequested(c, webhook.Repository, webhook.PullRequest, webhook.Review)
}
l.Info("at", "pull_request_updated")
w.WriteHeader(http.StatusOK)
}