1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::SystemNotes::IssuablesService, feature_category: :team_planning do
include ProjectForksHelper
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :repository, group: group) }
let_it_be(:author) { create(:user) }
let(:noteable) { create(:issue, project: project) }
let(:issue) { noteable }
let(:service) { described_class.new(noteable: noteable, container: project, author: author) }
describe '#relate_issuable' do
let_it_be(:issue1) { create(:issue, project: project) }
let_it_be(:issue2) { create(:issue, project: project) }
let(:noteable_ref) { issue1 }
subject(:system_note) { service.relate_issuable(noteable_ref) }
context 'when issue marks another as related' do
it_behaves_like 'a system note' do
let(:action) { 'relate' }
end
it 'sets the note text' do
expect(system_note.note).to eq "marked this issue as related to #{issue1.to_reference(project)}"
end
end
context 'when issue marks several other issues as related' do
let(:noteable_ref) { [issue1, issue2] }
it_behaves_like 'a system note' do
let(:action) { 'relate' }
end
it 'sets the note text' do
expect(system_note.note).to eq(
"marked this issue as related to #{issue1.to_reference(project)} and #{issue2.to_reference(project)}"
)
end
end
context 'with work items' do
let_it_be(:noteable) { create(:work_item, :task, project: project) }
it 'sets the note text with the correct work item type' do
expect(subject.note).to eq "marked this task as related to #{noteable_ref.to_reference(project)}"
end
end
end
describe '#unrelate_issuable' do
let(:noteable_ref) { create(:issue) }
subject { service.unrelate_issuable(noteable_ref) }
it_behaves_like 'a system note' do
let(:action) { 'unrelate' }
end
context 'when issue relation is removed' do
it 'sets the note text' do
expect(subject.note).to eq "removed the relation with #{noteable_ref.to_reference(project)}"
end
end
end
describe '#change_assignee' do
subject { service.change_assignee(assignee) }
let(:assignee) { create(:user) }
it_behaves_like 'a system note' do
let(:action) { 'assignee' }
end
context 'when assignee added' do
it_behaves_like 'a note with overridable created_at'
it 'sets the note text' do
expect(subject.note).to eq "assigned to @#{assignee.username}"
end
end
context 'when assignee removed' do
let(:assignee) { nil }
it_behaves_like 'a note with overridable created_at'
it 'sets the note text' do
expect(subject.note).to eq 'removed assignee'
end
end
end
describe '#change_issuable_assignees' do
subject { service.change_issuable_assignees([assignee]) }
let(:assignee) { create(:user) }
let(:assignee1) { create(:user) }
let(:assignee2) { create(:user) }
let(:assignee3) { create(:user) }
it_behaves_like 'a system note' do
let(:action) { 'assignee' }
end
def build_note(old_assignees, new_assignees)
issue.assignees = new_assignees
service.change_issuable_assignees(old_assignees).note
end
it_behaves_like 'a note with overridable created_at'
it 'builds a correct phrase when an assignee is added to a non-assigned issue' do
expect(build_note([], [assignee1])).to eq "assigned to @#{assignee1.username}"
end
it 'builds a correct phrase when assignee removed' do
expect(build_note([assignee1], [])).to eq "unassigned @#{assignee1.username}"
end
it 'builds a correct phrase when assignees changed' do
expect(build_note([assignee1], [assignee2])).to eq \
"assigned to @#{assignee2.username} and unassigned @#{assignee1.username}"
end
it 'builds a correct phrase when three assignees removed and one added' do
expect(build_note([assignee, assignee1, assignee2], [assignee3])).to eq \
"assigned to @#{assignee3.username} and unassigned @#{assignee.username}, @#{assignee1.username}, and @#{assignee2.username}"
end
it 'builds a correct phrase when one assignee changed from a set' do
expect(build_note([assignee, assignee1], [assignee, assignee2])).to eq \
"assigned to @#{assignee2.username} and unassigned @#{assignee1.username}"
end
it 'builds a correct phrase when one assignee removed from a set' do
expect(build_note([assignee, assignee1, assignee2], [assignee, assignee1])).to eq \
"unassigned @#{assignee2.username}"
end
it 'builds a correct phrase when the locale is different' do
Gitlab::I18n.with_locale('pt-BR') do
expect(build_note([assignee, assignee1, assignee2], [assignee3])).to eq \
"assigned to @#{assignee3.username} and unassigned @#{assignee.username}, @#{assignee1.username}, and @#{assignee2.username}"
end
end
end
describe '#change_issuable_reviewers' do
subject { service.change_issuable_reviewers([reviewer]) }
let_it_be(:noteable) { create(:merge_request, :simple, source_project: project) }
let_it_be(:reviewer) { create(:user) }
let_it_be(:reviewer1) { create(:user) }
let_it_be(:reviewer2) { create(:user) }
let_it_be(:reviewer3) { create(:user) }
it_behaves_like 'a system note' do
let(:action) { 'reviewer' }
end
def build_note(old_reviewers, new_reviewers)
noteable.reviewers = new_reviewers
service.change_issuable_reviewers(old_reviewers).note
end
it 'builds a correct phrase when a reviewer is added to a non-assigned merge request' do
expect(build_note([], [reviewer1])).to eq "requested review from @#{reviewer1.username}"
end
it 'builds a correct phrase when reviewer is removed' do
expect(build_note([reviewer], [])).to eq "removed review request for @#{reviewer.username}"
end
it 'builds a correct phrase when reviewers changed' do
expect(build_note([reviewer1], [reviewer2])).to(
eq("requested review from @#{reviewer2.username} and removed review request for @#{reviewer1.username}")
)
end
it 'builds a correct phrase when three reviewers removed and one added' do
expect(build_note([reviewer, reviewer1, reviewer2], [reviewer3])).to(
eq("requested review from @#{reviewer3.username} and removed review request for @#{reviewer.username}, @#{reviewer1.username}, and @#{reviewer2.username}")
)
end
it 'builds a correct phrase when one reviewer is changed from a set' do
expect(build_note([reviewer, reviewer1], [reviewer, reviewer2])).to(
eq("requested review from @#{reviewer2.username} and removed review request for @#{reviewer1.username}")
)
end
it 'builds a correct phrase when one reviewer removed from a set' do
expect(build_note([reviewer, reviewer1, reviewer2], [reviewer, reviewer1])).to(
eq("removed review request for @#{reviewer2.username}")
)
end
it 'builds a correct phrase when the locale is different' do
Gitlab::I18n.with_locale('pt-BR') do
expect(build_note([reviewer, reviewer1, reviewer2], [reviewer3])).to(
eq("requested review from @#{reviewer3.username} and removed review request for @#{reviewer.username}, @#{reviewer1.username}, and @#{reviewer2.username}")
)
end
end
end
describe '#request_review' do
subject(:request_review) { service.request_review(reviewer, unapproved) }
let_it_be(:reviewer) { create(:user) }
let_it_be(:noteable) { create(:merge_request, :simple, source_project: project, reviewers: [reviewer]) }
let(:unapproved) { false }
it_behaves_like 'a system note' do
let(:action) { 'reviewer' }
end
it 'builds a correct phrase when a review has been requested from a reviewer' do
expect(request_review.note).to eq "requested review from #{reviewer.to_reference}"
end
context 'when unapproving' do
let(:unapproved) { true }
it 'builds a correct phrase when a review has been requested from a reviewer and the reviewer has been unapproved' do
expect(request_review.note).to eq "requested review from #{reviewer.to_reference} and removed approval"
end
end
end
describe '#change_issuable_contacts' do
subject { service.change_issuable_contacts(1, 1) }
let_it_be(:noteable) { create(:issue, project: project) }
it_behaves_like 'a system note' do
let(:action) { 'contact' }
end
def build_note(added_count, removed_count)
service.change_issuable_contacts(added_count, removed_count).note
end
it 'builds a correct phrase when one contact is added' do
expect(build_note(1, 0)).to eq "added 1 contact"
end
it 'builds a correct phrase when one contact is removed' do
expect(build_note(0, 1)).to eq "removed 1 contact"
end
it 'builds a correct phrase when one contact is added and one contact is removed' do
expect(build_note(1, 1)).to(
eq("added 1 contact and removed 1 contact")
)
end
it 'builds a correct phrase when three contacts are added and one removed' do
expect(build_note(3, 1)).to(
eq("added 3 contacts and removed 1 contact")
)
end
it 'builds a correct phrase when three contacts are removed and one added' do
expect(build_note(1, 3)).to(
eq("added 1 contact and removed 3 contacts")
)
end
it 'builds a correct phrase when the locale is different' do
Gitlab::I18n.with_locale('pt-BR') do
expect(build_note(1, 3)).to(
eq("added 1 contact and removed 3 contacts")
)
end
end
end
describe '#change_status' do
subject { service.change_status(status, source) }
let(:status) { 'reopened' }
let(:source) { nil }
it 'creates a resource state event' do
expect { subject }.to change { ResourceStateEvent.count }.by(1)
end
end
describe '#change_title' do
let(:noteable) { create(:issue, project: project, title: 'Lorem ipsum') }
subject { service.change_title('Old title') }
context 'when noteable responds to `title`' do
it_behaves_like 'a system note' do
let(:action) { 'title' }
end
it_behaves_like 'a note with overridable created_at'
it 'sets the note text' do
expect(subject.note)
.to eq "changed title from **{-Old title-}** to **{+Lorem ipsum+}**"
end
end
end
describe '#change_description' do
subject { service.change_description }
context 'when noteable responds to `description`' do
it_behaves_like 'a system note' do
let(:action) { 'description' }
end
it_behaves_like 'a note with overridable created_at'
it 'sets the note text' do
expect(subject.note).to eq('changed the description')
end
it 'associates the related description version' do
noteable.update!(description: 'New description')
description_version_id = subject.system_note_metadata.description_version_id
expect(description_version_id).not_to be_nil
expect(description_version_id).to eq(noteable.saved_description_version.id)
end
end
end
describe '#change_issue_confidentiality' do
subject { service.change_issue_confidentiality }
context 'issue has been made confidential' do
before do
noteable.update_attribute(:confidential, true)
end
it_behaves_like 'a system note' do
let(:action) { 'confidential' }
end
it 'sets the note text' do
expect(subject.note).to eq 'made the issue confidential'
end
end
context 'issue has been made visible' do
it_behaves_like 'a system note' do
let(:action) { 'visible' }
end
it 'sets the note text' do
expect(subject.note).to eq 'made the issue visible to everyone'
end
end
end
describe '#cross_reference' do
let(:service) { described_class.new(noteable: noteable, author: author) }
let(:mentioned_in) { create(:issue, project: project) }
subject { service.cross_reference(mentioned_in) }
it_behaves_like 'a system note' do
let(:action) { 'cross_reference' }
end
context 'when cross-reference disallowed' do
before do
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:cross_reference_disallowed?).and_return(true)
end
end
it 'returns nil' do
expect(subject).to be_nil
end
it 'does not create a system note metadata record' do
expect { subject }.not_to change { SystemNoteMetadata.count }
end
end
context 'when cross-reference allowed' do
before do
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:cross_reference_disallowed?).and_return(false)
end
end
it_behaves_like 'a system note' do
let(:action) { 'cross_reference' }
end
it_behaves_like 'a note with overridable created_at'
describe 'note_body' do
context 'cross-project' do
let(:project2) { create(:project, :repository) }
let(:mentioned_in) { create(:issue, :task, project: project2) }
context 'from Commit' do
let(:mentioned_in) { project2.repository.commit }
it 'references the mentioning commit' do
expect(subject.note).to eq "mentioned in commit #{mentioned_in.to_reference(project)}"
end
end
context 'from non-Commit' do
it 'references the mentioning object' do
expect(subject.note).to eq "mentioned in task #{mentioned_in.to_reference(project)}"
end
end
end
context 'within the same project' do
context 'from Commit' do
let(:mentioned_in) { project.repository.commit }
it 'references the mentioning commit' do
expect(subject.note).to eq "mentioned in commit #{mentioned_in.to_reference}"
end
end
context 'from non-Commit' do
it 'references the mentioning object' do
expect(subject.note).to eq "mentioned in issue #{mentioned_in.to_reference}"
end
end
end
end
describe 'note_date' do
let(:mentioned_in) { project.repository.commit }
it 'uses commit date with USE_COMMIT_DATE_FOR_CROSS_REFERENCE_NOTE' do
stub_const("#{described_class}::USE_COMMIT_DATE_FOR_CROSS_REFERENCE_NOTE", true)
note = service.cross_reference(mentioned_in)
expect(note.created_at).to be_like_time(mentioned_in.created_at)
end
end
context 'with external issue' do
let(:noteable) { ExternalIssue.new('JIRA-123', project) }
let(:mentioned_in) { project.commit }
it 'queues a background worker' do
expect(Integrations::CreateExternalCrossReferenceWorker).to receive(:perform_async).with(
project.id,
'JIRA-123',
'Commit',
mentioned_in.id,
author.id
)
subject
end
end
end
end
describe '#cross_reference_exists?' do
let(:commit0) { project.commit }
let(:commit1) { project.commit('HEAD~2') }
context 'issue from commit' do
before do
# Mention issue (noteable) from commit0
service.cross_reference(commit0)
end
it 'is truthy when already mentioned' do
expect(service.cross_reference_exists?(commit0))
.to be_truthy
end
it 'is falsey when not already mentioned' do
expect(service.cross_reference_exists?(commit1))
.to be_falsey
end
context 'legacy capitalized cross reference' do
before do
# Mention issue (noteable) from commit0
system_note = service.cross_reference(commit0)
system_note.update!(note: system_note.note.capitalize)
end
it 'is truthy when already mentioned' do
expect(service.cross_reference_exists?(commit0))
.to be_truthy
end
end
end
context 'commit from commit' do
let(:service) { described_class.new(noteable: commit0, author: author) }
before do
# Mention commit1 from commit0
service.cross_reference(commit1)
end
it 'is truthy when already mentioned' do
expect(service.cross_reference_exists?(commit1))
.to be_truthy
end
it 'is falsey when not already mentioned' do
service = described_class.new(noteable: commit1, author: author)
expect(service.cross_reference_exists?(commit0))
.to be_falsey
end
context 'legacy capitalized cross reference' do
before do
# Mention commit1 from commit0
system_note = service.cross_reference(commit1)
system_note.update!(note: system_note.note.capitalize)
end
it 'is truthy when already mentioned' do
expect(service.cross_reference_exists?(commit1))
.to be_truthy
end
end
end
context 'commit with cross-reference from fork', :sidekiq_might_not_need_inline do
let(:author2) { create(:project_member, :reporter, user: create(:user), project: project).user }
let(:forked_project) { fork_project(project, author2, repository: true) }
let(:commit2) { forked_project.commit }
let(:service) { described_class.new(noteable: noteable, author: author2) }
before do
service.cross_reference(commit0)
end
it 'is true when a fork mentions an external issue' do
expect(service.cross_reference_exists?(commit2))
.to be true
end
context 'legacy capitalized cross reference' do
before do
system_note = service.cross_reference(commit0)
system_note.update!(note: system_note.note.capitalize)
end
it 'is true when a fork mentions an external issue' do
expect(service.cross_reference_exists?(commit2))
.to be true
end
end
end
end
describe '#change_task_status' do
let(:noteable) { create(:issue, project: project) }
let(:task) { double(:task, complete?: true, source: 'task') }
subject { service.change_task_status(task) }
it_behaves_like 'a system note' do
let(:action) { 'task' }
end
it "posts the 'marked the checklist item as complete' system note" do
expect(subject.note).to eq("marked the checklist item **task** as completed")
end
end
describe '#noteable_moved' do
let(:new_project) { create(:project) }
let(:new_noteable) { create(:issue, project: new_project) }
subject do
# service = described_class.new(noteable: noteable, project: project, author: author)
service.noteable_moved(new_noteable, direction)
end
shared_examples 'cross project mentionable' do
include MarkupHelper
it 'contains cross reference to new noteable' do
expect(subject.note).to include cross_project_reference(new_project, new_noteable)
end
it 'mentions referenced noteable' do
expect(subject.note).to include new_noteable.to_reference
end
it 'mentions referenced project' do
expect(subject.note).to include new_project.full_path
end
end
context 'moved to' do
let(:direction) { :to }
it_behaves_like 'cross project mentionable'
it_behaves_like 'a system note' do
let(:action) { 'moved' }
end
it 'notifies about noteable being moved to' do
expect(subject.note).to match('moved to')
end
end
context 'moved from' do
let(:direction) { :from }
it_behaves_like 'cross project mentionable'
it_behaves_like 'a system note' do
let(:action) { 'moved' }
end
it 'notifies about noteable being moved from' do
expect(subject.note).to match('moved from')
end
end
context 'invalid direction' do
let(:direction) { :invalid }
it 'raises error' do
expect { subject }.to raise_error StandardError, /Invalid direction/
end
end
end
describe '#noteable_cloned' do
let_it_be(:new_project) { create(:project) }
let_it_be(:new_noteable) { create(:issue, project: new_project) }
subject do
service.noteable_cloned(new_noteable, direction)
end
shared_examples 'cross project mentionable' do
include MarkupHelper
it 'contains cross reference to new noteable' do
expect(subject.note).to include cross_project_reference(new_project, new_noteable)
end
it 'mentions referenced noteable' do
expect(subject.note).to include new_noteable.to_reference
end
it 'mentions referenced project' do
expect(subject.note).to include new_project.full_path
end
end
context 'cloned to' do
let(:direction) { :to }
it_behaves_like 'cross project mentionable'
it_behaves_like 'a system note' do
let(:action) { 'cloned' }
end
it 'notifies about noteable being cloned to' do
expect(subject.note).to match('cloned to')
end
end
context 'cloned from' do
let(:direction) { :from }
it_behaves_like 'cross project mentionable'
it_behaves_like 'a system note' do
let(:action) { 'cloned' }
end
it 'notifies about noteable being cloned from' do
expect(subject.note).to match('cloned from')
end
end
context 'invalid direction' do
let(:direction) { :invalid }
it 'raises error' do
expect { subject }.to raise_error StandardError, /Invalid direction/
end
end
context 'custom created timestamp' do
let(:direction) { :from }
it 'allows setting of custom created_at value' do
timestamp = 1.day.ago
note = service.noteable_cloned(new_noteable, direction, created_at: timestamp)
expect(note.created_at).to be_like_time(timestamp)
end
it 'defaults to current time when created_at is not given', :freeze_time do
expect(subject.created_at).to be_like_time(Time.current)
end
end
context 'metrics', :clean_gitlab_redis_shared_state do
context 'cloned from' do
let(:direction) { :from }
it 'does not track usage' do
expect { subject }
.to not_trigger_internal_events(Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_CLONED)
.and not_increment_usage_metrics(
'redis_hll_counters.issues_edit.g_project_management_issue_cloned_monthly',
'redis_hll_counters.issues_edit.g_project_management_issue_cloned_weekly'
)
end
end
context 'cloned to' do
let(:direction) { :to }
it 'tracks internal events and increments usage metrics' do
expect { subject }
.to trigger_internal_events(Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_CLONED)
.with(project: project, user: author, category: 'InternalEventTracking')
.and increment_usage_metrics(
'redis_hll_counters.issues_edit.g_project_management_issue_cloned_monthly',
'redis_hll_counters.issues_edit.g_project_management_issue_cloned_weekly'
).by(1)
.and increment_usage_metrics(
# Cloner and original issue author are two unique users
# --> Not great that we're tracking the original author as an active user...
'redis_hll_counters.issues_edit.issues_edit_total_unique_counts_monthly',
'redis_hll_counters.issues_edit.issues_edit_total_unique_counts_weekly'
).by(2)
end
end
end
end
describe '#mark_duplicate_issue' do
subject { service.mark_duplicate_issue(canonical_issue) }
context 'within the same project' do
let(:canonical_issue) { create(:issue, project: project) }
it_behaves_like 'a system note' do
let(:action) { 'duplicate' }
end
it { expect(subject.note).to eq "marked this issue as a duplicate of #{canonical_issue.to_reference}" }
end
context 'across different projects' do
let(:other_project) { create(:project) }
let(:canonical_issue) { create(:issue, project: other_project) }
it_behaves_like 'a system note' do
let(:action) { 'duplicate' }
end
it { expect(subject.note).to eq "marked this issue as a duplicate of #{canonical_issue.to_reference(project)}" }
end
end
describe '#mark_canonical_issue_of_duplicate' do
subject { service.mark_canonical_issue_of_duplicate(duplicate_issue) }
context 'within the same project' do
let(:duplicate_issue) { create(:issue, project: project) }
it_behaves_like 'a system note' do
let(:action) { 'duplicate' }
end
it { expect(subject.note).to eq "marked #{duplicate_issue.to_reference} as a duplicate of this issue" }
end
context 'across different projects' do
let(:other_project) { create(:project) }
let(:duplicate_issue) { create(:issue, project: other_project) }
it_behaves_like 'a system note' do
let(:action) { 'duplicate' }
end
it { expect(subject.note).to eq "marked #{duplicate_issue.to_reference(project)} as a duplicate of this issue" }
end
end
describe '#email_participants' do
let(:body) { "added user@example.com" }
subject(:system_note) { service.email_participants(body) }
it { expect(system_note.note).to eq(body) }
end
describe '#discussion_lock' do
subject { service.discussion_lock }
context 'discussion unlocked' do
it_behaves_like 'a system note' do
let(:action) { 'unlocked' }
end
it 'creates the note text correctly' do
[:issue, :merge_request].each do |type|
issuable = create(type) # rubocop:disable Rails/SaveBang
service = described_class.new(noteable: issuable, author: author)
expect(service.discussion_lock.note)
.to eq("unlocked the discussion in this #{type.to_s.titleize.downcase}")
end
end
end
context 'discussion locked' do
before do
noteable.update_attribute(:discussion_locked, true)
end
it_behaves_like 'a system note' do
let(:action) { 'locked' }
end
it 'creates the note text correctly' do
[:issue, :merge_request].each do |type|
issuable = create(type, discussion_locked: true)
service = described_class.new(noteable: issuable, author: author)
expect(service.discussion_lock.note)
.to eq("locked the discussion in this #{type.to_s.titleize.downcase}")
end
end
end
end
describe '#cross_reference_disallowed?' do
context 'when mentioned_in is not a MergeRequest' do
it 'is falsey' do
mentioned_in = noteable.dup
expect(service.cross_reference_disallowed?(mentioned_in)).to be_falsey
end
end
context 'when mentioned_in is a MergeRequest' do
let(:mentioned_in) { create(:merge_request, :simple, source_project: project) }
let(:noteable) { project.commit }
it 'is truthy when noteable is in commits' do
expect(mentioned_in).to receive(:commits).and_return([noteable])
expect(service.cross_reference_disallowed?(mentioned_in)).to be_truthy
end
it 'is falsey when noteable is not in commits' do
expect(mentioned_in).to receive(:commits).and_return([])
expect(service.cross_reference_disallowed?(mentioned_in)).to be_falsey
end
end
context 'when notable is an ExternalIssue' do
let(:project) { create(:project) }
let(:noteable) { ExternalIssue.new('EXT-1234', project) }
it 'is false with issue tracker supporting referencing' do
create(:jira_integration, project: project)
project.reload
expect(service.cross_reference_disallowed?(noteable)).to be_falsey
end
it 'is true with issue tracker not supporting referencing' do
create(:bugzilla_integration, project: project)
project.reload
expect(service.cross_reference_disallowed?(noteable)).to be_truthy
end
it 'is true without issue tracker' do
expect(service.cross_reference_disallowed?(noteable)).to be_truthy
end
end
end
describe '#close_after_error_tracking_resolve' do
subject { service.close_after_error_tracking_resolve }
it 'creates the expected state event' do
subject
event = ResourceStateEvent.last
expect(event.close_after_error_tracking_resolve).to eq(true)
expect(event.state).to eq('closed')
end
end
describe '#auto_resolve_prometheus_alert' do
subject { service.auto_resolve_prometheus_alert }
it 'creates the expected state event' do
subject
event = ResourceStateEvent.last
expect(event.close_auto_resolve_prometheus_alert).to eq(true)
expect(event.state).to eq('closed')
end
end
describe '#change_issue_type' do
context 'with issue' do
let_it_be_with_reload(:noteable) { create(:issue, project: project) }
subject { service.change_issue_type('incident') }
it_behaves_like 'a system note' do
let(:action) { 'issue_type' }
end
it { expect(subject.note).to eq "changed type from incident to issue" }
end
context 'with work item' do
let_it_be_with_reload(:noteable) { create(:work_item, project: project) }
subject { service.change_issue_type('task') }
it_behaves_like 'a system note' do
let(:action) { 'issue_type' }
end
it { expect(subject.note).to eq "changed type from task to issue" }
end
end
describe '#hierarchy_changed' do
let_it_be_with_reload(:work_item) { create(:work_item, project: project) }
let_it_be_with_reload(:task) { create(:work_item, :task, project: project) }
let(:service) { described_class.new(noteable: work_item, container: project, author: author) }
subject { service.hierarchy_changed(task, hierarchy_change_action) }
context 'when task is added as a child' do
let(:hierarchy_change_action) { 'relate' }
it_behaves_like 'a system note' do
let(:expected_noteable) { task }
let(:action) { 'relate_to_parent' }
end
it 'sets the correct note text' do
expect { subject }.to change { Note.system.count }.by(2)
expect(work_item.notes.last.note).to eq("added ##{task.iid} as child task")
expect(task.notes.last.note).to eq("added ##{work_item.iid} as parent issue")
end
context 'when the parent belongs to a different namespace' do
let(:work_item) { create(:work_item, :group_level, namespace: group) }
it 'uses full references on the system notes' do
expect { subject }.to change { Note.system.count }.by(2)
expect(work_item.notes.last.note).to eq("added #{task.namespace.full_path}##{task.iid} as child task")
expect(task.notes.last.note).to eq("added #{work_item.namespace.full_path}##{work_item.iid} as parent issue")
end
end
end
context 'when child task is removed' do
let(:hierarchy_change_action) { 'unrelate' }
it_behaves_like 'a system note' do
let(:expected_noteable) { task }
let(:action) { 'unrelate_from_parent' }
end
it 'sets the correct note text' do
expect { subject }.to change { Note.system.count }.by(2)
expect(work_item.notes.last.note).to eq("removed child task ##{task.iid}")
expect(task.notes.last.note).to eq("removed parent issue ##{work_item.iid}")
end
context 'when the parent belongs to a different namespace' do
let(:work_item) { create(:work_item, :group_level, namespace: group) }
it 'uses full references on the system notes' do
expect { subject }.to change { Note.system.count }.by(2)
expect(work_item.notes.last.note).to eq("removed child task #{task.namespace.full_path}##{task.iid}")
expect(task.notes.last.note).to eq("removed parent issue #{work_item.namespace.full_path}##{work_item.iid}")
end
end
end
end
end
|