2020-09-03 17:08:18 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Operations
|
|
|
|
class FeatureFlag < ApplicationRecord
|
2020-10-28 11:08:49 -04:00
|
|
|
include AfterCommitQueue
|
2020-09-03 17:08:18 -04:00
|
|
|
include AtomicInternalId
|
|
|
|
include IidRoutes
|
2020-10-01 14:10:20 -04:00
|
|
|
include Limitable
|
2021-02-08 22:09:18 -05:00
|
|
|
include Referable
|
2020-09-03 17:08:18 -04:00
|
|
|
|
|
|
|
self.table_name = 'operations_feature_flags'
|
2020-10-01 14:10:20 -04:00
|
|
|
self.limit_scope = :project
|
|
|
|
self.limit_name = 'project_feature_flags'
|
2020-09-03 17:08:18 -04:00
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
has_internal_id :iid, scope: :project
|
2020-09-03 17:08:18 -04:00
|
|
|
|
|
|
|
default_value_for :active, true
|
2021-09-02 02:09:10 -04:00
|
|
|
default_value_for :version, :new_version_flag
|
2020-09-03 17:08:18 -04:00
|
|
|
|
|
|
|
# strategies exists only for the second version
|
|
|
|
has_many :strategies, class_name: 'Operations::FeatureFlags::Strategy'
|
|
|
|
has_many :feature_flag_issues
|
|
|
|
has_many :issues, through: :feature_flag_issues
|
|
|
|
|
|
|
|
validates :project, presence: true
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: 2..63,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.feature_flag_regex,
|
|
|
|
message: Gitlab::Regex.feature_flag_regex_message
|
|
|
|
}
|
|
|
|
validates :name, uniqueness: { scope: :project_id }
|
|
|
|
validates :description, allow_blank: true, length: 0..255
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :strategies, allow_destroy: true
|
|
|
|
|
|
|
|
scope :ordered, -> { order(:name) }
|
|
|
|
|
|
|
|
scope :enabled, -> { where(active: true) }
|
|
|
|
scope :disabled, -> { where(active: false) }
|
|
|
|
|
2021-06-01 05:09:36 -04:00
|
|
|
scope :new_version_only, -> { where(version: :new_version_flag)}
|
|
|
|
|
2020-09-03 17:08:18 -04:00
|
|
|
enum version: {
|
|
|
|
new_version_flag: 2
|
|
|
|
}
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def preload_relations
|
2021-09-23 08:11:29 -04:00
|
|
|
preload(strategies: :scopes)
|
2020-09-03 17:08:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def for_unleash_client(project, environment)
|
|
|
|
includes(strategies: [:scopes, :user_list])
|
|
|
|
.where(project: project)
|
|
|
|
.merge(Operations::FeatureFlags::Scope.on_environment(environment))
|
|
|
|
.reorder(:id)
|
|
|
|
.references(:operations_scopes)
|
|
|
|
end
|
2021-02-08 22:09:18 -05:00
|
|
|
|
|
|
|
def reference_prefix
|
|
|
|
'[feature_flag:'
|
|
|
|
end
|
|
|
|
|
|
|
|
def reference_pattern
|
|
|
|
@reference_pattern ||= %r{
|
|
|
|
#{Regexp.escape(reference_prefix)}(#{::Project.reference_pattern}\/)?(?<feature_flag>\d+)#{Regexp.escape(reference_postfix)}
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_reference_pattern
|
2021-07-14 14:08:31 -04:00
|
|
|
@link_reference_pattern ||= super("feature_flags", %r{(?<feature_flag>\d+)/edit})
|
2021-02-08 22:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reference_postfix
|
|
|
|
']'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_reference(from = nil, full: false)
|
|
|
|
project
|
|
|
|
.to_reference_base(from, full: full)
|
|
|
|
.then { |reference_base| reference_base.present? ? "#{reference_base}/" : nil }
|
|
|
|
.then { |reference_base| "#{self.class.reference_prefix}#{reference_base}#{iid}#{self.class.reference_postfix}" }
|
2020-09-03 17:08:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def related_issues(current_user, preload:)
|
|
|
|
issues = ::Issue
|
|
|
|
.select('issues.*, operations_feature_flags_issues.id AS link_id')
|
|
|
|
.joins(:feature_flag_issues)
|
2021-04-26 08:09:44 -04:00
|
|
|
.where(operations_feature_flags_issues: { feature_flag_id: id })
|
2020-09-03 17:08:18 -04:00
|
|
|
.order('operations_feature_flags_issues.id ASC')
|
|
|
|
.includes(preload)
|
|
|
|
|
|
|
|
Ability.issues_readable_by_user(issues, current_user)
|
|
|
|
end
|
|
|
|
|
2020-10-28 11:08:49 -04:00
|
|
|
def hook_attrs
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
active: active
|
|
|
|
}
|
|
|
|
end
|
2020-09-03 17:08:18 -04:00
|
|
|
end
|
|
|
|
end
|