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.

general_test.go 15KB

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