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

work_item_actions.rb « quick_actions « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa43308c9e27c954c16740c6d56fbbf8a15c846f (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
# frozen_string_literal: true

module Gitlab
  module QuickActions
    module WorkItemActions
      extend ActiveSupport::Concern
      include Gitlab::QuickActions::Dsl

      included do
        desc { _('Change work item type') }
        explanation do |target_type|
          format(_("Converts work item to %{type}. Widgets not supported in new type are removed."), type: target_type)
        end
        types WorkItem
        condition do
          quick_action_target&.project&.work_items_mvc_2_feature_flag_enabled?
        end
        params 'Task | Objective | Key Result | Issue'
        command :type do |type_name|
          work_item_type = ::WorkItems::Type.find_by_name(type_name)
          errors = validate_type(work_item_type)

          if errors.present?
            @execution_message[:type] = errors
          else
            @updates[:issue_type] = work_item_type.base_type
            @updates[:work_item_type] = work_item_type
            @execution_message[:type] = _('Type changed successfully.')
          end
        end
      end

      private

      def validate_type(type)
        return type_error(:not_found) unless type.present?
        return type_error(:same_type) if quick_action_target.work_item_type == type
        return type_error(:forbidden) unless current_user.can?(:"create_#{type.base_type}", quick_action_target)

        nil
      end

      def type_error(reason)
        message = {
          not_found: 'Provided type is not supported',
          same_type: 'Types are the same',
          forbidden: 'You have insufficient permissions'
        }.freeze

        format(_("Failed to convert this work item: %{reason}."), { reason: message[reason] })
      end
    end
  end
end