summaryrefslogtreecommitdiff
path: root/app/services/merge_requests/assign_issues_service.rb
blob: 8c6c484102039a914a29a185cd5a26f4aa30d227 (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
module MergeRequests
  class AssignIssuesService < BaseService
    def assignable_issues
      @assignable_issues ||= begin
        if current_user == merge_request.author
          closes_issues.select do |issue|
            !issue.is_a?(ExternalIssue) && !issue.assignees.present? && can?(current_user, :admin_issue, issue)
          end
        else
          []
        end
      end
    end

    def execute
      assignable_issues.each do |issue|
        Issues::UpdateService.new(issue.project, current_user, assignee_ids: [current_user.id]).execute(issue)
      end

      {
        count: assignable_issues.count
      }
    end

    private

    def merge_request
      params[:merge_request]
    end

    def closes_issues
      @closes_issues ||= params[:closes_issues] || merge_request.closes_issues(current_user)
    end
  end
end