-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (167 loc) · 5.86 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
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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/nlopes/slack"
"github.com/jokaorgua/trello"
"github.com/op/go-logging"
"github.com/joho/godotenv"
)
var (
listenPort string
serverAddr string
log logging.Logger
trelloClient *trello.Client
trelloUsername string
trelloWebhookCallbackURL string
trelloSlackLoginRelations []loginRelation
slackApi *slack.Client
)
type loginRelation struct {
trello string
slack string
}
type slackApiInterface interface {
PostMessage(string, ...slack.MsgOption) (string, string, error)
}
func init() {
log := logging.MustGetLogger("trello_to_slack")
logFormatter, err := logging.NewStringFormatter(`%{color}%{time:15:04:05.000} %{shortfunc} > %{level:.6s}%{color:reset} %{message}`)
logging.SetFormatter(logFormatter)
err = godotenv.Load()
if err != nil {
log.Panic("Can not load .env file")
}
loadLoginRelationsFromEnv(&trelloSlackLoginRelations)
listenPort = GetEnvVar("LISTEN_PORT", "80")
serverAddr = GetEnvVar("LISTEN_IP", "0.0.0.0")
trelloUsername = strings.Replace(GetEnvVar("TRELLO_USERNAME", "anonymous"), "@", "", -1)
trelloWebhookCallbackURL = GetEnvVar("TRELLO_WEBHOOK_URL", "")
if trelloWebhookCallbackURL == "" {
log.Panic("Please set TRELLO_WEBHOOK_URL")
}
slackApi = slack.New(GetEnvVar("SLACK_TOKEN", ""))
}
func loadLoginRelationsFromEnv(loginRelations *[]loginRelation) {
for i := 1; i < 100; i++ {
rel := GetEnvVar("LOGIN_RELATION_"+strconv.Itoa(i), "")
if len(rel) == 0 {
continue
}
splittedRel := strings.Split(rel, "|")
*loginRelations = append(*loginRelations, loginRelation{trello: splittedRel[0], slack: splittedRel[1]})
}
}
func setupTrelloWebhook() {
trelloClient = trello.NewClient(GetEnvVar("TRELLO_APIKEY", ""), GetEnvVar("TRELLO_TOKEN", ""))
log.Info("I'm trello user " + trelloUsername)
log.Info("Going to setup trello webhooks for URL: " + trelloWebhookCallbackURL)
member, err := trelloClient.GetMember(trelloUsername, trello.Defaults())
if err != nil {
log.Panic("Can not get trello member")
}
memberBoards, err := member.GetBoards(trello.Defaults())
if err != nil {
log.Panic("Can not get boards from trello")
}
token, _ := trelloClient.GetToken(trelloClient.Token, trello.Defaults())
webHooks, err := token.GetWebhooks(trello.Defaults())
if err != nil {
log.Error("Can not get webhooks for token " + token.ID + " Error: " + err.Error())
}
if GetEnvVar("TRELLO_CLEAR_PREVIOUS_WEBHOOKS", "0") != "0" {
log.Info("Clearing old webhooks ")
for _, webhook := range webHooks {
err = webhook.Delete(trello.Defaults())
if err != nil {
log.Error("Can not delete webhook " + webhook.ID)
}
}
webHooks, err = token.GetWebhooks(trello.Defaults())
}
for _, board := range memberBoards {
webhooksUrls := getBoardWebHooksUrls(webHooks, board.ID)
log.Debug("Current webhooks urls for board "+board.ID+"("+board.Name+")", webhooksUrls)
if !SliceContains(webhooksUrls, trelloWebhookCallbackURL) {
log.Debug("Creating webhook for board " + board.ID + "(" + board.Name + ")")
webhook := &trello.Webhook{IDModel: board.ID, Description: "Test webhook", CallbackURL: trelloWebhookCallbackURL}
err := trelloClient.CreateWebhook(webhook)
if webhook.Active == false {
log.Error("Can not create webhooks for board " + board.ID + "(" + board.Name + "). Active: false")
continue
}
if err != nil {
log.Error("Can not create webhooks for board " + board.ID + "(" + board.Name + "). Error: " + err.Error())
continue
}
log.Info("Created webhook for board " + board.ID + "(" + board.Name + ")")
}
}
log.Info("Setup of trello webhooks was made")
}
func main() {
go func() {
log.Debug("Will give our server some time to start. 5 seconds")
time.Sleep(5 * time.Second)
setupTrelloWebhook()
}()
log.Info("Will listen " + serverAddr + ":" + listenPort)
http.ListenAndServe(serverAddr+":"+listenPort, handlers())
}
func getBoardWebHooksUrls(webhooks []*trello.Webhook, boardId string) []string {
result := []string{}
for _, webhook := range webhooks {
result = append(result, webhook.CallbackURL)
}
return result
}
func sendToSlack(userId string, text string, slackApi slackApiInterface) error {
_, _, err := slackApi.PostMessage(userId, slack.MsgOptionText(text, false))
return err
}
func handlers() http.Handler {
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Debug("Got request")
w.WriteHeader(200)
_, err := w.Write([]byte("ok"))
if err != nil {
log.Error("Can not write answer to caller. Error: " + err.Error())
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Panic("Cant read request's body")
}
log.Debug("Received request: " + string(bodyBytes))
var requestJson = map[string]interface{}{}
err = json.Unmarshal(bodyBytes, &requestJson)
if err != nil {
log.Error("Cant parse request")
return
}
parsedRequest := trello.CardWebhookRequest{}
json.Unmarshal(bodyBytes, &parsedRequest)
//we will react only on comments
if parsedRequest.Action.Type != "commentCard" {
return
}
for _, relation := range trelloSlackLoginRelations {
if strings.Contains(parsedRequest.Action.Data.Text, relation.trello) {
message := ""
message = "<https://trello.com/c/" + parsedRequest.Action.Data.Card.ShortLink + "|" + parsedRequest.Action.Data.Card.Name + ">\n" +
"> " + parsedRequest.Action.MemberCreator.FullName + ": " + strings.ReplaceAll(parsedRequest.Action.Data.Text, "\n", "\n>")
err := sendToSlack(relation.slack, message, slackApi)
if err != nil {
log.Error("Can not send message to slack to " + relation.trello + "(" + relation.slack + ") Error: " + err.Error())
continue
}
log.Info("Sent message to slack to " + relation.trello + "(" + relation.slack + ")")
}
}
})
return r
}