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

Move Unleash API to core

Split feature flag helper between core and EE
Part of moving feature flags to core
parent 70c743ec
......@@ -168,7 +168,6 @@ Performance/Detect:
Exclude:
- 'ee/spec/controllers/projects/dependencies_controller_spec.rb'
- 'ee/spec/controllers/projects/feature_flags_controller_spec.rb'
- 'ee/spec/requests/api/unleash_spec.rb'
- 'spec/lib/gitlab/git/tree_spec.rb'
- 'spec/lib/gitlab/import_export/project/tree_restorer_spec.rb'
- 'spec/models/event_spec.rb'
......
......@@ -11,9 +11,9 @@ module FeatureFlagsHelper
project.feature_flags_client_token
end
def feature_flag_issues_links_endpoint(project, feature_flag, user)
return '' unless can?(user, :admin_feature_flags_issue_links, project)
project_feature_flag_issues_path(project, feature_flag)
def feature_flag_issues_links_endpoint(_project, _feature_flag, _user)
''
end
end
FeatureFlagsHelper.prepend_if_ee('::EE::FeatureFlagsHelper')
......@@ -2506,6 +2506,11 @@ class Project < ApplicationRecord
GroupDeployKey.for_groups(group.self_and_ancestors_ids)
end
def feature_flags_client_token
instance = operations_feature_flags_client || create_operations_feature_flags_client!
instance.token
end
private
def find_service(services, name)
......
# frozen_string_literal: true
module EE
module FeatureFlagsHelper
extend ::Gitlab::Utils::Override
override :feature_flag_issues_links_endpoint
def feature_flag_issues_links_endpoint(project, feature_flag, user)
return '' unless can?(user, :admin_feature_flags_issue_links, project)
project_feature_flag_issues_path(project, feature_flag)
end
end
end
......@@ -630,11 +630,6 @@ module EE
change_head(root_ref) if root_ref.present?
end
def feature_flags_client_token
instance = operations_feature_flags_client || create_operations_feature_flags_client!
instance.token
end
override :lfs_http_url_to_repo
def lfs_http_url_to_repo(operation)
return super unless ::Gitlab::Geo.secondary_with_primary?
......
......@@ -14,7 +14,6 @@ module EE
mount ::API::AuditEvents
mount ::API::ProjectApprovalRules
mount ::API::ProjectApprovalSettings
mount ::API::Unleash
mount ::API::DependencyProxy
mount ::API::EpicIssues
mount ::API::EpicLinks
......
# frozen_string_literal: true
module EE
module API
module Entities
class UnleashFeature < Grape::Entity
expose :name
expose :description, unless: ->(feature) { feature.description.nil? }
expose :active, as: :enabled
expose :strategies do |flag|
flag.strategies.map do |strategy|
if legacy_strategy?(strategy)
UnleashLegacyStrategy.represent(strategy)
elsif gitlab_user_list_strategy?(strategy)
UnleashGitlabUserListStrategy.represent(strategy)
else
UnleashStrategy.represent(strategy)
end
end
end
private
def legacy_strategy?(strategy)
!strategy.respond_to?(:name)
end
def gitlab_user_list_strategy?(strategy)
strategy.name == ::Operations::FeatureFlags::Strategy::STRATEGY_GITLABUSERLIST
end
end
end
end
end
# frozen_string_literal: true
module EE
module API
module Entities
class UnleashGitlabUserListStrategy < Grape::Entity
expose :name do |_strategy|
::Operations::FeatureFlags::Strategy::STRATEGY_USERWITHID
end
expose :parameters do |strategy|
{ userIds: strategy.user_list.user_xids }
end
end
end
end
end
# frozen_string_literal: true
module EE
module API
module Entities
class UnleashLegacyStrategy < Grape::Entity
expose :name do |strategy|
strategy['name']
end
expose :parameters do |strategy|
strategy['parameters']
end
end
end
end
end
# frozen_string_literal: true
module EE
module API
module Entities
class UnleashStrategy < Grape::Entity
expose :name
expose :parameters
end
end
end
end
......@@ -2,23 +2,11 @@
require 'spec_helper'
RSpec.describe FeatureFlagsHelper do
RSpec.describe EE::FeatureFlagsHelper do
let_it_be(:project) { create(:project) }
let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }
let_it_be(:user) { create(:user) }
describe '#unleash_api_url' do
subject { helper.unleash_api_url(project) }
it { is_expected.to end_with("/api/v4/feature_flags/unleash/#{project.id}") }
end
describe '#unleash_api_instance_id' do
subject { helper.unleash_api_instance_id(project) }
it { is_expected.not_to be_empty }
end
describe '#feature_flag_issues_links_endpoint' do
subject { helper.feature_flag_issues_links_endpoint(project, feature_flag, user) }
......
......@@ -239,6 +239,7 @@ module API
mount ::API::Templates
mount ::API::Todos
mount ::API::Triggers
mount ::API::Unleash
mount ::API::UsageData
mount ::API::UserCounts
mount ::API::Users
......
# frozen_string_literal: true
module API
module Entities
class UnleashFeature < Grape::Entity
expose :name
expose :description, unless: ->(feature) { feature.description.nil? }
expose :active, as: :enabled
expose :strategies do |flag|
flag.strategies.map do |strategy|
if legacy_strategy?(strategy)
UnleashLegacyStrategy.represent(strategy)
elsif gitlab_user_list_strategy?(strategy)
UnleashGitlabUserListStrategy.represent(strategy)
else
UnleashStrategy.represent(strategy)
end
end
end
private
def legacy_strategy?(strategy)
!strategy.respond_to?(:name)
end
def gitlab_user_list_strategy?(strategy)
strategy.name == ::Operations::FeatureFlags::Strategy::STRATEGY_GITLABUSERLIST
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class UnleashGitlabUserListStrategy < Grape::Entity
expose :name do |_strategy|
::Operations::FeatureFlags::Strategy::STRATEGY_USERWITHID
end
expose :parameters do |strategy|
{ userIds: strategy.user_list.user_xids }
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class UnleashLegacyStrategy < Grape::Entity
expose :name do |strategy|
strategy['name']
end
expose :parameters do |strategy|
strategy['parameters']
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class UnleashStrategy < Grape::Entity
expose :name
expose :parameters
end
end
end
......@@ -24,13 +24,13 @@ module API
desc 'Get a list of features (deprecated, v2 client support)'
get 'features' do
present :version, 1
present :features, feature_flags, with: ::EE::API::Entities::UnleashFeature
present :features, feature_flags, with: ::API::Entities::UnleashFeature
end
desc 'Get a list of features'
get 'client/features' do
present :version, 1
present :features, feature_flags, with: ::EE::API::Entities::UnleashFeature
present :features, feature_flags, with: ::API::Entities::UnleashFeature
end
post 'client/register' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe FeatureFlagsHelper do
let_it_be(:project) { create(:project) }
let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }
let_it_be(:user) { create(:user) }
describe '#unleash_api_url' do
subject { helper.unleash_api_url(project) }
it { is_expected.to end_with("/api/v4/feature_flags/unleash/#{project.id}") }
end
describe '#unleash_api_instance_id' do
subject { helper.unleash_api_instance_id(project) }
it { is_expected.not_to be_empty }
end
end
......@@ -119,8 +119,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do
subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first
feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(true)
expect(feature_flag_2['enabled']).to eq(false)
......@@ -133,8 +133,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do
subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first
feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(false)
expect(feature_flag_2['enabled']).to eq(false)
......@@ -147,8 +147,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do
subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first
feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(true)
expect(feature_flag_2['enabled']).to eq(false)
......@@ -199,7 +199,7 @@ RSpec.describe API::Unleash do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('unleash/unleash', dir: 'ee')
expect(response).to match_response_schema('unleash/unleash')
end
end
......
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