webhook_slack.go 12 KB

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