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.

slack.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. } else {
  64. commitString = fmt.Sprintf("%d new commits", len(p.Commits))
  65. commitString = SlackLinkFormatter(p.CompareUrl, commitString)
  66. }
  67. repoLink := SlackLinkFormatter(p.Repo.Url, p.Repo.Name)
  68. branchLink := SlackLinkFormatter(p.Repo.Url+"/src/"+branchName, branchName)
  69. text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.Name)
  70. var attachmentText string
  71. // for each commit, generate attachment text
  72. for i, commit := range p.Commits {
  73. attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.Url, commit.Id[:7]), SlackTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
  74. // add linebreak to each commit but the last
  75. if i < len(p.Commits)-1 {
  76. attachmentText += "\n"
  77. }
  78. }
  79. slackAttachments := []SlackAttachment{{Color: SLACK_COLOR, Text: attachmentText}}
  80. return &SlackPayload{
  81. Channel: slack.Channel,
  82. Text: text,
  83. Username: "gogs",
  84. IconUrl: "https://raw.githubusercontent.com/gogits/gogs/master/public/img/favicon.png",
  85. UnfurlLinks: 0,
  86. LinkNames: 0,
  87. Attachments: slackAttachments,
  88. }, nil
  89. }
  90. // see: https://api.slack.com/docs/formatting
  91. func SlackTextFormatter(s string) string {
  92. // take only first line of commit
  93. first := strings.Split(s, "\n")[0]
  94. // replace & < >
  95. first = strings.Replace(first, "&", "&amp;", -1)
  96. first = strings.Replace(first, "<", "&lt;", -1)
  97. first = strings.Replace(first, ">", "&gt;", -1)
  98. return first
  99. }
  100. func SlackLinkFormatter(url string, text string) string {
  101. return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
  102. }