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