summaryrefslogtreecommitdiffstats
path: root/models/webhook_discord.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2018-05-16 22:01:55 +0800
committerGitHub <noreply@github.com>2018-05-16 22:01:55 +0800
commit24941a10464dc27eaebafda2a208fa827b74ff8d (patch)
treec875dd6b7d659e2dbd926dbd3a04a036f9f41c94 /models/webhook_discord.go
parent188fe6c301f9c44d569b75cb339d6a6b3f6e03ad (diff)
downloadgitea-24941a10464dc27eaebafda2a208fa827b74ff8d.tar.gz
gitea-24941a10464dc27eaebafda2a208fa827b74ff8d.zip
Add more webhooks support and refactor webhook templates directory (#3929)
* add more webhook support * move hooks templates to standalone dir and add more webhooks ui * fix tests * update vendor checksum * add more webhook support * move hooks templates to standalone dir and add more webhooks ui * fix tests * update vendor checksum * update vendor Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * load attributes when created release * update comparsion doc
Diffstat (limited to 'models/webhook_discord.go')
-rw-r--r--models/webhook_discord.go186
1 files changed, 186 insertions, 0 deletions
diff --git a/models/webhook_discord.go b/models/webhook_discord.go
index 40d9d58992..04ebbc293f 100644
--- a/models/webhook_discord.go
+++ b/models/webhook_discord.go
@@ -115,6 +115,51 @@ func getDiscordCreatePayload(p *api.CreatePayload, meta *DiscordMeta) (*DiscordP
}, nil
}
+func getDiscordDeletePayload(p *api.DeletePayload, meta *DiscordMeta) (*DiscordPayload, error) {
+ // deleted tag/branch
+ refName := git.RefEndName(p.Ref)
+ title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
+
+ return &DiscordPayload{
+ Username: meta.Username,
+ AvatarURL: meta.IconURL,
+ Embeds: []DiscordEmbed{
+ {
+ Title: title,
+ URL: p.Repo.HTMLURL + "/src/" + refName,
+ Color: warnColor,
+ Author: DiscordEmbedAuthor{
+ Name: p.Sender.UserName,
+ URL: setting.AppURL + p.Sender.UserName,
+ IconURL: p.Sender.AvatarURL,
+ },
+ },
+ },
+ }, nil
+}
+
+func getDiscordForkPayload(p *api.ForkPayload, meta *DiscordMeta) (*DiscordPayload, error) {
+ // fork
+ title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
+
+ return &DiscordPayload{
+ Username: meta.Username,
+ AvatarURL: meta.IconURL,
+ Embeds: []DiscordEmbed{
+ {
+ Title: title,
+ URL: p.Repo.HTMLURL,
+ Color: successColor,
+ Author: DiscordEmbedAuthor{
+ Name: p.Sender.UserName,
+ URL: setting.AppURL + p.Sender.UserName,
+ IconURL: p.Sender.AvatarURL,
+ },
+ },
+ },
+ }, nil
+}
+
func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPayload, error) {
var (
branchName = git.RefEndName(p.Ref)
@@ -165,6 +210,108 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo
}, nil
}
+func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPayload, error) {
+ var text, title string
+ var color int
+ switch p.Action {
+ 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
+ case api.HookIssueClosed:
+ title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
+ color = failedColor
+ 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
+ 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
+ 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
+ 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
+ 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
+ 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
+ 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
+ }
+
+ return &DiscordPayload{
+ Username: meta.Username,
+ AvatarURL: meta.IconURL,
+ Embeds: []DiscordEmbed{
+ {
+ Title: title,
+ Description: text,
+ URL: p.Issue.URL,
+ Color: color,
+ Author: DiscordEmbedAuthor{
+ Name: p.Sender.UserName,
+ URL: setting.AppURL + p.Sender.UserName,
+ IconURL: p.Sender.AvatarURL,
+ },
+ },
+ },
+ }, nil
+}
+
+func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) {
+ 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
+ content = p.Comment.Body
+ color = successColor
+ case api.HookIssueCommentEdited:
+ title = "Comment edited: " + title
+ content = p.Comment.Body
+ color = warnColor
+ case api.HookIssueCommentDeleted:
+ title = "Comment deleted: " + title
+ url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
+ content = p.Comment.Body
+ color = warnColor
+ }
+
+ return &DiscordPayload{
+ Username: discord.Username,
+ AvatarURL: discord.IconURL,
+ Embeds: []DiscordEmbed{
+ {
+ Title: title,
+ Description: content,
+ URL: url,
+ Color: color,
+ Author: DiscordEmbedAuthor{
+ Name: p.Sender.UserName,
+ URL: setting.AppURL + p.Sender.UserName,
+ IconURL: p.Sender.AvatarURL,
+ },
+ },
+ },
+ }, nil
+}
+
func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta) (*DiscordPayload, error) {
var text, title string
var color int
@@ -267,6 +414,35 @@ func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*
}, nil
}
+func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*DiscordPayload, error) {
+ var title, url string
+ var color int
+ switch p.Action {
+ case api.HookReleasePublished:
+ title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
+ url = p.Release.URL
+ color = successColor
+ }
+
+ return &DiscordPayload{
+ Username: meta.Username,
+ AvatarURL: meta.IconURL,
+ Embeds: []DiscordEmbed{
+ {
+ Title: title,
+ Description: fmt.Sprintf("%s", p.Release.Note),
+ URL: url,
+ Color: color,
+ Author: DiscordEmbedAuthor{
+ Name: p.Sender.UserName,
+ URL: setting.AppURL + p.Sender.UserName,
+ IconURL: p.Sender.AvatarURL,
+ },
+ },
+ },
+ }, nil
+}
+
// GetDiscordPayload converts a discord webhook into a DiscordPayload
func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*DiscordPayload, error) {
s := new(DiscordPayload)
@@ -279,12 +455,22 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*Disc
switch event {
case HookEventCreate:
return getDiscordCreatePayload(p.(*api.CreatePayload), discord)
+ case HookEventDelete:
+ return getDiscordDeletePayload(p.(*api.DeletePayload), discord)
+ case HookEventFork:
+ return getDiscordForkPayload(p.(*api.ForkPayload), discord)
+ case HookEventIssues:
+ return getDiscordIssuesPayload(p.(*api.IssuePayload), discord)
+ case HookEventIssueComment:
+ return getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), discord)
case HookEventPush:
return getDiscordPushPayload(p.(*api.PushPayload), discord)
case HookEventPullRequest:
return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
case HookEventRepository:
return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
+ case HookEventRelease:
+ return getDiscordReleasePayload(p.(*api.ReleasePayload), discord)
}
return s, nil