webhook_slack.go 12 KB

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