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

msteams.go 20 kB

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