summaryrefslogtreecommitdiff
path: root/app/services/issues/close_service.rb
blob: 74459c3342cf2d720baee3cb67a303222bd0761f (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
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)

      close_issue(issue,
                  commit: 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, commit: nil, notifications: true, system_note: true)
      if project.jira_tracker? && project.jira_service.active && issue.is_a?(ExternalIssue)
        project.jira_service.close_issue(commit, issue)
        todo_service.close_issue(issue, current_user)
        return issue
      end

      if project.issues_enabled? && issue.close
        event_service.close_issue(issue, current_user)
        create_note(issue, commit) if system_note
        notification_service.close_issue(issue, current_user) if notifications
        todo_service.close_issue(issue, current_user)
        execute_hooks(issue, 'close')
        invalidate_cache_counts(issue, users: issue.assignees)
      end

      issue
    end

    private

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