webhook_slack.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "github.com/gogits/git-shell"
  11. api "github.com/gogits/go-gogs-client"
  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 SlackPayload struct {
  20. Channel string `json:"channel"`
  21. Text string `json:"text"`
  22. Username string `json:"username"`
  23. IconURL string `json:"icon_url"`
  24. UnfurlLinks int `json:"unfurl_links"`
  25. LinkNames int `json:"link_names"`
  26. Attachments []SlackAttachment `json:"attachments"`
  27. }
  28. type SlackAttachment struct {
  29. Fallback string `json:"fallback"`
  30. Color string `json:"color"`
  31. Text string `json:"text"`
  32. }
  33. func (p *SlackPayload) SetSecret(_ string) {}
  34. func (p *SlackPayload) JSONPayload() ([]byte, error) {
  35. data, err := json.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. // take only first line of commit
  44. first := strings.Split(s, "\n")[0]
  45. // replace & < >
  46. first = strings.Replace(first, "&", "&amp;", -1)
  47. first = strings.Replace(first, "<", "&lt;", -1)
  48. first = strings.Replace(first, ">", "&gt;", -1)
  49. return first
  50. }
  51. func SlackLinkFormatter(url string, text string) string {
  52. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  53. }
  54. func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) {
  55. // created tag/branch
  56. refName := git.RefEndName(p.Ref)
  57. repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
  58. refLink := SlackLinkFormatter(p.Repo.URL+"/src/"+refName, refName)
  59. text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
  60. return &SlackPayload{
  61. Channel: slack.Channel,
  62. Text: text,
  63. Username: slack.Username,
  64. IconURL: slack.IconURL,
  65. }, nil
  66. }
  67. func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
  68. // n new commits
  69. var (
  70. branchName = git.RefEndName(p.Ref)
  71. commitDesc string
  72. commitString string
  73. )
  74. if len(p.Commits) == 1 {
  75. commitDesc = "1 new commit"
  76. } else {
  77. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  78. }
  79. if len(p.CompareUrl) > 0 {
  80. commitString = SlackLinkFormatter(p.CompareUrl, commitDesc)
  81. } else {
  82. commitString = commitDesc
  83. }
  84. repoLink := SlackLinkFormatter(p.Repo.URL, p.Repo.Name)
  85. branchLink := SlackLinkFormatter(p.Repo.URL+"/src/"+branchName, branchName)
  86. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.Name)
  87. var attachmentText string
  88. // for each commit, generate attachment text
  89. for i, commit := range p.Commits {
  90. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  91. // add linebreak to each commit but the last
  92. if i < len(p.Commits)-1 {
  93. attachmentText += "\n"
  94. }
  95. }
  96. slackAttachments := []SlackAttachment{{
  97. Color: slack.Color,
  98. Text: attachmentText,
  99. }}
  100. return &SlackPayload{
  101. Channel: slack.Channel,
  102. Text: text,
  103. Username: slack.Username,
  104. IconURL: slack.IconURL,
  105. Attachments: slackAttachments,
  106. }, nil
  107. }
  108. func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
  109. s := new(SlackPayload)
  110. slack := &SlackMeta{}
  111. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  112. return s, errors.New("GetSlackPayload meta json:" + err.Error())
  113. }
  114. switch event {
  115. case HOOK_EVENT_CREATE:
  116. return getSlackCreatePayload(p.(*api.CreatePayload), slack)
  117. case HOOK_EVENT_PUSH:
  118. return getSlackPushPayload(p.(*api.PushPayload), slack)
  119. }
  120. return s, nil
  121. }