File: hierarchy.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (84 lines) | stat: -rw-r--r-- 2,383 bytes parent folder | download
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 Widgets
    class Hierarchy < Base
      include Gitlab::Utils::StrongMemoize

      def parent
        work_item.work_item_parent
      end

      def children
        work_item.work_item_children_by_relative_position
      end

      def ancestors
        work_item.ancestors
      end

      def has_parent?
        parent.present?
      end

      def rolled_up_counts_by_type
        work_item.work_item_type.descendant_types.map do |descendant_type|
          { work_item_type: descendant_type, counts_by_state: counts_by_state(descendant_type) }
        end
      end

      def depth_limit_reached_by_type
        work_item.work_item_type.descendant_types.map do |child_type|
          { work_item_type: child_type, depth_limit_reached: work_item.max_depth_reached?(child_type) }
        end
      end

      def self.quick_action_commands
        [:set_parent, :add_child, :remove_parent, :remove_child]
      end

      def self.quick_action_params
        [:set_parent, :add_child, :remove_parent, :remove_child]
      end

      def self.process_quick_action_param(param_name, value)
        return super unless param_name.in?(quick_action_params) && value.present?

        if [:set_parent, :remove_parent].include?(param_name)
          { parent: value.is_a?(WorkItem) ? value : nil }
        elsif param_name == :remove_child
          { remove_child: value }
        else
          { children: value }
        end
      end

      private

      def counts_by_state(work_item_type)
        type_id_column = Feature.enabled?(:issues_use_correct_work_item_type_id, :instance) ? 'correct_id' : 'id'
        open_count = counts_by_type_and_state.fetch(
          [work_item_type.attributes[type_id_column], WorkItem.available_states[:opened]],
          0
        )
        closed_count = counts_by_type_and_state.fetch(
          [work_item_type.attributes[type_id_column], WorkItem.available_states[:closed]],
          0
        )

        {
          all: open_count + closed_count,
          opened: open_count,
          closed: closed_count
        }
      end

      def counts_by_type_and_state
        work_item.descendants
          .group(:"#{::Gitlab::Issues::TypeAssociationGetter.call}_id", :state_id)
          .count
      end
      strong_memoize_attr :counts_by_type_and_state
    end
  end
end