user_merges_when_pipeline_succeeds_spec.rb 5.99 KB
Newer Older
1
require 'rails_helper'
2

3
describe 'Merge request > User merges when pipeline succeeds', :js do
4
  let(:project) { create(:project, :public, :repository) }
5
  let(:user) { project.creator }
6 7 8
  let(:merge_request) do
    create(:merge_request_with_diffs, source_project: project,
                                      author: user,
9 10
                                      title: 'Bug NS-04',
                                      merge_params: { force_remove_source_branch: '1' })
11 12 13 14
  end
  let(:pipeline) do
    create(:ci_pipeline, project: project,
                         sha: merge_request.diff_head_sha,
15 16
                         ref: merge_request.source_branch,
                         head_pipeline_of: merge_request)
17 18
  end

Felipe's avatar
Felipe committed
19 20 21
  before do
    project.add_master(user)
  end
22

23
  context 'when there is active pipeline for merge request' do
24
    before do
25 26 27
      create(:ci_build, pipeline: pipeline)
      sign_in(user)
      visit project_merge_request_path(project, merge_request)
28 29
    end

30 31 32 33
    describe 'enabling Merge when pipeline succeeds' do
      shared_examples 'Merge when pipeline succeeds activator' do
        it 'activates the Merge when pipeline succeeds feature' do
          click_button "Merge when pipeline succeeds"
34

35 36
          expect(page).to have_content "Set by #{user.name} to be merged automatically when the pipeline succeeds"
          expect(page).to have_content "The source branch will not be removed"
37
          expect(page).to have_selector ".js-cancel-auto-merge"
38
          visit project_merge_request_path(project, merge_request) # Needed to refresh the page
39 40
          expect(page).to have_content /enabled an automatic merge when the pipeline for \h{8} succeeds/i
        end
41 42
      end

43
      context "when enabled immediately" do
44
        it_behaves_like 'Merge when pipeline succeeds activator'
45 46 47 48 49
      end

      context 'when enabled after pipeline status changed' do
        before do
          pipeline.run!
50

51 52 53 54 55 56 57
          # We depend on merge request widget being reloaded
          # so we have to wait for asynchronous call to reload it
          # and have_content expectation handles that.
          #
          expect(page).to have_content "Pipeline ##{pipeline.id} running"
        end

58
        it_behaves_like 'Merge when pipeline succeeds activator'
59 60 61 62
      end

      context 'when enabled after it was previously canceled' do
        before do
63 64
          click_button "Merge when pipeline succeeds"
          click_link "Cancel automatic merge"
65 66
        end

67
        it_behaves_like 'Merge when pipeline succeeds activator'
68
      end
69

70 71 72
      context 'when it was enabled and then canceled' do
        let(:merge_request) do
          create(:merge_request_with_diffs,
73
                 :merge_when_pipeline_succeeds,
74 75 76
                   source_project: project,
                   title: 'Bug NS-04',
                   author: user,
77 78
                   merge_user: user,
                   merge_params: { force_remove_source_branch: '1' })
79 80 81
        end

        before do
82
          click_link "Cancel automatic merge"
83 84
        end

85
        it_behaves_like 'Merge when pipeline succeeds activator'
86 87
      end
    end
88 89 90

    describe 'enabling Merge when pipeline succeeds via dropdown' do
      it 'activates the Merge when pipeline succeeds feature' do
91
        find('.js-merge-moment').click
92
        click_link 'Merge when pipeline succeeds'
93

94 95
        expect(page).to have_content "Set by #{user.name} to be merged automatically when the pipeline succeeds"
        expect(page).to have_content "The source branch will not be removed"
96 97 98
        expect(page).to have_link "Cancel automatic merge"
      end
    end
99 100
  end

101
  context 'when merge when pipeline succeeds is enabled' do
102
    let(:merge_request) do
103 104 105 106
      create(:merge_request_with_diffs, :simple,  source_project: project,
                                                  author: user,
                                                  merge_user: user,
                                                  title: 'MepMep',
107
                                                  merge_when_pipeline_succeeds: true)
108
    end
109
    let!(:build) do
110 111
      create(:ci_build, pipeline: pipeline)
    end
112

113
    before do
114
      sign_in user
115
      visit project_merge_request_path(project, merge_request)
116 117
    end

118
    it 'allows to cancel the automatic merge' do
119
      click_link "Cancel automatic merge"
120

121
      expect(page).to have_button "Merge when pipeline succeeds"
122

123 124
      refresh

125
      expect(page).to have_content "canceled the automatic merge"
126 127
    end

128
    context 'when pipeline succeeds' do
129 130 131 132
      before do
        build.success
        refresh
      end
133 134 135 136 137 138

      it 'merges merge request' do
        expect(page).to have_content 'The changes were merged'
        expect(merge_request.reload).to be_merged
      end
    end
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

    context 'view merge request with MWPS enabled but automatically merge fails' do
      before do
        merge_request.update(
          merge_user: merge_request.author,
          merge_error: 'Something went wrong'
        )
        refresh
      end

      it 'shows information about the merge error' do
        # Wait for the `ci_status` and `merge_check` requests
        wait_for_requests

        page.within('.mr-widget-body') do
          expect(page).to have_content('Something went wrong')
        end
      end
    end

    context 'view merge request with MWPS enabled but automatically merge fails' do
      before do
        merge_request.update(
          merge_user: merge_request.author,
          merge_error: 'Something went wrong'
        )
        refresh
      end

      it 'shows information about the merge error' do
        # Wait for the `ci_status` and `merge_check` requests
        wait_for_requests

        page.within('.mr-widget-body') do
          expect(page).to have_content('Something went wrong')
        end
      end
    end
177 178
  end

179
  context 'when pipeline is not active' do
180 181
    it 'does not allow to enable merge when pipeline succeeds' do
      visit project_merge_request_path(project, merge_request)
182

183
      expect(page).not_to have_link 'Merge when pipeline succeeds'
184 185 186
    end
  end
end