webhook_discord.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // getDiscordCreatePayload composes Discord payload for create new branch or tag.
  58. func getDiscordCreatePayload(p *api.CreatePayload) (*DiscordPayload, error) {
  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. return &DiscordPayload{
  64. Embeds: []*DiscordEmbedObject{{
  65. Description: content,
  66. URL: setting.AppUrl + p.Sender.UserName,
  67. Author: &DiscordEmbedAuthorObject{
  68. Name: p.Sender.UserName,
  69. IconURL: p.Sender.AvatarUrl,
  70. },
  71. }},
  72. }, nil
  73. }
  74. // getDiscordDeletePayload composes Discord payload for delete a branch or tag.
  75. func getDiscordDeletePayload(p *api.DeletePayload) (*DiscordPayload, error) {
  76. refName := git.RefEndName(p.Ref)
  77. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  78. content := fmt.Sprintf("Deleted %s: %s/%s", p.RefType, repoLink, refName)
  79. return &DiscordPayload{
  80. Embeds: []*DiscordEmbedObject{{
  81. Description: content,
  82. URL: setting.AppUrl + p.Sender.UserName,
  83. Author: &DiscordEmbedAuthorObject{
  84. Name: p.Sender.UserName,
  85. IconURL: p.Sender.AvatarUrl,
  86. },
  87. }},
  88. }, nil
  89. }
  90. // getDiscordForkPayload composes Discord payload for forked by a repository.
  91. func getDiscordForkPayload(p *api.ForkPayload) (*DiscordPayload, error) {
  92. baseLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  93. forkLink := DiscordLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
  94. content := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
  95. return &DiscordPayload{
  96. Embeds: []*DiscordEmbedObject{{
  97. Description: content,
  98. URL: setting.AppUrl + p.Sender.UserName,
  99. Author: &DiscordEmbedAuthorObject{
  100. Name: p.Sender.UserName,
  101. IconURL: p.Sender.AvatarUrl,
  102. },
  103. }},
  104. }, nil
  105. }
  106. func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayload, error) {
  107. // n new commits
  108. var (
  109. branchName = git.RefEndName(p.Ref)
  110. commitDesc string
  111. commitString string
  112. )
  113. if len(p.Commits) == 1 {
  114. commitDesc = "1 new commit"
  115. } else {
  116. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  117. }
  118. if len(p.CompareURL) > 0 {
  119. commitString = DiscordLinkFormatter(p.CompareURL, commitDesc)
  120. } else {
  121. commitString = commitDesc
  122. }
  123. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  124. branchLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  125. content := fmt.Sprintf("Pushed %s to %s/%s\n", commitString, repoLink, branchLink)
  126. // for each commit, generate attachment text
  127. for i, commit := range p.Commits {
  128. content += fmt.Sprintf("%s %s - %s", DiscordSHALinkFormatter(commit.URL, commit.ID[:7]), DiscordTextFormatter(commit.Message), commit.Author.Name)
  129. // add linebreak to each commit but the last
  130. if i < len(p.Commits)-1 {
  131. content += "\n"
  132. }
  133. }
  134. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  135. return &DiscordPayload{
  136. Username: slack.Username,
  137. AvatarURL: slack.IconURL,
  138. Embeds: []*DiscordEmbedObject{{
  139. Description: content,
  140. URL: setting.AppUrl + p.Sender.UserName,
  141. Color: int(color),
  142. Author: &DiscordEmbedAuthorObject{
  143. Name: p.Sender.UserName,
  144. IconURL: p.Sender.AvatarUrl,
  145. },
  146. }},
  147. }, nil
  148. }
  149. func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*DiscordPayload, error) {
  150. title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  151. url := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  152. content := ""
  153. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  154. switch p.Action {
  155. case api.HOOK_ISSUE_OPENED:
  156. title = "New pull request: " + title
  157. content = p.PullRequest.Body
  158. case api.HOOK_ISSUE_CLOSED:
  159. if p.PullRequest.HasMerged {
  160. title = "Pull request merged: " + title
  161. } else {
  162. title = "Pull request closed: " + title
  163. }
  164. case api.HOOK_ISSUE_REOPENED:
  165. title = "Pull request re-opened: " + title
  166. case api.HOOK_ISSUE_EDITED:
  167. title = "Pull request edited: " + title
  168. content = p.PullRequest.Body
  169. case api.HOOK_ISSUE_ASSIGNED:
  170. title = "Pull request assigned: " + title
  171. fields = []*DiscordEmbedFieldObject{{
  172. Name: "New Assignee",
  173. Value: p.PullRequest.Assignee.UserName,
  174. }}
  175. case api.HOOK_ISSUE_UNASSIGNED:
  176. title = "Pull request unassigned: " + title
  177. case api.HOOK_ISSUE_LABEL_UPDATED:
  178. title = "Pull request labels updated: " + title
  179. labels := make([]string, len(p.PullRequest.Labels))
  180. for i := range p.PullRequest.Labels {
  181. labels[i] = p.PullRequest.Labels[i].Name
  182. }
  183. fields = []*DiscordEmbedFieldObject{{
  184. Name: "Labels",
  185. Value: strings.Join(labels, ", "),
  186. }}
  187. case api.HOOK_ISSUE_LABEL_CLEARED:
  188. title = "Pull request labels cleared: " + title
  189. case api.HOOK_ISSUE_SYNCHRONIZED:
  190. title = "Pull request synchronized: " + title
  191. }
  192. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  193. return &DiscordPayload{
  194. Username: slack.Username,
  195. AvatarURL: slack.IconURL,
  196. Embeds: []*DiscordEmbedObject{{
  197. Title: title,
  198. Description: content,
  199. URL: url,
  200. Color: int(color),
  201. Footer: &DiscordEmbedFooterObject{
  202. Text: p.Repository.FullName,
  203. },
  204. Author: &DiscordEmbedAuthorObject{
  205. Name: p.Sender.UserName,
  206. IconURL: p.Sender.AvatarUrl,
  207. },
  208. Fields: fields,
  209. }},
  210. }, nil
  211. }
  212. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
  213. slack := &SlackMeta{}
  214. if err := json.Unmarshal([]byte(meta), &slack); err != nil {
  215. return nil, fmt.Errorf("json.Unmarshal: %v", err)
  216. }
  217. switch event {
  218. case HOOK_EVENT_CREATE:
  219. payload, err = getDiscordCreatePayload(p.(*api.CreatePayload))
  220. case HOOK_EVENT_DELETE:
  221. payload, err = getDiscordDeletePayload(p.(*api.DeletePayload))
  222. case HOOK_EVENT_FORK:
  223. payload, err = getDiscordForkPayload(p.(*api.ForkPayload))
  224. case HOOK_EVENT_PUSH:
  225. payload, err = getDiscordPushPayload(p.(*api.PushPayload), slack)
  226. case HOOK_EVENT_PULL_REQUEST:
  227. payload, err = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
  228. }
  229. if err != nil {
  230. return nil, fmt.Errorf("event '%s': %v", event, err)
  231. }
  232. payload.Username = slack.Username
  233. payload.AvatarURL = slack.IconURL
  234. if len(payload.Embeds) > 0 {
  235. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  236. payload.Embeds[0].Color = int(color)
  237. }
  238. return payload, nil
  239. }