webhook_slack.go 12 KB

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