You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.5 KiB

  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. )
  11. const (
  12. SLACK_COLOR string = "#dd4b39"
  13. )
  14. type Slack struct {
  15. Domain string `json:"domain"`
  16. Token string `json:"token"`
  17. Channel string `json:"channel"`
  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. Color string `json:"color"`
  30. Text string `json:"text"`
  31. }
  32. func GetSlackURL(domain string, token string) string {
  33. return fmt.Sprintf(
  34. "https://%s.slack.com/services/hooks/incoming-webhook?token=%s",
  35. domain,
  36. token,
  37. )
  38. }
  39. func (p SlackPayload) GetJSONPayload() ([]byte, error) {
  40. data, err := json.Marshal(p)
  41. if err != nil {
  42. return []byte{}, err
  43. }
  44. return data, nil
  45. }
  46. func GetSlackPayload(p *Payload, meta string) (*SlackPayload, error) {
  47. slack := &Slack{}
  48. slackPayload := &SlackPayload{}
  49. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  50. return slackPayload, errors.New("GetSlackPayload meta json:" + err.Error())
  51. }
  52. // TODO: handle different payload types: push, new branch, delete branch etc.
  53. // when they are added to gogs. Only handles push now
  54. return getSlackPushPayload(p, slack)
  55. }
  56. func getSlackPushPayload(p *Payload, slack *Slack) (*SlackPayload, error) {
  57. // n new commits
  58. refSplit := strings.Split(p.Ref, "/")
  59. branchName := refSplit[len(refSplit)-1]
  60. var commitString string
  61. if len(p.Commits) == 1 {
  62. commitString = "1 new commit"
  63. if p.CompareUrl != "" {
  64. commitString = SlackLinkFormatter(p.CompareUrl, commitString)
  65. }
  66. } else {
  67. commitString = fmt.Sprintf("%d new commits", len(p.Commits))
  68. if p.CompareUrl != "" {
  69. commitString = SlackLinkFormatter(p.CompareUrl, commitString)
  70. }
  71. }
  72. repoLink := SlackLinkFormatter(p.Repo.Url, p.Repo.Name)
  73. branchLink := SlackLinkFormatter(p.Repo.Url+"/src/"+branchName, branchName)
  74. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.Name)
  75. var attachmentText string
  76. // for each commit, generate attachment text
  77. for i, commit := range p.Commits {
  78. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.Url, commit.Id[:7]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  79. // add linebreak to each commit but the last
  80. if i < len(p.Commits)-1 {
  81. attachmentText += "\n"
  82. }
  83. }
  84. slackAttachments := []SlackAttachment{{Color: SLACK_COLOR, Text: attachmentText}}
  85. return &SlackPayload{
  86. Channel: slack.Channel,
  87. Text: text,
  88. Username: "gogs",
  89. IconUrl: "https://raw.githubusercontent.com/gogits/gogs/master/public/img/favicon.png",
  90. UnfurlLinks: 0,
  91. LinkNames: 0,
  92. Attachments: slackAttachments,
  93. }, nil
  94. }
  95. // see: https://api.slack.com/docs/formatting
  96. func SlackTextFormatter(s string) string {
  97. // take only first line of commit
  98. first := strings.Split(s, "\n")[0]
  99. // replace & < >
  100. first = strings.Replace(first, "&", "&amp;", -1)
  101. first = strings.Replace(first, "<", "&lt;", -1)
  102. first = strings.Replace(first, ">", "&gt;", -1)
  103. return first
  104. }
  105. func SlackLinkFormatter(url string, text string) string {
  106. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  107. }