slack.go 3.3 KB

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