Commit 533c399f authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'unleash-api-new-strategies' into 'master'

Support New Feature Flags in Unleash API

See merge request gitlab-org/gitlab!26191
parents 59fa9a11 c0715b38
...@@ -50,6 +50,14 @@ module Operations ...@@ -50,6 +50,14 @@ module Operations
def preload_relations def preload_relations
preload(:scopes) preload(:scopes)
end 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 end
private 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 ...@@ -16,6 +16,7 @@ module Operations
self.table_name = 'operations_strategies' self.table_name = 'operations_strategies'
belongs_to :feature_flag belongs_to :feature_flag
has_many :scopes, class_name: 'Operations::FeatureFlags::Scope'
validates :name, validates :name,
inclusion: { inclusion: {
......
...@@ -72,8 +72,13 @@ module API ...@@ -72,8 +72,13 @@ module API
def feature_flags def feature_flags
return [] unless unleash_app_name.present? return [] unless unleash_app_name.present?
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) Operations::FeatureFlagScope.for_unleash_client(project, unleash_app_name)
end end
end end
end end
end
end end
...@@ -7,7 +7,7 @@ module EE ...@@ -7,7 +7,7 @@ module EE
expose :name expose :name
expose :description, unless: ->(feature) { feature.description.nil? } expose :description, unless: ->(feature) { feature.description.nil? }
expose :active, as: :enabled expose :active, as: :enabled
expose :strategies expose :strategies, using: UnleashStrategy
end end
end 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 ...@@ -213,4 +213,45 @@ describe Operations::FeatureFlag do
end end
end 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 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