Commit 3c1652f3 authored by Patrick Derichs's avatar Patrick Derichs

Add limit_metric to lists table

Update responses so limit_metric is also provided
to frontend

Add validation for limit_metric values

Move label declaration to top and remove redundant declarations

Refactor list create attributes

Refactor lists max limits update
parent 6f09f635
---
title: Add limit metric to lists
merge_request: 25532
author:
type: added
# frozen_string_literal: true
class AddLimitMetricTypeToList < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column :lists, :limit_metric, :string, limit: 20
end
end
......@@ -2424,6 +2424,7 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do
t.integer "milestone_id"
t.integer "max_issue_count", default: 0, null: false
t.integer "max_issue_weight", default: 0, null: false
t.string "limit_metric", limit: 20
t.index ["board_id", "label_id"], name: "index_lists_on_board_id_and_label_id", unique: true
t.index ["label_id"], name: "index_lists_on_label_id"
t.index ["list_type"], name: "index_lists_on_list_type"
......
......@@ -9,7 +9,7 @@ module EE
before_action :push_wip_limits
end
EE_MAX_LIMITS_PARAMS = %i[max_issue_count max_issue_weight].freeze
EE_MAX_LIMITS_PARAMS = %i[max_issue_count max_issue_weight limit_metric].freeze
override :list_creation_attrs
def list_creation_attrs
......
......@@ -6,6 +6,8 @@ module EE
include ::Gitlab::Utils::StrongMemoize
LIMIT_METRIC_TYPES = %w[all_metrics issue_count issue_weights].freeze
# ActiveSupport::Concern does not prepend the ClassMethods,
# so we cannot call `super` if we use it.
def self.prepended(base)
......@@ -24,6 +26,11 @@ module EE
base.validates :milestone_id, uniqueness: { scope: :board_id }, if: :milestone?
base.validates :max_issue_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
base.validates :max_issue_weight, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
base.validates :limit_metric, inclusion: {
in: LIMIT_METRIC_TYPES,
allow_blank: true,
allow_nil: true
}
base.validates :list_type,
exclusion: { in: %w[assignee], message: _('Assignee lists not available with your current license') },
unless: -> { board&.resource_parent&.feature_available?(:board_assignee_lists) }
......
......@@ -37,13 +37,13 @@ module EE
override :create_list_attributes
def create_list_attributes(type, target, position)
max_issue_count, max_issue_weight = if wip_limits_available?
[max_issue_count_by_params, max_issue_weight_by_params]
else
[0, 0]
end
return super unless wip_limits_available?
super.merge(max_issue_count: max_issue_count, max_issue_weight: max_issue_weight)
super.merge(
max_issue_count: max_issue_count_by_params,
max_issue_weight: max_issue_weight_by_params,
limit_metric: limit_metric_by_params
)
end
private
......@@ -71,6 +71,10 @@ module EE
def wip_limits_available?
parent.feature_available?(:wip_limits)
end
def limit_metric_by_params
params[:limit_metric]
end
end
end
end
......
......@@ -18,13 +18,25 @@ module EE
end
def update_max_limits(list)
return unless max_limits_update_possible?(list)
return unless list.wip_limits_available? && can_admin?(list)
list.update(list_max_limit_attributes_by_params)
attrs = max_limit_settings_by_params
list.update(attrs) unless attrs.empty?
end
def max_limits_update_possible?(list)
max_limits_provided? && list.wip_limits_available? && can_admin?(list)
def max_limit_settings_by_params
{}.tap do |attrs|
attrs.merge!(list_max_limit_attributes_by_params) if max_limits_provided?
attrs.merge!(limit_metric_by_params) if limit_metric_provided?
end
end
def limit_metric_by_params
{ limit_metric: params[:limit_metric] }
end
def limit_metric_provided?
params.key?(:limit_metric)
end
end
end
......
......@@ -13,6 +13,7 @@ describe List do
describe 'validations' do
it { is_expected.to validate_numericality_of(:max_issue_count).only_integer.is_greater_than_or_equal_to(0) }
it { is_expected.to validate_numericality_of(:max_issue_weight).only_integer.is_greater_than_or_equal_to(0) }
it { is_expected.to validate_inclusion_of(:limit_metric).in_array([nil, *EE::List::LIMIT_METRIC_TYPES]) }
end
context 'when it is an assignee type' do
......
......@@ -55,24 +55,30 @@ describe Boards::Lists::CreateService do
stub_feature_flags(wip_limits: wip_limits_enabled)
end
where(:params, :expected_max_issue_count, :expected_max_issue_weight) do
where(:params, :expected_max_issue_count, :expected_max_issue_weight, :expected_limit_metric) do
[
[{ max_issue_count: 0 }, 0, 0],
[{ max_issue_count: nil }, 0, 0],
[{ max_issue_count: 1 }, 1, 0],
[{ max_issue_count: 0 }, 0, 0, nil],
[{ max_issue_count: nil }, 0, 0, nil],
[{ max_issue_count: 1 }, 1, 0, nil],
[{ max_issue_weight: 0 }, 0, 0],
[{ max_issue_weight: nil }, 0, 0],
[{ max_issue_weight: 1 }, 0, 1],
[{ max_issue_weight: 0 }, 0, 0, nil],
[{ max_issue_weight: nil }, 0, 0, nil],
[{ max_issue_weight: 1 }, 0, 1, nil],
[{ max_issue_count: 1, max_issue_weight: 0 }, 1, 0],
[{ max_issue_count: 0, max_issue_weight: 1 }, 0, 1],
[{ max_issue_count: 1, max_issue_weight: 1 }, 1, 1],
[{ max_issue_count: 1, max_issue_weight: 0 }, 1, 0, nil],
[{ max_issue_count: 0, max_issue_weight: 1 }, 0, 1, nil],
[{ max_issue_count: 1, max_issue_weight: 1 }, 1, 1, nil],
[{ max_issue_count: nil, max_issue_weight: 1 }, 0, 1],
[{ max_issue_count: 1, max_issue_weight: nil }, 1, 0],
[{ max_issue_count: nil, max_issue_weight: 1 }, 0, 1, nil],
[{ max_issue_count: 1, max_issue_weight: nil }, 1, 0, nil],
[{ max_issue_count: nil, max_issue_weight: nil }, 0, 0]
[{ max_issue_count: nil, max_issue_weight: nil }, 0, 0, nil],
[{ limit_metric: 'all_metrics' }, 0, 0, 'all_metrics'],
[{ limit_metric: 'issue_count' }, 0, 0, 'issue_count'],
[{ limit_metric: 'issue_weights' }, 0, 0, 'issue_weights'],
[{ limit_metric: '' }, 0, 0, ''],
[{ limit_metric: nil }, 0, 0, nil]
]
end
......@@ -83,9 +89,11 @@ describe Boards::Lists::CreateService do
attrs = service.create_list_attributes(nil, nil, nil)
if wip_limits_enabled
expect(attrs).to include(max_issue_count: expected_max_issue_count, max_issue_weight: expected_max_issue_weight)
expect(attrs).to include(max_issue_count: expected_max_issue_count,
max_issue_weight: expected_max_issue_weight,
limit_metric: expected_limit_metric)
else
expect(attrs).to include(max_issue_count: 0, max_issue_weight: 0)
expect(attrs).not_to include(max_issue_count: 0, max_issue_weight: 0, limit_metric: nil)
end
end
end
......
......@@ -7,6 +7,7 @@ FactoryBot.define do
list_type { :label }
max_issue_count { 0 }
max_issue_weight { 0 }
limit_metric { nil }
sequence(:position)
end
......
......@@ -37,7 +37,8 @@
"title": { "type": "string" },
"position": { "type": ["integer", "null"] },
"max_issue_count": { "type": "integer" },
"max_issue_weight": { "type": "integer" }
"max_issue_weight": { "type": "integer" },
"limit_metric": { "type": ["string", "null"] }
},
"additionalProperties": true
}
......@@ -756,6 +756,7 @@ List:
- user_id
- max_issue_count
- max_issue_weight
- limit_metric
ExternalPullRequest:
- id
- created_at
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment