webhook_discord.go 6.3 KB

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