webhook_discord.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 db
  5. import (
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. jsoniter "github.com/json-iterator/go"
  10. "github.com/pkg/errors"
  11. "github.com/gogs/git-module"
  12. api "github.com/gogs/go-gogs-client"
  13. "gogs.io/gogs/internal/conf"
  14. )
  15. type DiscordEmbedFooterObject struct {
  16. Text string `json:"text"`
  17. }
  18. type DiscordEmbedAuthorObject struct {
  19. Name string `json:"name"`
  20. URL string `json:"url"`
  21. IconURL string `json:"icon_url"`
  22. }
  23. type DiscordEmbedFieldObject struct {
  24. Name string `json:"name"`
  25. Value string `json:"value"`
  26. }
  27. type DiscordEmbedObject struct {
  28. Title string `json:"title"`
  29. Description string `json:"description"`
  30. URL string `json:"url"`
  31. Color int `json:"color"`
  32. Footer *DiscordEmbedFooterObject `json:"footer"`
  33. Author *DiscordEmbedAuthorObject `json:"author"`
  34. Fields []*DiscordEmbedFieldObject `json:"fields"`
  35. }
  36. type DiscordPayload struct {
  37. Content string `json:"content"`
  38. Username string `json:"username"`
  39. AvatarURL string `json:"avatar_url"`
  40. Embeds []*DiscordEmbedObject `json:"embeds"`
  41. }
  42. func (p *DiscordPayload) JSONPayload() ([]byte, error) {
  43. data, err := jsoniter.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, text string) string {
  53. return fmt.Sprintf("[%s](%s)", text, url)
  54. }
  55. func DiscordSHALinkFormatter(url, text string) string {
  56. return fmt.Sprintf("[`%s`](%s)", text, url)
  57. }
  58. // getDiscordCreatePayload composes Discord payload for create new branch or tag.
  59. func getDiscordCreatePayload(p *api.CreatePayload) *DiscordPayload {
  60. refName := git.RefShortName(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. return &DiscordPayload{
  65. Embeds: []*DiscordEmbedObject{{
  66. Description: content,
  67. URL: conf.Server.ExternalURL + p.Sender.UserName,
  68. Author: &DiscordEmbedAuthorObject{
  69. Name: p.Sender.UserName,
  70. IconURL: p.Sender.AvatarUrl,
  71. },
  72. }},
  73. }
  74. }
  75. // getDiscordDeletePayload composes Discord payload for delete a branch or tag.
  76. func getDiscordDeletePayload(p *api.DeletePayload) *DiscordPayload {
  77. refName := git.RefShortName(p.Ref)
  78. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  79. content := fmt.Sprintf("Deleted %s: %s/%s", p.RefType, repoLink, refName)
  80. return &DiscordPayload{
  81. Embeds: []*DiscordEmbedObject{{
  82. Description: content,
  83. URL: conf.Server.ExternalURL + p.Sender.UserName,
  84. Author: &DiscordEmbedAuthorObject{
  85. Name: p.Sender.UserName,
  86. IconURL: p.Sender.AvatarUrl,
  87. },
  88. }},
  89. }
  90. }
  91. // getDiscordForkPayload composes Discord payload for forked by a repository.
  92. func getDiscordForkPayload(p *api.ForkPayload) *DiscordPayload {
  93. baseLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  94. forkLink := DiscordLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
  95. content := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
  96. return &DiscordPayload{
  97. Embeds: []*DiscordEmbedObject{{
  98. Description: content,
  99. URL: conf.Server.ExternalURL + p.Sender.UserName,
  100. Author: &DiscordEmbedAuthorObject{
  101. Name: p.Sender.UserName,
  102. IconURL: p.Sender.AvatarUrl,
  103. },
  104. }},
  105. }
  106. }
  107. func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) *DiscordPayload {
  108. // n new commits
  109. var (
  110. branchName = git.RefShortName(p.Ref)
  111. commitDesc string
  112. commitString string
  113. )
  114. if len(p.Commits) == 1 {
  115. commitDesc = "1 new commit"
  116. } else {
  117. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  118. }
  119. if len(p.CompareURL) > 0 {
  120. commitString = DiscordLinkFormatter(p.CompareURL, commitDesc)
  121. } else {
  122. commitString = commitDesc
  123. }
  124. repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
  125. branchLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
  126. content := fmt.Sprintf("Pushed %s to %s/%s\n", commitString, repoLink, branchLink)
  127. // for each commit, generate attachment text
  128. for i, commit := range p.Commits {
  129. content += fmt.Sprintf("%s %s - %s", DiscordSHALinkFormatter(commit.URL, commit.ID[:7]), DiscordTextFormatter(commit.Message), commit.Author.Name)
  130. // add linebreak to each commit but the last
  131. if i < len(p.Commits)-1 {
  132. content += "\n"
  133. }
  134. }
  135. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  136. return &DiscordPayload{
  137. Username: slack.Username,
  138. AvatarURL: slack.IconURL,
  139. Embeds: []*DiscordEmbedObject{{
  140. Description: content,
  141. URL: conf.Server.ExternalURL + p.Sender.UserName,
  142. Color: int(color),
  143. Author: &DiscordEmbedAuthorObject{
  144. Name: p.Sender.UserName,
  145. IconURL: p.Sender.AvatarUrl,
  146. },
  147. }},
  148. }
  149. }
  150. func getDiscordIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) *DiscordPayload {
  151. title := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  152. url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
  153. content := ""
  154. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  155. switch p.Action {
  156. case api.HOOK_ISSUE_OPENED:
  157. title = "New issue: " + title
  158. content = p.Issue.Body
  159. case api.HOOK_ISSUE_CLOSED:
  160. title = "Issue closed: " + title
  161. case api.HOOK_ISSUE_REOPENED:
  162. title = "Issue re-opened: " + title
  163. case api.HOOK_ISSUE_EDITED:
  164. title = "Issue edited: " + title
  165. content = p.Issue.Body
  166. case api.HOOK_ISSUE_ASSIGNED:
  167. title = "Issue assigned: " + title
  168. fields = []*DiscordEmbedFieldObject{{
  169. Name: "New Assignee",
  170. Value: p.Issue.Assignee.UserName,
  171. }}
  172. case api.HOOK_ISSUE_UNASSIGNED:
  173. title = "Issue unassigned: " + title
  174. case api.HOOK_ISSUE_LABEL_UPDATED:
  175. title = "Issue labels updated: " + title
  176. labels := make([]string, len(p.Issue.Labels))
  177. for i := range p.Issue.Labels {
  178. labels[i] = p.Issue.Labels[i].Name
  179. }
  180. if len(labels) == 0 {
  181. labels = []string{"<empty>"}
  182. }
  183. fields = []*DiscordEmbedFieldObject{{
  184. Name: "Labels",
  185. Value: strings.Join(labels, ", "),
  186. }}
  187. case api.HOOK_ISSUE_LABEL_CLEARED:
  188. title = "Issue labels cleared: " + title
  189. case api.HOOK_ISSUE_SYNCHRONIZED:
  190. title = "Issue synchronized: " + title
  191. case api.HOOK_ISSUE_MILESTONED:
  192. title = "Issue milestoned: " + title
  193. fields = []*DiscordEmbedFieldObject{{
  194. Name: "New Milestone",
  195. Value: p.Issue.Milestone.Title,
  196. }}
  197. case api.HOOK_ISSUE_DEMILESTONED:
  198. title = "Issue demilestoned: " + title
  199. }
  200. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  201. return &DiscordPayload{
  202. Username: slack.Username,
  203. AvatarURL: slack.IconURL,
  204. Embeds: []*DiscordEmbedObject{{
  205. Title: title,
  206. Description: content,
  207. URL: url,
  208. Color: int(color),
  209. Footer: &DiscordEmbedFooterObject{
  210. Text: p.Repository.FullName,
  211. },
  212. Author: &DiscordEmbedAuthorObject{
  213. Name: p.Sender.UserName,
  214. IconURL: p.Sender.AvatarUrl,
  215. },
  216. Fields: fields,
  217. }},
  218. }
  219. }
  220. func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) *DiscordPayload {
  221. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  222. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  223. content := ""
  224. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  225. switch p.Action {
  226. case api.HOOK_ISSUE_COMMENT_CREATED:
  227. title = "New comment: " + title
  228. content = p.Comment.Body
  229. case api.HOOK_ISSUE_COMMENT_EDITED:
  230. title = "Comment edited: " + title
  231. content = p.Comment.Body
  232. case api.HOOK_ISSUE_COMMENT_DELETED:
  233. title = "Comment deleted: " + title
  234. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  235. content = p.Comment.Body
  236. }
  237. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  238. return &DiscordPayload{
  239. Username: slack.Username,
  240. AvatarURL: slack.IconURL,
  241. Embeds: []*DiscordEmbedObject{{
  242. Title: title,
  243. Description: content,
  244. URL: url,
  245. Color: int(color),
  246. Footer: &DiscordEmbedFooterObject{
  247. Text: p.Repository.FullName,
  248. },
  249. Author: &DiscordEmbedAuthorObject{
  250. Name: p.Sender.UserName,
  251. IconURL: p.Sender.AvatarUrl,
  252. },
  253. Fields: fields,
  254. }},
  255. }
  256. }
  257. func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) *DiscordPayload {
  258. title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  259. url := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  260. content := ""
  261. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  262. switch p.Action {
  263. case api.HOOK_ISSUE_OPENED:
  264. title = "New pull request: " + title
  265. content = p.PullRequest.Body
  266. case api.HOOK_ISSUE_CLOSED:
  267. if p.PullRequest.HasMerged {
  268. title = "Pull request merged: " + title
  269. } else {
  270. title = "Pull request closed: " + title
  271. }
  272. case api.HOOK_ISSUE_REOPENED:
  273. title = "Pull request re-opened: " + title
  274. case api.HOOK_ISSUE_EDITED:
  275. title = "Pull request edited: " + title
  276. content = p.PullRequest.Body
  277. case api.HOOK_ISSUE_ASSIGNED:
  278. title = "Pull request assigned: " + title
  279. fields = []*DiscordEmbedFieldObject{{
  280. Name: "New Assignee",
  281. Value: p.PullRequest.Assignee.UserName,
  282. }}
  283. case api.HOOK_ISSUE_UNASSIGNED:
  284. title = "Pull request unassigned: " + title
  285. case api.HOOK_ISSUE_LABEL_UPDATED:
  286. title = "Pull request labels updated: " + title
  287. labels := make([]string, len(p.PullRequest.Labels))
  288. for i := range p.PullRequest.Labels {
  289. labels[i] = p.PullRequest.Labels[i].Name
  290. }
  291. fields = []*DiscordEmbedFieldObject{{
  292. Name: "Labels",
  293. Value: strings.Join(labels, ", "),
  294. }}
  295. case api.HOOK_ISSUE_LABEL_CLEARED:
  296. title = "Pull request labels cleared: " + title
  297. case api.HOOK_ISSUE_SYNCHRONIZED:
  298. title = "Pull request synchronized: " + title
  299. case api.HOOK_ISSUE_MILESTONED:
  300. title = "Pull request milestoned: " + title
  301. fields = []*DiscordEmbedFieldObject{{
  302. Name: "New Milestone",
  303. Value: p.PullRequest.Milestone.Title,
  304. }}
  305. case api.HOOK_ISSUE_DEMILESTONED:
  306. title = "Pull request demilestoned: " + title
  307. }
  308. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  309. return &DiscordPayload{
  310. Username: slack.Username,
  311. AvatarURL: slack.IconURL,
  312. Embeds: []*DiscordEmbedObject{{
  313. Title: title,
  314. Description: content,
  315. URL: url,
  316. Color: int(color),
  317. Footer: &DiscordEmbedFooterObject{
  318. Text: p.Repository.FullName,
  319. },
  320. Author: &DiscordEmbedAuthorObject{
  321. Name: p.Sender.UserName,
  322. IconURL: p.Sender.AvatarUrl,
  323. },
  324. Fields: fields,
  325. }},
  326. }
  327. }
  328. func getDiscordReleasePayload(p *api.ReleasePayload) *DiscordPayload {
  329. repoLink := DiscordLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  330. refLink := DiscordLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
  331. content := fmt.Sprintf("Published new release %s of %s", refLink, repoLink)
  332. return &DiscordPayload{
  333. Embeds: []*DiscordEmbedObject{{
  334. Description: content,
  335. URL: conf.Server.ExternalURL + p.Sender.UserName,
  336. Author: &DiscordEmbedAuthorObject{
  337. Name: p.Sender.UserName,
  338. IconURL: p.Sender.AvatarUrl,
  339. },
  340. }},
  341. }
  342. }
  343. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
  344. slack := &SlackMeta{}
  345. if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil {
  346. return nil, fmt.Errorf("jsoniter.Unmarshal: %v", err)
  347. }
  348. switch event {
  349. case HOOK_EVENT_CREATE:
  350. payload = getDiscordCreatePayload(p.(*api.CreatePayload))
  351. case HOOK_EVENT_DELETE:
  352. payload = getDiscordDeletePayload(p.(*api.DeletePayload))
  353. case HOOK_EVENT_FORK:
  354. payload = getDiscordForkPayload(p.(*api.ForkPayload))
  355. case HOOK_EVENT_PUSH:
  356. payload = getDiscordPushPayload(p.(*api.PushPayload), slack)
  357. case HOOK_EVENT_ISSUES:
  358. payload = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
  359. case HOOK_EVENT_ISSUE_COMMENT:
  360. payload = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
  361. case HOOK_EVENT_PULL_REQUEST:
  362. payload = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
  363. case HOOK_EVENT_RELEASE:
  364. payload = getDiscordReleasePayload(p.(*api.ReleasePayload))
  365. default:
  366. return nil, errors.Errorf("unexpected event %q", event)
  367. }
  368. payload.Username = slack.Username
  369. payload.AvatarURL = slack.IconURL
  370. if len(payload.Embeds) > 0 {
  371. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  372. payload.Embeds[0].Color = int(color)
  373. }
  374. return payload, nil
  375. }