You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhook_slack.go 6.4KB

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