diff options
author | Unknwon <u@gogs.io> | 2016-08-14 03:32:24 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-14 03:32:24 -0700 |
commit | 3f7f4852efaaa56a0dada832dc652a1fc8869ae7 (patch) | |
tree | e3bb1769b2967dea560b2400abf830dc6cf70067 /models/webhook_slack.go | |
parent | 0f33b04c876593e592887450302774654fef2787 (diff) | |
download | gitea-3f7f4852efaaa56a0dada832dc652a1fc8869ae7.tar.gz gitea-3f7f4852efaaa56a0dada832dc652a1fc8869ae7.zip |
#2246 fully support of webhooks for pull request
Diffstat (limited to 'models/webhook_slack.go')
-rw-r--r-- | models/webhook_slack.go | 90 |
1 files changed, 74 insertions, 16 deletions
diff --git a/models/webhook_slack.go b/models/webhook_slack.go index e41bbbc79f..5578c816a5 100644 --- a/models/webhook_slack.go +++ b/models/webhook_slack.go @@ -12,6 +12,8 @@ import ( "github.com/gogits/git-module" api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/modules/setting" ) type SlackMeta struct { @@ -34,6 +36,7 @@ type SlackPayload struct { type SlackAttachment struct { Fallback string `json:"fallback"` Color string `json:"color"` + Title string `json:"title"` Text string `json:"text"` } @@ -49,13 +52,20 @@ func (p *SlackPayload) JSONPayload() ([]byte, error) { // see: https://api.slack.com/docs/formatting func SlackTextFormatter(s string) string { - // take only first line of commit - first := strings.Split(s, "\n")[0] // replace & < > - first = strings.Replace(first, "&", "&", -1) - first = strings.Replace(first, "<", "<", -1) - first = strings.Replace(first, ">", ">", -1) - return first + s = strings.Replace(s, "&", "&", -1) + s = strings.Replace(s, "<", "<", -1) + s = strings.Replace(s, ">", ">", -1) + return s +} + +func SlackShortTextFormatter(s string) string { + s = strings.Split(s, "\n")[0] + // replace & < > + s = strings.Replace(s, "&", "&", -1) + s = strings.Replace(s, "<", "<", -1) + s = strings.Replace(s, ">", ">", -1) + return s } func SlackLinkFormatter(url string, text string) string { @@ -104,24 +114,70 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e var attachmentText string // for each commit, generate attachment text for i, commit := range p.Commits { - attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name)) + attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name)) // add linebreak to each commit but the last if i < len(p.Commits)-1 { attachmentText += "\n" } } - slackAttachments := []SlackAttachment{{ - Color: slack.Color, - Text: attachmentText, - }} + return &SlackPayload{ + Channel: slack.Channel, + Text: text, + Username: slack.Username, + IconURL: slack.IconURL, + Attachments: []SlackAttachment{{ + Color: slack.Color, + Text: attachmentText, + }}, + }, nil +} + +func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) { + senderLink := SlackLinkFormatter(setting.AppUrl+p.Sender.UserName, p.Sender.UserName) + titleLink := SlackLinkFormatter(fmt.Sprintf("%s/%d", setting.AppUrl+p.Repository.FullName+"/pulls", p.Index), + fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)) + var text, title, attachmentText string + switch p.Action { + case api.HOOK_ISSUE_OPENED: + text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink) + title = titleLink + attachmentText = SlackTextFormatter(p.PullRequest.Body) + case api.HOOK_ISSUE_CLOSED: + if p.PullRequest.HasMerged { + text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink) + } else { + text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink) + } + case api.HOOK_ISSUE_REOPENED: + text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink) + case api.HOOK_ISSUE_EDITED: + text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink) + attachmentText = SlackTextFormatter(p.PullRequest.Body) + case api.HOOK_ISSUE_ASSIGNED: + text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName, + SlackLinkFormatter(setting.AppUrl+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName), + titleLink, senderLink) + case api.HOOK_ISSUE_UNASSIGNED: + text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink) + case api.HOOK_ISSUE_LABEL_UPDATED: + text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink) + case api.HOOK_ISSUE_LABEL_CLEARED: + text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink) + case api.HOOK_ISSUE_SYNCHRONIZED: + text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink) + } return &SlackPayload{ - Channel: slack.Channel, - Text: text, - Username: slack.Username, - IconURL: slack.IconURL, - Attachments: slackAttachments, + Channel: slack.Channel, + Text: text, + Username: slack.Username, + IconURL: slack.IconURL, + Attachments: []SlackAttachment{{ + Color: slack.Color, + Title: title, + Text: attachmentText, + }}, }, nil } @@ -138,6 +194,8 @@ func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackP return getSlackCreatePayload(p.(*api.CreatePayload), slack) case HOOK_EVENT_PUSH: return getSlackPushPayload(p.(*api.PushPayload), slack) + case HOOK_EVENT_PULL_REQUEST: + return getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack) } return s, nil |