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: f313881470a3bb9a7bd6d52796b3a2df6301cdbb (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
# 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

        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 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

      override :issuables_already_assigned_message
      def issuables_already_assigned_message
        _('Work 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')