summaryrefslogtreecommitdiff
path: root/app/services/issues/close_service.rb
blob: d5a542a418d37183b057e93a7133932bd39f47f3 (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
# frozen_string_literal: true

module Issues
  class CloseService < Issues::BaseService
    # Closes the supplied issue if the current user is able to do so.
    def execute(issue, commit: nil, notifications: true, system_note: true)
      return issue unless can?(current_user, :update_issue, issue) || issue.is_a?(ExternalIssue)

      close_issue(issue,
                  closed_via: commit,
                  notifications: notifications,
                  system_note: system_note)
    end

    # Closes the supplied issue without checking if the user is authorized to
    # do so.
    #
    # The code calling this method is responsible for ensuring that a user is
    # allowed to close the given issue.
    def close_issue(issue, closed_via: nil, notifications: true, system_note: true)
      if project.jira_tracker_active? && issue.is_a?(ExternalIssue)
        project.jira_service.close_issue(closed_via, issue)
        todo_service.close_issue(issue, current_user)
        return issue
      end

      if project.issues_enabled? && issue.close
        issue.update(closed_by: current_user)
        event_service.close_issue(issue, current_user)
        create_note(issue, closed_via) if system_note

        closed_via = _("commit %{commit_id}") % { commit_id: closed_via.id } if closed_via.is_a?(Commit)

        notification_service.async.close_issue(issue, current_user, closed_via: closed_via) if notifications
        todo_service.close_issue(issue, current_user)
        execute_hooks(issue, 'close')
        invalidate_cache_counts(issue, users: issue.assignees)
        issue.update_project_counter_caches

        store_first_mentioned_in_commit_at(issue, closed_via) if closed_via.is_a?(MergeRequest)

        delete_milestone_closed_issue_counter_cache(issue.milestone)
      end

      issue
    end

    private

    def create_note(issue, current_commit)
      SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
    end

    def store_first_mentioned_in_commit_at(issue, merge_request)
      return unless Feature.enabled?(:store_first_mentioned_in_commit_on_issue_close, issue.project, default_enabled: true)

      metrics = issue.metrics
      return if metrics.nil? || metrics.first_mentioned_in_commit_at

      first_commit_timestamp = merge_request.commits(limit: 1).first.try(:authored_date)
      return unless first_commit_timestamp

      metrics.update!(first_mentioned_in_commit_at: first_commit_timestamp)
    end
  end
end