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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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/sdk/gitea"
  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. Facts: []MSTeamsFact{
  219. {
  220. Name: "Repository:",
  221. Value: p.Repo.FullName,
  222. },
  223. {
  224. Name: "Commit count:",
  225. Value: fmt.Sprintf("%d", len(p.Commits)),
  226. },
  227. },
  228. },
  229. },
  230. PotentialAction: []MSTeamsAction{
  231. {
  232. Type: "OpenUri",
  233. Name: "View in Gitea",
  234. Targets: []MSTeamsActionTarget{
  235. {
  236. Os: "default",
  237. URI: titleLink,
  238. },
  239. },
  240. },
  241. },
  242. }, nil
  243. }
  244. func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
  245. var text, title string
  246. var color int
  247. url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  248. switch p.Action {
  249. case api.HookIssueOpened:
  250. title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  251. text = p.Issue.Body
  252. color = warnColor
  253. case api.HookIssueClosed:
  254. title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  255. color = failedColor
  256. text = p.Issue.Body
  257. case api.HookIssueReOpened:
  258. title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  259. text = p.Issue.Body
  260. color = warnColor
  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 = warnColor
  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. text = p.Issue.Body
  269. color = successColor
  270. case api.HookIssueUnassigned:
  271. title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  272. text = p.Issue.Body
  273. color = warnColor
  274. case api.HookIssueLabelUpdated:
  275. title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  276. text = p.Issue.Body
  277. color = warnColor
  278. case api.HookIssueLabelCleared:
  279. title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  280. text = p.Issue.Body
  281. color = warnColor
  282. case api.HookIssueSynchronized:
  283. title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  284. text = p.Issue.Body
  285. color = warnColor
  286. case api.HookIssueMilestoned:
  287. title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  288. text = p.Issue.Body
  289. color = warnColor
  290. case api.HookIssueDemilestoned:
  291. title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  292. text = p.Issue.Body
  293. color = warnColor
  294. }
  295. return &MSTeamsPayload{
  296. Type: "MessageCard",
  297. Context: "https://schema.org/extensions",
  298. ThemeColor: fmt.Sprintf("%x", color),
  299. Title: title,
  300. Summary: title,
  301. Sections: []MSTeamsSection{
  302. {
  303. ActivityTitle: p.Sender.FullName,
  304. ActivitySubtitle: p.Sender.UserName,
  305. ActivityImage: p.Sender.AvatarURL,
  306. Text: text,
  307. Facts: []MSTeamsFact{
  308. {
  309. Name: "Repository:",
  310. Value: p.Repository.FullName,
  311. },
  312. {
  313. Name: "Issue #:",
  314. Value: fmt.Sprintf("%d", p.Issue.ID),
  315. },
  316. },
  317. },
  318. },
  319. PotentialAction: []MSTeamsAction{
  320. {
  321. Type: "OpenUri",
  322. Name: "View in Gitea",
  323. Targets: []MSTeamsActionTarget{
  324. {
  325. Os: "default",
  326. URI: url,
  327. },
  328. },
  329. },
  330. },
  331. }, nil
  332. }
  333. func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, error) {
  334. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  335. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  336. content := ""
  337. var color int
  338. switch p.Action {
  339. case api.HookIssueCommentCreated:
  340. title = "New comment: " + title
  341. content = p.Comment.Body
  342. color = successColor
  343. case api.HookIssueCommentEdited:
  344. title = "Comment edited: " + title
  345. content = p.Comment.Body
  346. color = warnColor
  347. case api.HookIssueCommentDeleted:
  348. title = "Comment deleted: " + title
  349. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  350. content = p.Comment.Body
  351. color = warnColor
  352. }
  353. return &MSTeamsPayload{
  354. Type: "MessageCard",
  355. Context: "https://schema.org/extensions",
  356. ThemeColor: fmt.Sprintf("%x", color),
  357. Title: title,
  358. Summary: title,
  359. Sections: []MSTeamsSection{
  360. {
  361. ActivityTitle: p.Sender.FullName,
  362. ActivitySubtitle: p.Sender.UserName,
  363. ActivityImage: p.Sender.AvatarURL,
  364. Text: content,
  365. Facts: []MSTeamsFact{
  366. {
  367. Name: "Repository:",
  368. Value: p.Repository.FullName,
  369. },
  370. {
  371. Name: "Issue #:",
  372. Value: fmt.Sprintf("%d", p.Issue.ID),
  373. },
  374. },
  375. },
  376. },
  377. PotentialAction: []MSTeamsAction{
  378. {
  379. Type: "OpenUri",
  380. Name: "View in Gitea",
  381. Targets: []MSTeamsActionTarget{
  382. {
  383. Os: "default",
  384. URI: url,
  385. },
  386. },
  387. },
  388. },
  389. }, nil
  390. }
  391. func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, error) {
  392. var text, title string
  393. var color int
  394. switch p.Action {
  395. case api.HookIssueOpened:
  396. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  397. text = p.PullRequest.Body
  398. color = warnColor
  399. case api.HookIssueClosed:
  400. if p.PullRequest.HasMerged {
  401. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  402. color = successColor
  403. } else {
  404. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  405. color = failedColor
  406. }
  407. text = p.PullRequest.Body
  408. case api.HookIssueReOpened:
  409. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  410. text = p.PullRequest.Body
  411. color = warnColor
  412. case api.HookIssueEdited:
  413. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  414. text = p.PullRequest.Body
  415. color = warnColor
  416. case api.HookIssueAssigned:
  417. list := make([]string, len(p.PullRequest.Assignees))
  418. for i, user := range p.PullRequest.Assignees {
  419. list[i] = user.UserName
  420. }
  421. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d by %s", p.Repository.FullName,
  422. strings.Join(list, ", "),
  423. p.Index, p.PullRequest.Title)
  424. text = p.PullRequest.Body
  425. color = successColor
  426. case api.HookIssueUnassigned:
  427. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  428. text = p.PullRequest.Body
  429. color = warnColor
  430. case api.HookIssueLabelUpdated:
  431. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  432. text = p.PullRequest.Body
  433. color = warnColor
  434. case api.HookIssueLabelCleared:
  435. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  436. text = p.PullRequest.Body
  437. color = warnColor
  438. case api.HookIssueSynchronized:
  439. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  440. text = p.PullRequest.Body
  441. color = warnColor
  442. case api.HookIssueMilestoned:
  443. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  444. text = p.PullRequest.Body
  445. color = warnColor
  446. case api.HookIssueDemilestoned:
  447. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  448. text = p.PullRequest.Body
  449. color = warnColor
  450. }
  451. return &MSTeamsPayload{
  452. Type: "MessageCard",
  453. Context: "https://schema.org/extensions",
  454. ThemeColor: fmt.Sprintf("%x", color),
  455. Title: title,
  456. Summary: title,
  457. Sections: []MSTeamsSection{
  458. {
  459. ActivityTitle: p.Sender.FullName,
  460. ActivitySubtitle: p.Sender.UserName,
  461. ActivityImage: p.Sender.AvatarURL,
  462. Text: text,
  463. Facts: []MSTeamsFact{
  464. {
  465. Name: "Repository:",
  466. Value: p.Repository.FullName,
  467. },
  468. {
  469. Name: "Pull request #:",
  470. Value: fmt.Sprintf("%d", p.PullRequest.ID),
  471. },
  472. },
  473. },
  474. },
  475. PotentialAction: []MSTeamsAction{
  476. {
  477. Type: "OpenUri",
  478. Name: "View in Gitea",
  479. Targets: []MSTeamsActionTarget{
  480. {
  481. Os: "default",
  482. URI: p.PullRequest.HTMLURL,
  483. },
  484. },
  485. },
  486. },
  487. }, nil
  488. }
  489. func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*MSTeamsPayload, error) {
  490. var text, title string
  491. var color int
  492. switch p.Action {
  493. case api.HookIssueSynchronized:
  494. action, err := parseHookPullRequestEventType(event)
  495. if err != nil {
  496. return nil, err
  497. }
  498. title = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  499. text = p.PullRequest.Body
  500. color = warnColor
  501. }
  502. return &MSTeamsPayload{
  503. Type: "MessageCard",
  504. Context: "https://schema.org/extensions",
  505. ThemeColor: fmt.Sprintf("%x", color),
  506. Title: title,
  507. Summary: title,
  508. Sections: []MSTeamsSection{
  509. {
  510. ActivityTitle: p.Sender.FullName,
  511. ActivitySubtitle: p.Sender.UserName,
  512. ActivityImage: p.Sender.AvatarURL,
  513. Text: text,
  514. Facts: []MSTeamsFact{
  515. {
  516. Name: "Repository:",
  517. Value: p.Repository.FullName,
  518. },
  519. {
  520. Name: "Pull request #:",
  521. Value: fmt.Sprintf("%d", p.PullRequest.ID),
  522. },
  523. },
  524. },
  525. },
  526. PotentialAction: []MSTeamsAction{
  527. {
  528. Type: "OpenUri",
  529. Name: "View in Gitea",
  530. Targets: []MSTeamsActionTarget{
  531. {
  532. Os: "default",
  533. URI: p.PullRequest.HTMLURL,
  534. },
  535. },
  536. },
  537. },
  538. }, nil
  539. }
  540. func getMSTeamsRepositoryPayload(p *api.RepositoryPayload) (*MSTeamsPayload, error) {
  541. var title, url string
  542. var color int
  543. switch p.Action {
  544. case api.HookRepoCreated:
  545. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  546. url = p.Repository.HTMLURL
  547. color = successColor
  548. case api.HookRepoDeleted:
  549. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  550. color = warnColor
  551. }
  552. return &MSTeamsPayload{
  553. Type: "MessageCard",
  554. Context: "https://schema.org/extensions",
  555. ThemeColor: fmt.Sprintf("%x", color),
  556. Title: title,
  557. Summary: title,
  558. Sections: []MSTeamsSection{
  559. {
  560. ActivityTitle: p.Sender.FullName,
  561. ActivitySubtitle: p.Sender.UserName,
  562. ActivityImage: p.Sender.AvatarURL,
  563. Facts: []MSTeamsFact{
  564. {
  565. Name: "Repository:",
  566. Value: p.Repository.FullName,
  567. },
  568. },
  569. },
  570. },
  571. PotentialAction: []MSTeamsAction{
  572. {
  573. Type: "OpenUri",
  574. Name: "View in Gitea",
  575. Targets: []MSTeamsActionTarget{
  576. {
  577. Os: "default",
  578. URI: url,
  579. },
  580. },
  581. },
  582. },
  583. }, nil
  584. }
  585. func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
  586. var title, url string
  587. var color int
  588. switch p.Action {
  589. case api.HookReleasePublished:
  590. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  591. url = p.Release.URL
  592. color = successColor
  593. case api.HookReleaseUpdated:
  594. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  595. url = p.Release.URL
  596. color = successColor
  597. case api.HookReleaseDeleted:
  598. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  599. url = p.Release.URL
  600. color = successColor
  601. }
  602. return &MSTeamsPayload{
  603. Type: "MessageCard",
  604. Context: "https://schema.org/extensions",
  605. ThemeColor: fmt.Sprintf("%x", color),
  606. Title: title,
  607. Summary: title,
  608. Sections: []MSTeamsSection{
  609. {
  610. ActivityTitle: p.Sender.FullName,
  611. ActivitySubtitle: p.Sender.UserName,
  612. ActivityImage: p.Sender.AvatarURL,
  613. Text: p.Release.Note,
  614. Facts: []MSTeamsFact{
  615. {
  616. Name: "Repository:",
  617. Value: p.Repository.FullName,
  618. },
  619. {
  620. Name: "Tag:",
  621. Value: p.Release.TagName,
  622. },
  623. },
  624. },
  625. },
  626. PotentialAction: []MSTeamsAction{
  627. {
  628. Type: "OpenUri",
  629. Name: "View in Gitea",
  630. Targets: []MSTeamsActionTarget{
  631. {
  632. Os: "default",
  633. URI: url,
  634. },
  635. },
  636. },
  637. },
  638. }, nil
  639. }
  640. // GetMSTeamsPayload converts a MSTeams webhook into a MSTeamsPayload
  641. func GetMSTeamsPayload(p api.Payloader, event HookEventType, meta string) (*MSTeamsPayload, error) {
  642. s := new(MSTeamsPayload)
  643. switch event {
  644. case HookEventCreate:
  645. return getMSTeamsCreatePayload(p.(*api.CreatePayload))
  646. case HookEventDelete:
  647. return getMSTeamsDeletePayload(p.(*api.DeletePayload))
  648. case HookEventFork:
  649. return getMSTeamsForkPayload(p.(*api.ForkPayload))
  650. case HookEventIssues:
  651. return getMSTeamsIssuesPayload(p.(*api.IssuePayload))
  652. case HookEventIssueComment:
  653. return getMSTeamsIssueCommentPayload(p.(*api.IssueCommentPayload))
  654. case HookEventPush:
  655. return getMSTeamsPushPayload(p.(*api.PushPayload))
  656. case HookEventPullRequest:
  657. return getMSTeamsPullRequestPayload(p.(*api.PullRequestPayload))
  658. case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
  659. return getMSTeamsPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
  660. case HookEventRepository:
  661. return getMSTeamsRepositoryPayload(p.(*api.RepositoryPayload))
  662. case HookEventRelease:
  663. return getMSTeamsReleasePayload(p.(*api.ReleasePayload))
  664. }
  665. return s, nil
  666. }