-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.go
72 lines (65 loc) · 2.15 KB
/
bot.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/slack-go/slack"
"github.com/zaiminc/gocat/deploy"
)
func main() {
config, err := InitConfig()
if err != nil {
log.Fatal(err)
}
client := slack.New(
config.SlackOAuthToken,
slack.OptionLog(log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)),
)
github := CreateGitHubInstance("", config.GitHubAccessToken, config.ManifestRepositoryOrg, config.ManifestRepositoryName, config.GitHubDefaultBranch,
config,
)
git := CreateGitOperatorInstance(
config.GitHubUserName,
config.GitHubAccessToken,
config.ManifestRepository,
config.GitHubDefaultBranch,
os.Getenv("GOCAT_GITROOT"),
)
userList := UserList{github: github, slackClient: client}
projectList := NewProjectList()
interactorContext := InteractorContext{projectList: &projectList, userList: &userList, github: github, git: git, client: client, config: *config}
interactorFactory := NewInteractorFactory(interactorContext)
autoDeploy := NewAutoDeploy(client, &github, &git, &projectList)
log.SetOutput(os.Stdout)
if config.EnableAutoDeploy {
autoDeploy.Watch(60)
}
http.Handle("/events", SlackListener{
client: client,
verificationToken: config.SlackVerificationToken,
projectList: &projectList,
userList: &userList,
interactorFactory: &interactorFactory,
coordinator: deploy.NewCoordinator(config.Namespace, config.LocksConfigMapName),
})
http.Handle("/interaction", interactionHandler{
verificationToken: config.SlackVerificationToken,
client: client,
projectList: &projectList,
userList: &userList,
interactorFactory: &interactorFactory,
})
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
})
// As the Go documentation says, ListenAndServe always returns a non-nil error,
// and the error is usually ErrServerClosed on graceful stop.
//
// Therefore, we exit with 0 when the error is ErrServerClosed,
// and log the error then exit with 1 otherwise for diagnosis.
if err := http.ListenAndServe(":3000", nil); err != http.ErrServerClosed {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}