|
19 | 19 | import subprocess
|
20 | 20 | import sys
|
21 | 21 | import json
|
| 22 | +import textwrap |
22 | 23 | import tempfile
|
23 | 24 |
|
24 | 25 | import pytest
|
@@ -406,5 +407,240 @@ def all_time_keys(time):
|
406 | 407 | )
|
407 | 408 |
|
408 | 409 |
|
| 410 | +def assert_in(needle: str, haystack: str): |
| 411 | + if needle not in haystack: |
| 412 | + raise AssertionError(f"item not found:\n{needle}\nin:\n{haystack}") |
| 413 | + |
| 414 | + |
| 415 | +def test_github_tag_teams(tmpdir_factory): |
| 416 | + tag_script = REPO_ROOT / "tests" / "scripts" / "github_tag_teams.py" |
| 417 | + |
| 418 | + def run(type, data, check): |
| 419 | + git = TempGit(tmpdir_factory.mktemp("tmp_git_dir")) |
| 420 | + git.run("init") |
| 421 | + git.run("checkout", "-b", "main") |
| 422 | + git.run("remote", "add", "origin", "https://github.com/apache/tvm.git") |
| 423 | + |
| 424 | + issue_body = """ |
| 425 | + some text |
| 426 | + [temporary] opt-in: @person5 |
| 427 | +
|
| 428 | + - something: @person1 @person2 |
| 429 | + - something else @person1 @person2 |
| 430 | + - something else2: @person1 @person2 |
| 431 | + - something-else @person1 @person2 |
| 432 | + """ |
| 433 | + comment1 = """ |
| 434 | + another thing: @person3 |
| 435 | + another-thing @person3 |
| 436 | + """ |
| 437 | + comment2 = """ |
| 438 | + something @person4 |
| 439 | + """ |
| 440 | + teams = { |
| 441 | + "data": { |
| 442 | + "repository": { |
| 443 | + "issue": { |
| 444 | + "body": issue_body, |
| 445 | + "comments": {"nodes": [{"body": comment1}, {"body": comment2}]}, |
| 446 | + } |
| 447 | + } |
| 448 | + } |
| 449 | + } |
| 450 | + env = { |
| 451 | + type: json.dumps(data), |
| 452 | + } |
| 453 | + proc = subprocess.run( |
| 454 | + [ |
| 455 | + str(tag_script), |
| 456 | + "--dry-run", |
| 457 | + "--team-issue-json", |
| 458 | + json.dumps(teams), |
| 459 | + ], |
| 460 | + stdout=subprocess.PIPE, |
| 461 | + stderr=subprocess.PIPE, |
| 462 | + encoding="utf-8", |
| 463 | + cwd=git.cwd, |
| 464 | + env=env, |
| 465 | + ) |
| 466 | + if proc.returncode != 0: |
| 467 | + raise RuntimeError(f"Process failed:\nstdout:\n{proc.stdout}\n\nstderr:\n{proc.stderr}") |
| 468 | + |
| 469 | + assert_in(check, proc.stdout) |
| 470 | + |
| 471 | + run( |
| 472 | + "ISSUE", |
| 473 | + { |
| 474 | + "title": "A title", |
| 475 | + "number": 1234, |
| 476 | + "user": { |
| 477 | + "login": "person5", |
| 478 | + }, |
| 479 | + "labels": [{"name": "abc"}], |
| 480 | + "body": textwrap.dedent( |
| 481 | + """ |
| 482 | + hello |
| 483 | + """.strip() |
| 484 | + ), |
| 485 | + }, |
| 486 | + "No one to cc, exiting", |
| 487 | + ) |
| 488 | + |
| 489 | + run( |
| 490 | + "ISSUE", |
| 491 | + { |
| 492 | + "title": "A title", |
| 493 | + "number": 1234, |
| 494 | + "user": { |
| 495 | + "login": "person5", |
| 496 | + }, |
| 497 | + "labels": [{"name": "abc"}], |
| 498 | + "body": textwrap.dedent( |
| 499 | + """ |
| 500 | + hello |
| 501 | +
|
| 502 | + cc @test |
| 503 | + """.strip() |
| 504 | + ), |
| 505 | + }, |
| 506 | + "No one to cc, exiting", |
| 507 | + ) |
| 508 | + |
| 509 | + run( |
| 510 | + type="ISSUE", |
| 511 | + data={ |
| 512 | + "title": "A title", |
| 513 | + "number": 1234, |
| 514 | + "user": { |
| 515 | + "login": "person5", |
| 516 | + }, |
| 517 | + "labels": [{"name": "something"}], |
| 518 | + "body": textwrap.dedent( |
| 519 | + """ |
| 520 | + hello |
| 521 | +
|
| 522 | + something""" |
| 523 | + ), |
| 524 | + }, |
| 525 | + check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}", |
| 526 | + ) |
| 527 | + |
| 528 | + run( |
| 529 | + type="ISSUE", |
| 530 | + data={ |
| 531 | + "title": "A title", |
| 532 | + "number": 1234, |
| 533 | + "user": { |
| 534 | + "login": "person6", |
| 535 | + }, |
| 536 | + "labels": [{"name": "something"}], |
| 537 | + "body": textwrap.dedent( |
| 538 | + """ |
| 539 | + hello |
| 540 | +
|
| 541 | + something""" |
| 542 | + ), |
| 543 | + }, |
| 544 | + check="Author person6 is not opted in, quitting", |
| 545 | + ) |
| 546 | + |
| 547 | + run( |
| 548 | + type="ISSUE", |
| 549 | + data={ |
| 550 | + "title": "A title", |
| 551 | + "number": 1234, |
| 552 | + "user": { |
| 553 | + "login": "person5", |
| 554 | + }, |
| 555 | + "labels": [{"name": "something"}], |
| 556 | + "body": textwrap.dedent( |
| 557 | + """ |
| 558 | + hello |
| 559 | +
|
| 560 | + cc @person1 @person2 @person4""" |
| 561 | + ), |
| 562 | + }, |
| 563 | + check="Everyone to cc is already cc'ed, no update needed", |
| 564 | + ) |
| 565 | + |
| 566 | + run( |
| 567 | + type="ISSUE", |
| 568 | + data={ |
| 569 | + "title": "[something] A title", |
| 570 | + "number": 1234, |
| 571 | + "user": { |
| 572 | + "login": "person5", |
| 573 | + }, |
| 574 | + "labels": [{"name": "something2"}], |
| 575 | + "body": textwrap.dedent( |
| 576 | + """ |
| 577 | + hello |
| 578 | +
|
| 579 | + something""" |
| 580 | + ), |
| 581 | + }, |
| 582 | + check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}", |
| 583 | + ) |
| 584 | + |
| 585 | + run( |
| 586 | + type="ISSUE", |
| 587 | + data={ |
| 588 | + "title": "[something] A title", |
| 589 | + "number": 1234, |
| 590 | + "user": { |
| 591 | + "login": "person5", |
| 592 | + }, |
| 593 | + "labels": [{"name": "something2"}], |
| 594 | + "body": textwrap.dedent( |
| 595 | + """ |
| 596 | + hello |
| 597 | +
|
| 598 | + cc @person1 @person2 @person4""" |
| 599 | + ), |
| 600 | + }, |
| 601 | + check="Everyone to cc is already cc'ed, no update needed", |
| 602 | + ) |
| 603 | + |
| 604 | + run( |
| 605 | + type="PR", |
| 606 | + data={ |
| 607 | + "title": "[something] A title", |
| 608 | + "number": 1234, |
| 609 | + "draft": False, |
| 610 | + "user": { |
| 611 | + "login": "person5", |
| 612 | + }, |
| 613 | + "labels": [{"name": "something2"}], |
| 614 | + "body": textwrap.dedent( |
| 615 | + """ |
| 616 | + hello |
| 617 | +
|
| 618 | + cc @person1 @person2 @person4""" |
| 619 | + ), |
| 620 | + }, |
| 621 | + check="Everyone to cc is already cc'ed, no update needed", |
| 622 | + ) |
| 623 | + |
| 624 | + run( |
| 625 | + type="PR", |
| 626 | + data={ |
| 627 | + "title": "[something] A title", |
| 628 | + "number": 1234, |
| 629 | + "draft": True, |
| 630 | + "user": { |
| 631 | + "login": "person5", |
| 632 | + }, |
| 633 | + "labels": [{"name": "something2"}], |
| 634 | + "body": textwrap.dedent( |
| 635 | + """ |
| 636 | + hello |
| 637 | +
|
| 638 | + cc @person1 @person2 @person4""" |
| 639 | + ), |
| 640 | + }, |
| 641 | + check="Terminating since 1234 is a draft", |
| 642 | + ) |
| 643 | + |
| 644 | + |
409 | 645 | if __name__ == "__main__":
|
410 | 646 | sys.exit(pytest.main([__file__] + sys.argv[1:]))
|
0 commit comments