Skip to content

Commit 0a004a6

Browse files
jolheiserzeripath
authored andcommitted
Improve webhooks (#8583)
* Improve webhooks Signed-off-by: jolheiser <[email protected]> * Update MSTeams and ReviewPayload comment Signed-off-by: jolheiser <[email protected]> * Add repo.FullName to comments Signed-off-by: jolheiser <[email protected]>
1 parent 05e437f commit 0a004a6

File tree

6 files changed

+167
-94
lines changed

6 files changed

+167
-94
lines changed

models/webhook_dingtalk.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,36 @@ func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
183183
}
184184

185185
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
186-
title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
186+
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
187187
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
188188
var content string
189189
switch p.Action {
190190
case api.HookIssueCommentCreated:
191-
title = "New comment: " + title
191+
if p.IsPull {
192+
title = "New comment on pull request " + title
193+
} else {
194+
title = "New comment on issue " + title
195+
}
192196
content = p.Comment.Body
193197
case api.HookIssueCommentEdited:
194-
title = "Comment edited: " + title
198+
if p.IsPull {
199+
title = "Comment edited on pull request " + title
200+
} else {
201+
title = "Comment edited on issue " + title
202+
}
195203
content = p.Comment.Body
196204
case api.HookIssueCommentDeleted:
197-
title = "Comment deleted: " + title
205+
if p.IsPull {
206+
title = "Comment deleted on pull request " + title
207+
} else {
208+
title = "Comment deleted on issue " + title
209+
}
198210
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
199211
content = p.Comment.Body
200212
}
201213

214+
title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
215+
202216
return &DingtalkPayload{
203217
MsgType: "actionCard",
204218
ActionCard: dingtalk.ActionCard{
@@ -282,7 +296,7 @@ func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event Hook
282296
}
283297

284298
title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
285-
text = p.PullRequest.Body
299+
text = p.Review.Content
286300

287301
}
288302

models/webhook_discord.go

+67-47
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,14 @@ func color(clr string) int {
7575
}
7676

7777
var (
78-
successColor = color("1ac600")
79-
warnColor = color("ffd930")
80-
failedColor = color("ff3232")
78+
greenColor = color("1ac600")
79+
greenColorLight = color("bfe5bf")
80+
yellowColor = color("ffd930")
81+
greyColor = color("4f545c")
82+
purpleColor = color("7289da")
83+
orangeColor = color("eb6420")
84+
orangeColorLight = color("e68d60")
85+
redColor = color("ff3232")
8186
)
8287

