build_service_spec.rb 5.55 KB
Newer Older
1 2 3
require 'spec_helper.rb'

describe Issues::BuildService, services: true do
4
  let(:project) { create(:project, :repository) }
5 6 7 8 9 10
  let(:user) { create(:user) }

  before do
    project.team << [user, :developer]
  end

11 12
  context 'for a single discussion' do
    describe '#execute' do
13
      let(:merge_request) { create(:merge_request, title: "Hello world", source_project: project) }
Douwe Maan's avatar
Douwe Maan committed
14
      let(:discussion) { create(:diff_note_on_merge_request, project: project, noteable: merge_request, note: "Almost done").to_discussion }
Bob Van Landuyt's avatar
Bob Van Landuyt committed
15
      let(:service) { described_class.new(project, user, merge_request_to_resolve_discussions_of: merge_request.iid, discussion_to_resolve: discussion.id) }
16

17
      it 'references the noteable title in the issue title' do
18 19 20 21 22 23 24 25 26 27 28 29 30
        issue = service.execute

        expect(issue.title).to include('Hello world')
      end

      it 'adds the note content to the description' do
        issue = service.execute

        expect(issue.description).to include('Almost done')
      end
    end
  end

31 32
  context 'for discussions in a merge request' do
    let(:merge_request) { create(:merge_request_with_diff_notes, source_project: project) }
Bob Van Landuyt's avatar
Bob Van Landuyt committed
33
    let(:issue) { described_class.new(project, user, merge_request_to_resolve_discussions_of: merge_request.iid).execute }
34 35 36

    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 69 70 71 72 73 74 75 76 77 78
      end
    end

    describe '#execute' do
      it 'has the merge request reference in the title' do
        expect(issue.title).to include(merge_request.title)
      end

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

      it 'does not assign title when a title was given' do
        issue = described_class.new(project, user,
Bob Van Landuyt's avatar
Bob Van Landuyt committed
79
                                    merge_request_to_resolve_discussions_of: merge_request,
80 81 82 83 84 85 86
                                    title: 'What an issue').execute

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

      it 'does not assign description when a description was given' do
        issue = described_class.new(project, user,
Bob Van Landuyt's avatar
Bob Van Landuyt committed
87
                                    merge_request_to_resolve_discussions_of: merge_request,
88 89 90 91 92 93
                                    description: 'Fix at your earliest conveignance').execute

        expect(issue.description).to eq('Fix at your earliest conveignance')
      end

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

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

          expect(issue.description).to include(*authors.map(&:to_reference))
        end

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

          expect(issue.description).to include(*links)
        end

        it 'mentions additional notes' do
Douwe Maan's avatar
Douwe Maan committed
110
          create_list(:diff_note_on_merge_request, 2, noteable: merge_request, project: merge_request.target_project, in_reply_to: diff_note)
111 112 113 114 115 116 117 118 119 120 121 122

          expect(issue.description).to include('(+2 comments)')
        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
Bob Van Landuyt's avatar
Bob Van Landuyt committed
123
        issue = described_class.new(project, user, merge_request_to_resolve_discussions_of: merge_request.iid).execute
124 125 126 127 128 129 130

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

  describe '#execute' do
131 132
    let(:milestone) { create(:milestone, project: project) }

133
    it 'builds a new issues with given params' do
134 135 136 137 138
      issue = described_class.new(
        project,
        user,
        title: 'Issue #1',
        description: 'Issue description',
139
        milestone_id: milestone.id
140
      ).execute
141 142 143

      expect(issue.title).to eq('Issue #1')
      expect(issue.description).to eq('Issue description')
144
      expect(issue.milestone).to eq(milestone)
145 146 147
    end
  end
end