webhook_discord.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2017 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. "fmt"
  8. "strconv"
  9. "strings"
  10. "github.com/gogits/git-module"
  11. api "github.com/gogits/go-gogs-client"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. type DiscordEmbedFooterObject struct {
  15. Text string `json:"text"`
  16. }
  17. type DiscordEmbedAuthorObject struct {
  18. Name string `json:"name"`
  19. URL string `json:"url"`
  20. IconURL string `json:"icon_url"`
  21. }
  22. type DiscordEmbedFieldObject struct {
  23. Name string `json:"name"`
  24. Value string `json:"value"`
  25. }
  26. type DiscordEmbedObject struct {
  27. Title string `json:"title"`
  28. Description string `json:"description"`
  29. URL string `json:"url"`
  30. Color int `json:"color"`
  31. Footer *DiscordEmbedFooterObject `json:"footer"`
  32. Author *DiscordEmbedAuthorObject `json:"author"`
  33. Fields []*DiscordEmbedFieldObject `json:"fields"`
  34. }
  35. type DiscordPayload struct {
  36. Content string `json:"content"`
  37. Username string `json:"username"`
  38. AvatarURL string `json:"avatar_url"`
  39. Embeds []*DiscordEmbedObject `json:"embeds"`
  40. }
  41. func (p *DiscordPayload) SetSecret(_ string) {}
  42. func (p *DiscordPayload) JSONPayload() ([]byte, error) {
  43. data, err := json.MarshalIndent(p, "", " ")
  44. if err != nil {
  45. return []byte{}, err
  46. }
  47. return data, nil
  48. }
  49. func DiscordTextFormatter(s string) string {
  50. return strings.Split(s, "\n")[0]
  51. }
  52. func DiscordLinkFormatter(url string, text string) string {
  53. return fmt.Sprintf("[%s](%s)", text, url)
  54. }
  55. func DiscordSHALinkFormatter(url string, text string) string {
  56. return fmt.Sprintf("[`%s`](%s)", text, url)
  57. }
  58. func getDiscordCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*DiscordPayload, error) {
  59. // Created tag/branch
  60. refName := git.RefEndName(p.Ref)
  61. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  62. refLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
  63. content := fmt.Sprintf("Created new %s: %s/%s", p.RefType, repoLink, refLink)
  64. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  65. return &DiscordPayload{
  66. Username: slack.Username,
  67. AvatarURL: slack.IconURL,
  68. Embeds: []*DiscordEmbedObject{{
  69. Description: content,
  70. URL: setting.AppUrl + p.Sender.UserName,
  71. Color: int(color),
  72. Author: &DiscordEmbedAuthorObject{
  73. Name: p.Sender.UserName,
  74. IconURL: p.Sender.AvatarUrl,
  75. },
  76. }},
  77. }, nil
  78. }
  79. func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayload, error) {
  80. // n new commits
  81. var (
  82. branchName = git.RefEndName(p.Ref)
  83. commitDesc string
  84. commitString string
  85. )
  86. if len(p.Commits) == 1 {
  87. commitDesc = "1 new commit"
  88. } else {
  89. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  90. }
  91. if len(p.CompareURL) > 0 {
  92. commitString = DiscordLinkFormatter(p.CompareURL, commitDesc)
  93. } else {
  94. commitString = commitDesc
  95. }
  96. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  97. branchLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  98. content := fmt.Sprintf("Pushed %s to %s/%s\n", commitString, repoLink, branchLink)
  99. // for each commit, generate attachment text
  100. for i, commit := range p.Commits {
  101. content += fmt.Sprintf("%s %s - %s", DiscordSHALinkFormatter(commit.URL, commit.ID[:7]), DiscordTextFormatter(commit.Message), commit.Author.Name)
  102. // add linebreak to each commit but the last
  103. if i < len(p.Commits)-1 {
  104. content += "\n"
  105. }
  106. }
  107. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  108. return &DiscordPayload{
  109. Username: slack.Username,
  110. AvatarURL: slack.IconURL,
  111. Embeds: []*DiscordEmbedObject{{
  112. Description: content,
  113. URL: setting.AppUrl + p.Sender.UserName,
  114. Color: int(color),
  115. Author: &DiscordEmbedAuthorObject{
  116. Name: p.Sender.UserName,
  117. IconURL: p.Sender.AvatarUrl,
  118. },
  119. }},
  120. }, nil
  121. }
  122. func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*DiscordPayload, error) {
  123. title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  124. url := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  125. content := ""
  126. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  127. switch p.Action {
  128. case api.HOOK_ISSUE_OPENED:
  129. title = "New pull request: " + title
  130. content = p.PullRequest.Body
  131. case api.HOOK_ISSUE_CLOSED:
  132. if p.PullRequest.HasMerged {
  133. title = "Pull request merged: " + title
  134. } else {
  135. title = "Pull request closed: " + title
  136. }
  137. case api.HOOK_ISSUE_REOPENED:
  138. title = "Pull request re-opened: " + title
  139. case api.HOOK_ISSUE_EDITED:
  140. title = "Pull request edited: " + title
  141. content = p.PullRequest.Body
  142. case api.HOOK_ISSUE_ASSIGNED:
  143. title = "Pull request assigned: " + title
  144. fields = []*DiscordEmbedFieldObject{{
  145. Name: "New Assignee",
  146. Value: p.PullRequest.Assignee.UserName,
  147. }}
  148. case api.HOOK_ISSUE_UNASSIGNED:
  149. title = "Pull request unassigned: " + title
  150. case api.HOOK_ISSUE_LABEL_UPDATED:
  151. title = "Pull request labels updated: " + title
  152. labels := make([]string, len(p.PullRequest.Labels))
  153. for i := range p.PullRequest.Labels {
  154. labels[i] = p.PullRequest.Labels[i].Name
  155. }
  156. fields = []*DiscordEmbedFieldObject{{
  157. Name: "Labels",
  158. Value: strings.Join(labels, ", "),
  159. }}
  160. case api.HOOK_ISSUE_LABEL_CLEARED:
  161. title = "Pull request labels cleared: " + title
  162. case api.HOOK_ISSUE_SYNCHRONIZED:
  163. title = "Pull request synchronized: " + title
  164. }
  165. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  166. return &DiscordPayload{
  167. Username: slack.Username,
  168. AvatarURL: slack.IconURL,
  169. Embeds: []*DiscordEmbedObject{{
  170. Title: title,
  171. Description: content,
  172. URL: url,
  173. Color: int(color),
  174. Footer: &DiscordEmbedFooterObject{
  175. Text: p.Repository.FullName,
  176. },
  177. Author: &DiscordEmbedAuthorObject{
  178. Name: p.Sender.UserName,
  179. IconURL: p.Sender.AvatarUrl,
  180. },
  181. Fields: fields,
  182. }},
  183. }, nil
  184. }
  185. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*DiscordPayload, error) {
  186. d := new(DiscordPayload)
  187. slack := &SlackMeta{}
  188. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  189. return d, fmt.Errorf("GetDiscordPayload meta json: %v", err)
  190. }
  191. switch event {
  192. case HOOK_EVENT_CREATE:
  193. return getDiscordCreatePayload(p.(*api.CreatePayload), slack)
  194. case HOOK_EVENT_PUSH:
  195. return getDiscordPushPayload(p.(*api.PushPayload), slack)
  196. case HOOK_EVENT_PULL_REQUEST:
  197. return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
  198. }
  199. return d, nil
  200. }