summaryrefslogtreecommitdiffstats
path: root/models/webhook_slack.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-08-28 23:36:13 +0800
committerUnknwon <u@gogs.io>2015-08-28 23:36:13 +0800
commitf509c59ac13dffdff6a246572de04150ee295314 (patch)
tree3be3923779cedce3058f28f1d32604226985be03 /models/webhook_slack.go
parenta541ca16b67d492340ac55b84ff190d2a52fc39d (diff)
downloadgitea-f509c59ac13dffdff6a246572de04150ee295314.tar.gz
gitea-f509c59ac13dffdff6a246572de04150ee295314.zip
new create webhook event
Diffstat (limited to 'models/webhook_slack.go')
-rw-r--r--models/webhook_slack.go100
1 files changed, 64 insertions, 36 deletions
diff --git a/models/webhook_slack.go b/models/webhook_slack.go
index 0b1b579ff0..9ae125873f 100644
--- a/models/webhook_slack.go
+++ b/models/webhook_slack.go
@@ -9,13 +9,18 @@ import (
"errors"
"fmt"
"strings"
+
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/modules/git"
+ "github.com/gogits/gogs/modules/setting"
)
const (
SLACK_COLOR string = "#dd4b39"
)
-type Slack struct {
+type SlackMeta struct {
Channel string `json:"channel"`
}
@@ -34,7 +39,9 @@ type SlackAttachment struct {
Text string `json:"text"`
}
-func (p SlackPayload) GetJSONPayload() ([]byte, error) {
+func (p *SlackPayload) SetSecret(_ string) {}
+
+func (p *SlackPayload) JSONPayload() ([]byte, error) {
data, err := json.Marshal(p)
if err != nil {
return []byte{}, err
@@ -42,27 +49,47 @@ func (p SlackPayload) GetJSONPayload() ([]byte, error) {
return data, nil
}
-func GetSlackPayload(p *Payload, meta string) (*SlackPayload, error) {
- slack := &Slack{}
- slackPayload := &SlackPayload{}
- if err := json.Unmarshal([]byte(meta), &slack); err != nil {
- return slackPayload, errors.New("GetSlackPayload meta json:" + err.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, "&", "&amp;", -1)
+ first = strings.Replace(first, "<", "&lt;", -1)
+ first = strings.Replace(first, ">", "&gt;", -1)
+ return first
+}
- // TODO: handle different payload types: push, new branch, delete branch etc.
- // when they are added to gogs. Only handles push now
- return getSlackPushPayload(p, slack)
+func SlackLinkFormatter(url string, text string) string {
+ return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
}
-func getSlackPushPayload(p *Payload, slack *Slack) (*SlackPayload, error) {
+func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) {
+ // created tag/branch
+ refName := git.RefEndName(p.Ref)
+
+ repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
+ refLink := SlackLinkFormatter(p.Repo.URL+"/src/"+refName, refName)
+ text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
+
+ return &SlackPayload{
+ Channel: slack.Channel,
+ Text: text,
+ Username: setting.AppName,
+ IconUrl: setting.AppUrl + "/img/favicon.png",
+ }, nil
+}
+
+func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
// n new commits
- refSplit := strings.Split(p.Ref, "/")
- branchName := refSplit[len(refSplit)-1]
- var commitString string
+ var (
+ branchName = git.RefEndName(p.Ref)
+ commitString string
+ )
if len(p.Commits) == 1 {
commitString = "1 new commit"
- if p.CompareUrl != "" {
+ if len(p.CompareUrl) > 0 {
commitString = SlackLinkFormatter(p.CompareUrl, commitString)
}
} else {
@@ -72,14 +99,14 @@ func getSlackPushPayload(p *Payload, slack *Slack) (*SlackPayload, error) {
}
}
- repoLink := SlackLinkFormatter(p.Repo.Url, p.Repo.Name)
- branchLink := SlackLinkFormatter(p.Repo.Url+"/src/"+branchName, branchName)
+ repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
+ branchLink := SlackLinkFormatter(p.Repo.URL+"/src/"+branchName, branchName)
text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.Name)
- var attachmentText string
+ 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]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
// add linebreak to each commit but the last
if i < len(p.Commits)-1 {
attachmentText += "\n"
@@ -91,25 +118,26 @@ func getSlackPushPayload(p *Payload, slack *Slack) (*SlackPayload, error) {
return &SlackPayload{
Channel: slack.Channel,
Text: text,
- Username: "gogs",
- IconUrl: "https://raw.githubusercontent.com/gogits/gogs/master/public/img/favicon.png",
- UnfurlLinks: 0,
- LinkNames: 0,
+ Username: setting.AppName,
+ IconUrl: setting.AppUrl + "/img/favicon.png",
Attachments: slackAttachments,
}, nil
}
-// 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, "&", "&amp;", -1)
- first = strings.Replace(first, "<", "&lt;", -1)
- first = strings.Replace(first, ">", "&gt;", -1)
- return first
-}
+func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
+ s := new(SlackPayload)
-func SlackLinkFormatter(url string, text string) string {
- return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
+ slack := &SlackMeta{}
+ if err := json.Unmarshal([]byte(meta), &slack); err != nil {
+ return s, errors.New("GetSlackPayload meta json:" + err.Error())
+ }
+
+ switch event {
+ case HOOK_EVENT_CREATE:
+ return getSlackCreatePayload(p.(*api.CreatePayload), slack)
+ case HOOK_EVENT_PUSH:
+ return getSlackPushPayload(p.(*api.PushPayload), slack)
+ }
+
+ return s, nil
}