BigW Consortium Gitlab

merge_when_pipeline_succeeds_spec.rb 5.32 KB
Newer Older
1 2
require 'spec_helper'

3
feature 'Merge When Pipeline Succeeds', :feature, :js do
4
  let(:user) { create(:user) }
5
  let(:project) { create(:project, :public) }
6

7 8 9 10 11
  let(:merge_request) do
    create(:merge_request_with_diffs, source_project: project,
                                      author: user,
                                      title: 'Bug NS-04')
  end
12

13 14 15 16
  let(:pipeline) do
    create(:ci_pipeline, project: project,
                         sha: merge_request.diff_head_sha,
                         ref: merge_request.source_branch)
17 18
  end

19 20
  before { project.team << [user, :master] }

21
  context 'when there is active pipeline for merge request' do
22 23 24
    background do
      create(:ci_build, pipeline: pipeline)
    end
25

26
    before do
27 28 29 30
      login_as user
      visit_merge_request(merge_request)
    end

31 32
    it 'displays the Merge when pipeline succeeds button' do
      expect(page).to have_button "Merge when pipeline succeeds"
33 34
    end

35 36 37 38
    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"
39 40 41

          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."
42
          expect(page).to have_link "Cancel automatic merge"
43 44 45
          visit_merge_request(merge_request) # Needed to refresh the page
          expect(page).to have_content /enabled an automatic merge when the pipeline for \h{8} succeeds/i
        end
46 47
      end

48
      context "when enabled immediately" do
49
        it_behaves_like 'Merge when pipeline succeeds activator'
50 51 52 53 54
      end

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

56 57 58 59 60 61 62
          # 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

63
        it_behaves_like 'Merge when pipeline succeeds activator'
64 65 66 67
      end

      context 'when enabled after it was previously canceled' do
        before do
68 69
          click_button "Merge when pipeline succeeds"
          click_link "Cancel automatic merge"
70 71
        end

72
        it_behaves_like 'Merge when pipeline succeeds activator'
73
      end
74

75 76 77
      context 'when it was enabled and then canceled' do
        let(:merge_request) do
          create(:merge_request_with_diffs,
78
                 :merge_when_pipeline_succeeds,
79 80 81 82 83 84 85
                   source_project: project,
                   title: 'Bug NS-04',
                   author: user,
                   merge_user: user)
        end

        before do
86
          click_link "Cancel automatic merge"
87 88
        end

89
        it_behaves_like 'Merge when pipeline succeeds activator'
90 91
      end
    end
92 93 94 95 96 97 98 99 100 101 102 103 104

    describe 'enabling Merge when pipeline succeeds via dropdown' do
      it 'activates the Merge when pipeline succeeds feature' do
        click_button 'Select merge moment'
        within('.js-merge-dropdown') do
          click_link 'Merge when pipeline succeeds'
        end

        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."
        expect(page).to have_link "Cancel automatic merge"
      end
    end
105 106
  end

107
  context 'when merge when pipeline succeeds is enabled' do
108
    let(:merge_request) do
109 110 111 112
      create(:merge_request_with_diffs, :simple,  source_project: project,
                                                  author: user,
                                                  merge_user: user,
                                                  title: 'MepMep',
113
                                                  merge_when_pipeline_succeeds: true)
114
    end
115

116
    let!(:build) do
117 118
      create(:ci_build, pipeline: pipeline)
    end
119

120 121 122 123 124
    before do
      login_as user
      visit_merge_request(merge_request)
    end

125
    it 'allows to cancel the automatic merge' do
126
      click_link "Cancel automatic merge"
127

128
      expect(page).to have_button "Merge when pipeline succeeds"
129

130
      visit_merge_request(merge_request) # refresh the page
131
      expect(page).to have_content "canceled the automatic merge"
132 133 134
    end

    it "allows the user to remove the source branch" do
135
      expect(page).to have_link "Remove source branch when merged"
136

137
      click_link "Remove source branch when merged"
138
      expect(page).to have_content "The source branch will be removed"
139
    end
140

141
    context 'when pipeline succeeds' do
142 143 144 145 146 147 148 149 150
      background { build.success }

      it 'merges merge request' do
        visit_merge_request(merge_request) # refresh the page

        expect(page).to have_content 'The changes were merged'
        expect(merge_request.reload).to be_merged
      end
    end
151 152
  end

153 154
  context 'when pipeline is not active' do
    it "does not allow to enable merge when pipeline succeeds" do
155
      visit_merge_request(merge_request)
156

157
      expect(page).not_to have_link 'Merge when pipeline succeeds'
158 159
    end
  end
160

161 162 163 164
  def visit_merge_request(merge_request)
    visit namespace_project_merge_request_path(merge_request.project.namespace, merge_request.project, merge_request)
  end
end