webhook_slack.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "fmt"
  8. "strings"
  9. "github.com/gogits/git-module"
  10. api "github.com/gogits/go-gogs-client"
  11. "github.com/gogits/gogs/modules/setting"
  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 SlackAttachment struct {
  20. Fallback string `json:"fallback"`
  21. Color string `json:"color"`
  22. Title string `json:"title"`
  23. Text string `json:"text"`
  24. }
  25. type SlackPayload struct {
  26. Channel string `json:"channel"`
  27. Text string `json:"text"`
  28. Username string `json:"username"`
  29. IconURL string `json:"icon_url"`
  30. UnfurlLinks int `json:"unfurl_links"`
  31. LinkNames int `json:"link_names"`
  32. Attachments []*SlackAttachment `json:"attachments"`
  33. }
  34. func (p *SlackPayload) JSONPayload() ([]byte, error) {
  35. data, err := json.MarshalIndent(p, "", " ")
  36. if err != nil {
  37. return []byte{}, err
  38. }
  39. return data, nil
  40. }
  41. // see: https://api.slack.com/docs/formatting
  42. func SlackTextFormatter(s string) string {
  43. // replace & < >
  44. s = strings.Replace(s, "&", "&amp;", -1)
  45. s = strings.Replace(s, "<", "&lt;", -1)
  46. s = strings.Replace(s, ">", "&gt;", -1)
  47. return s
  48. }
  49. func SlackShortTextFormatter(s string) string {
  50. s = strings.Split(s, "\n")[0]
  51. // replace & < >
  52. s = strings.Replace(s, "&", "&amp;", -1)
  53. s = strings.Replace(s, "<", "&lt;", -1)
  54. s = strings.Replace(s, ">", "&gt;", -1)
  55. return s
  56. }
  57. func SlackLinkFormatter(url string, text string) string {
  58. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  59. }
  60. func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) {
  61. // Created tag/branch
  62. refName := git.RefEndName(p.Ref)
  63. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  64. refLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
  65. text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
  66. return &SlackPayload{
  67. Channel: slack.Channel,
  68. Text: text,
  69. Username: slack.Username,
  70. IconURL: slack.IconURL,
  71. }, nil
  72. }
  73. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  74. // n new commits
  75. var (
  76. branchName = git.RefEndName(p.Ref)
  77. commitDesc string
  78. commitString string
  79. )
  80. if len(p.Commits) == 1 {
  81. commitDesc = "1 new commit"
  82. } else {
  83. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  84. }
  85. if len(p.CompareURL) > 0 {
  86. commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
  87. } else {
  88. commitString = commitDesc
  89. }
  90. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  91. branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  92. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName)
  93. var attachmentText string
  94. // for each commit, generate attachment text
  95. for i, commit := range p.Commits {
  96. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  97. // add linebreak to each commit but the last
  98. if i < len(p.Commits)-1 {
  99. attachmentText += "\n"
  100. }
  101. }
  102. return &SlackPayload{
  103. Channel: slack.Channel,
  104. Text: text,
  105. Username: slack.Username,
  106. IconURL: slack.IconURL,
  107. Attachments: []*SlackAttachment{{
  108. Color: slack.Color,
  109. Text: attachmentText,
  110. }},
  111. }, nil
  112. }
  113. func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) {
  114. senderLink := SlackLinkFormatter(setting.AppUrl+p.Sender.UserName, p.Sender.UserName)
  115. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
  116. fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  117. var text, title, attachmentText string
  118. switch p.Action {
  119. case api.HOOK_ISSUE_OPENED:
  120. text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink)
  121. title = titleLink
  122. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  123. case api.HOOK_ISSUE_CLOSED:
  124. if p.PullRequest.HasMerged {
  125. text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink)
  126. } else {
  127. text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  128. }
  129. case api.HOOK_ISSUE_REOPENED:
  130. text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  131. case api.HOOK_ISSUE_EDITED:
  132. text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  133. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  134. case api.HOOK_ISSUE_ASSIGNED:
  135. text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName,
  136. SlackLinkFormatter(setting.AppUrl+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName),
  137. titleLink, senderLink)
  138. case api.HOOK_ISSUE_UNASSIGNED:
  139. text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  140. case api.HOOK_ISSUE_LABEL_UPDATED:
  141. text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  142. case api.HOOK_ISSUE_LABEL_CLEARED:
  143. text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  144. case api.HOOK_ISSUE_SYNCHRONIZED:
  145. text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink)
  146. }
  147. return &SlackPayload{
  148. Channel: slack.Channel,
  149. Text: text,
  150. Username: slack.Username,
  151. IconURL: slack.IconURL,
  152. Attachments: []*SlackAttachment{{
  153. Color: slack.Color,
  154. Title: title,
  155. Text: attachmentText,
  156. }},
  157. }, nil
  158. }
  159. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
  160. s := new(SlackPayload)
  161. slack := &SlackMeta{}
  162. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  163. return s, fmt.Errorf("GetSlackPayload meta json: %v", err)
  164. }
  165. switch event {
  166. case HOOK_EVENT_CREATE:
  167. return getSlackCreatePayload(p.(*api.CreatePayload), slack)
  168. case HOOK_EVENT_PUSH:
  169. return getSlackPushPayload(p.(*api.PushPayload), slack)
  170. case HOOK_EVENT_PULL_REQUEST:
  171. return getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
  172. }
  173. return s, nil
  174. }