slack.go 3.1 KB

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