Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve webhooks #8583

Merged
merged 4 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions models/webhook_dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,36 @@ func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
}

func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
var content string
switch p.Action {
case api.HookIssueCommentCreated:
title = "New comment: " + title
if p.IsPull {
title = "New comment on pull request " + title
} else {
title = "New comment on issue " + title
}
content = p.Comment.Body
case api.HookIssueCommentEdited:
title = "Comment edited: " + title
if p.IsPull {
title = "Comment edited on pull request " + title
} else {
title = "Comment edited on issue " + title
}
content = p.Comment.Body
case api.HookIssueCommentDeleted:
title = "Comment deleted: " + title
if p.IsPull {
title = "Comment deleted on pull request " + title
} else {
title = "Comment deleted on issue " + title
}
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
content = p.Comment.Body
}

title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)

return &DingtalkPayload{
MsgType: "actionCard",
ActionCard: dingtalk.ActionCard{
Expand Down Expand Up @@ -282,7 +296,7 @@ func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event Hook
}

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

}

Expand Down
114 changes: 67 additions & 47 deletions models/webhook_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ func color(clr string) int {
}

var (
successColor = color("1ac600")
warnColor = color("ffd930")
failedColor = color("ff3232")
greenColor = color("1ac600")
greenColorLight = color("bfe5bf")
yellowColor = color("ffd930")
greyColor = color("4f545c")
purpleColor = color("7289da")
orangeColor = color("eb6420")
orangeColorLight = color("e68d60")
redColor = color("ff3232")
)

// SetSecret sets the discord secret
Expand All @@ -104,7 +109,7 @@ func getDiscordCreatePayload(p *api.CreatePayload, meta *DiscordMeta) (*DiscordP
{
Title: title,
URL: p.Repo.HTMLURL + "/src/" + refName,
Color: successColor,
Color: greenColor,
Author: DiscordEmbedAuthor{
Name: p.Sender.UserName,
URL: setting.AppURL + p.Sender.UserName,
Expand All @@ -127,7 +132,7 @@ func getDiscordDeletePayload(p *api.DeletePayload, meta *DiscordMeta) (*DiscordP
{
Title: title,
URL: p.Repo.HTMLURL + "/src/" + refName,
Color: warnColor,
Color: redColor,
Author: DiscordEmbedAuthor{
Name: p.Sender.UserName,
URL: setting.AppURL + p.Sender.UserName,
Expand All @@ -149,7 +154,7 @@ func getDiscordForkPayload(p *api.ForkPayload, meta *DiscordMeta) (*DiscordPaylo
{
Title: title,
URL: p.Repo.HTMLURL,
Color: successColor,
Color: greenColor,
Author: DiscordEmbedAuthor{
Name: p.Sender.UserName,
URL: setting.AppURL + p.Sender.UserName,
Expand Down Expand Up @@ -199,7 +204,7 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo
Title: title,
Description: text,
URL: titleLink,
Color: successColor,
Color: greenColor,
Author: DiscordEmbedAuthor{
Name: p.Sender.UserName,
URL: setting.AppURL + p.Sender.UserName,
Expand All @@ -218,48 +223,48 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa
case api.HookIssueOpened:
title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = orangeColor
case api.HookIssueClosed:
title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
color = failedColor
color = redColor
text = p.Issue.Body
case api.HookIssueReOpened:
title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueEdited:
title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueAssigned:
title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = successColor
color = greenColor
case api.HookIssueUnassigned:
title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueLabelUpdated:
title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueLabelCleared:
title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueSynchronized:
title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueMilestoned:
title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
case api.HookIssueDemilestoned:
title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
text = p.Issue.Body
color = warnColor
color = yellowColor
}

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

func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) {
title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
content := ""
var color int
switch p.Action {
case api.HookIssueCommentCreated:
title = "New comment: " + title
if p.IsPull {
title = "New comment on pull request " + title
color = greenColorLight
} else {
title = "New comment on issue " + title
color = orangeColorLight
}
content = p.Comment.Body
color = successColor
case api.HookIssueCommentEdited:
title = "Comment edited: " + title
if p.IsPull {
title = "Comment edited on pull request " + title
} else {
title = "Comment edited on issue " + title
}
content = p.Comment.Body
color = warnColor
color = yellowColor
case api.HookIssueCommentDeleted:
title = "Comment deleted: " + title
if p.IsPull {
title = "Comment deleted on pull request " + title
} else {
title = "Comment deleted on issue " + title
}
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
content = p.Comment.Body
color = warnColor
color = redColor
}

title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)

return &DiscordPayload{
Username: discord.Username,
AvatarURL: discord.IconURL,
Expand All @@ -328,24 +348,24 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
case api.HookIssueOpened:
title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = greenColor
case api.HookIssueClosed:
if p.PullRequest.HasMerged {
title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
color = successColor
color = purpleColor
} else {
title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
color = failedColor
color = redColor
}
text = p.PullRequest.Body
case api.HookIssueReOpened:
title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueEdited:
title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueAssigned:
list := make([]string, len(p.PullRequest.Assignees))
for i, user := range p.PullRequest.Assignees {
Expand All @@ -355,31 +375,31 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
strings.Join(list, ", "),
p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = successColor
color = greenColor
case api.HookIssueUnassigned:
title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueLabelUpdated:
title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueLabelCleared:
title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueSynchronized:
title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueMilestoned:
title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
case api.HookIssueDemilestoned:
title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
color = yellowColor
}

return &DiscordPayload{
Expand Down Expand Up @@ -412,17 +432,17 @@ func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *Disco
}

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

switch event {
case HookEventPullRequestApproved:
color = successColor
color = greenColor
case HookEventPullRequestRejected:
color = failedColor
color = redColor
case HookEventPullRequestComment:
fallthrough
color = greyColor
default:
color = warnColor
color = yellowColor
}
}

Expand Down Expand Up @@ -452,10 +472,10 @@ func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*
case api.HookRepoCreated:
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
url = p.Repository.HTMLURL
color = successColor
color = greenColor
case api.HookRepoDeleted:
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
color = warnColor
color = redColor
}

return &DiscordPayload{
Expand Down Expand Up @@ -483,15 +503,15 @@ func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*Discor
case api.HookReleasePublished:
title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
url = p.Release.URL
color = successColor
color = greenColor
case api.HookReleaseUpdated:
title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
url = p.Release.URL
color = successColor
color = yellowColor
case api.HookReleaseDeleted:
title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
url = p.Release.URL
color = successColor
color = redColor
}

return &DiscordPayload{
Expand Down
Loading