This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_handlers.go
240 lines (180 loc) · 5.79 KB
/
webhook_handlers.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
234
235
236
237
238
239
240
package main
import (
"net/http"
"time"
"fmt"
"log"
"bytes"
"io/ioutil"
"os"
"html/template"
"github.com/google/go-github/github"
"github.com/mmcdole/gofeed"
"github.com/mybb/mybb-blog-mailer/mail"
)
type WebHookService struct {
mailHandler mail.Handler
templates *template.Template
httpClient *http.Client
webHookSecret []byte
xmlFeedUrl string
lastPostDateFilePath string
}
type newBlogPost struct {
Title string
Summary string
Url string
PublishedAt time.Time
Author string
}
func NewWebHookService(mailHandler mail.Handler, templates *template.Template, webHookSecret string, xmlFeedUrl string,
lastPostDateFilePath string) (*WebHookService) {
return &WebHookService{
mailHandler: mailHandler,
templates: templates,
httpClient: &http.Client{
Timeout: time.Second * 5,
},
webHookSecret: []byte(webHookSecret),
xmlFeedUrl: xmlFeedUrl,
lastPostDateFilePath: lastPostDateFilePath,
}
}
/// Index handles a request to /webhook, handling an incoming web hook request from GitHub.
func (whService *WebHookService) Index(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, whService.webHookSecret)
if err != nil {
errorMessage := fmt.Sprintf("error validating request body: %s", err)
log.Printf("[ERROR] " + errorMessage + "\n")
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
defer r.Body.Close()
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
errorMessage := fmt.Sprintf("could not parse webhook: %s", err)
log.Printf("[ERROR] " + errorMessage + "\n")
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
switch e := event.(type) {
case *github.PingEvent:
log.Println("[DEBUG] received ping event")
case *github.PageBuildEvent:
switch buildStatus := e.Build.GetStatus(); buildStatus {
case "built":
log.Println("[DEBUG] received successful page build event, reading feed to send emails")
// Build was successful, so get the newest post and send email
whService.sendMailNotification()
case "queued":
log.Println("[DEBUG] rceived page build event with queued status")
case "building":
log.Println("[DEBUG] rceived page build event with building status")
case "errored":
buildError := e.Build.GetError()
buildErrorMessage := ""
if buildError != nil {
buildErrorMessage = buildError.GetMessage()
}
if len(buildErrorMessage) > 0 {
log.Printf("[WARN] received page build event with error message: %s\n")
} else {
log.Println("[WARN] received page build event with error status but no error message")
}
default:
log.Printf("[WARN] received page build event with unknown build status: %s\n", buildStatus)
}
default:
warningMessage := fmt.Sprintf("unknown event type: %s", github.WebHookType(r))
log.Printf("[WARN] " + warningMessage + "\n")
http.Error(w, warningMessage, http.StatusNotImplemented)
return
}
}
func (whService *WebHookService) getLastPostDate() (*time.Time, error) {
fileContent, err := ioutil.ReadFile(whService.lastPostDateFilePath)
if os.IsNotExist(err) {
currentTime := time.Now()
ioutil.WriteFile(whService.lastPostDateFilePath, []byte(currentTime.Format(time.RFC3339)), 0644)
return ¤tTime, nil
}
if err != nil || len(fileContent) == 0 {
return nil, err
}
parsedTime, err := time.Parse(time.RFC3339, string(fileContent))
if err != nil {
return nil, err
}
return &parsedTime, nil
}
func (whService *WebHookService) tryGetNewPost() (*newBlogPost, error) {
resp, err := whService.httpClient.Get(whService.xmlFeedUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
feedParser := gofeed.NewParser()
feed, err := feedParser.Parse(resp.Body)
if err != nil {
return nil, err
}
if len(feed.Items) == 0 {
return nil, nil
}
lastPostDate, err := whService.getLastPostDate()
if err != nil {
return nil, err
}
// We only check the most recent post
mostRecentPost := feed.Items[0]
if lastPostDate != nil && (mostRecentPost.PublishedParsed == nil || !mostRecentPost.PublishedParsed.After(*lastPostDate)) {
return nil, nil
}
author := ""
if mostRecentPost.Author != nil {
author = mostRecentPost.Author.Name
}
return &newBlogPost{
Title: mostRecentPost.Title,
Summary: mostRecentPost.Description,
Url: mostRecentPost.Link,
PublishedAt: *mostRecentPost.PublishedParsed,
Author: author,
}, nil
}
func (whService *WebHookService) sendMailNotification() {
newBlogPost, err := whService.tryGetNewPost()
if err != nil {
log.Printf("[ERROR] unable to get new blog post: %s\n", err)
return
}
if newBlogPost == nil {
log.Println("[DEBUG] no new blog post found")
return
} else {
log.Printf("[DEBUG] found new blog post: %+v\n", *newBlogPost)
}
var plainTextContentBuffer bytes.Buffer
err = whService.templates.ExecuteTemplate(&plainTextContentBuffer, "emails/blog_post_notification.txt", newBlogPost)
if err != nil {
log.Printf("[ERROR] unable to create plaintext email content: %s\n", err)
return
}
var htmlContentBuffer bytes.Buffer
err = whService.templates.ExecuteTemplate(&htmlContentBuffer, "emails/blog_post_notification.html", newBlogPost)
if err != nil {
log.Printf("[ERROR] unable to create HTML email content: %s\n", err)
return
}
err = whService.mailHandler.SendNotificationToMailingList(newBlogPost.Title, plainTextContentBuffer.String(),
htmlContentBuffer.String())
if err != nil {
log.Printf("[ERROR] sending blog post notification for post '%s': %s", newBlogPost.Title, err)
} else {
lastPostDate := newBlogPost.PublishedAt.Format(time.RFC3339)
err = ioutil.WriteFile(whService.lastPostDateFilePath, []byte(lastPostDate), 0644)
if err != nil {
log.Printf("[WARN] saving last post date '%s' for '%s': %s\n", lastPostDate, newBlogPost.Title, err)
}
}
}