upstream_merge_service_spec.rb 4.95 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
describe ReleaseTools::Services::UpstreamMergeService do
6 7
  let(:upstream_merge) { double(execute!: []) }

8
  around do |example|
9
    Timecop.freeze('2017-11-15 18:12 UTC') do
10 11 12 13
      example.run
    end
  end

14
  shared_context 'stub collaborators' do
15
    before do
16
      expect(ReleaseTools::UpstreamMergeRequest).to receive(:new)
17 18
        .with(mention_people: subject.mention_people).and_call_original

19
      expect(ReleaseTools::UpstreamMerge).to receive(:new)
20
        .with(
21 22
          origin: ReleaseTools::Project::GitlabEe.remotes[:gitlab],
          upstream: ReleaseTools::Project::GitlabCe.remotes[:gitlab],
John Skarbek's avatar
John Skarbek committed
23
          merge_branch: 'ce-to-ee-2017-11-15'
24
        ).and_return(upstream_merge)
25
    end
26 27
  end

28 29
  shared_context 'without conflicts' do
    before do
30
      allow(subject.upstream_merge_request).to receive(:conflicts?).and_return(false)
31 32 33 34 35
    end
  end

  shared_context 'with conflicts' do
    before do
36
      allow(subject.upstream_merge_request).to receive(:conflicts?).and_return(true)
37 38 39
    end
  end

40 41
  shared_examples 'successful MR creation without automatic acceptance' do
    include_context 'stub collaborators'
42
    include_context 'with conflicts'
43 44 45 46 47 48 49 50 51 52 53 54 55

    it 'returns a successful result object' do
      expect(subject.upstream_merge_request).to receive(:create)
      expect(subject.upstream_merge_request).not_to receive(:accept)

      result = subject.perform

      expect(result).to be_success
      expect(result.payload).to eq({ upstream_mr: subject.upstream_merge_request })
    end
  end

  shared_examples 'successful MR creation and automatic acceptance' do
56
    include_context 'stub collaborators'
57
    include_context 'without conflicts'
58 59

    it 'returns a successful result object' do
60
      expect(subject.upstream_merge_request).to receive(:create)
61 62
      expect(subject.upstream_merge_request).to receive(:approve)
      expect(subject.upstream_merge_request).to receive(:accept)
63

64
      result = subject.perform
65 66

      expect(result).to be_success
67 68 69 70 71 72 73 74 75
      expect(result.payload).to eq({ upstream_mr: subject.upstream_merge_request })
    end
  end

  shared_examples 'dry-run MR creation' do
    include_context 'stub collaborators'

    it 'returns a successful result object' do
      expect(subject.upstream_merge_request).not_to receive(:create)
76
      expect(subject.upstream_merge_request).not_to receive(:accept)
77 78 79 80 81

      result = subject.perform

      expect(result).to be_success
      expect(result.payload).to eq({ upstream_mr: subject.upstream_merge_request })
82 83 84 85
    end
  end

  describe '#perform' do
86
    context 'when open upstream MR exists', vcr: { cassette_name: 'merge_requests/existing_upstream_mr' } do
87 88 89 90 91
      context 'when not forced' do
        it 'returns a non-successful result object' do
          result = subject.perform

          expect(result).not_to be_success
92 93

          in_progress_mr = result.payload[:in_progress_mr]
94
          expect(in_progress_mr).to be_an_instance_of(ReleaseTools::UpstreamMergeRequest)
95 96
          expect(in_progress_mr.created_at).to be_an_instance_of(Time)
          expect(in_progress_mr.url).to eq('https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/4023')
97 98 99 100
        end
      end

      context 'when forced' do
101 102
        subject { described_class.new(force: true) }

103
        before do
104
          expect(ReleaseTools::UpstreamMergeRequest).not_to receive(:open_mrs)
105 106
        end

107
        context 'when real run (default)' do
108 109
          it_behaves_like 'successful MR creation and automatic acceptance'
          it_behaves_like 'successful MR creation without automatic acceptance'
110 111 112 113 114 115
        end

        context 'when dry run' do
          subject { described_class.new(dry_run: true, force: true) }

          before do
116
            expect(ReleaseTools::UpstreamMergeRequest).not_to receive(:open_mrs)
117 118 119
          end

          it_behaves_like 'dry-run MR creation'
120 121 122 123 124
        end
      end
    end

    context 'when no upstream MR exist' do
125
      before do
126
        expect(ReleaseTools::UpstreamMergeRequest).to receive(:open_mrs).and_return([])
127 128
      end

129
      context 'when real run (default)' do
130
        it_behaves_like 'successful MR creation and automatic acceptance'
131 132 133
      end

      context 'when dry run' do
134 135 136
        subject { described_class.new(dry_run: true) }

        it_behaves_like 'dry-run MR creation'
137 138 139
      end

      context 'when mentioning people' do
140 141
        subject { described_class.new(mention_people: true) }

142
        it_behaves_like 'successful MR creation and automatic acceptance'
143
      end
144 145 146 147 148

      context 'when downstream is already up-to-date with upstream' do
        include_context 'stub collaborators'

        before do
149
          expect(upstream_merge).to receive(:execute!).and_raise(ReleaseTools::UpstreamMerge::DownstreamAlreadyUpToDate)
150 151 152 153 154 155 156 157 158
        end

        it 'returns a non-successful result object' do
          result = subject.perform

          expect(result).not_to be_success
          expect(result.payload[:already_up_to_date]).to be(true)
        end
      end
159 160 161
    end
  end
end