webhook_slack.go 12 KB

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