webhook_slack.go 12 KB

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