選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

msteams.go 20KB

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