summaryrefslogtreecommitdiff
path: root/spec/services/merge_requests/add_todo_when_build_fails_service_spec.rb
blob: 39a2ef579dddd368f5102b67da56f6a71d9524c6 (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
require 'spec_helper'

describe MergeRequests::AddTodoWhenBuildFailsService do
  let(:user) { create(:user) }
  let(:merge_request) { create(:merge_request) }
  let(:project) { create(:project, :repository) }
  let(:sha) { '1234567890abcdef1234567890abcdef12345678' }
  let(:ref) { merge_request.source_branch }

  let(:pipeline) do
    create(:ci_pipeline_with_one_job, ref: ref,
                                      project: project,
                                      sha: sha)
  end

  let(:service) do
    described_class.new(project, user, commit_message: 'Awesome message')
  end

  let(:todo_service) { spy('todo service') }

  let(:merge_request) do
    create(:merge_request, merge_user: user,
                           source_branch: 'master',
                           target_branch: 'feature',
                           source_project: project,
                           target_project: project,
                           state: 'opened')
  end

  before do
    allow_any_instance_of(MergeRequest)
      .to receive(:head_pipeline)
      .and_return(pipeline)

    allow(service).to receive(:todo_service).and_return(todo_service)
  end

  describe '#execute' do
    context 'commit status with ref' do
      let(:commit_status) do
        create(:generic_commit_status, ref: ref, pipeline: pipeline)
      end

      it 'notifies the todo service' do
        expect(todo_service).to receive(:merge_request_build_failed).with(merge_request)
        service.execute(commit_status)
      end
    end

    context 'commit status with non-HEAD ref' do
      let(:commit_status) { create(:generic_commit_status, ref: ref) }

      it 'does not notify the todo service' do
        expect(todo_service).not_to receive(:merge_request_build_failed)
        service.execute(commit_status)
      end
    end

    context 'commit status without ref' do
      let(:commit_status) { create(:generic_commit_status) }

      it 'does not notify the todo service' do
        expect(todo_service).not_to receive(:merge_request_build_failed)
        service.execute(commit_status)
      end
    end

    context 'when commit status is a build allowed to fail' do
      let(:commit_status) do
        create(:ci_build, :allowed_to_fail, ref: ref, pipeline: pipeline)
      end

      it 'does not create todo' do
        expect(todo_service).not_to receive(:merge_request_build_failed)

        service.execute(commit_status)
      end
    end

    context 'when build belongs to a merge request pipeline' do
      let(:pipeline) do
        create(:ci_pipeline, source: :merge_request_event,
                             ref: merge_request.merge_ref_path,
                             merge_request: merge_request,
                             merge_requests_as_head_pipeline: [merge_request])
      end

      let(:commit_status) { create(:ci_build, ref: merge_request.merge_ref_path, pipeline: pipeline) }

      it 'notifies the todo service' do
        expect(todo_service).to receive(:merge_request_build_failed).with(merge_request)
        service.execute(commit_status)
      end
    end
  end

  describe '#close' do
    context 'commit status with ref' do
      let(:commit_status) { create(:generic_commit_status, ref: merge_request.source_branch, pipeline: pipeline) }

      it 'notifies the todo service' do
        expect(todo_service).to receive(:merge_request_build_retried).with(merge_request)
        service.close(commit_status)
      end
    end

    context 'commit status with non-HEAD ref' do
      let(:commit_status) { create(:generic_commit_status, ref: merge_request.source_branch) }

      it 'does not notify the todo service' do
        expect(todo_service).not_to receive(:merge_request_build_retried)
        service.close(commit_status)
      end
    end

    context 'commit status without ref' do
      let(:commit_status) { create(:generic_commit_status) }

      it 'does not notify the todo service' do
        expect(todo_service).not_to receive(:merge_request_build_retried)
        service.close(commit_status)
      end
    end

    context 'when build belongs to a merge request pipeline' do
      let(:pipeline) do
        create(:ci_pipeline, source: :merge_request_event,
                             ref: merge_request.merge_ref_path,
                             merge_request: merge_request,
                             merge_requests_as_head_pipeline: [merge_request])
      end

      let(:commit_status) { create(:ci_build, ref: merge_request.merge_ref_path, pipeline: pipeline) }

      it 'notifies the todo service' do
        expect(todo_service).to receive(:merge_request_build_retried).with(merge_request)
        service.close(commit_status)
      end
    end
  end

  describe '#close_all' do
    context 'when using pipeline that belongs to merge request' do
      it 'resolves todos about failed builds for pipeline' do
        service.close_all(pipeline)

        expect(todo_service)
          .to have_received(:merge_request_build_retried)
          .with(merge_request)
      end
    end

    context 'when pipeline is not related to merge request' do
      let(:pipeline) { create(:ci_empty_pipeline) }

      it 'does not resolve any todos about failed builds' do
        service.close_all(pipeline)

        expect(todo_service)
          .not_to have_received(:merge_request_build_retried)
      end
    end
  end
end