summaryrefslogtreecommitdiff
path: root/spec/services/issues/close_service_spec.rb
blob: 8950bdd465fbdfa93ec55e2f95058060be1e6027 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Issues::CloseService do
  subject(:close_issue) { described_class.new(project: project, current_user: user).close_issue(issue) }

  let_it_be(:project, refind: true) { create(:project, :repository) }
  let_it_be(:label1) { create(:label, project: project) }
  let_it_be(:label2) { create(:label, project: project, remove_on_close: true) }
  let_it_be(:author) { create(:user) }
  let_it_be(:user) { create(:user, email: "user@example.com") }
  let_it_be(:user2) { create(:user, email: "user2@example.com") }
  let_it_be(:guest) { create(:user) }
  let_it_be(:closing_merge_request) { create(:merge_request, source_project: project) }

  let(:external_issue) { ExternalIssue.new('JIRA-123', project) }
  let!(:issue) { create(:issue, title: "My issue", project: project, assignees: [user2], author: author) }

  before_all do
    project.add_maintainer(user)
    project.add_developer(user2)
    project.add_guest(guest)
  end

  shared_examples 'removes labels marked for removal from issue when closed' do
    before do
      issue.update!(label_ids: [label1.id, label2.id])
    end

    it 'removes labels marked for removal' do
      expect do
        close_issue
      end.to change { issue.reload.label_ids }.from(containing_exactly(label1.id, label2.id)).to(containing_exactly(label1.id))
    end

    it 'creates system notes for the removed labels' do
      expect do
        close_issue
      end.to change(ResourceLabelEvent, :count).by(1)

      expect(ResourceLabelEvent.last.slice(:action, :issue_id, :label_id)).to eq(
        'action' => 'remove',
        'issue_id' => issue.id,
        'label_id' => label2.id
      )
    end
  end

  describe '#execute' do
    let(:service) { described_class.new(project: project, current_user: user) }

    it 'checks if the user is authorized to update the issue' do
      expect(service).to receive(:can?).with(user, :update_issue, issue)
        .and_call_original

      service.execute(issue)
    end

    it 'does not close the issue when the user is not authorized to do so' do
      allow(service).to receive(:can?).with(user, :update_issue, issue)
        .and_return(false)

      expect(service).not_to receive(:close_issue)
      expect(service.execute(issue)).to eq(issue)
    end

    it 'closes the external issue even when the user is not authorized to do so' do
      allow(service).to receive(:can?).with(user, :update_issue, external_issue)
        .and_return(false)

      expect(service).to receive(:close_issue)
        .with(external_issue, closed_via: nil, notifications: true, system_note: true)

      service.execute(external_issue)
    end

    it 'closes the issue when the user is authorized to do so' do
      allow(service).to receive(:can?).with(user, :update_issue, issue)
        .and_return(true)

      expect(service).to receive(:close_issue)
        .with(issue, closed_via: nil, notifications: true, system_note: true)

      service.execute(issue)
    end

    it 'refreshes the number of open issues', :use_clean_rails_memory_store_caching do
      expect { service.execute(issue) }
        .to change { project.open_issues_count }.from(1).to(0)
    end

    it 'invalidates counter cache for assignees' do
      expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)

      service.execute(issue)
    end

    context 'issue is incident type' do
      let(:issue) { create(:incident, project: project) }
      let(:current_user) { user }

      subject { service.execute(issue) }

      it_behaves_like 'an incident management tracked event', :incident_management_incident_closed
    end
  end

  describe '#close_issue' do
    context 'with external issue' do
      context 'with an active external issue tracker supporting close_issue' do
        let!(:external_issue_tracker) { create(:jira_service, project: project) }

        it 'closes the issue on the external issue tracker' do
          project.reload
          expect(project.external_issue_tracker).to receive(:close_issue)

          described_class.new(project: project, current_user: user).close_issue(external_issue)
        end
      end

      context 'with inactive external issue tracker supporting close_issue' do
        let!(:external_issue_tracker) { create(:jira_service, project: project, active: false) }

        it 'does not close the issue on the external issue tracker' do
          project.reload
          expect(project.external_issue_tracker).not_to receive(:close_issue)

          described_class.new(project: project, current_user: user).close_issue(external_issue)
        end
      end

      context 'with an active external issue tracker not supporting close_issue' do
        let!(:external_issue_tracker) { create(:bugzilla_service, project: project) }

        it 'does not close the issue on the external issue tracker' do
          project.reload
          expect(project.external_issue_tracker).not_to receive(:close_issue)

          described_class.new(project: project, current_user: user).close_issue(external_issue)
        end
      end
    end

    context "closed by a merge request", :sidekiq_might_not_need_inline do
      subject(:close_issue) do
        perform_enqueued_jobs do
          described_class.new(project: project, current_user: user).close_issue(issue, closed_via: closing_merge_request)
        end
      end

      it_behaves_like 'removes labels marked for removal from issue when closed'

      it 'mentions closure via a merge request' do
        close_issue

        email = ActionMailer::Base.deliveries.last

        expect(email.to.first).to eq(user2.email)
        expect(email.subject).to include(issue.title)
        expect(email.body.parts.map(&:body)).to all(include(closing_merge_request.to_reference))
      end

      it_behaves_like 'records an onboarding progress action', :issue_auto_closed do
        let(:namespace) { project.namespace }
      end

      context 'when user cannot read merge request' do
        it 'does not mention merge request' do
          project.project_feature.update_attribute(:repository_access_level, ProjectFeature::DISABLED)

          close_issue

          email = ActionMailer::Base.deliveries.last
          body_text = email.body.parts.map(&:body).join(" ")

          expect(email.to.first).to eq(user2.email)
          expect(email.subject).to include(issue.title)
          expect(body_text).not_to include(closing_merge_request.to_reference)
        end
      end

      context 'updating `metrics.first_mentioned_in_commit_at`' do
        context 'when `metrics.first_mentioned_in_commit_at` is not set' do
          it 'uses the first commit authored timestamp' do
            expected = closing_merge_request.commits.first.authored_date

            close_issue

            expect(issue.metrics.first_mentioned_in_commit_at).to eq(expected)
          end
        end

        context 'when `metrics.first_mentioned_in_commit_at` is already set' do
          before do
            issue.metrics.update!(first_mentioned_in_commit_at: Time.current)
          end

          it 'does not update the metrics' do
            expect { close_issue }.not_to change { issue.metrics.first_mentioned_in_commit_at }
          end
        end

        context 'when merge request has no commits' do
          let(:closing_merge_request) { create(:merge_request, :without_diffs, source_project: project) }

          it 'does not update the metrics' do
            close_issue

            expect(issue.metrics.first_mentioned_in_commit_at).to be_nil
          end
        end
      end
    end

    context "closed by a commit", :sidekiq_might_not_need_inline do
      subject(:close_issue) do
        perform_enqueued_jobs do
          described_class.new(project: project, current_user: user).close_issue(issue, closed_via: closing_commit)
        end
      end

      let(:closing_commit) { create(:commit, project: project) }

      it_behaves_like 'removes labels marked for removal from issue when closed'

      it 'mentions closure via a commit' do
        close_issue

        email = ActionMailer::Base.deliveries.last

        expect(email.to.first).to eq(user2.email)
        expect(email.subject).to include(issue.title)
        expect(email.body.parts.map(&:body)).to all(include(closing_commit.id))
      end

      context 'when user cannot read the commit' do
        it 'does not mention the commit id' do
          project.project_feature.update_attribute(:repository_access_level, ProjectFeature::DISABLED)

          close_issue

          email = ActionMailer::Base.deliveries.last
          body_text = email.body.parts.map(&:body).join(" ")

          expect(email.to.first).to eq(user2.email)
          expect(email.subject).to include(issue.title)
          expect(body_text).not_to include(closing_commit.id)
        end
      end
    end

    context "valid params" do
      subject(:close_issue) do
        perform_enqueued_jobs do
          described_class.new(project: project, current_user: user).close_issue(issue)
        end
      end

      it 'verifies the number of queries' do
        recorded = ActiveRecord::QueryRecorder.new { close_issue }
        expected_queries = 32

        expect(recorded.count).to be <= expected_queries
        expect(recorded.cached_count).to eq(0)
      end

      it_behaves_like 'removes labels marked for removal from issue when closed'

      it 'closes the issue' do
        close_issue

        expect(issue).to be_valid
        expect(issue).to be_closed
      end

      it 'records closed user' do
        close_issue

        expect(issue.reload.closed_by_id).to be(user.id)
      end

      it 'sends email to user2 about assign of new issue', :sidekiq_might_not_need_inline do
        close_issue

        email = ActionMailer::Base.deliveries.last
        expect(email.to.first).to eq(user2.email)
        expect(email.subject).to include(issue.title)
      end

      it 'creates resource state event about the issue being closed' do
        close_issue

        event = issue.resource_state_events.last
        expect(event.state).to eq('closed')
      end

      it 'marks todos as done' do
        todo = create(:todo, :assigned, user: user, project: project, target: issue, author: user2)

        close_issue

        expect(todo.reload).to be_done
      end

      context 'when closing the issue fails' do
        it 'does not assign a closed_by value for the issue' do
          allow(issue).to receive(:close).and_return(false)

          close_issue

          expect(issue.closed_by_id).to be_nil
        end
      end

      context 'when there is an associated Alert Management Alert' do
        context 'when alert can be resolved' do
          let!(:alert) { create(:alert_management_alert, issue: issue, project: project) }

          it 'resolves an alert and sends a system note' do
            expect_next_instance_of(SystemNotes::AlertManagementService) do |notes_service|
              expect(notes_service).to receive(:closed_alert_issue).with(issue)
            end

            close_issue

            expect(alert.reload.resolved?).to eq(true)
          end
        end

        context 'when alert cannot be resolved' do
          let!(:alert) { create(:alert_management_alert, :with_validation_errors, issue: issue, project: project) }

          before do
            allow(Gitlab::AppLogger).to receive(:warn).and_call_original
          end

          it 'writes a warning into the log' do
            close_issue

            expect(Gitlab::AppLogger).to have_received(:warn).with(
              message: 'Cannot resolve an associated Alert Management alert',
              issue_id: issue.id,
              alert_id: alert.id,
              alert_errors: { hosts: ['hosts array is over 255 chars'] }
            )
          end
        end
      end

      it 'deletes milestone issue counters cache' do
        issue.update!(milestone: create(:milestone, project: project))

        expect_next_instance_of(Milestones::ClosedIssuesCountService, issue.milestone) do |service|
          expect(service).to receive(:delete_cache).and_call_original
        end

        close_issue
      end

      it_behaves_like 'does not record an onboarding progress action'
    end

    context 'when issue is not confidential' do
      it_behaves_like 'removes labels marked for removal from issue when closed'

      it 'executes issue hooks' do
        expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks)
        expect(project).to receive(:execute_services).with(an_instance_of(Hash), :issue_hooks)

        close_issue
      end
    end

    context 'when issue is confidential' do
      let(:issue) { create(:issue, :confidential, project: project) }

      it_behaves_like 'removes labels marked for removal from issue when closed'

      it 'executes confidential issue hooks' do
        expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :confidential_issue_hooks)
        expect(project).to receive(:execute_services).with(an_instance_of(Hash), :confidential_issue_hooks)

        close_issue
      end
    end

    context 'internal issues disabled' do
      let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }

      before do
        project.issues_enabled = false
        project.save!
      end

      it 'does not close the issue' do
        expect(issue).to be_valid
        expect(issue).to be_opened
        expect(todo.reload).to be_pending
      end
    end
  end
end