-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessages.go
176 lines (145 loc) · 4.2 KB
/
messages.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
package main
import (
"MuseBot/models"
"MuseBot/utils"
"errors"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"math/rand"
"strconv"
"strings"
)
var SkipMessage = errors.New("skip message")
type Message interface {
GetText() (string, error)
GetTgOptions() []interface{}
}
type PRMessage struct {
ID int
BaseUrl string
}
func (m PRMessage) GetText() (string, error) {
url := m.BaseUrl + strconv.Itoa(m.ID)
if utils.CheckUrlExist(url) {
return fmt.Sprintf("<a href='%s'>PR #%d</a>", url, m.ID), nil
}
return "", SkipMessage
}
func (m PRMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type NodeMessage struct {
ID int
BaseUrl string
}
func (m NodeMessage) GetText() (string, error) {
url := m.BaseUrl + strconv.Itoa(m.ID)
if utils.CheckUrlExist(url) {
return fmt.Sprintf("<a href='%s'>Node #%d</a>", url, m.ID), nil
}
return "", SkipMessage
}
func (m NodeMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type WikiMessage struct {
Texts []string
}
func (m WikiMessage) GetText() (string, error) {
i := rand.Intn(len(m.Texts))
url, err := utils.GetRedirectUrl("https://en.wikipedia.org/wiki/Special:Random")
if err != nil {
return "", err
}
return fmt.Sprintf("<a href='%s'>%s</a>", url, m.Texts[i]), nil
}
func (m WikiMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type BasicMessage struct {
Text string
}
func (m BasicMessage) GetText() (string, error) {
return m.Text, nil
}
func (m BasicMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type BasicRandMessage struct {
Texts []string
}
func (m BasicRandMessage) GetText() (string, error) {
i := rand.Intn(len(m.Texts))
return m.Texts[i], nil
}
func (m BasicRandMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type TravisHookMessage struct {
Payload models.TravisHookPayload
}
func (m TravisHookMessage) GetText() (string, error) {
if m.Payload.PullRequest {
return "", SkipMessage
}
if strings.ToLower(m.Payload.Repository.OwnerName) != "musescore" {
return "", SkipMessage
}
status := ""
switch strings.ToLower(m.Payload.StatusMessage) {
case "fixed":
status = "has been fixed"
case "broken":
status = "has been broken"
case "still failing":
status = "is still failing"
case "errored":
status = "has errored"
default:
return "", SkipMessage
}
commitUrl := fmt.Sprintf("<a href='%s%s'>%s</a>", Config.GitHubCommitUrl, m.Payload.Commit, m.Payload.Commit[0:6])
buildUrl := fmt.Sprintf("<a href='%s'>build</a>", m.Payload.BuildURL)
msg := fmt.Sprintf("MuseScore/%s : %s by %s: %s %s", m.Payload.Branch, commitUrl, m.Payload.CommitterName, buildUrl, status)
return msg, nil
}
func (m TravisHookMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML}
}
type GitHubHookPRMessage struct {
Payload models.GitHubPRPayload
}
func (m GitHubHookPRMessage) GetText() (string, error) {
if m.Payload.Action != "opened" {
return "", SkipMessage
}
msg := fmt.Sprintf("New Pull Request: <a href='%s'>#%d - %s</a> by %s",
m.Payload.PullRequest.HTMLURL, m.Payload.PullRequest.Number,
utils.SanitizeText(m.Payload.PullRequest.Title), m.Payload.PullRequest.User.Login,
)
return msg, nil
}
func (m GitHubHookPRMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML, tb.NoPreview}
}
type GitHubHookPushMessage struct {
Payload models.GitHubPushPayload
}
func (m GitHubHookPushMessage) GetText() (string, error) {
if len(m.Payload.Commits) == 0 {
return "", SkipMessage
}
branch := strings.Replace(m.Payload.Ref, "refs/heads/", "", -1)
commitMsg := utils.StripToLength(utils.SanitizeText(m.Payload.Commits[0].Message), 70, "...")
commitLink := fmt.Sprintf("<a href='%s'>%s</a> - <i>%s</i>", m.Payload.Commits[0].URL, m.Payload.Commits[0].ID[0:6], commitMsg)
msg := ""
if len(m.Payload.Commits) > 1 {
msg = fmt.Sprintf("%s pushed %d commits to %s, including %s", m.Payload.Pusher.Name, len(m.Payload.Commits), branch, commitLink)
} else {
msg = fmt.Sprintf("%s pushed commit to %s: %s", m.Payload.Pusher.Name, branch, commitLink)
}
return msg, nil
}
func (m GitHubHookPushMessage) GetTgOptions() []interface{} {
return []interface{}{tb.ModeHTML, tb.NoPreview}
}