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.

webhook_msteams.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // Copyright 2019 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. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/modules/git"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. type (
  13. // MSTeamsFact for Fact Structure
  14. MSTeamsFact struct {
  15. Name string `json:"name"`
  16. Value string `json:"value"`
  17. }
  18. // MSTeamsSection is a MessageCard section
  19. MSTeamsSection struct {
  20. ActivityTitle string `json:"activityTitle"`
  21. ActivitySubtitle string `json:"activitySubtitle"`
  22. ActivityImage string `json:"activityImage"`
  23. Facts []MSTeamsFact `json:"facts"`
  24. Text string `json:"text"`
  25. }
  26. // MSTeamsAction is an action (creates buttons, links etc)
  27. MSTeamsAction struct {
  28. Type string `json:"@type"`
  29. Name string `json:"name"`
  30. Targets []MSTeamsActionTarget `json:"targets,omitempty"`
  31. }
  32. // MSTeamsActionTarget is the actual link to follow, etc
  33. MSTeamsActionTarget struct {
  34. Os string `json:"os"`
  35. URI string `json:"uri"`
  36. }
  37. // MSTeamsPayload is the parent object
  38. MSTeamsPayload struct {
  39. Type string `json:"@type"`
  40. Context string `json:"@context"`
  41. ThemeColor string `json:"themeColor"`
  42. Title string `json:"title"`
  43. Summary string `json:"summary"`
  44. Sections []MSTeamsSection `json:"sections"`
  45. PotentialAction []MSTeamsAction `json:"potentialAction"`
  46. }
  47. )
  48. // SetSecret sets the MSTeams secret
  49. func (p *MSTeamsPayload) SetSecret(_ string) {}
  50. // JSONPayload Marshals the MSTeamsPayload to json
  51. func (p *MSTeamsPayload) JSONPayload() ([]byte, error) {
  52. data, err := json.MarshalIndent(p, "", " ")
  53. if err != nil {
  54. return []byte{}, err
  55. }
  56. return data, nil
  57. }
  58. func getMSTeamsCreatePayload(p *api.CreatePayload) (*MSTeamsPayload, error) {
  59. // created tag/branch
  60. refName := git.RefEndName(p.Ref)
  61. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  62. return &MSTeamsPayload{
  63. Type: "MessageCard",
  64. Context: "https://schema.org/extensions",
  65. ThemeColor: fmt.Sprintf("%x", successColor),
  66. Title: title,
  67. Summary: title,
  68. Sections: []MSTeamsSection{
  69. {
  70. ActivityTitle: p.Sender.FullName,
  71. ActivitySubtitle: p.Sender.UserName,
  72. ActivityImage: p.Sender.AvatarURL,
  73. Facts: []MSTeamsFact{
  74. {
  75. Name: "Repository:",
  76. Value: p.Repo.FullName,
  77. },
  78. {
  79. Name: fmt.Sprintf("%s:", p.RefType),
  80. Value: refName,
  81. },
  82. },
  83. },
  84. },
  85. PotentialAction: []MSTeamsAction{
  86. {
  87. Type: "OpenUri",
  88. Name: "View in Gitea",
  89. Targets: []MSTeamsActionTarget{
  90. {
  91. Os: "default",
  92. URI: p.Repo.HTMLURL + "/src/" + refName,
  93. },
  94. },
  95. },
  96. },
  97. }, nil
  98. }
  99. func getMSTeamsDeletePayload(p *api.DeletePayload) (*MSTeamsPayload, error) {
  100. // deleted tag/branch
  101. refName := git.RefEndName(p.Ref)
  102. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  103. return &MSTeamsPayload{
  104. Type: "MessageCard",
  105. Context: "https://schema.org/extensions",
  106. ThemeColor: fmt.Sprintf("%x", warnColor),
  107. Title: title,
  108. Summary: title,
  109. Sections: []MSTeamsSection{
  110. {
  111. ActivityTitle: p.Sender.FullName,
  112. ActivitySubtitle: p.Sender.UserName,
  113. ActivityImage: p.Sender.AvatarURL,
  114. Facts: []MSTeamsFact{
  115. {
  116. Name: "Repository:",
  117. Value: p.Repo.FullName,
  118. },
  119. {
  120. Name: fmt.Sprintf("%s:", p.RefType),
  121. Value: refName,
  122. },
  123. },
  124. },
  125. },
  126. PotentialAction: []MSTeamsAction{
  127. {
  128. Type: "OpenUri",
  129. Name: "View in Gitea",
  130. Targets: []MSTeamsActionTarget{
  131. {
  132. Os: "default",
  133. URI: p.Repo.HTMLURL + "/src/" + refName,
  134. },
  135. },
  136. },
  137. },
  138. }, nil
  139. }
  140. func getMSTeamsForkPayload(p *api.ForkPayload) (*MSTeamsPayload, error) {
  141. // fork
  142. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  143. return &MSTeamsPayload{
  144. Type: "MessageCard",
  145. Context: "https://schema.org/extensions",
  146. ThemeColor: fmt.Sprintf("%x", successColor),
  147. Title: title,
  148. Summary: title,
  149. Sections: []MSTeamsSection{
  150. {
  151. ActivityTitle: p.Sender.FullName,
  152. ActivitySubtitle: p.Sender.UserName,
  153. ActivityImage: p.Sender.AvatarURL,
  154. Facts: []MSTeamsFact{
  155. {
  156. Name: "Forkee:",
  157. Value: p.Forkee.FullName,
  158. },
  159. {
  160. Name: "Repository:",
  161. Value: p.Repo.FullName,
  162. },
  163. },
  164. },
  165. },
  166. PotentialAction: []MSTeamsAction{
  167. {
  168. Type: "OpenUri",
  169. Name: "View in Gitea",
  170. Targets: []MSTeamsActionTarget{
  171. {
  172. Os: "default",
  173. URI: p.Repo.HTMLURL,
  174. },
  175. },
  176. },
  177. },
  178. }, nil
  179. }
  180. func getMSTeamsPushPayload(p *api.PushPayload) (*MSTeamsPayload, error) {
  181. var (
  182. branchName = git.RefEndName(p.Ref)
  183. commitDesc string
  184. )
  185. var titleLink string
  186. if len(p.Commits) == 1 {
  187. commitDesc = "1 new commit"
  188. titleLink = p.Commits[0].URL
  189. } else {
  190. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  191. titleLink = p.CompareURL
  192. }
  193. if titleLink == "" {
  194. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  195. }
  196. title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
  197. var text string
  198. // for each commit, generate attachment text
  199. for i, commit := range p.Commits {
  200. text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL,
  201. strings.TrimRight(commit.Message, "\r\n"), commit.Author.Name)
  202. // add linebreak to each commit but the last
  203. if i < len(p.Commits)-1 {
  204. text += "\n"
  205. }
  206. }
  207. return &MSTeamsPayload{
  208. Type: "MessageCard",
  209. Context: "https://schema.org/extensions",
  210. ThemeColor: fmt.Sprintf("%x", successColor),
  211. Title: title,
  212. Summary: title,
  213. Sections: []MSTeamsSection{
  214. {
  215. ActivityTitle: p.Sender.FullName,
  216. ActivitySubtitle: p.Sender.UserName,
  217. ActivityImage: p.Sender.AvatarURL,
  218. Text: text,
  219. Facts: []MSTeamsFact{
  220. {
  221. Name: "Repository:",
  222. Value: p.Repo.FullName,
  223. },
  224. {
  225. Name: "Commit count:",
  226. Value: fmt.Sprintf("%d", len(p.Commits)),
  227. },
  228. },
  229. },
  230. },
  231. PotentialAction: []MSTeamsAction{
  232. {
  233. Type: "OpenUri",
  234. Name: "View in Gitea",
  235. Targets: []MSTeamsActionTarget{
  236. {
  237. Os: "default",
  238. URI: titleLink,
  239. },
  240. },
  241. },
  242. },
  243. }, nil
  244. }
  245. func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
  246. var text, title string
  247. var color int
  248. url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  249. switch p.Action {
  250. case api.HookIssueOpened:
  251. title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  252. text = p.Issue.Body
  253. color = warnColor
  254. case api.HookIssueClosed:
  255. title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  256. color = failedColor
  257. text = p.Issue.Body
  258. case api.HookIssueReOpened:
  259. title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  260. text = p.Issue.Body
  261. color = warnColor
  262. case api.HookIssueEdited:
  263. title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  264. text = p.Issue.Body
  265. color = warnColor
  266. case api.HookIssueAssigned:
  267. title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
  268. p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
  269. text = p.Issue.Body
  270. color = successColor
  271. case api.HookIssueUnassigned:
  272. title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  273. text = p.Issue.Body
  274. color = warnColor
  275. case api.HookIssueLabelUpdated:
  276. title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  277. text = p.Issue.Body
  278. color = warnColor
  279. case api.HookIssueLabelCleared:
  280. title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  281. text = p.Issue.Body
  282. color = warnColor
  283. case api.HookIssueSynchronized:
  284. title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  285. text = p.Issue.Body
  286. color = warnColor
  287. case api.HookIssueMilestoned:
  288. title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  289. text = p.Issue.Body
  290. color = warnColor
  291. case api.HookIssueDemilestoned:
  292. title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  293. text = p.Issue.Body
  294. color = warnColor
  295. }
  296. return &MSTeamsPayload{
  297. Type: "MessageCard",
  298. Context: "https://schema.org/extensions",
  299. ThemeColor: fmt.Sprintf("%x", color),
  300. Title: title,
  301. Summary: title,
  302. Sections: []MSTeamsSection{
  303. {
  304. ActivityTitle: p.Sender.FullName,
  305. ActivitySubtitle: p.Sender.UserName,
  306. ActivityImage: p.Sender.AvatarURL,
  307. Text: text,
  308. Facts: []MSTeamsFact{
  309. {
  310. Name: "Repository:",
  311. Value: p.Repository.FullName,
  312. },
  313. {
  314. Name: "Issue #:",
  315. Value: fmt.Sprintf("%d", p.Issue.ID),
  316. },
  317. },
  318. },
  319. },
  320. PotentialAction: []MSTeamsAction{
  321. {
  322. Type: "OpenUri",
  323. Name: "View in Gitea",
  324. Targets: []MSTeamsActionTarget{
  325. {
  326. Os: "default",
  327. URI: url,
  328. },
  329. },
  330. },
  331. },
  332. }, nil
  333. }
  334. func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, error) {
  335. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  336. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  337. content := ""
  338. var color int
  339. switch p.Action {
  340. case api.HookIssueCommentCreated:
  341. title = "New comment: " + title
  342. content = p.Comment.Body
  343. color = successColor
  344. case api.HookIssueCommentEdited:
  345. title = "Comment edited: " + title
  346. content = p.Comment.Body
  347. color = warnColor
  348. case api.HookIssueCommentDeleted:
  349. title = "Comment deleted: " + title
  350. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  351. content = p.Comment.Body
  352. color = warnColor
  353. }
  354. return &MSTeamsPayload{
  355. Type: "MessageCard",
  356. Context: "https://schema.org/extensions",
  357. ThemeColor: fmt.Sprintf("%x", color),
  358. Title: title,
  359. Summary: title,
  360. Sections: []MSTeamsSection{
  361. {
  362. ActivityTitle: p.Sender.FullName,
  363. ActivitySubtitle: p.Sender.UserName,
  364. ActivityImage: p.Sender.AvatarURL,
  365. Text: content,
  366. Facts: []MSTeamsFact{
  367. {
  368. Name: "Repository:",
  369. Value: p.Repository.FullName,
  370. },
  371. {
  372. Name: "Issue #:",
  373. Value: fmt.Sprintf("%d", p.Issue.ID),
  374. },
  375. },
  376. },
  377. },
  378. PotentialAction: []MSTeamsAction{
  379. {
  380. Type: "OpenUri",
  381. Name: "View in Gitea",
  382. Targets: []MSTeamsActionTarget{
  383. {
  384. Os: "default",
  385. URI: url,
  386. },
  387. },
  388. },
  389. },
  390. }, nil
  391. }
  392. func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, error) {
  393. var text, title string
  394. var color int
  395. switch p.Action {
  396. case api.HookIssueOpened:
  397. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  398. text = p.PullRequest.Body
  399. color = warnColor
  400. case api.HookIssueClosed:
  401. if p.PullRequest.HasMerged {
  402. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  403. color = successColor
  404. } else {
  405. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  406. color = failedColor
  407. }
  408. text = p.PullRequest.Body
  409. case api.HookIssueReOpened:
  410. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  411. text = p.PullRequest.Body
  412. color = warnColor
  413. case api.HookIssueEdited:
  414. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  415. text = p.PullRequest.Body
  416. color = warnColor
  417. case api.HookIssueAssigned:
  418. list := make([]string, len(p.PullRequest.Assignees))
  419. for i, user := range p.PullRequest.Assignees {
  420. list[i] = user.UserName
  421. }
  422. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d by %s", p.Repository.FullName,
  423. strings.Join(list, ", "),
  424. p.Index, p.PullRequest.Title)
  425. text = p.PullRequest.Body
  426. color = successColor
  427. case api.HookIssueUnassigned:
  428. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  429. text = p.PullRequest.Body
  430. color = warnColor
  431. case api.HookIssueLabelUpdated:
  432. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  433. text = p.PullRequest.Body
  434. color = warnColor
  435. case api.HookIssueLabelCleared:
  436. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  437. text = p.PullRequest.Body
  438. color = warnColor
  439. case api.HookIssueSynchronized:
  440. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  441. text = p.PullRequest.Body
  442. color = warnColor
  443. case api.HookIssueMilestoned:
  444. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  445. text = p.PullRequest.Body
  446. color = warnColor
  447. case api.HookIssueDemilestoned:
  448. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  449. text = p.PullRequest.Body
  450. color = warnColor
  451. }
  452. return &MSTeamsPayload{
  453. Type: "MessageCard",
  454. Context: "https://schema.org/extensions",
  455. ThemeColor: fmt.Sprintf("%x", color),
  456. Title: title,
  457. Summary: title,
  458. Sections: []MSTeamsSection{
  459. {
  460. ActivityTitle: p.Sender.FullName,
  461. ActivitySubtitle: p.Sender.UserName,
  462. ActivityImage: p.Sender.AvatarURL,
  463. Text: text,
  464. Facts: []MSTeamsFact{
  465. {
  466. Name: "Repository:",
  467. Value: p.Repository.FullName,
  468. },
  469. {
  470. Name: "Pull request #:",
  471. Value: fmt.Sprintf("%d", p.PullRequest.ID),
  472. },
  473. },
  474. },
  475. },
  476. PotentialAction: []MSTeamsAction{
  477. {
  478. Type: "OpenUri",
  479. Name: "View in Gitea",
  480. Targets: []MSTeamsActionTarget{
  481. {
  482. Os: "default",
  483. URI: p.PullRequest.HTMLURL,
  484. },
  485. },
  486. },
  487. },
  488. }, nil
  489. }
  490. func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*MSTeamsPayload, error) {
  491. var text, title string
  492. var color int
  493. switch p.Action {
  494. case api.HookIssueSynchronized:
  495. action, err := parseHookPullRequestEventType(event)
  496. if err != nil {
  497. return nil, err
  498. }
  499. title = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  500. text = p.PullRequest.Body
  501. color = warnColor
  502. }
  503. return &MSTeamsPayload{
  504. Type: "MessageCard",
  505. Context: "https://schema.org/extensions",
  506. ThemeColor: fmt.Sprintf("%x", color),
  507. Title: title,
  508. Summary: title,
  509. Sections: []MSTeamsSection{
  510. {
  511. ActivityTitle: p.Sender.FullName,
  512. ActivitySubtitle: p.Sender.UserName,
  513. ActivityImage: p.Sender.AvatarURL,
  514. Text: text,
  515. Facts: []MSTeamsFact{
  516. {
  517. Name: "Repository:",
  518. Value: p.Repository.FullName,
  519. },
  520. {
  521. Name: "Pull request #:",
  522. Value: fmt.Sprintf("%d", p.PullRequest.ID),
  523. },
  524. },
  525. },
  526. },
  527. PotentialAction: []MSTeamsAction{
  528. {
  529. Type: "OpenUri",
  530. Name: "View in Gitea",
  531. Targets: []MSTeamsActionTarget{
  532. {
  533. Os: "default",
  534. URI: p.PullRequest.HTMLURL,
  535. },
  536. },
  537. },
  538. },
  539. }, nil
  540. }
  541. func getMSTeamsRepositoryPayload(p *api.RepositoryPayload) (*MSTeamsPayload, error) {
  542. var title, url string
  543. var color int
  544. switch p.Action {
  545. case api.HookRepoCreated:
  546. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  547. url = p.Repository.HTMLURL
  548. color = successColor
  549. case api.HookRepoDeleted:
  550. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  551. color = warnColor
  552. }
  553. return &MSTeamsPayload{
  554. Type: "MessageCard",
  555. Context: "https://schema.org/extensions",
  556. ThemeColor: fmt.Sprintf("%x", color),
  557. Title: title,
  558. Summary: title,
  559. Sections: []MSTeamsSection{
  560. {
  561. ActivityTitle: p.Sender.FullName,
  562. ActivitySubtitle: p.Sender.UserName,
  563. ActivityImage: p.Sender.AvatarURL,
  564. Facts: []MSTeamsFact{
  565. {
  566. Name: "Repository:",
  567. Value: p.Repository.FullName,
  568. },
  569. },
  570. },
  571. },
  572. PotentialAction: []MSTeamsAction{
  573. {
  574. Type: "OpenUri",
  575. Name: "View in Gitea",
  576. Targets: []MSTeamsActionTarget{
  577. {
  578. Os: "default",
  579. URI: url,
  580. },
  581. },
  582. },
  583. },
  584. }, nil
  585. }
  586. func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
  587. var title, url string
  588. var color int
  589. switch p.Action {
  590. case api.HookReleasePublished:
  591. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  592. url = p.Release.URL
  593. color = successColor
  594. case api.HookReleaseUpdated:
  595. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  596. url = p.Release.URL
  597. color = successColor
  598. case api.HookReleaseDeleted:
  599. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  600. url = p.Release.URL
  601. color = successColor
  602. }
  603. return &MSTeamsPayload{
  604. Type: "MessageCard",
  605. Context: "https://schema.org/extensions",
  606. ThemeColor: fmt.Sprintf("%x", color),
  607. Title: title,
  608. Summary: title,
  609. Sections: []MSTeamsSection{
  610. {
  611. ActivityTitle: p.Sender.FullName,
  612. ActivitySubtitle: p.Sender.UserName,
  613. ActivityImage: p.Sender.AvatarURL,
  614. Text: p.Release.Note,
  615. Facts: []MSTeamsFact{
  616. {
  617. Name: "Repository:",
  618. Value: p.Repository.FullName,
  619. },
  620. {
  621. Name: "Tag:",
  622. Value: p.Release.TagName,
  623. },
  624. },
  625. },
  626. },
  627. PotentialAction: []MSTeamsAction{
  628. {
  629. Type: "OpenUri",
  630. Name: "View in Gitea",
  631. Targets: []MSTeamsActionTarget{
  632. {
  633. Os: "default",
  634. URI: url,
  635. },
  636. },
  637. },
  638. },
  639. }, nil
  640. }
  641. // GetMSTeamsPayload converts a MSTeams webhook into a MSTeamsPayload
  642. func GetMSTeamsPayload(p api.Payloader, event HookEventType, meta string) (*MSTeamsPayload, error) {
  643. s := new(MSTeamsPayload)
  644. switch event {
  645. case HookEventCreate:
  646. return getMSTeamsCreatePayload(p.(*api.CreatePayload))
  647. case HookEventDelete:
  648. return getMSTeamsDeletePayload(p.(*api.DeletePayload))
  649. case HookEventFork:
  650. return getMSTeamsForkPayload(p.(*api.ForkPayload))
  651. case HookEventIssues:
  652. return getMSTeamsIssuesPayload(p.(*api.IssuePayload))
  653. case HookEventIssueComment:
  654. return getMSTeamsIssueCommentPayload(p.(*api.IssueCommentPayload))
  655. case HookEventPush:
  656. return getMSTeamsPushPayload(p.(*api.PushPayload))
  657. case HookEventPullRequest:
  658. return getMSTeamsPullRequestPayload(p.(*api.PullRequestPayload))
  659. case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
  660. return getMSTeamsPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
  661. case HookEventRepository:
  662. return getMSTeamsRepositoryPayload(p.(*api.RepositoryPayload))
  663. case HookEventRelease:
  664. return getMSTeamsReleasePayload(p.(*api.ReleasePayload))
  665. }
  666. return s, nil
  667. }