-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
56 lines (50 loc) · 1.93 KB
/
message.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
package main
import (
"fmt"
"strings"
"github.com/dborzov/catbot/hookserve"
)
var MssgTemplatePullRequest = "%s repo: @%v made a pull request: '%s'! w00t w00t!"
var MssgTemplatePush = "%v repo: @%v pushed '%v' commit to %v branch"
var MssgUnsupportedEvent = "%v repo: whoah, there was an event with type \"%s\", I dunno what that means :P"
func fmtEventMessage(ev hookserve.Event) (msg string) {
if ev.Type == "ping" {
msg += fmt.Sprintf("repo *<%v|%v>* tickles me! I feel special :blush: \n ", ev.Repo.Url, ev.Repo.FullName)
msg += fmt.Sprintf("@<%v|%v> set up a webhook for that repo. \n", ev.Sender.Url, ev.Sender.FullName)
return
}
if ev.Type == "watch" {
msg += fmt.Sprintf("repo *<%v|%v>* was *%v* ", ev.Repo.Url, ev.Repo.FullName, ev.Action)
msg += fmt.Sprintf(" by @<%v|%v>! \n", ev.Sender.Url, ev.Sender.FullName)
return
}
msg = fmt.Sprintf("*<%v|%v>*:*%v* (%v) \n", ev.Repo.Url, ev.Repo.FullName, ev.Branch, niceTypeMessage(ev.Type))
for _, commit := range ev.Commits {
// escaping msg as documented at https://api.slack.com/docs/formatting
commit.Message = strings.Replace(commit.Message, "&", `&`, -1)
commit.Message = strings.Replace(commit.Message, ">", `>`, -1)
commit.Message = strings.Replace(commit.Message, "<", `<`, -1)
msg += fmt.Sprintf(" *[<%v|%v>]* <%v|%v>\n", commit.Url, commit.Author.Username, commit.Url, commit.Message)
}
if ev.Branch != "" {
msg += fmt.Sprintf("The branch is %v \n", ev.Branch)
}
if ev.Sender.FullName != "" {
msg += fmt.Sprintf("Authored by @<%v|%v>. \n", ev.Sender.Url, ev.Sender.FullName)
}
return
}
func niceTypeMessage(msgType string) string {
switch msgType {
case "pull_request":
return "Pull Request Posted!"
case "push":
return "*New Commits*"
case "ping":
return "is pinging me. I feel special :blush:"
}
return "Event with status'" + msgType + "' happened"
}
func slackLink(title, href string) string {
return fmt.Sprintf("<%v|%v>", href, title)
}