8388
// SetSecret sets the discord secret
@@ -104,7 +109,7 @@ func getDiscordCreatePayload(p *api.CreatePayload, meta *DiscordMeta) (*DiscordP
104109
{
105110
Title: title,
106111
URL: p.Repo.HTMLURL + "/src/" + refName,
107-
Color: successColor,
112+
Color: greenColor,
108113
Author: DiscordEmbedAuthor{
109114
Name: p.Sender.UserName,
110115
URL: setting.AppURL + p.Sender.UserName,
@@ -127,7 +132,7 @@ func getDiscordDeletePayload(p *api.DeletePayload, meta *DiscordMeta) (*DiscordP
127132
{
128133
Title: title,
129134
URL: p.Repo.HTMLURL + "/src/" + refName,
130-
Color: warnColor,
135+
Color: redColor,
131136
Author: DiscordEmbedAuthor{
132137
Name: p.Sender.UserName,
133138
URL: setting.AppURL + p.Sender.UserName,
@@ -149,7 +154,7 @@ func getDiscordForkPayload(p *api.ForkPayload, meta *DiscordMeta) (*DiscordPaylo
149154
{
150155
Title: title,
151156
URL: p.Repo.HTMLURL,
152-
Color: successColor,
157+
Color: greenColor,
153158
Author: DiscordEmbedAuthor{
154159
Name: p.Sender.UserName,
155160
URL: setting.AppURL + p.Sender.UserName,
@@ -199,7 +204,7 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo
199204
Title: title,
200205
Description: text,
201206
URL: titleLink,
202-
Color: successColor,
207+
Color: greenColor,
203208
Author: DiscordEmbedAuthor{
204209
Name: p.Sender.UserName,
205210
URL: setting.AppURL + p.Sender.UserName,
@@ -218,48 +223,48 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa
218223
case api.HookIssueOpened:
219224
title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
220225
text = p.Issue.Body
221-
color = warnColor
226+
color = orangeColor
222227
case api.HookIssueClosed:
223228
title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
224-
color = failedColor
229+
color = redColor
225230
text = p.Issue.Body
226231
case api.HookIssueReOpened:
227232
title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
228233
text = p.Issue.Body
229-
color = warnColor
234+
color = yellowColor
230235
case api.HookIssueEdited:
231236
title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
232237
text = p.Issue.Body
233-
color = warnColor
238+
color = yellowColor
234239
case api.HookIssueAssigned:
235240
title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
236241
p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
237242
text = p.Issue.Body
238-
color = successColor
243+
color = greenColor
239244
case api.HookIssueUnassigned:
240245
title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
241246
text = p.Issue.Body
242-
color = warnColor
247+
color = yellowColor
243248
case api.HookIssueLabelUpdated:
244249
title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
245250
text = p.Issue.Body
246-
color = warnColor
251+
color = yellowColor
247252
case api.HookIssueLabelCleared:
248253
title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
249254
text = p.Issue.Body
250-
color = warnColor
255+
color = yellowColor
251256
case api.HookIssueSynchronized:
252257
title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
253258
text = p.Issue.Body
254-
color = warnColor
259+
color = yellowColor
255260
case api.HookIssueMilestoned:
256261
title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
257262
text = p.Issue.Body
258-
color = warnColor
263+
color = yellowColor
259264
case api.HookIssueDemilestoned:
260265
title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
261266
text = p.Issue.Body
262-
color = warnColor
267+
color = yellowColor
263268
}
264269

265270
return &DiscordPayload{
@@ -282,26 +287,41 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa
282287
}
283288

284289
func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) {
285-
title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
290+
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
286291
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
287292
content := ""
288293
var color int
289294
switch p.Action {
290295
case api.HookIssueCommentCreated:
291-
title = "New comment: " + title
296+
if p.IsPull {
297+
title = "New comment on pull request " + title
298+
color = greenColorLight
299+
} else {
300+
title = "New comment on issue " + title
301+
color = orangeColorLight
302+
}
292303
content = p.Comment.Body
293-
color = successColor
294304
case api.HookIssueCommentEdited:
295-
title = "Comment edited: " + title
305+
if p.IsPull {
306+
title = "Comment edited on pull request " + title
307+
} else {
308+
title = "Comment edited on issue " + title
309+
}
296310
content = p.Comment.Body
297-
color = warnColor
311+
color = yellowColor
298312
case api.HookIssueCommentDeleted:
299-
title = "Comment deleted: " + title
313+
if p.IsPull {
314+
title = "Comment deleted on pull request " + title
315+
} else {
316+
title = "Comment deleted on issue " + title
317+
}
300318
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
301319
content = p.Comment.Body
302-
color = warnColor
320+
color = redColor
303321
}
304322

323+
title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
324+
305325
return &DiscordPayload{
306326
Username: discord.Username,
307327
AvatarURL: discord.IconURL,
@@ -328,24 +348,24 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
328348
case api.HookIssueOpened:
329349
title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
330350
text = p.PullRequest.Body
331-
color = warnColor
351+
color = greenColor
332352
case api.HookIssueClosed:
333353
if p.PullRequest.HasMerged {
334354
title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
335-
color = successColor
355+
color = purpleColor
336356
} else {
337357
title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
338-
color = failedColor
358+
color = redColor
339359
}
340360
text = p.PullRequest.Body
341361
case api.HookIssueReOpened:
342362
title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
343363
text = p.PullRequest.Body
344-
color = warnColor
364+
color = yellowColor
345365
case api.HookIssueEdited:
346366
title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
347367
text = p.PullRequest.Body
348-
color = warnColor
368+
color = yellowColor
349369
case api.HookIssueAssigned:
350370
list := make([]string, len(p.PullRequest.Assignees))
351371
for i, user := range p.PullRequest.Assignees {
@@ -355,31 +375,31 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
355375
strings.Join(list, ", "),
356376
p.Index, p.PullRequest.Title)
357377
text = p.PullRequest.Body
358-
color = successColor
378+
color = greenColor
359379
case api.HookIssueUnassigned:
360380
title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
361381
text = p.PullRequest.Body
362-
color = warnColor
382+
color = yellowColor
363383
case api.HookIssueLabelUpdated:
364384
title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
365385
text = p.PullRequest.Body
366-
color = warnColor
386+
color = yellowColor
367387
case api.HookIssueLabelCleared:
368388
title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
369389
text = p.PullRequest.Body
370-
color = warnColor
390+
color = yellowColor
371391
case api.HookIssueSynchronized:
372392
title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
373393
text = p.PullRequest.Body
374-
color = warnColor
394+
color = yellowColor
375395
case api.HookIssueMilestoned:
376396
title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
377397
text = p.PullRequest.Body
378-
color = warnColor
398+
color = yellowColor
379399
case api.HookIssueDemilestoned:
380400
title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
381401
text = p.PullRequest.Body
382-
color = warnColor
402+
color = yellowColor
383403
}
384404

385405
return &DiscordPayload{
@@ -412,17 +432,17 @@ func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *Disco
412432
}
413433

414434
title = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
415-
text = p.PullRequest.Body
435+
text = p.Review.Content
416436

417437
switch event {
418438
case HookEventPullRequestApproved:
419-
color = successColor
439+
color = greenColor
420440
case HookEventPullRequestRejected:
421-
color = failedColor
441+
color = redColor
422442
case HookEventPullRequestComment:
423-
fallthrough
443+
color = greyColor
424444
default:
425-
color = warnColor
445+
color = yellowColor
426446
}
427447
}
428448

@@ -452,10 +472,10 @@ func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*
452472
case api.HookRepoCreated:
453473
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
454474
url = p.Repository.HTMLURL
455-
color = successColor
475+
color = greenColor
456476
case api.HookRepoDeleted:
457477
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
458-
color = warnColor
478+
color = redColor
459479
}
460480

461481
return &DiscordPayload{
@@ -483,15 +503,15 @@ func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*Discor
483503
case api.HookReleasePublished:
484504
title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
485505
url = p.Release.URL
486-
color = successColor
506+
color = greenColor
487507
case api.HookReleaseUpdated:
488508
title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
489509
url = p.Release.URL
490-
color = successColor
510+
color = yellowColor
491511
case api.HookReleaseDeleted:
492512
title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
493513
url = p.Release.URL
494-
color = successColor
514+
color = redColor
495515
}
496516

497517
return &DiscordPayload{

0 commit comments

Comments
 (0)