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

20230129154819_add_widgets_for_work_item_types.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b936ea2e409bdd37d37558e627fe66be97691738 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# frozen_string_literal: true

class AddWidgetsForWorkItemTypes < Gitlab::Database::Migration[2.1]
  class WorkItemType < MigrationRecord
    self.table_name = 'work_item_types'
  end

  class WidgetDefinition < MigrationRecord
    self.table_name = 'work_item_widget_definitions'
  end

  restrict_gitlab_migration gitlab_schema: :gitlab_main
  disable_ddl_transaction!

  def up
    widget_names = {
      assignees: 'Assignees',
      labels: 'Labels',
      description: 'Description',
      hierarchy: 'Hierarchy',
      start_and_due_date: 'Start and due date',
      milestone: 'Milestone',
      notes: 'Notes',
      iteration: 'Iteration',
      weight: 'Weight',
      health_status: 'Health status',
      progress: 'Progress',
      status: 'Status',
      requirement_legacy: 'Requirement legacy',
      test_reports: 'Test reports'
    }

    widgets_for_type = {
      'Issue' => [
        :assignees,
        :labels,
        :description,
        :hierarchy,
        :start_and_due_date,
        :milestone,
        :notes,
        # EE widgets
        :iteration,
        :weight,
        :health_status
      ],
      'Incident' => [
        :description,
        :hierarchy,
        :notes
      ],
      'Test Case' => [
        :description,
        :notes
      ],
      'Requirement' => [
        :description,
        :notes,
        :status,
        :requirement_legacy,
        :test_reports
      ],
      'Task' => [
        :assignees,
        :labels,
        :description,
        :hierarchy,
        :start_and_due_date,
        :milestone,
        :notes,
        :iteration,
        :weight
      ],
      'Objective' => [
        :assignees,
        :labels,
        :description,
        :hierarchy,
        :milestone,
        :notes,
        :health_status,
        :progress
      ],
      'Key Result' => [
        :assignees,
        :labels,
        :description,
        :hierarchy,
        :start_and_due_date,
        :notes,
        :health_status,
        :progress
      ]
    }

    widgets_enum = {
      assignees: 0,
      description: 1,
      hierarchy: 2,
      labels: 3,
      milestone: 4,
      notes: 5,
      start_and_due_date: 6,
      health_status: 7, # EE-only
      weight: 8, # EE-only
      iteration: 9, # EE-only
      progress: 10, # EE-only
      status: 11, # EE-only
      requirement_legacy: 12, # EE-only
      test_reports: 13
    }

    widgets = []
    widgets_for_type.each do |type_name, widget_syms|
      type = WorkItemType.find_by_name_and_namespace_id(type_name, nil)

      unless type
        Gitlab::AppLogger.warn("type #{type_name} is missing, not adding widgets")

        next
      end

      widgets += widget_syms.map do |widget_sym|
        {
          work_item_type_id: type.id,
          name: widget_names[widget_sym],
          widget_type: widgets_enum[widget_sym]
        }
      end
    end

    return if widgets.empty?

    WidgetDefinition.upsert_all(
      widgets,
      unique_by: :index_work_item_widget_definitions_on_default_witype_and_name
    )
  end

  def down
    WidgetDefinition.delete_all
  end
end