Commit c0715b38 authored by Jason Goodman's avatar Jason Goodman Committed by Shinya Maeda

Return new version feature flags to unleash clients

Support both legacy and new feature flags
parent 0a1a71fb
......@@ -50,6 +50,14 @@ module Operations
def preload_relations
preload(:scopes)
end
def for_unleash_client(project, environment)
includes(strategies: :scopes)
.where(project: project)
.merge(Operations::FeatureFlags::Scope.on_environment(environment))
.reorder(:id)
.references(:operations_scopes)
end
end
private
......
# frozen_string_literal: true
module Operations
module FeatureFlags
class Scope < ApplicationRecord
prepend HasEnvironmentScope
self.table_name = 'operations_scopes'
belongs_to :strategy, class_name: 'Operations::FeatureFlags::Strategy'
end
end
end
......@@ -16,6 +16,7 @@ module Operations
self.table_name = 'operations_strategies'
belongs_to :feature_flag
has_many :scopes, class_name: 'Operations::FeatureFlags::Scope'
validates :name,
inclusion: {
......
......@@ -72,7 +72,12 @@ module API
def feature_flags
return [] unless unleash_app_name.present?
Operations::FeatureFlagScope.for_unleash_client(project, unleash_app_name)
if Feature.enabled?(:feature_flags_new_version, project)
Operations::FeatureFlagScope.for_unleash_client(project, unleash_app_name) +
Operations::FeatureFlag.for_unleash_client(project, unleash_app_name)
else
Operations::FeatureFlagScope.for_unleash_client(project, unleash_app_name)
end
end
end
end
......
......@@ -7,7 +7,7 @@ module EE
expose :name
expose :description, unless: ->(feature) { feature.description.nil? }
expose :active, as: :enabled
expose :strategies
expose :strategies, using: UnleashStrategy
end
end
end
......
# frozen_string_literal: true
module EE
module API
module Entities
class UnleashStrategy < Grape::Entity
expose :name do |strategy|
if strategy.respond_to?(:name)
strategy.name
else
strategy['name']
end
end
expose :parameters do |strategy|
if strategy.respond_to?(:parameters)
strategy.parameters
else
strategy['parameters']
end
end
end
end
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :operations_scope, class: 'Operations::FeatureFlags::Scope' do
association :strategy, factory: :operations_strategy
sequence(:environment_scope) { |n| "review/patch-#{n}" }
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :operations_strategy, class: 'Operations::FeatureFlags::Strategy' do
association :feature_flag, factory: :operations_feature_flag
name { "default" }
parameters { {} }
end
end
......@@ -213,4 +213,45 @@ describe Operations::FeatureFlag do
end
end
end
describe '.for_unleash_client' do
let_it_be(:project) { create(:project) }
let!(:feature_flag) do
create(:operations_feature_flag, project: project,
name: 'feature1', active: true, version: 2)
end
let!(:strategy) do
create(:operations_strategy, feature_flag: feature_flag,
name: 'default', parameters: {})
end
it 'matches wild cards in the scope' do
create(:operations_scope, strategy: strategy, environment_scope: 'review/*')
flags = described_class.for_unleash_client(project, 'review/feature-branch')
expect(flags).to eq([feature_flag])
end
it 'matches wild cards case sensitively' do
create(:operations_scope, strategy: strategy, environment_scope: 'Staging/*')
flags = described_class.for_unleash_client(project, 'staging/feature')
expect(flags).to eq([])
end
it 'returns feature flags ordered by id' do
create(:operations_scope, strategy: strategy, environment_scope: 'production')
feature_flag_b = create(:operations_feature_flag, project: project,
name: 'feature2', active: true, version: 2)
strategy_b = create(:operations_strategy, feature_flag: feature_flag_b,
name: 'default', parameters: {})
create(:operations_scope, strategy: strategy_b, environment_scope: '*')
flags = described_class.for_unleash_client(project, 'production')
expect(flags.map(&:id)).to eq([feature_flag.id, feature_flag_b.id])
end
end
end
This diff is collapsed.
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