-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (79 loc) · 1.9 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/robfig/cron"
"github.com/google/go-github/github"
"github.com/vektorprogrammet/github-bot/bot"
)
var eventChan chan interface{}
const rootDir = "/var/www/github-bot"
const botUsername = "vektorbot"
type GitHubEventMonitor struct {
secret []byte
}
type GitHubEventHandler interface {
HandleEvent(event interface{})
}
func (s *GitHubEventMonitor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, s.secret)
if err != nil {
fmt.Printf("Failed to valdidate payload: %s\n", err)
return
}
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
fmt.Printf("Failed to parse webhook: %s\n", err)
return
}
go func(event interface{}) {
eventChan <- event
}(event)
}
func startGitHubEventBots() {
gitHubBot := bot.Bot{
Username: botUsername,
RootDir: rootDir,
}
gitHubEventBots := []GitHubEventHandler{
bot.CodeStyleBot{
Bot: gitHubBot,
},
bot.DbMigrationBot{
Bot: gitHubBot,
},
}
eventChan = make(chan interface{})
go func() {
for event := range eventChan {
fmt.Println("Event received")
for _, gitHubBot := range gitHubEventBots {
go gitHubBot.HandleEvent(event)
}
}
}()
}
func startUpdateBot(repoUrl, cronInterval string) {
composerUpdateBot := bot.ComposerUpdateBot{
Bot: bot.Bot{
Username: botUsername,
RootDir: rootDir,
},
}
c := cron.New()
c.AddFunc(cronInterval, func() {
composerUpdateBot.UpdatePackages(repoUrl)
})
c.Start()
}
func main() {
startGitHubEventBots()
startUpdateBot("[email protected]:vektorprogrammet/vektorprogrammet.git", "0 0 20 * * SUN")
secret := os.Getenv("GITHUB_WEBHOOKS_SECRET")
eventMonitor := GitHubEventMonitor{secret: []byte(secret)}
http.HandleFunc("/webhooks", eventMonitor.ServeHTTP)
fmt.Println("Listening to webhooks on port 5555")
log.Fatal(http.ListenAndServe(":5555", nil))
}