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.

502 lines
15 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 getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*DiscordPayload, error) {
  372. var title, url string
  373. var color int
  374. switch p.Action {
  375. case api.HookRepoCreated:
  376. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  377. url = p.Repository.HTMLURL
  378. color = successColor
  379. case api.HookRepoDeleted:
  380. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  381. color = warnColor
  382. }
  383. return &DiscordPayload{
  384. Username: meta.Username,
  385. AvatarURL: meta.IconURL,
  386. Embeds: []DiscordEmbed{
  387. {
  388. Title: title,
  389. URL: url,
  390. Color: color,
  391. Author: DiscordEmbedAuthor{
  392. Name: p.Sender.UserName,
  393. URL: setting.AppURL + p.Sender.UserName,
  394. IconURL: p.Sender.AvatarURL,
  395. },
  396. },
  397. },
  398. }, nil
  399. }
  400. func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*DiscordPayload, error) {
  401. var title, url string
  402. var color int
  403. switch p.Action {
  404. case api.HookReleasePublished:
  405. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  406. url = p.Release.URL
  407. color = successColor
  408. case api.HookReleaseUpdated:
  409. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  410. url = p.Release.URL
  411. color = successColor
  412. case api.HookReleaseDeleted:
  413. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  414. url = p.Release.URL
  415. color = successColor
  416. }
  417. return &DiscordPayload{
  418. Username: meta.Username,
  419. AvatarURL: meta.IconURL,
  420. Embeds: []DiscordEmbed{
  421. {
  422. Title: title,
  423. Description: fmt.Sprintf("%s", p.Release.Note),
  424. URL: url,
  425. Color: color,
  426. Author: DiscordEmbedAuthor{
  427. Name: p.Sender.UserName,
  428. URL: setting.AppURL + p.Sender.UserName,
  429. IconURL: p.Sender.AvatarURL,
  430. },
  431. },
  432. },
  433. }, nil
  434. }
  435. // GetDiscordPayload converts a discord webhook into a DiscordPayload
  436. func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*DiscordPayload, error) {
  437. s := new(DiscordPayload)
  438. discord := &DiscordMeta{}
  439. if err := json.Unmarshal([]byte(meta), &discord); err != nil {
  440. return s, errors.New("GetDiscordPayload meta json:" + err.Error())
  441. }
  442. switch event {
  443. case HookEventCreate:
  444. return getDiscordCreatePayload(p.(*api.CreatePayload), discord)
  445. case HookEventDelete:
  446. return getDiscordDeletePayload(p.(*api.DeletePayload), discord)
  447. case HookEventFork:
  448. return getDiscordForkPayload(p.(*api.ForkPayload), discord)
  449. case HookEventIssues:
  450. return getDiscordIssuesPayload(p.(*api.IssuePayload), discord)
  451. case HookEventIssueComment:
  452. return getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), discord)
  453. case HookEventPush:
  454. return getDiscordPushPayload(p.(*api.PushPayload), discord)
  455. case HookEventPullRequest:
  456. return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
  457. case HookEventRepository:
  458. return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
  459. case HookEventRelease:
  460. return getDiscordReleasePayload(p.(*api.ReleasePayload), discord)
  461. }
  462. return s, nil
  463. }