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