You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

554 lines
16 KiB

  1. // Copyright 2017 The Gitea 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. "errors"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/sdk/gitea"
  14. )
  15. type (
  16. // DiscordEmbedFooter for Embed Footer Structure.
  17. DiscordEmbedFooter struct {
  18. Text string `json:"text"`
  19. }
  20. // DiscordEmbedAuthor for Embed Author Structure
  21. DiscordEmbedAuthor struct {
  22. Name string `json:"name"`
  23. URL string `json:"url"`
  24. IconURL string `json:"icon_url"`
  25. }
  26. // DiscordEmbedField for Embed Field Structure
  27. DiscordEmbedField struct {
  28. Name string `json:"name"`
  29. Value string `json:"value"`
  30. }
  31. // DiscordEmbed is for Embed Structure
  32. DiscordEmbed struct {
  33. Title string `json:"title"`
  34. Description string `json:"description"`
  35. URL string `json:"url"`
  36. Color int `json:"color"`
  37. Footer DiscordEmbedFooter `json:"footer"`
  38. Author DiscordEmbedAuthor `json:"author"`
  39. Fields []DiscordEmbedField `json:"fields"`
  40. }
  41. // DiscordPayload represents
  42. DiscordPayload struct {
  43. Wait bool `json:"wait"`
  44. Content string `json:"content"`
  45. Username string `json:"username"`
  46. AvatarURL string `json:"avatar_url"`
  47. TTS bool `json:"tts"`
  48. Embeds []DiscordEmbed `json:"embeds"`
  49. }
  50. // DiscordMeta contains the discord metadata
  51. DiscordMeta struct {
  52. Username string `json:"username"`
  53. IconURL string `json:"icon_url"`
  54. }
  55. )
  56. func color(clr string) int {
  57. if clr != "" {
  58. clr = strings.TrimLeft(clr, "#")
  59. if s, err := strconv.ParseInt(clr, 16, 32); err == nil {
  60. return int(s)
  61. }
  62. }
  63. return 0
  64. }
  65. var (
  66. successColor = color("1ac600")
  67. warnColor = color("ffd930")
  68. failedColor = color("ff3232")
  69. )
  70. // SetSecret sets the discord secret
  71. func (p *DiscordPayload) SetSecret(_ string) {}
  72. // JSONPayload Marshals the DiscordPayload to json
  73. func (p *DiscordPayload) JSONPayload() ([]byte, error) {
  74. data, err := json.MarshalIndent(p, "", " ")
  75. if err != nil {
  76. return []byte{}, err
  77. }
  78. return data, nil
  79. }
  80. func getDiscordCreatePayload(p *api.CreatePayload, meta *DiscordMeta) (*DiscordPayload, error) {
  81. // created tag/branch
  82. refName := git.RefEndName(p.Ref)
  83. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  84. return &DiscordPayload{
  85. Username: meta.Username,
  86. AvatarURL: meta.IconURL,
  87. Embeds: []DiscordEmbed{
  88. {
  89. Title: title,
  90. URL: p.Repo.HTMLURL + "/src/" + refName,
  91. Color: successColor,
  92. Author: DiscordEmbedAuthor{
  93. Name: p.Sender.UserName,
  94. URL: setting.AppURL + p.Sender.UserName,
  95. IconURL: p.Sender.AvatarURL,
  96. },
  97. },
  98. },
  99. }, nil
  100. }
  101. func getDiscordDeletePayload(p *api.DeletePayload, meta *DiscordMeta) (*DiscordPayload, error) {
  102. // deleted tag/branch
  103. refName := git.RefEndName(p.Ref)
  104. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  105. return &DiscordPayload{
  106. Username: meta.Username,
  107. AvatarURL: meta.IconURL,
  108. Embeds: []DiscordEmbed{
  109. {
  110. Title: title,
  111. URL: p.Repo.HTMLURL + "/src/" + refName,
  112. Color: warnColor,
  113. Author: DiscordEmbedAuthor{
  114. Name: p.Sender.UserName,
  115. URL: setting.AppURL + p.Sender.UserName,
  116. IconURL: p.Sender.AvatarURL,
  117. },
  118. },
  119. },
  120. }, nil
  121. }
  122. func getDiscordForkPayload(p *api.ForkPayload, meta *DiscordMeta) (*DiscordPayload, error) {
  123. // fork
  124. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  125. return &DiscordPayload{
  126. Username: meta.Username,
  127. AvatarURL: meta.IconURL,
  128. Embeds: []DiscordEmbed{
  129. {
  130. Title: title,
  131. URL: p.Repo.HTMLURL,
  132. Color: successColor,
  133. Author: DiscordEmbedAuthor{
  134. Name: p.Sender.UserName,
  135. URL: setting.AppURL + p.Sender.UserName,
  136. IconURL: p.Sender.AvatarURL,
  137. },
  138. },
  139. },
  140. }, nil
  141. }
  142. func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPayload, error) {
  143. var (
  144. branchName = git.RefEndName(p.Ref)
  145. commitDesc string
  146. )
  147. var titleLink string
  148. if len(p.Commits) == 1 {
  149. commitDesc = "1 new commit"
  150. titleLink = p.Commits[0].URL
  151. } else {
  152. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  153. titleLink = p.CompareURL
  154. }
  155. if titleLink == "" {
  156. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  157. }
  158. title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
  159. var text string
  160. // for each commit, generate attachment text
  161. for i, commit := range p.Commits {
  162. text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL,
  163. strings.TrimRight(commit.Message, "\r\n"), commit.Author.Name)
  164. // add linebreak to each commit but the last
  165. if i < len(p.Commits)-1 {
  166. text += "\n"
  167. }
  168. }
  169. return &DiscordPayload{
  170. Username: meta.Username,
  171. AvatarURL: meta.IconURL,
  172. Embeds: []DiscordEmbed{
  173. {
  174. Title: title,
  175. Description: text,
  176. URL: titleLink,
  177. Color: successColor,
  178. Author: DiscordEmbedAuthor{
  179. Name: p.Sender.UserName,
  180. URL: setting.AppURL + p.Sender.UserName,
  181. IconURL: p.Sender.AvatarURL,
  182. },
  183. },
  184. },
  185. }, nil
  186. }
  187. func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPayload, error) {
  188. var text, title string
  189. var color int
  190. url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  191. switch p.Action {
  192. case api.HookIssueOpened:
  193. title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  194. text = p.Issue.Body
  195. color = warnColor
  196. case api.HookIssueClosed:
  197. title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  198. color = failedColor
  199. text = p.Issue.Body
  200. case api.HookIssueReOpened:
  201. title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  202. text = p.Issue.Body
  203. color = warnColor
  204. case api.HookIssueEdited:
  205. title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  206. text = p.Issue.Body
  207. color = warnColor
  208. case api.HookIssueAssigned:
  209. title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
  210. p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
  211. text = p.Issue.Body
  212. color = successColor
  213. case api.HookIssueUnassigned:
  214. title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  215. text = p.Issue.Body
  216. color = warnColor
  217. case api.HookIssueLabelUpdated:
  218. title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  219. text = p.Issue.Body
  220. color = warnColor
  221. case api.HookIssueLabelCleared:
  222. title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  223. text = p.Issue.Body
  224. color = warnColor
  225. case api.HookIssueSynchronized:
  226. title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  227. text = p.Issue.Body
  228. color = warnColor
  229. case api.HookIssueMilestoned:
  230. title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  231. text = p.Issue.Body
  232. color = warnColor
  233. case api.HookIssueDemilestoned:
  234. title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  235. text = p.Issue.Body
  236. color = warnColor
  237. }
  238. return &DiscordPayload{
  239. Username: meta.Username,
  240. AvatarURL: meta.IconURL,
  241. Embeds: []DiscordEmbed{
  242. {
  243. Title: title,
  244. Description: text,
  245. URL: url,
  246. Color: color,
  247. Author: DiscordEmbedAuthor{
  248. Name: p.Sender.UserName,
  249. URL: setting.AppURL + p.Sender.UserName,
  250. IconURL: p.Sender.AvatarURL,
  251. },
  252. },
  253. },
  254. }, nil
  255. }
  256. func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) {
  257. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  258. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  259. content := ""
  260. var color int
  261. switch p.Action {
  262. case api.HookIssueCommentCreated:
  263. title = "New comment: " + title
  264. content = p.Comment.Body
  265. color = successColor
  266. case api.HookIssueCommentEdited:
  267. title = "Comment edited: " + title
  268. content = p.Comment.Body
  269. color = warnColor
  270. case api.HookIssueCommentDeleted:
  271. title = "Comment deleted: " + title
  272. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  273. content = p.Comment.Body
  274. color = warnColor
  275. }
  276. return &DiscordPayload{
  277. Username: discord.Username,
  278. AvatarURL: discord.IconURL,
  279. Embeds: []DiscordEmbed{
  280. {
  281. Title: title,
  282. Description: content,
  283. URL: url,
  284. Color: color,
  285. Author: DiscordEmbedAuthor{
  286. Name: p.Sender.UserName,
  287. URL: setting.AppURL + p.Sender.UserName,
  288. IconURL: p.Sender.AvatarURL,
  289. },
  290. },
  291. },
  292. }, nil
  293. }
  294. func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta) (*DiscordPayload, error) {
  295. var text, title string
  296. var color int
  297. switch p.Action {
  298. case api.HookIssueOpened:
  299. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  300. text = p.PullRequest.Body
  301. color = warnColor
  302. case api.HookIssueClosed:
  303. if p.PullRequest.HasMerged {
  304. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  305. color = successColor
  306. } else {
  307. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  308. color = failedColor
  309. }
  310. text = p.PullRequest.Body
  311. case api.HookIssueReOpened:
  312. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  313. text = p.PullRequest.Body
  314. color = warnColor
  315. case api.HookIssueEdited:
  316. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  317. text = p.PullRequest.Body
  318. color = warnColor
  319. case api.HookIssueAssigned:
  320. list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID})
  321. if err != nil {
  322. return &DiscordPayload{}, err
  323. }
  324. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  325. list, p.Index, p.PullRequest.Title)
  326. text = p.PullRequest.Body
  327. color = successColor
  328. case api.HookIssueUnassigned:
  329. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  330. text = p.PullRequest.Body
  331. color = warnColor
  332. case api.HookIssueLabelUpdated:
  333. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  334. text = p.PullRequest.Body
  335. color = warnColor
  336. case api.HookIssueLabelCleared:
  337. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  338. text = p.PullRequest.Body
  339. color = warnColor
  340. case api.HookIssueSynchronized:
  341. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  342. text = p.PullRequest.Body
  343. color = warnColor
  344. case api.HookIssueMilestoned:
  345. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  346. text = p.PullRequest.Body
  347. color = warnColor
  348. case api.HookIssueDemilestoned:
  349. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  350. text = p.PullRequest.Body
  351. color = warnColor
  352. }
  353. return &DiscordPayload{
  354. Username: meta.Username,
  355. AvatarURL: meta.IconURL,
  356. Embeds: []DiscordEmbed{
  357. {
  358. Title: title,
  359. Description: text,
  360. URL: p.PullRequest.HTMLURL,
  361. Color: color,
  362. Author: DiscordEmbedAuthor{
  363. Name: p.Sender.UserName,
  364. URL: setting.AppURL + p.Sender.UserName,
  365. IconURL: p.Sender.AvatarURL,
  366. },
  367. },
  368. },
  369. }, nil
  370. }
  371. func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *DiscordMeta, event HookEventType) (*DiscordPayload, error) {
  372. var text, title string
  373. var color int
  374. switch p.Action {
  375. case api.HookIssueSynchronized:
  376. action, err := parseHookPullRequestEventType(event)
  377. if err != nil {
  378. return nil, err
  379. }
  380. title = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  381. text = p.PullRequest.Body
  382. color = warnColor
  383. }
  384. return &DiscordPayload{
  385. Username: meta.Username,
  386. AvatarURL: meta.IconURL,
  387. Embeds: []DiscordEmbed{
  388. {
  389. Title: title,
  390. Description: text,
  391. URL: p.PullRequest.HTMLURL,
  392. Color: color,
  393. Author: DiscordEmbedAuthor{
  394. Name: p.Sender.UserName,
  395. URL: setting.AppURL + p.Sender.UserName,
  396. IconURL: p.Sender.AvatarURL,
  397. },
  398. },
  399. },
  400. }, nil
  401. }
  402. func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*DiscordPayload, error) {
  403. var title, url string
  404. var color int
  405. switch p.Action {
  406. case api.HookRepoCreated:
  407. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  408. url = p.Repository.HTMLURL
  409. color = successColor
  410. case api.HookRepoDeleted:
  411. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  412. color = warnColor
  413. }
  414. return &DiscordPayload{
  415. Username: meta.Username,
  416. AvatarURL: meta.IconURL,
  417. Embeds: []DiscordEmbed{
  418. {
  419. Title: title,
  420. URL: url,
  421. Color: color,
  422. Author: DiscordEmbedAuthor{
  423. Name: p.Sender.UserName,
  424. URL: setting.AppURL + p.Sender.UserName,
  425. IconURL: p.Sender.AvatarURL,
  426. },
  427. },
  428. },
  429. }, nil
  430. }
  431. func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*DiscordPayload, error) {
  432. var title, url string
  433. var color int
  434. switch p.Action {
  435. case api.HookReleasePublished:
  436. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  437. url = p.Release.URL
  438. color = successColor
  439. case api.HookReleaseUpdated:
  440. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  441. url = p.Release.URL
  442. color = successColor
  443. case api.HookReleaseDeleted:
  444. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  445. url = p.Release.URL
  446. color = successColor
  447. }
  448. return &DiscordPayload{
  449. Username: meta.Username,
  450. AvatarURL: meta.IconURL,
  451. Embeds: []DiscordEmbed{
  452. {
  453. Title: title,
  454. Description: fmt.Sprintf("%s", p.Release.Note),
  455. URL: url,
  456. Color: color,
  457. Author: DiscordEmbedAuthor{
  458. Name: p.Sender.UserName,
  459. URL: setting.AppURL + p.Sender.UserName,
  460. IconURL: p.Sender.AvatarURL,
  461. },
  462. },
  463. },
  464. }, nil
  465. }
  466. // GetDiscordPayload converts a discord webhook into a DiscordPayload
  467. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*DiscordPayload, error) {
  468. s := new(DiscordPayload)
  469. discord := &DiscordMeta{}
  470. if err := json.Unmarshal([]byte(meta), &discord); err != nil {
  471. return s, errors.New("GetDiscordPayload meta json:" + err.Error())
  472. }
  473. switch event {
  474. case HookEventCreate:
  475. return getDiscordCreatePayload(p.(*api.CreatePayload), discord)
  476. case HookEventDelete:
  477. return getDiscordDeletePayload(p.(*api.DeletePayload), discord)
  478. case HookEventFork:
  479. return getDiscordForkPayload(p.(*api.ForkPayload), discord)
  480. case HookEventIssues:
  481. return getDiscordIssuesPayload(p.(*api.IssuePayload), discord)
  482. case HookEventIssueComment:
  483. return getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), discord)
  484. case HookEventPush:
  485. return getDiscordPushPayload(p.(*api.PushPayload), discord)
  486. case HookEventPullRequest:
  487. return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
  488. case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
  489. return getDiscordPullRequestApprovalPayload(p.(*api.PullRequestPayload), discord, event)
  490. case HookEventRepository:
  491. return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
  492. case HookEventRelease:
  493. return getDiscordReleasePayload(p.(*api.ReleasePayload), discord)
  494. }
  495. return s, nil
  496. }
  497. func parseHookPullRequestEventType(event HookEventType) (string, error) {
  498. switch event {
  499. case HookEventPullRequestApproved:
  500. return "approved", nil
  501. case HookEventPullRequestRejected:
  502. return "rejected", nil
  503. case HookEventPullRequestComment:
  504. return "comment", nil
  505. default:
  506. return "", errors.New("unknown event type")
  507. }
  508. }