Commit f8a0fa95 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'move-ff-unleash-api-to-core' into 'master'

Move Unleash API to Core RUN AS-IF-FOSS

See merge request gitlab-org/gitlab!43608
parents 566c4743 df8726f1
...@@ -168,7 +168,6 @@ Performance/Detect: ...@@ -168,7 +168,6 @@ Performance/Detect:
Exclude: Exclude:
- 'ee/spec/controllers/projects/dependencies_controller_spec.rb' - 'ee/spec/controllers/projects/dependencies_controller_spec.rb'
- 'ee/spec/controllers/projects/feature_flags_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/git/tree_spec.rb'
- 'spec/lib/gitlab/import_export/project/tree_restorer_spec.rb' - 'spec/lib/gitlab/import_export/project/tree_restorer_spec.rb'
- 'spec/models/event_spec.rb' - 'spec/models/event_spec.rb'
......
...@@ -11,9 +11,9 @@ module FeatureFlagsHelper ...@@ -11,9 +11,9 @@ module FeatureFlagsHelper
project.feature_flags_client_token project.feature_flags_client_token
end end
def feature_flag_issues_links_endpoint(project, feature_flag, user) 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 end
FeatureFlagsHelper.prepend_if_ee('::EE::FeatureFlagsHelper')
...@@ -2508,6 +2508,11 @@ class Project < ApplicationRecord ...@@ -2508,6 +2508,11 @@ class Project < ApplicationRecord
GroupDeployKey.for_groups(group.self_and_ancestors_ids) GroupDeployKey.for_groups(group.self_and_ancestors_ids)
end end
def feature_flags_client_token
instance = operations_feature_flags_client || create_operations_feature_flags_client!
instance.token
end
private private
def find_service(services, name) 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 ...@@ -630,11 +630,6 @@ module EE
change_head(root_ref) if root_ref.present? change_head(root_ref) if root_ref.present?
end 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 override :lfs_http_url_to_repo
def lfs_http_url_to_repo(operation) def lfs_http_url_to_repo(operation)
return super unless ::Gitlab::Geo.secondary_with_primary? return super unless ::Gitlab::Geo.secondary_with_primary?
......
...@@ -14,7 +14,6 @@ module EE ...@@ -14,7 +14,6 @@ module EE
mount ::API::AuditEvents mount ::API::AuditEvents
mount ::API::ProjectApprovalRules mount ::API::ProjectApprovalRules
mount ::API::ProjectApprovalSettings mount ::API::ProjectApprovalSettings
mount ::API::Unleash
mount ::API::DependencyProxy mount ::API::DependencyProxy
mount ::API::EpicIssues mount ::API::EpicIssues
mount ::API::EpicLinks 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 @@ ...@@ -2,23 +2,11 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe FeatureFlagsHelper do RSpec.describe EE::FeatureFlagsHelper do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) } let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }
let_it_be(:user) { create(:user) } 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 describe '#feature_flag_issues_links_endpoint' do
subject { helper.feature_flag_issues_links_endpoint(project, feature_flag, user) } subject { helper.feature_flag_issues_links_endpoint(project, feature_flag, user) }
......
...@@ -239,6 +239,7 @@ module API ...@@ -239,6 +239,7 @@ module API
mount ::API::Templates mount ::API::Templates
mount ::API::Todos mount ::API::Todos
mount ::API::Triggers mount ::API::Triggers
mount ::API::Unleash
mount ::API::UsageData mount ::API::UsageData
mount ::API::UserCounts mount ::API::UserCounts
mount ::API::Users 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 ...@@ -24,13 +24,13 @@ module API
desc 'Get a list of features (deprecated, v2 client support)' desc 'Get a list of features (deprecated, v2 client support)'
get 'features' do get 'features' do
present :version, 1 present :version, 1
present :features, feature_flags, with: ::EE::API::Entities::UnleashFeature present :features, feature_flags, with: ::API::Entities::UnleashFeature
end end
desc 'Get a list of features' desc 'Get a list of features'
get 'client/features' do get 'client/features' do
present :version, 1 present :version, 1
present :features, feature_flags, with: ::EE::API::Entities::UnleashFeature present :features, feature_flags, with: ::API::Entities::UnleashFeature
end end
post 'client/register' do 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 ...@@ -119,8 +119,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do it 'returns correct active values' do
subject subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(true) expect(feature_flag_1['enabled']).to eq(true)
expect(feature_flag_2['enabled']).to eq(false) expect(feature_flag_2['enabled']).to eq(false)
...@@ -133,8 +133,8 @@ RSpec.describe API::Unleash do ...@@ -133,8 +133,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do it 'returns correct active values' do
subject subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(false) expect(feature_flag_1['enabled']).to eq(false)
expect(feature_flag_2['enabled']).to eq(false) expect(feature_flag_2['enabled']).to eq(false)
...@@ -147,8 +147,8 @@ RSpec.describe API::Unleash do ...@@ -147,8 +147,8 @@ RSpec.describe API::Unleash do
it 'returns correct active values' do it 'returns correct active values' do
subject subject
feature_flag_1 = json_response['features'].select { |f| f['name'] == 'feature_flag_1' }.first feature_flag_1 = json_response['features'].find { |f| f['name'] == 'feature_flag_1' }
feature_flag_2 = json_response['features'].select { |f| f['name'] == 'feature_flag_2' }.first feature_flag_2 = json_response['features'].find { |f| f['name'] == 'feature_flag_2' }
expect(feature_flag_1['enabled']).to eq(true) expect(feature_flag_1['enabled']).to eq(true)
expect(feature_flag_2['enabled']).to eq(false) expect(feature_flag_2['enabled']).to eq(false)
...@@ -199,7 +199,7 @@ RSpec.describe API::Unleash do ...@@ -199,7 +199,7 @@ RSpec.describe API::Unleash do
subject subject
expect(response).to have_gitlab_http_status(:ok) 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
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