summaryrefslogtreecommitdiff
path: root/spec/tooling/danger/stable_branch_spec.rb
blob: b0a8ab3c1325c10b8309b9ade4591c39d03b19b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# frozen_string_literal: true

require 'gitlab-dangerfiles'
require 'gitlab/dangerfiles/spec_helper'
require 'rspec-parameterized'
require 'httparty'

require_relative '../../../tooling/danger/stable_branch'

RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do
  using RSpec::Parameterized::TableSyntax

  include_context 'with dangerfile'
  let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
  let(:fake_api) { double('Api') } # rubocop:disable RSpec/VerifiedDoubles
  let(:gitlab_gem_client) { double('gitlab') } # rubocop:disable RSpec/VerifiedDoubles

  let(:stable_branch) { fake_danger.new(helper: fake_helper) }

  describe '#check!' do
    subject { stable_branch.check! }

    shared_examples 'without a failure' do
      it 'does not add a failure' do
        expect(stable_branch).not_to receive(:fail)

        subject
      end
    end

    shared_examples 'with a failure' do |failure_message|
      it 'fails' do
        expect(stable_branch).to receive(:fail).with(failure_message)

        subject
      end
    end

    shared_examples 'with a warning' do |warning_message|
      it 'warns' do
        expect(stable_branch).to receive(:warn).with(warning_message)

        subject
      end
    end

    shared_examples 'bypassing when flaky test or docs only' do
      context 'when failure::flaky-test label is present' do
        let(:flaky_test_label_present) { true }

        it_behaves_like 'without a failure'
      end

      context 'with only docs changes' do
        let(:changes_by_category_response) { { docs: ['foo.md'] } }

        it_behaves_like 'without a failure'
      end
    end

    context 'when not applicable' do
      where(:stable_branch?, :security_mr?) do
        true  | true
        false | true
        false | false
      end

      with_them do
        before do
          allow(fake_helper).to receive(:mr_target_branch).and_return(stable_branch? ? '15-1-stable-ee' : 'main')
          allow(fake_helper).to receive(:security_mr?).and_return(security_mr?)
        end

        it_behaves_like 'without a failure'
      end
    end

    context 'when applicable' do
      let(:target_branch) { '15-1-stable-ee' }
      let(:source_branch) { 'my_bug_branch' }
      let(:feature_label_present) { false }
      let(:bug_label_present) { true }
      let(:pipeline_expedite_label_present) { false }
      let(:flaky_test_label_present) { false }
      let(:response_success) { true }

      let(:changes_by_category_response) do
        {
          graphql: ['bar.rb']
        }
      end

      let(:pipeline_bridges_response) do
        [
          {
            'name' => 'e2e:package-and-test-ee',
            'status' => pipeline_bridge_state,
            'downstream_pipeline' => {
              'id' => '123',
              'status' => package_and_qa_state
            }
          }
        ]
      end

      let(:pipeline_bridge_state) { 'running' }
      let(:package_and_qa_state) { 'success' }

      let(:parsed_response) do
        [
          { 'version' => '15.1.1' },
          { 'version' => '15.1.0' },
          { 'version' => '15.0.2' },
          { 'version' => '15.0.1' },
          { 'version' => '15.0.0' },
          { 'version' => '14.10.3' },
          { 'version' => '14.10.2' },
          { 'version' => '14.9.3' }
        ]
      end

      let(:version_response) do
        instance_double(
          HTTParty::Response,
          success?: response_success,
          parsed_response: parsed_response
        )
      end

      before do
        allow(fake_helper).to receive(:mr_target_branch).and_return(target_branch)
        allow(fake_helper).to receive(:mr_source_branch).and_return(source_branch)
        allow(fake_helper).to receive(:security_mr?).and_return(false)
        allow(fake_helper).to receive(:mr_target_project_id).and_return(1)
        allow(fake_helper).to receive(:mr_has_labels?).with('type::feature').and_return(feature_label_present)
        allow(fake_helper).to receive(:mr_has_labels?).with('type::bug').and_return(bug_label_present)
        allow(fake_helper).to receive(:mr_has_labels?).with('pipeline:expedite')
          .and_return(pipeline_expedite_label_present)
        allow(fake_helper).to receive(:mr_has_labels?).with('failure::flaky-test')
          .and_return(flaky_test_label_present)
        allow(fake_helper).to receive(:changes_by_category).and_return(changes_by_category_response)
        allow(HTTParty).to receive(:get).with(/page=1/).and_return(version_response)
        allow(fake_helper).to receive(:api).and_return(fake_api)
        allow(stable_branch).to receive(:gitlab).and_return(gitlab_gem_client)
        allow(gitlab_gem_client).to receive(:mr_json).and_return({ 'head_pipeline' => { 'id' => '1' } })
        allow(gitlab_gem_client).to receive(:api).and_return(fake_api)
        allow(fake_api).to receive(:pipeline_bridges).with(1, '1')
          .and_return(pipeline_bridges_response)
      end

      # the stubbed behavior above is the success path
      it_behaves_like 'without a failure'

      context 'with a feature label' do
        let(:feature_label_present) { true }

        it_behaves_like 'with a failure', described_class::FEATURE_ERROR_MESSAGE
      end

      context 'without a bug label' do
        let(:bug_label_present) { false }

        it_behaves_like 'with a failure', described_class::BUG_ERROR_MESSAGE
      end

      context 'with only documentation changes and no bug label' do
        let(:bug_label_present) { false }
        let(:changes_by_category_response) { { docs: ['foo.md'] } }

        it_behaves_like 'without a failure'
      end

      context 'with a pipeline:expedite label' do
        let(:pipeline_expedite_label_present) { true }

        it_behaves_like 'with a failure', described_class::PIPELINE_EXPEDITE_ERROR_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when no package-and-test bridge is found' do
        let(:pipeline_bridges_response) { nil }

        it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when package-and-test bridge is created' do
        let(:pipeline_bridge_state) { 'created' }

        it_behaves_like 'with a warning', described_class::WARN_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when package-and-test bridge has been canceled and no downstream pipeline is generated' do
        let(:pipeline_bridge_state) { 'canceled' }

        let(:pipeline_bridges_response) do
          [
            {
              'name' => 'e2e:package-and-test-ee',
              'status' => pipeline_bridge_state,
              'downstream_pipeline' => nil
            }
          ]
        end

        it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when package-and-test job is in a non-successful state' do
        let(:package_and_qa_state) { 'running' }

        it_behaves_like 'with a warning', described_class::WARN_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when package-and-test job is in manual state' do
        let(:package_and_qa_state) { 'manual' }

        it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when package-and-test job is canceled' do
        let(:package_and_qa_state) { 'canceled' }

        it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when no pipeline is found' do
        before do
          allow(gitlab_gem_client).to receive(:mr_json).and_return({})
        end

        it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE
        it_behaves_like 'bypassing when flaky test or docs only'
      end

      context 'when not an applicable version' do
        let(:target_branch) { '14-9-stable-ee' }

        it_behaves_like 'with a warning', described_class::VERSION_WARNING_MESSAGE
      end

      context 'when the version API request fails' do
        let(:response_success) { false }

        it_behaves_like 'with a warning', described_class::FAILED_VERSION_REQUEST_MESSAGE
      end

      context 'when more than one page of versions is needed' do
        # we target a version we know will not be returned in the first request
        let(:target_branch) { '14-10-stable-ee' }

        let(:first_version_response) do
          instance_double(
            HTTParty::Response,
            success?: response_success,
            parsed_response: [
              { 'version' => '15.1.1' },
              { 'version' => '15.1.0' },
              { 'version' => '15.0.2' },
              { 'version' => '15.0.1' }
            ]
          )
        end

        let(:second_version_response) do
          instance_double(
            HTTParty::Response,
            success?: response_success,
            parsed_response: [
              { 'version' => '15.0.0' },
              { 'version' => '14.10.3' },
              { 'version' => '14.10.2' },
              { 'version' => '14.9.3' }
            ]
          )
        end

        before do
          allow(HTTParty).to receive(:get).with(/page=1/).and_return(first_version_response)
          allow(HTTParty).to receive(:get).with(/page=2/).and_return(second_version_response)
        end

        it_behaves_like 'without a failure'
      end

      context 'when too many version API requests are made' do
        let(:parsed_response) { [{ 'version' => '15.0.0' }] }

        it 'adds a warning' do
          expect(HTTParty).to receive(:get).and_return(version_response).at_least(10).times
          expect(stable_branch).to receive(:warn).with(described_class::FAILED_VERSION_REQUEST_MESSAGE)

          subject
        end
      end
    end
  end

  describe '#encourage_package_and_qa_execution?' do
    subject { stable_branch.encourage_package_and_qa_execution? }

    where(:stable_branch?, :security_mr?, :documentation?, :flaky?, :result) do
      # security merge requests
      true  | true  | true  | true  | false
      true  | true  | true  | false | false
      true  | true  | false | true  | false
      true  | true  | false | false | false
      # canonical merge requests with doc and flaky changes only
      true  | false | true  | true  | false
      true  | false | true  | false | false
      true  | false | false | true  | false
      # canonical merge requests with app code
      true  | false | false | false | true
    end

    with_them do
      before do
        allow(fake_helper)
          .to receive(:mr_target_branch)
          .and_return(stable_branch? ? '15-1-stable-ee' : 'main')

        allow(fake_helper)
          .to receive(:security_mr?)
          .and_return(security_mr?)

        allow(fake_helper)
          .to receive(:has_only_documentation_changes?)
          .and_return(documentation?)

        changes_by_category =
          if documentation?
            { docs: ['foo.md'] }
          else
            { graphql: ['bar.rb'] }
          end

        allow(fake_helper)
          .to receive(:changes_by_category)
          .and_return(changes_by_category)

        allow(fake_helper)
          .to receive(:mr_has_labels?)
          .and_return(flaky?)
      end

      it { is_expected.to eq(result) }
    end
  end
end