webhook_slack.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // getSlackCreatePayload composes Slack payload for create new branch or tag.
  61. func getSlackCreatePayload(p *api.CreatePayload) (*SlackPayload, error) {
  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. Text: text,
  68. }, nil
  69. }
  70. // getSlackDeletePayload composes Slack payload for delete a branch or tag.
  71. func getSlackDeletePayload(p *api.DeletePayload) (*SlackPayload, error) {
  72. refName := git.RefEndName(p.Ref)
  73. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  74. text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
  75. return &SlackPayload{
  76. Text: text,
  77. }, nil
  78. }
  79. // getSlackForkPayload composes Slack payload for forked by a repository.
  80. func getSlackForkPayload(p *api.ForkPayload) (*SlackPayload, error) {
  81. baseLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  82. forkLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
  83. text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
  84. return &SlackPayload{
  85. Text: text,
  86. }, nil
  87. }
  88. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  89. // n new commits
  90. var (
  91. branchName = git.RefEndName(p.Ref)
  92. commitDesc string
  93. commitString string
  94. )
  95. if len(p.Commits) == 1 {
  96. commitDesc = "1 new commit"
  97. } else {
  98. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  99. }
  100. if len(p.CompareURL) > 0 {
  101. commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
  102. } else {
  103. commitString = commitDesc
  104. }
  105. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  106. branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  107. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName)
  108. var attachmentText string
  109. // for each commit, generate attachment text
  110. for i, commit := range p.Commits {
  111. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  112. // add linebreak to each commit but the last
  113. if i < len(p.Commits)-1 {
  114. attachmentText += "\n"
  115. }
  116. }
  117. return &SlackPayload{
  118. Channel: slack.Channel,
  119. Text: text,
  120. Username: slack.Username,
  121. IconURL: slack.IconURL,
  122. Attachments: []*SlackAttachment{{
  123. Color: slack.Color,
  124. Text: attachmentText,
  125. }},
  126. }, nil
  127. }
  128. func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) {
  129. senderLink := SlackLinkFormatter(setting.AppUrl+p.Sender.UserName, p.Sender.UserName)
  130. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
  131. fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  132. var text, title, attachmentText string
  133. switch p.Action {
  134. case api.HOOK_ISSUE_OPENED:
  135. text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink)
  136. title = titleLink
  137. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  138. case api.HOOK_ISSUE_CLOSED:
  139. if p.PullRequest.HasMerged {
  140. text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink)
  141. } else {
  142. text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  143. }
  144. case api.HOOK_ISSUE_REOPENED:
  145. text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  146. case api.HOOK_ISSUE_EDITED:
  147. text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  148. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  149. case api.HOOK_ISSUE_ASSIGNED:
  150. text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName,
  151. SlackLinkFormatter(setting.AppUrl+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName),
  152. titleLink, senderLink)
  153. case api.HOOK_ISSUE_UNASSIGNED:
  154. text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  155. case api.HOOK_ISSUE_LABEL_UPDATED:
  156. text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  157. case api.HOOK_ISSUE_LABEL_CLEARED:
  158. text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  159. case api.HOOK_ISSUE_SYNCHRONIZED:
  160. text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink)
  161. }
  162. return &SlackPayload{
  163. Channel: slack.Channel,
  164. Text: text,
  165. Username: slack.Username,
  166. IconURL: slack.IconURL,
  167. Attachments: []*SlackAttachment{{
  168. Color: slack.Color,
  169. Title: title,
  170. Text: attachmentText,
  171. }},
  172. }, nil
  173. }
  174. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload *SlackPayload, err error) {
  175. slack := &SlackMeta{}
  176. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  177. return nil, fmt.Errorf("json.Unmarshal: %v", err)
  178. }
  179. switch event {
  180. case HOOK_EVENT_CREATE:
  181. payload, err = getSlackCreatePayload(p.(*api.CreatePayload))
  182. case HOOK_EVENT_DELETE:
  183. payload, err = getSlackDeletePayload(p.(*api.DeletePayload))
  184. case HOOK_EVENT_FORK:
  185. payload, err = getSlackForkPayload(p.(*api.ForkPayload))
  186. case HOOK_EVENT_PUSH:
  187. payload, err = getSlackPushPayload(p.(*api.PushPayload), slack)
  188. case HOOK_EVENT_PULL_REQUEST:
  189. payload, err = getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
  190. }
  191. if err != nil {
  192. return nil, fmt.Errorf("event '%s': %v", event, err)
  193. }
  194. payload.Channel = slack.Channel
  195. payload.Username = slack.Username
  196. payload.IconURL = slack.IconURL
  197. if len(payload.Attachments) > 0 {
  198. payload.Attachments[0].Color = slack.Color
  199. }
  200. return payload, nil
  201. }