webhook_discord.go 13 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/gogs/git-module"
  11. api "github.com/gogs/go-gogs-client"
  12. "gogs.io/gogs/internal/conf"
  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 := jsoniter.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.RefShortName(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: conf.Server.ExternalURL + 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.RefShortName(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: conf.Server.ExternalURL + 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: conf.Server.ExternalURL + 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.RefShortName(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: conf.Server.ExternalURL + 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 getDiscordIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*DiscordPayload, error) {
  150. title := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  151. url := fmt.Sprintf("%s/issues/%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 issue: " + title
  157. content = p.Issue.Body
  158. case api.HOOK_ISSUE_CLOSED:
  159. title = "Issue closed: " + title
  160. case api.HOOK_ISSUE_REOPENED:
  161. title = "Issue re-opened: " + title
  162. case api.HOOK_ISSUE_EDITED:
  163. title = "Issue edited: " + title
  164. content = p.Issue.Body
  165. case api.HOOK_ISSUE_ASSIGNED:
  166. title = "Issue assigned: " + title
  167. fields = []*DiscordEmbedFieldObject{{
  168. Name: "New Assignee",
  169. Value: p.Issue.Assignee.UserName,
  170. }}
  171. case api.HOOK_ISSUE_UNASSIGNED:
  172. title = "Issue unassigned: " + title
  173. case api.HOOK_ISSUE_LABEL_UPDATED:
  174. title = "Issue labels updated: " + title
  175. labels := make([]string, len(p.Issue.Labels))
  176. for i := range p.Issue.Labels {
  177. labels[i] = p.Issue.Labels[i].Name
  178. }
  179. if len(labels) == 0 {
  180. labels = []string{"<empty>"}
  181. }
  182. fields = []*DiscordEmbedFieldObject{{
  183. Name: "Labels",
  184. Value: strings.Join(labels, ", "),
  185. }}
  186. case api.HOOK_ISSUE_LABEL_CLEARED:
  187. title = "Issue labels cleared: " + title
  188. case api.HOOK_ISSUE_SYNCHRONIZED:
  189. title = "Issue synchronized: " + title
  190. case api.HOOK_ISSUE_MILESTONED:
  191. title = "Issue milestoned: " + title
  192. fields = []*DiscordEmbedFieldObject{{
  193. Name: "New Milestone",
  194. Value: p.Issue.Milestone.Title,
  195. }}
  196. case api.HOOK_ISSUE_DEMILESTONED:
  197. title = "Issue demilestoned: " + title
  198. }
  199. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  200. return &DiscordPayload{
  201. Username: slack.Username,
  202. AvatarURL: slack.IconURL,
  203. Embeds: []*DiscordEmbedObject{{
  204. Title: title,
  205. Description: content,
  206. URL: url,
  207. Color: int(color),
  208. Footer: &DiscordEmbedFooterObject{
  209. Text: p.Repository.FullName,
  210. },
  211. Author: &DiscordEmbedAuthorObject{
  212. Name: p.Sender.UserName,
  213. IconURL: p.Sender.AvatarUrl,
  214. },
  215. Fields: fields,
  216. }},
  217. }, nil
  218. }
  219. func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*DiscordPayload, error) {
  220. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  221. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  222. content := ""
  223. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  224. switch p.Action {
  225. case api.HOOK_ISSUE_COMMENT_CREATED:
  226. title = "New comment: " + title
  227. content = p.Comment.Body
  228. case api.HOOK_ISSUE_COMMENT_EDITED:
  229. title = "Comment edited: " + title
  230. content = p.Comment.Body
  231. case api.HOOK_ISSUE_COMMENT_DELETED:
  232. title = "Comment deleted: " + title
  233. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  234. content = p.Comment.Body
  235. }
  236. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  237. return &DiscordPayload{
  238. Username: slack.Username,
  239. AvatarURL: slack.IconURL,
  240. Embeds: []*DiscordEmbedObject{{
  241. Title: title,
  242. Description: content,
  243. URL: url,
  244. Color: int(color),
  245. Footer: &DiscordEmbedFooterObject{
  246. Text: p.Repository.FullName,
  247. },
  248. Author: &DiscordEmbedAuthorObject{
  249. Name: p.Sender.UserName,
  250. IconURL: p.Sender.AvatarUrl,
  251. },
  252. Fields: fields,
  253. }},
  254. }, nil
  255. }
  256. func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*DiscordPayload, error) {
  257. title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  258. url := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  259. content := ""
  260. fields := make([]*DiscordEmbedFieldObject, 0, 1)
  261. switch p.Action {
  262. case api.HOOK_ISSUE_OPENED:
  263. title = "New pull request: " + title
  264. content = p.PullRequest.Body
  265. case api.HOOK_ISSUE_CLOSED:
  266. if p.PullRequest.HasMerged {
  267. title = "Pull request merged: " + title
  268. } else {
  269. title = "Pull request closed: " + title
  270. }
  271. case api.HOOK_ISSUE_REOPENED:
  272. title = "Pull request re-opened: " + title
  273. case api.HOOK_ISSUE_EDITED:
  274. title = "Pull request edited: " + title
  275. content = p.PullRequest.Body
  276. case api.HOOK_ISSUE_ASSIGNED:
  277. title = "Pull request assigned: " + title
  278. fields = []*DiscordEmbedFieldObject{{
  279. Name: "New Assignee",
  280. Value: p.PullRequest.Assignee.UserName,
  281. }}
  282. case api.HOOK_ISSUE_UNASSIGNED:
  283. title = "Pull request unassigned: " + title
  284. case api.HOOK_ISSUE_LABEL_UPDATED:
  285. title = "Pull request labels updated: " + title
  286. labels := make([]string, len(p.PullRequest.Labels))
  287. for i := range p.PullRequest.Labels {
  288. labels[i] = p.PullRequest.Labels[i].Name
  289. }
  290. fields = []*DiscordEmbedFieldObject{{
  291. Name: "Labels",
  292. Value: strings.Join(labels, ", "),
  293. }}
  294. case api.HOOK_ISSUE_LABEL_CLEARED:
  295. title = "Pull request labels cleared: " + title
  296. case api.HOOK_ISSUE_SYNCHRONIZED:
  297. title = "Pull request synchronized: " + title
  298. case api.HOOK_ISSUE_MILESTONED:
  299. title = "Pull request milestoned: " + title
  300. fields = []*DiscordEmbedFieldObject{{
  301. Name: "New Milestone",
  302. Value: p.PullRequest.Milestone.Title,
  303. }}
  304. case api.HOOK_ISSUE_DEMILESTONED:
  305. title = "Pull request demilestoned: " + title
  306. }
  307. color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
  308. return &DiscordPayload{
  309. Username: slack.Username,
  310. AvatarURL: slack.IconURL,
  311. Embeds: []*DiscordEmbedObject{{
  312. Title: title,
  313. Description: content,
  314. URL: url,
  315. Color: int(color),
  316. Footer: &DiscordEmbedFooterObject{
  317. Text: p.Repository.FullName,
  318. },
  319. Author: &DiscordEmbedAuthorObject{
  320. Name: p.Sender.UserName,
  321. IconURL: p.Sender.AvatarUrl,
  322. },
  323. Fields: fields,
  324. }},
  325. }, nil
  326. }
  327. func getDiscordReleasePayload(p *api.ReleasePayload) (*DiscordPayload, error) {
  328. repoLink := DiscordLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  329. refLink := DiscordLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
  330. content := fmt.Sprintf("Published new release %s of %s", refLink, repoLink)
  331. return &DiscordPayload{
  332. Embeds: []*DiscordEmbedObject{{
  333. Description: content,
  334. URL: conf.Server.ExternalURL + p.Sender.UserName,
  335. Author: &DiscordEmbedAuthorObject{
  336. Name: p.Sender.UserName,
  337. IconURL: p.Sender.AvatarUrl,
  338. },
  339. }},
  340. }, nil
  341. }
  342. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
  343. slack := &SlackMeta{}
  344. if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil {
  345. return nil, fmt.Errorf("jsoniter.Unmarshal: %v", err)
  346. }
  347. switch event {
  348. case HOOK_EVENT_CREATE:
  349. payload, err = getDiscordCreatePayload(p.(*api.CreatePayload))
  350. case HOOK_EVENT_DELETE:
  351. payload, err = getDiscordDeletePayload(p.(*api.DeletePayload))
  352. case HOOK_EVENT_FORK:
  353. payload, err = getDiscordForkPayload(p.(*api.ForkPayload))
  354. case HOOK_EVENT_PUSH:
  355. payload, err = getDiscordPushPayload(p.(*api.PushPayload), slack)
  356. case HOOK_EVENT_ISSUES:
  357. payload, err = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
  358. case HOOK_EVENT_ISSUE_COMMENT:
  359. payload, err = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
  360. case HOOK_EVENT_PULL_REQUEST:
  361. payload, err = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
  362. case HOOK_EVENT_RELEASE:
  363. payload, err = getDiscordReleasePayload(p.(*api.ReleasePayload))
  364. }
  365. if err != nil {
  366. return nil, fmt.Errorf("event '%s': %v", event, err)
  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. }