webhook_discord.go 6.5 KB

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