webhook_slack.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/setting"
  10. "strings"
  11. "github.com/json-iterator/go"
  12. "gitlab.com/gitote/git-module"
  13. api "gitlab.com/gitote/go-gitote-client"
  14. )
  15. type SlackMeta struct {
  16. Channel string `json:"channel"`
  17. Username string `json:"username"`
  18. IconURL string `json:"icon_url"`
  19. Color string `json:"color"`
  20. }
  21. type SlackAttachment struct {
  22. Fallback string `json:"fallback"`
  23. Color string `json:"color"`
  24. Title string `json:"title"`
  25. Text string `json:"text"`
  26. }
  27. type SlackPayload struct {
  28. Channel string `json:"channel"`
  29. Text string `json:"text"`
  30. Username string `json:"username"`
  31. IconURL string `json:"icon_url"`
  32. UnfurlLinks int `json:"unfurl_links"`
  33. LinkNames int `json:"link_names"`
  34. Attachments []*SlackAttachment `json:"attachments"`
  35. }
  36. func (p *SlackPayload) JSONPayload() ([]byte, error) {
  37. data, err := jsoniter.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. // getSlackCreatePayload composes Slack payload for create new branch or tag.
  63. func getSlackCreatePayload(p *api.CreatePayload) (*SlackPayload, error) {
  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. Text: text,
  70. }, nil
  71. }
  72. // getSlackDeletePayload composes Slack payload for delete a branch or tag.
  73. func getSlackDeletePayload(p *api.DeletePayload) (*SlackPayload, error) {
  74. refName := git.RefEndName(p.Ref)
  75. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  76. text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
  77. return &SlackPayload{
  78. Text: text,
  79. }, nil
  80. }
  81. // getSlackForkPayload composes Slack payload for forked by a repository.
  82. func getSlackForkPayload(p *api.ForkPayload) (*SlackPayload, error) {
  83. baseLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  84. forkLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
  85. text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
  86. return &SlackPayload{
  87. Text: text,
  88. }, nil
  89. }
  90. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  91. // n new commits
  92. var (
  93. branchName = git.RefEndName(p.Ref)
  94. commitDesc string
  95. commitString string
  96. )
  97. if len(p.Commits) == 1 {
  98. commitDesc = "1 new commit"
  99. } else {
  100. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  101. }
  102. if len(p.CompareURL) > 0 {
  103. commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
  104. } else {
  105. commitString = commitDesc
  106. }
  107. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  108. branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  109. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName)
  110. var attachmentText string
  111. // for each commit, generate attachment text
  112. for i, commit := range p.Commits {
  113. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  114. // add linebreak to each commit but the last
  115. if i < len(p.Commits)-1 {
  116. attachmentText += "\n"
  117. }
  118. }
  119. return &SlackPayload{
  120. Channel: slack.Channel,
  121. Text: text,
  122. Username: slack.Username,
  123. IconURL: slack.IconURL,
  124. Attachments: []*SlackAttachment{{
  125. Color: slack.Color,
  126. Text: attachmentText,
  127. }},
  128. }, nil
  129. }
  130. func getSlackIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*SlackPayload, error) {
  131. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  132. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index),
  133. fmt.Sprintf("#%d %s", p.Index, p.Issue.Title))
  134. var text, title, attachmentText string
  135. switch p.Action {
  136. case api.HOOK_ISSUE_OPENED:
  137. text = fmt.Sprintf("[%s] New issue created by %s", p.Repository.FullName, senderLink)
  138. title = titleLink
  139. attachmentText = SlackTextFormatter(p.Issue.Body)
  140. case api.HOOK_ISSUE_CLOSED:
  141. text = fmt.Sprintf("[%s] Issue closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  142. case api.HOOK_ISSUE_REOPENED:
  143. text = fmt.Sprintf("[%s] Issue re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  144. case api.HOOK_ISSUE_EDITED:
  145. text = fmt.Sprintf("[%s] Issue edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  146. attachmentText = SlackTextFormatter(p.Issue.Body)
  147. case api.HOOK_ISSUE_ASSIGNED:
  148. text = fmt.Sprintf("[%s] Issue assigned to %s: %s by %s", p.Repository.FullName,
  149. SlackLinkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName),
  150. titleLink, senderLink)
  151. case api.HOOK_ISSUE_UNASSIGNED:
  152. text = fmt.Sprintf("[%s] Issue unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  153. case api.HOOK_ISSUE_LABEL_UPDATED:
  154. text = fmt.Sprintf("[%s] Issue labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  155. case api.HOOK_ISSUE_LABEL_CLEARED:
  156. text = fmt.Sprintf("[%s] Issue labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  157. case api.HOOK_ISSUE_MILESTONED:
  158. text = fmt.Sprintf("[%s] Issue milestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  159. case api.HOOK_ISSUE_DEMILESTONED:
  160. text = fmt.Sprintf("[%s] Issue demilestoned: %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 getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*SlackPayload, error) {
  175. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  176. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID)),
  177. fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title))
  178. var text, title, attachmentText string
  179. switch p.Action {
  180. case api.HOOK_ISSUE_COMMENT_CREATED:
  181. text = fmt.Sprintf("[%s] New comment created by %s", p.Repository.FullName, senderLink)
  182. title = titleLink
  183. attachmentText = SlackTextFormatter(p.Comment.Body)
  184. case api.HOOK_ISSUE_COMMENT_EDITED:
  185. text = fmt.Sprintf("[%s] Comment edited by %s", p.Repository.FullName, senderLink)
  186. title = titleLink
  187. attachmentText = SlackTextFormatter(p.Comment.Body)
  188. case api.HOOK_ISSUE_COMMENT_DELETED:
  189. text = fmt.Sprintf("[%s] Comment deleted by %s", p.Repository.FullName, senderLink)
  190. title = SlackLinkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index),
  191. fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title))
  192. attachmentText = SlackTextFormatter(p.Comment.Body)
  193. }
  194. return &SlackPayload{
  195. Channel: slack.Channel,
  196. Text: text,
  197. Username: slack.Username,
  198. IconURL: slack.IconURL,
  199. Attachments: []*SlackAttachment{{
  200. Color: slack.Color,
  201. Title: title,
  202. Text: attachmentText,
  203. }},
  204. }, nil
  205. }
  206. func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) {
  207. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  208. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
  209. fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  210. var text, title, attachmentText string
  211. switch p.Action {
  212. case api.HOOK_ISSUE_OPENED:
  213. text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink)
  214. title = titleLink
  215. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  216. case api.HOOK_ISSUE_CLOSED:
  217. if p.PullRequest.HasMerged {
  218. text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink)
  219. } else {
  220. text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  221. }
  222. case api.HOOK_ISSUE_REOPENED:
  223. text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  224. case api.HOOK_ISSUE_EDITED:
  225. text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  226. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  227. case api.HOOK_ISSUE_ASSIGNED:
  228. text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName,
  229. SlackLinkFormatter(setting.AppURL+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName),
  230. titleLink, senderLink)
  231. case api.HOOK_ISSUE_UNASSIGNED:
  232. text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  233. case api.HOOK_ISSUE_LABEL_UPDATED:
  234. text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  235. case api.HOOK_ISSUE_LABEL_CLEARED:
  236. text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  237. case api.HOOK_ISSUE_SYNCHRONIZED:
  238. text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink)
  239. case api.HOOK_ISSUE_MILESTONED:
  240. text = fmt.Sprintf("[%s] Pull request milestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  241. case api.HOOK_ISSUE_DEMILESTONED:
  242. text = fmt.Sprintf("[%s] Pull request demilestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  243. }
  244. return &SlackPayload{
  245. Channel: slack.Channel,
  246. Text: text,
  247. Username: slack.Username,
  248. IconURL: slack.IconURL,
  249. Attachments: []*SlackAttachment{{
  250. Color: slack.Color,
  251. Title: title,
  252. Text: attachmentText,
  253. }},
  254. }, nil
  255. }
  256. func getSlackReleasePayload(p *api.ReleasePayload) (*SlackPayload, error) {
  257. repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  258. refLink := SlackLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
  259. text := fmt.Sprintf("[%s] new release %s published by %s", repoLink, refLink, p.Sender.UserName)
  260. return &SlackPayload{
  261. Text: text,
  262. }, nil
  263. }
  264. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload *SlackPayload, err error) {
  265. slack := &SlackMeta{}
  266. if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil {
  267. return nil, fmt.Errorf("Unmarshal: %v", err)
  268. }
  269. switch event {
  270. case HOOK_EVENT_CREATE:
  271. payload, err = getSlackCreatePayload(p.(*api.CreatePayload))
  272. case HOOK_EVENT_DELETE:
  273. payload, err = getSlackDeletePayload(p.(*api.DeletePayload))
  274. case HOOK_EVENT_FORK:
  275. payload, err = getSlackForkPayload(p.(*api.ForkPayload))
  276. case HOOK_EVENT_PUSH:
  277. payload, err = getSlackPushPayload(p.(*api.PushPayload), slack)
  278. case HOOK_EVENT_ISSUES:
  279. payload, err = getSlackIssuesPayload(p.(*api.IssuesPayload), slack)
  280. case HOOK_EVENT_ISSUE_COMMENT:
  281. payload, err = getSlackIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
  282. case HOOK_EVENT_PULL_REQUEST:
  283. payload, err = getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
  284. case HOOK_EVENT_RELEASE:
  285. payload, err = getSlackReleasePayload(p.(*api.ReleasePayload))
  286. }
  287. if err != nil {
  288. return nil, fmt.Errorf("event '%s': %v", event, err)
  289. }
  290. payload.Channel = slack.Channel
  291. payload.Username = slack.Username
  292. payload.IconURL = slack.IconURL
  293. if len(payload.Attachments) > 0 {
  294. payload.Attachments[0].Color = slack.Color
  295. }
  296. return payload, nil
  297. }