gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "testing"
  6. api "code.gitea.io/gitea/modules/structs"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func createTestPayload() *api.CreatePayload {
  10. return &api.CreatePayload{
  11. Sha: "2020558fe2e34debb818a514715839cabd25e777",
  12. Ref: "refs/heads/test",
  13. RefType: "branch",
  14. Repo: &api.Repository{
  15. HTMLURL: "http://localhost:3000/test/repo",
  16. Name: "repo",
  17. FullName: "test/repo",
  18. },
  19. Sender: &api.User{
  20. UserName: "user1",
  21. AvatarURL: "http://localhost:3000/user1/avatar",
  22. },
  23. }
  24. }
  25. func deleteTestPayload() *api.DeletePayload {
  26. return &api.DeletePayload{
  27. Ref: "refs/heads/test",
  28. RefType: "branch",
  29. Repo: &api.Repository{
  30. HTMLURL: "http://localhost:3000/test/repo",
  31. Name: "repo",
  32. FullName: "test/repo",
  33. },
  34. Sender: &api.User{
  35. UserName: "user1",
  36. AvatarURL: "http://localhost:3000/user1/avatar",
  37. },
  38. }
  39. }
  40. func forkTestPayload() *api.ForkPayload {
  41. return &api.ForkPayload{
  42. Forkee: &api.Repository{
  43. HTMLURL: "http://localhost:3000/test/repo2",
  44. Name: "repo2",
  45. FullName: "test/repo2",
  46. },
  47. Repo: &api.Repository{
  48. HTMLURL: "http://localhost:3000/test/repo",
  49. Name: "repo",
  50. FullName: "test/repo",
  51. },
  52. Sender: &api.User{
  53. UserName: "user1",
  54. AvatarURL: "http://localhost:3000/user1/avatar",
  55. },
  56. }
  57. }
  58. func pushTestPayload() *api.PushPayload {
  59. return pushTestPayloadWithCommitMessage("commit message")
  60. }
  61. func pushTestMultilineCommitMessagePayload() *api.PushPayload {
  62. return pushTestPayloadWithCommitMessage("chore: This is a commit summary\n\nThis is a commit description.")
  63. }
  64. func pushTestPayloadWithCommitMessage(message string) *api.PushPayload {
  65. commit := &api.PayloadCommit{
  66. ID: "2020558fe2e34debb818a514715839cabd25e778",
  67. Message: message,
  68. URL: "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
  69. Author: &api.PayloadUser{
  70. Name: "user1",
  71. Email: "user1@localhost",
  72. UserName: "user1",
  73. },
  74. Committer: &api.PayloadUser{
  75. Name: "user1",
  76. Email: "user1@localhost",
  77. UserName: "user1",
  78. },
  79. }
  80. return &api.PushPayload{
  81. Ref: "refs/heads/test",
  82. Before: "2020558fe2e34debb818a514715839cabd25e777",
  83. After: "2020558fe2e34debb818a514715839cabd25e778",
  84. CompareURL: "",
  85. HeadCommit: commit,
  86. Commits: []*api.PayloadCommit{commit, commit},
  87. TotalCommits: 2,
  88. Repo: &api.Repository{
  89. HTMLURL: "http://localhost:3000/test/repo",
  90. Name: "repo",
  91. FullName: "test/repo",
  92. },
  93. Pusher: &api.User{
  94. UserName: "user1",
  95. AvatarURL: "http://localhost:3000/user1/avatar",
  96. },
  97. Sender: &api.User{
  98. UserName: "user1",
  99. AvatarURL: "http://localhost:3000/user1/avatar",
  100. },
  101. }
  102. }
  103. func issueTestPayload() *api.IssuePayload {
  104. return &api.IssuePayload{
  105. Index: 2,
  106. Sender: &api.User{
  107. UserName: "user1",
  108. AvatarURL: "http://localhost:3000/user1/avatar",
  109. },
  110. Repository: &api.Repository{
  111. HTMLURL: "http://localhost:3000/test/repo",
  112. Name: "repo",
  113. FullName: "test/repo",
  114. },
  115. Issue: &api.Issue{
  116. ID: 2,
  117. Index: 2,
  118. URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
  119. HTMLURL: "http://localhost:3000/test/repo/issues/2",
  120. Title: "crash",
  121. Body: "issue body",
  122. Poster: &api.User{
  123. UserName: "user1",
  124. AvatarURL: "http://localhost:3000/user1/avatar",
  125. },
  126. Assignees: []*api.User{
  127. {
  128. UserName: "user1",
  129. AvatarURL: "http://localhost:3000/user1/avatar",
  130. },
  131. },
  132. Milestone: &api.Milestone{
  133. ID: 1,
  134. Title: "Milestone Title",
  135. Description: "Milestone Description",
  136. },
  137. },
  138. }
  139. }
  140. func issueCommentTestPayload() *api.IssueCommentPayload {
  141. return &api.IssueCommentPayload{
  142. Action: api.HookIssueCommentCreated,
  143. Sender: &api.User{
  144. UserName: "user1",
  145. AvatarURL: "http://localhost:3000/user1/avatar",
  146. },
  147. Repository: &api.Repository{
  148. HTMLURL: "http://localhost:3000/test/repo",
  149. Name: "repo",
  150. FullName: "test/repo",
  151. },
  152. Comment: &api.Comment{
  153. HTMLURL: "http://localhost:3000/test/repo/issues/2#issuecomment-4",
  154. IssueURL: "http://localhost:3000/test/repo/issues/2",
  155. Body: "more info needed",
  156. },
  157. Issue: &api.Issue{
  158. ID: 2,
  159. Index: 2,
  160. URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
  161. HTMLURL: "http://localhost:3000/test/repo/issues/2",
  162. Title: "crash",
  163. Poster: &api.User{
  164. UserName: "user1",
  165. AvatarURL: "http://localhost:3000/user1/avatar",
  166. },
  167. Body: "this happened",
  168. },
  169. }
  170. }
  171. func pullRequestCommentTestPayload() *api.IssueCommentPayload {
  172. return &api.IssueCommentPayload{
  173. Action: api.HookIssueCommentCreated,
  174. Sender: &api.User{
  175. UserName: "user1",
  176. AvatarURL: "http://localhost:3000/user1/avatar",
  177. },
  178. Repository: &api.Repository{
  179. HTMLURL: "http://localhost:3000/test/repo",
  180. Name: "repo",
  181. FullName: "test/repo",
  182. },
  183. Comment: &api.Comment{
  184. HTMLURL: "http://localhost:3000/test/repo/pulls/12#issuecomment-4",
  185. PRURL: "http://localhost:3000/test/repo/pulls/12",
  186. Body: "changes requested",
  187. },
  188. Issue: &api.Issue{
  189. ID: 12,
  190. Index: 12,
  191. URL: "http://localhost:3000/api/v1/repos/test/repo/pulls/12",
  192. HTMLURL: "http://localhost:3000/test/repo/pulls/12",
  193. Title: "Fix bug",
  194. Body: "fixes bug #2",
  195. Poster: &api.User{
  196. UserName: "user1",
  197. AvatarURL: "http://localhost:3000/user1/avatar",
  198. },
  199. },
  200. IsPull: true,
  201. }
  202. }
  203. func wikiTestPayload() *api.WikiPayload {
  204. return &api.WikiPayload{
  205. Repository: &api.Repository{
  206. HTMLURL: "http://localhost:3000/test/repo",
  207. Name: "repo",
  208. FullName: "test/repo",
  209. },
  210. Sender: &api.User{
  211. UserName: "user1",
  212. AvatarURL: "http://localhost:3000/user1/avatar",
  213. },
  214. Page: "index",
  215. Comment: "Wiki change comment",
  216. }
  217. }
  218. func pullReleaseTestPayload() *api.ReleasePayload {
  219. return &api.ReleasePayload{
  220. Action: api.HookReleasePublished,
  221. Sender: &api.User{
  222. UserName: "user1",
  223. AvatarURL: "http://localhost:3000/user1/avatar",
  224. },
  225. Repository: &api.Repository{
  226. HTMLURL: "http://localhost:3000/test/repo",
  227. Name: "repo",
  228. FullName: "test/repo",
  229. },
  230. Release: &api.Release{
  231. TagName: "v1.0",
  232. Target: "master",
  233. Title: "First stable release",
  234. Note: "Note of first stable release",
  235. HTMLURL: "http://localhost:3000/test/repo/releases/tag/v1.0",
  236. },
  237. }
  238. }
  239. func pullRequestTestPayload() *api.PullRequestPayload {
  240. return &api.PullRequestPayload{
  241. Action: api.HookIssueOpened,
  242. Index: 12,
  243. Sender: &api.User{
  244. UserName: "user1",
  245. AvatarURL: "http://localhost:3000/user1/avatar",
  246. },
  247. Repository: &api.Repository{
  248. HTMLURL: "http://localhost:3000/test/repo",
  249. Name: "repo",
  250. FullName: "test/repo",
  251. },
  252. PullRequest: &api.PullRequest{
  253. ID: 12,
  254. Index: 12,
  255. URL: "http://localhost:3000/test/repo/pulls/12",
  256. HTMLURL: "http://localhost:3000/test/repo/pulls/12",
  257. Title: "Fix bug",
  258. Body: "fixes bug #2",
  259. Mergeable: true,
  260. Poster: &api.User{
  261. UserName: "user1",
  262. AvatarURL: "http://localhost:3000/user1/avatar",
  263. },
  264. Assignees: []*api.User{
  265. {
  266. UserName: "user1",
  267. AvatarURL: "http://localhost:3000/user1/avatar",
  268. },
  269. },
  270. Milestone: &api.Milestone{
  271. ID: 1,
  272. Title: "Milestone Title",
  273. Description: "Milestone Description",
  274. },
  275. },
  276. Review: &api.ReviewPayload{
  277. Content: "good job",
  278. },
  279. }
  280. }
  281. func repositoryTestPayload() *api.RepositoryPayload {
  282. return &api.RepositoryPayload{
  283. Action: api.HookRepoCreated,
  284. Sender: &api.User{
  285. UserName: "user1",
  286. AvatarURL: "http://localhost:3000/user1/avatar",
  287. },
  288. Repository: &api.Repository{
  289. HTMLURL: "http://localhost:3000/test/repo",
  290. Name: "repo",
  291. FullName: "test/repo",
  292. },
  293. }
  294. }
  295. func packageTestPayload() *api.PackagePayload {
  296. return &api.PackagePayload{
  297. Action: api.HookPackageCreated,
  298. Sender: &api.User{
  299. UserName: "user1",
  300. AvatarURL: "http://localhost:3000/user1/avatar",
  301. },
  302. Repository: nil,
  303. Organization: &api.Organization{
  304. Name: "org1",
  305. AvatarURL: "http://localhost:3000/org1/avatar",
  306. },
  307. Package: &api.Package{
  308. Owner: &api.User{
  309. UserName: "user1",
  310. AvatarURL: "http://localhost:3000/user1/avatar",
  311. },
  312. Repository: nil,
  313. Creator: &api.User{
  314. UserName: "user1",
  315. AvatarURL: "http://localhost:3000/user1/avatar",
  316. },
  317. Type: "container",
  318. Name: "GiteaContainer",
  319. Version: "latest",
  320. HTMLURL: "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest",
  321. },
  322. }
  323. }
  324. func TestGetIssuesPayloadInfo(t *testing.T) {
  325. p := issueTestPayload()
  326. cases := []struct {
  327. action api.HookIssueAction
  328. text string
  329. issueTitle string
  330. attachmentText string
  331. color int
  332. }{
  333. {
  334. api.HookIssueOpened,
  335. "[test/repo] Issue opened: #2 crash by user1",
  336. "#2 crash",
  337. "issue body",
  338. orangeColor,
  339. },
  340. {
  341. api.HookIssueClosed,
  342. "[test/repo] Issue closed: #2 crash by user1",
  343. "#2 crash",
  344. "",
  345. redColor,
  346. },
  347. {
  348. api.HookIssueReOpened,
  349. "[test/repo] Issue re-opened: #2 crash by user1",
  350. "#2 crash",
  351. "",
  352. yellowColor,
  353. },
  354. {
  355. api.HookIssueEdited,
  356. "[test/repo] Issue edited: #2 crash by user1",
  357. "#2 crash",
  358. "issue body",
  359. yellowColor,
  360. },
  361. {
  362. api.HookIssueAssigned,
  363. "[test/repo] Issue assigned to user1: #2 crash by user1",
  364. "#2 crash",
  365. "",
  366. greenColor,
  367. },
  368. {
  369. api.HookIssueUnassigned,
  370. "[test/repo] Issue unassigned: #2 crash by user1",
  371. "#2 crash",
  372. "",
  373. yellowColor,
  374. },
  375. {
  376. api.HookIssueLabelUpdated,
  377. "[test/repo] Issue labels updated: #2 crash by user1",
  378. "#2 crash",
  379. "",
  380. yellowColor,
  381. },
  382. {
  383. api.HookIssueLabelCleared,
  384. "[test/repo] Issue labels cleared: #2 crash by user1",
  385. "#2 crash",
  386. "",
  387. yellowColor,
  388. },
  389. {
  390. api.HookIssueSynchronized,
  391. "[test/repo] Issue synchronized: #2 crash by user1",
  392. "#2 crash",
  393. "",
  394. yellowColor,
  395. },
  396. {
  397. api.HookIssueMilestoned,
  398. "[test/repo] Issue milestoned to Milestone Title: #2 crash by user1",
  399. "#2 crash",
  400. "",
  401. yellowColor,
  402. },
  403. {
  404. api.HookIssueDemilestoned,
  405. "[test/repo] Issue milestone cleared: #2 crash by user1",
  406. "#2 crash",
  407. "",
  408. yellowColor,
  409. },
  410. }
  411. for i, c := range cases {
  412. p.Action = c.action
  413. text, issueTitle, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
  414. assert.Equal(t, c.text, text, "case %d", i)
  415. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  416. assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
  417. assert.Equal(t, c.color, color, "case %d", i)
  418. }
  419. }
  420. func TestGetPullRequestPayloadInfo(t *testing.T) {
  421. p := pullRequestTestPayload()
  422. cases := []struct {
  423. action api.HookIssueAction
  424. text string
  425. issueTitle string
  426. attachmentText string
  427. color int
  428. }{
  429. {
  430. api.HookIssueOpened,
  431. "[test/repo] Pull request opened: #12 Fix bug by user1",
  432. "#12 Fix bug",
  433. "fixes bug #2",
  434. greenColor,
  435. },
  436. {
  437. api.HookIssueClosed,
  438. "[test/repo] Pull request closed: #12 Fix bug by user1",
  439. "#12 Fix bug",
  440. "",
  441. redColor,
  442. },
  443. {
  444. api.HookIssueReOpened,
  445. "[test/repo] Pull request re-opened: #12 Fix bug by user1",
  446. "#12 Fix bug",
  447. "",
  448. yellowColor,
  449. },
  450. {
  451. api.HookIssueEdited,
  452. "[test/repo] Pull request edited: #12 Fix bug by user1",
  453. "#12 Fix bug",
  454. "fixes bug #2",
  455. yellowColor,
  456. },
  457. {
  458. api.HookIssueAssigned,
  459. "[test/repo] Pull request assigned to user1: #12 Fix bug by user1",
  460. "#12 Fix bug",
  461. "",
  462. greenColor,
  463. },
  464. {
  465. api.HookIssueUnassigned,
  466. "[test/repo] Pull request unassigned: #12 Fix bug by user1",
  467. "#12 Fix bug",
  468. "",
  469. yellowColor,
  470. },
  471. {
  472. api.HookIssueLabelUpdated,
  473. "[test/repo] Pull request labels updated: #12 Fix bug by user1",
  474. "#12 Fix bug",
  475. "",
  476. yellowColor,
  477. },
  478. {
  479. api.HookIssueLabelCleared,
  480. "[test/repo] Pull request labels cleared: #12 Fix bug by user1",
  481. "#12 Fix bug",
  482. "",
  483. yellowColor,
  484. },
  485. {
  486. api.HookIssueSynchronized,
  487. "[test/repo] Pull request synchronized: #12 Fix bug by user1",
  488. "#12 Fix bug",
  489. "",
  490. yellowColor,
  491. },
  492. {
  493. api.HookIssueMilestoned,
  494. "[test/repo] Pull request milestoned to Milestone Title: #12 Fix bug by user1",
  495. "#12 Fix bug",
  496. "",
  497. yellowColor,
  498. },
  499. {
  500. api.HookIssueDemilestoned,
  501. "[test/repo] Pull request milestone cleared: #12 Fix bug by user1",
  502. "#12 Fix bug",
  503. "",
  504. yellowColor,
  505. },
  506. }
  507. for i, c := range cases {
  508. p.Action = c.action
  509. text, issueTitle, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
  510. assert.Equal(t, c.text, text, "case %d", i)
  511. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  512. assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
  513. assert.Equal(t, c.color, color, "case %d", i)
  514. }
  515. }
  516. func TestGetWikiPayloadInfo(t *testing.T) {
  517. p := wikiTestPayload()
  518. cases := []struct {
  519. action api.HookWikiAction
  520. text string
  521. color int
  522. link string
  523. }{
  524. {
  525. api.HookWikiCreated,
  526. "[test/repo] New wiki page 'index' (Wiki change comment) by user1",
  527. greenColor,
  528. "index",
  529. },
  530. {
  531. api.HookWikiEdited,
  532. "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1",
  533. yellowColor,
  534. "index",
  535. },
  536. {
  537. api.HookWikiDeleted,
  538. "[test/repo] Wiki page 'index' deleted by user1",
  539. redColor,
  540. "index",
  541. },
  542. }
  543. for i, c := range cases {
  544. p.Action = c.action
  545. text, color, link := getWikiPayloadInfo(p, noneLinkFormatter, true)
  546. assert.Equal(t, c.text, text, "case %d", i)
  547. assert.Equal(t, c.color, color, "case %d", i)
  548. assert.Equal(t, c.link, link, "case %d", i)
  549. }
  550. }
  551. func TestGetReleasePayloadInfo(t *testing.T) {
  552. p := pullReleaseTestPayload()
  553. cases := []struct {
  554. action api.HookReleaseAction
  555. text string
  556. color int
  557. }{
  558. {
  559. api.HookReleasePublished,
  560. "[test/repo] Release created: v1.0 by user1",
  561. greenColor,
  562. },
  563. {
  564. api.HookReleaseUpdated,
  565. "[test/repo] Release updated: v1.0 by user1",
  566. yellowColor,
  567. },
  568. {
  569. api.HookReleaseDeleted,
  570. "[test/repo] Release deleted: v1.0 by user1",
  571. redColor,
  572. },
  573. }
  574. for i, c := range cases {
  575. p.Action = c.action
  576. text, color := getReleasePayloadInfo(p, noneLinkFormatter, true)
  577. assert.Equal(t, c.text, text, "case %d", i)
  578. assert.Equal(t, c.color, color, "case %d", i)
  579. }
  580. }
  581. func TestGetIssueCommentPayloadInfo(t *testing.T) {
  582. p := pullRequestCommentTestPayload()
  583. cases := []struct {
  584. action api.HookIssueCommentAction
  585. text string
  586. issueTitle string
  587. color int
  588. }{
  589. {
  590. api.HookIssueCommentCreated,
  591. "[test/repo] New comment on pull request #12 Fix bug by user1",
  592. "#12 Fix bug",
  593. greenColorLight,
  594. },
  595. {
  596. api.HookIssueCommentEdited,
  597. "[test/repo] Comment edited on pull request #12 Fix bug by user1",
  598. "#12 Fix bug",
  599. yellowColor,
  600. },
  601. {
  602. api.HookIssueCommentDeleted,
  603. "[test/repo] Comment deleted on pull request #12 Fix bug by user1",
  604. "#12 Fix bug",
  605. redColor,
  606. },
  607. }
  608. for i, c := range cases {
  609. p.Action = c.action
  610. text, issueTitle, color := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
  611. assert.Equal(t, c.text, text, "case %d", i)
  612. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  613. assert.Equal(t, c.color, color, "case %d", i)
  614. }
  615. }