webhook_slack.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. api "github.com/gogits/go-gogs-client"
  11. "github.com/gogits/gogs/modules/git"
  12. )
  13. type SlackMeta struct {
  14. Channel string `json:"channel"`
  15. Username string `json:"username"`
  16. IconURL string `json:"icon_url"`
  17. Color string `json:"color"`
  18. }
  19. type SlackPayload struct {
  20. Channel string `json:"channel"`
  21. Text string `json:"text"`
  22. Username string `json:"username"`
  23. IconURL string `json:"icon_url"`
  24. UnfurlLinks int `json:"unfurl_links"`
  25. LinkNames int `json:"link_names"`
  26. Attachments []SlackAttachment `json:"attachments"`
  27. }
  28. type SlackAttachment struct {
  29. Color string `json:"color"`
  30. Text string `json:"text"`
  31. }
  32. func (p *SlackPayload) SetSecret(_ string) {}
  33. func (p *SlackPayload) JSONPayload() ([]byte, error) {
  34. data, err := json.MarshalIndent(p, "", " ")
  35. if err != nil {
  36. return []byte{}, err
  37. }
  38. return data, nil
  39. }
  40. // see: https://api.slack.com/docs/formatting
  41. func SlackTextFormatter(s string) string {
  42. // take only first line of commit
  43. first := strings.Split(s, "\n")[0]
  44. // replace & < >
  45. first = strings.Replace(first, "&", "&amp;", -1)
  46. first = strings.Replace(first, "<", "&lt;", -1)
  47. first = strings.Replace(first, ">", "&gt;", -1)
  48. return first
  49. }
  50. func SlackLinkFormatter(url string, text string) string {
  51. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  52. }
  53. func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) {
  54. // created tag/branch
  55. refName := git.RefEndName(p.Ref)
  56. repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
  57. refLink := SlackLinkFormatter(p.Repo.URL+"/src/"+refName, refName)
  58. text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
  59. return &SlackPayload{
  60. Channel: slack.Channel,
  61. Text: text,
  62. Username: slack.Username,
  63. IconURL: slack.IconURL,
  64. }, nil
  65. }
  66. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  67. // n new commits
  68. var (
  69. branchName = git.RefEndName(p.Ref)
  70. commitString string
  71. )
  72. if len(p.Commits) == 1 {
  73. commitString = "1 new commit"
  74. if len(p.CompareUrl) > 0 {
  75. commitString = SlackLinkFormatter(p.CompareUrl, commitString)
  76. }
  77. } else {
  78. commitString = fmt.Sprintf("%d new commits", len(p.Commits))
  79. if p.CompareUrl != "" {
  80. commitString = SlackLinkFormatter(p.CompareUrl, commitString)
  81. }
  82. }
  83. repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
  84. branchLink := SlackLinkFormatter(p.Repo.URL+"/src/"+branchName, branchName)
  85. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.Name)
  86. var attachmentText string
  87. // for each commit, generate attachment text
  88. for i, commit := range p.Commits {
  89. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  90. // add linebreak to each commit but the last
  91. if i < len(p.Commits)-1 {
  92. attachmentText += "\n"
  93. }
  94. }
  95. slackAttachments := []SlackAttachment{{Color: slack.Color, Text: attachmentText}}
  96. return &SlackPayload{
  97. Channel: slack.Channel,
  98. Text: text,
  99. Username: slack.Username,
  100. IconURL: slack.IconURL,
  101. Attachments: slackAttachments,
  102. }, nil
  103. }
  104. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
  105. s := new(SlackPayload)
  106. slack := &SlackMeta{}
  107. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  108. return s, errors.New("GetSlackPayload meta json:" + err.Error())
  109. }
  110. switch event {
  111. case HOOK_EVENT_CREATE:
  112. return getSlackCreatePayload(p.(*api.CreatePayload), slack)
  113. case HOOK_EVENT_PUSH:
  114. return getSlackPushPayload(p.(*api.PushPayload), slack)
  115. }
  116. return s, nil
  117. }