webhook_slack.go 6.4 KB

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