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

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

module WorkItems
  module RelatedWorkItemLinks
    class CreateService < IssuableLinks::CreateService
      extend ::Gitlab::Utils::Override

      def execute
        return error(_('No matching work item found.'), 404) unless can?(current_user, :admin_work_item_link, issuable)

        response = super
        create_notes_async if new_links.any?

        if response[:status] == :success
          response[:message] = format(
            _('Successfully linked ID(s): %{item_ids}.'),
            item_ids: linked_ids(response[:created_references]).to_sentence
          )
        end

        response
      end

      def linkable_issuables(work_items)
        @linkable_issuables ||= work_items.select { |work_item| can_link_item?(work_item) }
      end

      def previous_related_issuables
        @related_issues ||= issuable.linked_work_items(authorize: false).to_a
      end

      private

      def create_notes(_issuable_link)
        # no-op notes are created asynchronously
      end

      def link_class
        WorkItems::RelatedWorkItemLink
      end

      def can_link_item?(work_item)
        return true if can?(current_user, :admin_work_item_link, work_item)

        @errors << format(
          _("Item with ID: %{id} cannot be added. You don't have permission to perform this action."),
          id: work_item.id
        )

        false
      end

      def linked_ids(created_links)
        created_links.collect(&:target_id)
      end

      def create_notes_async
        link_ids = new_links.collect(&:id)

        worker_params = {
          issuable_class: issuable.class.name,
          issuable_id: issuable.id,
          link_ids: link_ids,
          link_type: params[:link_type] || 'relates_to',
          user_id: current_user.id
        }

        Issuable::RelatedLinksCreateWorker.perform_async(worker_params)
      end

      override :issuables_already_assigned_message
      def issuables_already_assigned_message
        _('Items are already linked')
      end

      override :issuables_not_found_message
      def issuables_not_found_message
        _('No matching work item found. Make sure you are adding a valid ID and you have access to the item.')
      end
    end
  end
end

WorkItems::RelatedWorkItemLinks::CreateService.prepend_mod_with('WorkItems::RelatedWorkItemLinks::CreateService')