@@ -111,92 +111,63 @@ func (s *SlackPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
111
111
refLink := SlackLinkToRef (p .Repo .HTMLURL , p .Ref )
112
112
text := fmt .Sprintf ("[%s:%s] %s created by %s" , repoLink , refLink , p .RefType , p .Sender .UserName )
113
113
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
120
115
}
121
116
122
117
// Delete composes Slack payload for delete a branch or tag.
123
118
func (s * SlackPayload ) Delete (p * api.DeletePayload ) (api.Payloader , error ) {
124
119
refName := git .RefEndName (p .Ref )
125
120
repoLink := SlackLinkFormatter (p .Repo .HTMLURL , p .Repo .FullName )
126
121
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
133
124
}
134
125
135
126
// Fork composes Slack payload for forked by a repository.
136
127
func (s * SlackPayload ) Fork (p * api.ForkPayload ) (api.Payloader , error ) {
137
128
baseLink := SlackLinkFormatter (p .Forkee .HTMLURL , p .Forkee .FullName )
138
129
forkLink := SlackLinkFormatter (p .Repo .HTMLURL , p .Repo .FullName )
139
130
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
146
133
}
147
134
148
135
// Issue implements PayloadConvertor Issue method
149
136
func (s * SlackPayload ) Issue (p * api.IssuePayload ) (api.Payloader , error ) {
150
137
text , issueTitle , attachmentText , color := getIssuesPayloadInfo (p , SlackLinkFormatter , true )
151
138
152
- pl := & SlackPayload {
153
- Channel : s .Channel ,
154
- Text : text ,
155
- Username : s .Username ,
156
- IconURL : s .IconURL ,
157
- }
139
+ var attachments []SlackAttachment
158
140
if attachmentText != "" {
159
141
attachmentText = SlackTextFormatter (attachmentText )
160
142
issueTitle = SlackTextFormatter (issueTitle )
161
- pl . Attachments = [] SlackAttachment { {
143
+ attachments = append ( attachments , SlackAttachment {
162
144
Color : fmt .Sprintf ("%x" , color ),
163
145
Title : issueTitle ,
164
146
TitleLink : p .Issue .HTMLURL ,
165
147
Text : attachmentText ,
166
- }}
148
+ })
167
149
}
168
150
169
- return pl , nil
151
+ return s . createPayload ( text , attachments ) , nil
170
152
}
171
153
172
154
// IssueComment implements PayloadConvertor IssueComment method
173
155
func (s * SlackPayload ) IssueComment (p * api.IssueCommentPayload ) (api.Payloader , error ) {
174
156
text , issueTitle , color := getIssueCommentPayloadInfo (p , SlackLinkFormatter , true )
175
157
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
188
164
}
189
165
190
166
// Release implements PayloadConvertor Release method
191
167
func (s * SlackPayload ) Release (p * api.ReleasePayload ) (api.Payloader , error ) {
192
168
text , _ := getReleasePayloadInfo (p , SlackLinkFormatter , true )
193
169
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
200
171
}
201
172
202
173
// Push implements PayloadConvertor Push method
@@ -232,42 +203,31 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
232
203
}
233
204
}
234
205
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
247
212
}
248
213
249
214
// PullRequest implements PayloadConvertor PullRequest method
250
215
func (s * SlackPayload ) PullRequest (p * api.PullRequestPayload ) (api.Payloader , error ) {
251
216
text , issueTitle , attachmentText , color := getPullRequestPayloadInfo (p , SlackLinkFormatter , true )
252
217
253
- pl := & SlackPayload {
254
- Channel : s .Channel ,
255
- Text : text ,
256
- Username : s .Username ,
257
- IconURL : s .IconURL ,
258
- }
218
+ var attachments []SlackAttachment
259
219
if attachmentText != "" {
260
220
attachmentText = SlackTextFormatter (p .PullRequest .Body )
261
221
issueTitle = SlackTextFormatter (issueTitle )
262
- pl . Attachments = [] SlackAttachment { {
222
+ attachments = append ( attachments , SlackAttachment {
263
223
Color : fmt .Sprintf ("%x" , color ),
264
224
Title : issueTitle ,
265
225
TitleLink : p .PullRequest .URL ,
266
226
Text : attachmentText ,
267
- }}
227
+ })
268
228
}
269
229
270
- return pl , nil
230
+ return s . createPayload ( text , attachments ) , nil
271
231
}
272
232
273
233
// Review implements PayloadConvertor Review method
@@ -288,12 +248,7 @@ func (s *SlackPayload) Review(p *api.PullRequestPayload, event models.HookEventT
288
248
text = fmt .Sprintf ("[%s] Pull request review %s: [%s](%s) by %s" , repoLink , action , title , titleLink , senderLink )
289
249
}
290
250
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
297
252
}
298
253
299
254
// Repository implements PayloadConvertor Repository method
@@ -309,12 +264,17 @@ func (s *SlackPayload) Repository(p *api.RepositoryPayload) (api.Payloader, erro
309
264
text = fmt .Sprintf ("[%s] Repository deleted by %s" , repoLink , senderLink )
310
265
}
311
266
267
+ return s .createPayload (text , nil ), nil
268
+ }
269
+
270
+ func (s * SlackPayload ) createPayload (text string , attachments []SlackAttachment ) * SlackPayload {
312
271
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
+ }
318
278
}
319
279
320
280
// GetSlackPayload converts a slack webhook into a SlackPayload
0 commit comments