build_service_spec.rb 5.37 KB
Newer Older
1 2
require 'spec_helper.rb'

3
describe Issues::BuildService do
4
  let(:project) { create(:project, :repository) }
5 6 7
  let(:user) { create(:user) }

  before do
8
    project.add_developer(user)
9 10
  end

11 12 13 14
  def build_issue(issue_params = {})
    described_class.new(project, user, issue_params).execute
  end

15 16
  context 'for a single discussion' do
    describe '#execute' do
17
      let(:merge_request) { create(:merge_request, title: "Hello world", source_project: project) }
Douwe Maan's avatar
Douwe Maan committed
18
      let(:discussion) { create(:diff_note_on_merge_request, project: project, noteable: merge_request, note: "Almost done").to_discussion }
19

20
      subject { build_issue(merge_request_to_resolve_discussions_of: merge_request.iid, discussion_to_resolve: discussion.id) }
21

22 23
      it 'references the noteable title in the issue title' do
        expect(subject.title).to include('Hello world')
24 25 26
      end

      it 'adds the note content to the description' do
27
        expect(subject.description).to include('Almost done')
28 29 30 31
      end
    end
  end

32 33 34 35 36
  context 'for discussions in a merge request' do
    let(:merge_request) { create(:merge_request_with_diff_notes, source_project: project) }

    describe '#items_for_discussions' do
      it 'has an item for each discussion' do
37
        create(:diff_note_on_merge_request, noteable: merge_request, project: merge_request.source_project, line_number: 13)
Bob Van Landuyt's avatar
Bob Van Landuyt committed
38
        service = described_class.new(project, user, merge_request_to_resolve_discussions_of: merge_request.iid)
39 40 41 42 43 44 45 46

        service.execute

        expect(service.items_for_discussions.size).to eq(2)
      end
    end

    describe '#item_for_discussion' do
Bob Van Landuyt's avatar
Bob Van Landuyt committed
47
      let(:service) { described_class.new(project, user, merge_request_to_resolve_discussions_of: merge_request.iid) }
48 49

      it 'mentions the author of the note' do
Douwe Maan's avatar
Douwe Maan committed
50
        discussion = create(:diff_note_on_merge_request, author: create(:user, username: 'author')).to_discussion
51 52 53 54 55 56 57 58 59
        expect(service.item_for_discussion(discussion)).to include('@author')
      end

      it 'wraps the note in a blockquote' do
        note_text = "This is a string\n"\
                    ">>>\n"\
                    "with a blockquote\n"\
                    "> That has a quote\n"\
                    ">>>\n"
60 61 62
        note_result = "    > This is a string\n"\
                      "    > > with a blockquote\n"\
                      "    > > > That has a quote\n"
Douwe Maan's avatar
Douwe Maan committed
63
        discussion = create(:diff_note_on_merge_request, note: note_text).to_discussion
64
        expect(service.item_for_discussion(discussion)).to include(note_result)
65 66 67 68
      end
    end

    describe '#execute' do
69
      let(:base_params) { { merge_request_to_resolve_discussions_of: merge_request.iid } }
70

71 72 73 74 75 76 77 78 79 80
      context 'without additional params' do
        subject { build_issue(base_params) }

        it 'has the merge request reference in the title' do
          expect(subject.title).to include(merge_request.title)
        end

        it 'has the reference of the merge request in the description' do
          expect(subject.description).to include(merge_request.to_reference)
        end
81 82
      end

83 84
      it 'uses provided title if title param given' do
        issue = build_issue(base_params.merge(title: 'What an issue'))
85 86 87 88

        expect(issue.title).to eq('What an issue')
      end

89 90
      it 'uses provided description if description param given' do
        issue = build_issue(base_params.merge(description: 'Fix at your earliest convenience'))
91

92
        expect(issue.description).to eq('Fix at your earliest convenience')
93 94 95
      end

      describe 'with multiple discussions' do
Douwe Maan's avatar
Douwe Maan committed
96
        let!(:diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: merge_request.target_project, line_number: 15) }
97 98

        it 'mentions all the authors in the description' do
99
          authors = merge_request.resolvable_discussions.map(&:author)
100

101
          expect(build_issue(base_params).description).to include(*authors.map(&:to_reference))
102 103 104
        end

        it 'has a link for each unresolved discussion in the description' do
105
          notes = merge_request.resolvable_discussions.map(&:first_note)
106 107
          links = notes.map { |note| Gitlab::UrlBuilder.build(note) }

108
          expect(build_issue(base_params).description).to include(*links)
109 110 111
        end

        it 'mentions additional notes' do
Douwe Maan's avatar
Douwe Maan committed
112
          create_list(:diff_note_on_merge_request, 2, noteable: merge_request, project: merge_request.target_project, in_reply_to: diff_note)
113

114
          expect(build_issue(base_params).description).to include('(+2 comments)')
115 116 117 118 119 120 121 122 123 124
        end
      end
    end
  end

  context 'For a merge request without discussions' do
    let(:merge_request) { create(:merge_request, source_project: project) }

    describe '#execute' do
      it 'mentions the merge request in the description' do
125
        issue = build_issue(merge_request_to_resolve_discussions_of: merge_request.iid)
126 127 128 129 130 131 132 133

        expect(issue.description).to include("Review the conversation in #{merge_request.to_reference}")
      end
    end
  end

  describe '#execute' do
    it 'builds a new issues with given params' do
134 135 136
      milestone = create(:milestone, project: project)
      issue = build_issue(milestone_id: milestone.id)

137
      expect(issue.milestone).to eq(milestone)
138
    end
139 140 141 142 143 144 145

    it 'sets milestone to nil if it is not available for the project' do
      milestone = create(:milestone, project: create(:project))
      issue = build_issue(milestone_id: milestone.id)

      expect(issue.milestone).to be_nil
    end
146 147
  end
end