Skip to content

Commit 7cc8554

Browse files
committed
Added tests for Slack.
1 parent 0df3158 commit 7cc8554

File tree

2 files changed

+166
-127
lines changed

2 files changed

+166
-127
lines changed

services/webhook/slack.go

Lines changed: 37 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -111,92 +111,63 @@ func (s *SlackPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
111111
refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
112112
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
113113

114-
return &SlackPayload{
115-
Channel: s.Channel,
116-
Text: text,
117-
Username: s.Username,
118-
IconURL: s.IconURL,
119-
}, nil
114+
return s.createPayload(text, nil), nil
120115
}
121116

122117
// Delete composes Slack payload for delete a branch or tag.
123118
func (s *SlackPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
124119
refName := git.RefEndName(p.Ref)
125120
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
126121
text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
127-
return &SlackPayload{
128-
Channel: s.Channel,
129-
Text: text,
130-
Username: s.Username,
131-
IconURL: s.IconURL,
132-
}, nil
122+
123+
return s.createPayload(text, nil), nil
133124
}
134125

135126
// Fork composes Slack payload for forked by a repository.
136127
func (s *SlackPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
137128
baseLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
138129
forkLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
139130
text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
140-
return &SlackPayload{
141-
Channel: s.Channel,
142-
Text: text,
143-
Username: s.Username,
144-
IconURL: s.IconURL,
145-
}, nil
131+
132+
return s.createPayload(text, nil), nil
146133
}
147134

148135
// Issue implements PayloadConvertor Issue method
149136
func (s *SlackPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
150137
text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, SlackLinkFormatter, true)
151138

152-
pl := &SlackPayload{
153-
Channel: s.Channel,
154-
Text: text,
155-
Username: s.Username,
156-
IconURL: s.IconURL,
157-
}
139+
var attachments []SlackAttachment
158140
if attachmentText != "" {
159141
attachmentText = SlackTextFormatter(attachmentText)
160142
issueTitle = SlackTextFormatter(issueTitle)
161-
pl.Attachments = []SlackAttachment{{
143+
attachments = append(attachments, SlackAttachment{
162144
Color: fmt.Sprintf("%x", color),
163145
Title: issueTitle,
164146
TitleLink: p.Issue.HTMLURL,
165147
Text: attachmentText,
166-
}}
148+
})
167149
}
168150

169-
return pl, nil
151+
return s.createPayload(text, attachments), nil
170152
}
171153

172154
// IssueComment implements PayloadConvertor IssueComment method
173155
func (s *SlackPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
174156
text, issueTitle, color := getIssueCommentPayloadInfo(p, SlackLinkFormatter, true)
175157

176-
return &SlackPayload{
177-
Channel: s.Channel,
178-
Text: text,
179-
Username: s.Username,
180-
IconURL: s.IconURL,
181-
Attachments: []SlackAttachment{{
182-
Color: fmt.Sprintf("%x", color),
183-
Title: issueTitle,
184-
TitleLink: p.Comment.HTMLURL,
185-
Text: SlackTextFormatter(p.Comment.Body),
186-
}},
187-
}, nil
158+
return s.createPayload(text, []SlackAttachment{{
159+
Color: fmt.Sprintf("%x", color),
160+
Title: issueTitle,
161+
TitleLink: p.Comment.HTMLURL,
162+
Text: SlackTextFormatter(p.Comment.Body),
163+
}}), nil
188164
}
189165

190166
// Release implements PayloadConvertor Release method
191167
func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
192168
text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true)
193169

194-
return &SlackPayload{
195-
Channel: s.Channel,
196-
Text: text,
197-
Username: s.Username,
198-
IconURL: s.IconURL,
199-
}, nil
170+
return s.createPayload(text, nil), nil
200171
}
201172

202173
// Push implements PayloadConvertor Push method
@@ -232,42 +203,31 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
232203
}
233204
}
234205

235-
return &SlackPayload{
236-
Channel: s.Channel,
237-
Text: text,
238-
Username: s.Username,
239-
IconURL: s.IconURL,
240-
Attachments: []SlackAttachment{{
241-
Color: s.Color,
242-
Title: p.Repo.HTMLURL,
243-
TitleLink: p.Repo.HTMLURL,
244-
Text: attachmentText,
245-
}},
246-
}, nil
206+
return s.createPayload(text, []SlackAttachment{{
207+
Color: s.Color,
208+
Title: p.Repo.HTMLURL,
209+
TitleLink: p.Repo.HTMLURL,
210+
Text: attachmentText,
211+
}}), nil
247212
}
248213

249214
// PullRequest implements PayloadConvertor PullRequest method
250215
func (s *SlackPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
251216
text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, SlackLinkFormatter, true)
252217

253-
pl := &SlackPayload{
254-
Channel: s.Channel,
255-
Text: text,
256-
Username: s.Username,
257-
IconURL: s.IconURL,
258-
}
218+
var attachments []SlackAttachment
259219
if attachmentText != "" {
260220
attachmentText = SlackTextFormatter(p.PullRequest.Body)
261221
issueTitle = SlackTextFormatter(issueTitle)
262-
pl.Attachments = []SlackAttachment{{
222+
attachments = append(attachments, SlackAttachment{
263223
Color: fmt.Sprintf("%x", color),
264224
Title: issueTitle,
265225
TitleLink: p.PullRequest.URL,
266226
Text: attachmentText,
267-
}}
227+
})
268228
}
269229

270-
return pl, nil
230+
return s.createPayload(text, attachments), nil
271231
}
272232

273233
// Review implements PayloadConvertor Review method
@@ -288,12 +248,7 @@ func (s *SlackPayload) Review(p *api.PullRequestPayload, event models.HookEventT
288248
text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", repoLink, action, title, titleLink, senderLink)
289249
}
290250

291-
return &SlackPayload{
292-
Channel: s.Channel,
293-
Text: text,
294-
Username: s.Username,
295-
IconURL: s.IconURL,
296-
}, nil
251+
return s.createPayload(text, nil), nil
297252
}
298253

299254
// Repository implements PayloadConvertor Repository method
@@ -309,12 +264,17 @@ func (s *SlackPayload) Repository(p *api.RepositoryPayload) (api.Payloader, erro
309264
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink)
310265
}
311266

267+
return s.createPayload(text, nil), nil
268+
}
269+
270+
func (s *SlackPayload) createPayload(text string, attachments []SlackAttachment) *SlackPayload {
312271
return &SlackPayload{
313-
Channel: s.Channel,
314-
Text: text,
315-
Username: s.Username,
316-
IconURL: s.IconURL,
317-
}, nil
272+
Channel: s.Channel,
273+
Text: text,
274+
Username: s.Username,
275+
IconURL: s.IconURL,
276+
Attachments: attachments,
277+
}
318278
}
319279

320280
// GetSlackPayload converts a slack webhook into a SlackPayload

0 commit comments

Comments
 (0)