webhook_slack.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. // SlackTextFormatter formats text 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. // SlackShortTextFormatter formats short text
  52. func SlackShortTextFormatter(s string) string {
  53. s = strings.Split(s, "\n")[0]
  54. // replace & < >
  55. s = strings.Replace(s, "&", "&amp;", -1)
  56. s = strings.Replace(s, "<", "&lt;", -1)
  57. s = strings.Replace(s, ">", "&gt;", -1)
  58. return s
  59. }
  60. // SlackLinkFormatter formats link
  61. func SlackLinkFormatter(url string, text string) string {
  62. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  63. }
  64. // getSlackCreatePayload composes Slack payload for create new branch or tag.
  65. func getSlackCreatePayload(p *api.CreatePayload) (*SlackPayload, error) {
  66. refName := git.RefEndName(p.Ref)
  67. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  68. refLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
  69. text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
  70. return &SlackPayload{
  71. Text: text,
  72. }, nil
  73. }
  74. // getSlackDeletePayload composes Slack payload for delete a branch or tag.
  75. func getSlackDeletePayload(p *api.DeletePayload) (*SlackPayload, error) {
  76. refName := git.RefEndName(p.Ref)
  77. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  78. text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
  79. return &SlackPayload{
  80. Text: text,
  81. }, nil
  82. }
  83. // getSlackForkPayload composes Slack payload for forked by a repository.
  84. func getSlackForkPayload(p *api.ForkPayload) (*SlackPayload, error) {
  85. baseLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  86. forkLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
  87. text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
  88. return &SlackPayload{
  89. Text: text,
  90. }, nil
  91. }
  92. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  93. // n new commits
  94. var (
  95. branchName = git.RefEndName(p.Ref)
  96. commitDesc string
  97. commitString string
  98. )
  99. if len(p.Commits) == 1 {
  100. commitDesc = "1 new commit"
  101. } else {
  102. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  103. }
  104. if len(p.CompareURL) > 0 {
  105. commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
  106. } else {
  107. commitString = commitDesc
  108. }
  109. repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  110. branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  111. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName)
  112. var attachmentText string
  113. // for each commit, generate attachment text
  114. for i, commit := range p.Commits {
  115. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  116. // add linebreak to each commit but the last
  117. if i < len(p.Commits)-1 {
  118. attachmentText += "\n"
  119. }
  120. }
  121. return &SlackPayload{
  122. Channel: slack.Channel,
  123. Text: text,
  124. Username: slack.Username,
  125. IconURL: slack.IconURL,
  126. Attachments: []*SlackAttachment{{
  127. Color: slack.Color,
  128. Text: attachmentText,
  129. }},
  130. }, nil
  131. }
  132. func getSlackIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*SlackPayload, error) {
  133. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  134. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index),
  135. fmt.Sprintf("#%d %s", p.Index, p.Issue.Title))
  136. var text, title, attachmentText string
  137. switch p.Action {
  138. case api.HOOK_ISSUE_OPENED:
  139. text = fmt.Sprintf("[%s] New issue created by %s", p.Repository.FullName, senderLink)
  140. title = titleLink
  141. attachmentText = SlackTextFormatter(p.Issue.Body)
  142. case api.HOOK_ISSUE_CLOSED:
  143. text = fmt.Sprintf("[%s] Issue closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  144. case api.HOOK_ISSUE_REOPENED:
  145. text = fmt.Sprintf("[%s] Issue re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  146. case api.HOOK_ISSUE_EDITED:
  147. text = fmt.Sprintf("[%s] Issue edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  148. attachmentText = SlackTextFormatter(p.Issue.Body)
  149. case api.HOOK_ISSUE_ASSIGNED:
  150. text = fmt.Sprintf("[%s] Issue assigned to %s: %s by %s", p.Repository.FullName,
  151. SlackLinkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName),
  152. titleLink, senderLink)
  153. case api.HOOK_ISSUE_UNASSIGNED:
  154. text = fmt.Sprintf("[%s] Issue unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  155. case api.HOOK_ISSUE_LABEL_UPDATED:
  156. text = fmt.Sprintf("[%s] Issue labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  157. case api.HOOK_ISSUE_LABEL_CLEARED:
  158. text = fmt.Sprintf("[%s] Issue labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  159. case api.HOOK_ISSUE_MILESTONED:
  160. text = fmt.Sprintf("[%s] Issue milestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  161. case api.HOOK_ISSUE_DEMILESTONED:
  162. text = fmt.Sprintf("[%s] Issue demilestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  163. }
  164. return &SlackPayload{
  165. Channel: slack.Channel,
  166. Text: text,
  167. Username: slack.Username,
  168. IconURL: slack.IconURL,
  169. Attachments: []*SlackAttachment{{
  170. Color: slack.Color,
  171. Title: title,
  172. Text: attachmentText,
  173. }},
  174. }, nil
  175. }
  176. func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*SlackPayload, error) {
  177. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  178. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID)),
  179. fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title))
  180. var text, title, attachmentText string
  181. switch p.Action {
  182. case api.HOOK_ISSUE_COMMENT_CREATED:
  183. text = fmt.Sprintf("[%s] New comment created by %s", p.Repository.FullName, senderLink)
  184. title = titleLink
  185. attachmentText = SlackTextFormatter(p.Comment.Body)
  186. case api.HOOK_ISSUE_COMMENT_EDITED:
  187. text = fmt.Sprintf("[%s] Comment edited by %s", p.Repository.FullName, senderLink)
  188. title = titleLink
  189. attachmentText = SlackTextFormatter(p.Comment.Body)
  190. case api.HOOK_ISSUE_COMMENT_DELETED:
  191. text = fmt.Sprintf("[%s] Comment deleted by %s", p.Repository.FullName, senderLink)
  192. title = SlackLinkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index),
  193. fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title))
  194. attachmentText = SlackTextFormatter(p.Comment.Body)
  195. }
  196. return &SlackPayload{
  197. Channel: slack.Channel,
  198. Text: text,
  199. Username: slack.Username,
  200. IconURL: slack.IconURL,
  201. Attachments: []*SlackAttachment{{
  202. Color: slack.Color,
  203. Title: title,
  204. Text: attachmentText,
  205. }},
  206. }, nil
  207. }
  208. func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) {
  209. senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  210. titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
  211. fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  212. var text, title, attachmentText string
  213. switch p.Action {
  214. case api.HOOK_ISSUE_OPENED:
  215. text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink)
  216. title = titleLink
  217. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  218. case api.HOOK_ISSUE_CLOSED:
  219. if p.PullRequest.HasMerged {
  220. text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink)
  221. } else {
  222. text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
  223. }
  224. case api.HOOK_ISSUE_REOPENED:
  225. text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
  226. case api.HOOK_ISSUE_EDITED:
  227. text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
  228. attachmentText = SlackTextFormatter(p.PullRequest.Body)
  229. case api.HOOK_ISSUE_ASSIGNED:
  230. text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName,
  231. SlackLinkFormatter(setting.AppURL+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName),
  232. titleLink, senderLink)
  233. case api.HOOK_ISSUE_UNASSIGNED:
  234. text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  235. case api.HOOK_ISSUE_LABEL_UPDATED:
  236. text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
  237. case api.HOOK_ISSUE_LABEL_CLEARED:
  238. text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
  239. case api.HOOK_ISSUE_SYNCHRONIZED:
  240. text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink)
  241. case api.HOOK_ISSUE_MILESTONED:
  242. text = fmt.Sprintf("[%s] Pull request milestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  243. case api.HOOK_ISSUE_DEMILESTONED:
  244. text = fmt.Sprintf("[%s] Pull request demilestoned: %s by %s", p.Repository.FullName, titleLink, senderLink)
  245. }
  246. return &SlackPayload{
  247. Channel: slack.Channel,
  248. Text: text,
  249. Username: slack.Username,
  250. IconURL: slack.IconURL,
  251. Attachments: []*SlackAttachment{{
  252. Color: slack.Color,
  253. Title: title,
  254. Text: attachmentText,
  255. }},
  256. }, nil
  257. }
  258. func getSlackReleasePayload(p *api.ReleasePayload) (*SlackPayload, error) {
  259. repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  260. refLink := SlackLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
  261. text := fmt.Sprintf("[%s] new release %s published by %s", repoLink, refLink, p.Sender.UserName)
  262. return &SlackPayload{
  263. Text: text,
  264. }, nil
  265. }
  266. // GetSlackPayload converts a slack webhook into a SlackPayload
  267. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload *SlackPayload, err error) {
  268. slack := &SlackMeta{}
  269. if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil {
  270. return nil, fmt.Errorf("Unmarshal: %v", err)
  271. }
  272. switch event {
  273. case HookEventCreate:
  274. payload, err = getSlackCreatePayload(p.(*api.CreatePayload))
  275. case HookEventDelete:
  276. payload, err = getSlackDeletePayload(p.(*api.DeletePayload))
  277. case HookEventFork:
  278. payload, err = getSlackForkPayload(p.(*api.ForkPayload))
  279. case HookEventPush:
  280. payload, err = getSlackPushPayload(p.(*api.PushPayload), slack)
  281. case HookEventIssues:
  282. payload, err = getSlackIssuesPayload(p.(*api.IssuesPayload), slack)
  283. case HookEventIssueComment:
  284. payload, err = getSlackIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
  285. case HookEventPullRequest:
  286. payload, err = getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
  287. case HookEventRelease:
  288. payload, err = getSlackReleasePayload(p.(*api.ReleasePayload))
  289. }
  290. if err != nil {
  291. return nil, fmt.Errorf("event '%s': %v", event, err)
  292. }
  293. payload.Channel = slack.Channel
  294. payload.Username = slack.Username
  295. payload.IconURL = slack.IconURL
  296. if len(payload.Attachments) > 0 {
  297. payload.Attachments[0].Color = slack.Color
  298. }
  299. return payload, nil
  300. }