webhook_discord.go 6.0 KB

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