Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_from_task_service.rb « work_items « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4203c96e67681a40d11882f566c90e9f89ef6756 (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
# frozen_string_literal: true

module WorkItems
  class CreateFromTaskService
    def initialize(work_item:, current_user: nil, work_item_params: {}, spam_params:)
      @work_item = work_item
      @current_user = current_user
      @work_item_params = work_item_params
      @spam_params = spam_params
      @errors = []
    end

    def execute
      transaction_result = ApplicationRecord.transaction do
        create_and_link_result = CreateAndLinkService.new(
          project: @work_item.project,
          current_user: @current_user,
          params: @work_item_params.slice(:title, :work_item_type_id),
          spam_params: @spam_params,
          link_params: { target_issuable: @work_item }
        ).execute

        if create_and_link_result.error?
          @errors += create_and_link_result.errors
          raise ActiveRecord::Rollback
        end

        replacement_result = TaskListReferenceReplacementService.new(
          work_item: @work_item,
          work_item_reference: create_and_link_result[:work_item].to_reference,
          line_number_start: @work_item_params[:line_number_start],
          line_number_end: @work_item_params[:line_number_end],
          title: @work_item_params[:title],
          lock_version: @work_item_params[:lock_version]
        ).execute

        if replacement_result.error?
          @errors += replacement_result.errors
          raise ActiveRecord::Rollback
        end

        create_and_link_result
      end

      return transaction_result if transaction_result

      ::ServiceResponse.error(message: @errors, http_status: 422)
    end
  end
end