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