Commit 29a8d9df authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'link-ff-lists-strategies-api' into 'master'

Support gitlabUserList Strategy in Feature Flags Controller

See merge request gitlab-org/gitlab!31510
parents bc048d82 349d6edc
...@@ -111,7 +111,7 @@ class Projects::FeatureFlagsController < Projects::ApplicationController ...@@ -111,7 +111,7 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
.permit(:name, :description, :active, :version, .permit(:name, :description, :active, :version,
scopes_attributes: [:environment_scope, :active, scopes_attributes: [:environment_scope, :active,
strategies: [:name, parameters: [:groupId, :percentage, :userIds]]], strategies: [:name, parameters: [:groupId, :percentage, :userIds]]],
strategies_attributes: [:name, parameters: [:groupId, :percentage, :userIds], scopes_attributes: [:environment_scope]]) strategies_attributes: [:name, :user_list_id, parameters: [:groupId, :percentage, :userIds], scopes_attributes: [:environment_scope]])
end end
def update_params def update_params
...@@ -119,7 +119,8 @@ class Projects::FeatureFlagsController < Projects::ApplicationController ...@@ -119,7 +119,8 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
.permit(:name, :description, :active, .permit(:name, :description, :active,
scopes_attributes: [:id, :environment_scope, :active, :_destroy, scopes_attributes: [:id, :environment_scope, :active, :_destroy,
strategies: [:name, parameters: [:groupId, :percentage, :userIds]]], strategies: [:name, parameters: [:groupId, :percentage, :userIds]]],
strategies_attributes: [:id, :name, :_destroy, parameters: [:groupId, :percentage, :userIds], strategies_attributes: [:id, :name, :user_list_id, :_destroy,
parameters: [:groupId, :percentage, :userIds],
scopes_attributes: [:id, :environment_scope, :_destroy]]) scopes_attributes: [:id, :environment_scope, :_destroy]])
end end
......
...@@ -35,6 +35,10 @@ module Operations ...@@ -35,6 +35,10 @@ module Operations
accepts_nested_attributes_for :scopes, allow_destroy: true accepts_nested_attributes_for :scopes, allow_destroy: true
def user_list_id=(user_list_id)
self.user_list = ::Operations::FeatureFlags::UserList.find(user_list_id)
end
private private
def same_project_validation def same_project_validation
......
...@@ -6,5 +6,6 @@ module FeatureFlags ...@@ -6,5 +6,6 @@ module FeatureFlags
expose :name expose :name
expose :parameters expose :parameters
expose :scopes, with: FeatureFlags::ScopeEntity expose :scopes, with: FeatureFlags::ScopeEntity
expose :user_list, with: FeatureFlags::UserListEntity, expose_nil: false
end end
end end
# frozen_string_literal: true
module FeatureFlags
class UserListEntity < Grape::Entity
expose :id
expose :iid
expose :name
expose :user_xids
end
end
...@@ -693,6 +693,50 @@ describe Projects::FeatureFlagsController do ...@@ -693,6 +693,50 @@ describe Projects::FeatureFlagsController do
end end
end end
context 'when creating a version 2 feature flag with a gitlabUserList strategy' do
let!(:user_list) do
create(:operations_feature_flag_user_list, project: project,
name: 'My List', user_xids: 'user1,user2')
end
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
operations_feature_flag: {
name: 'my_feature_flag',
active: true,
version: 'new_version_flag',
strategies_attributes: [{
name: 'gitlabUserList',
parameters: {},
user_list_id: user_list.id,
scopes_attributes: [{ environment_scope: 'production' }]
}]
}
}
end
it 'creates the new strategy' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to match([a_hash_including({
'name' => 'gitlabUserList',
'parameters' => {},
'user_list' => {
'id' => user_list.id,
'iid' => user_list.iid,
'name' => 'My List',
'user_xids' => 'user1,user2'
},
'scopes' => [a_hash_including({
'environment_scope' => 'production'
})]
})])
end
end
context 'when version parameter is invalid' do context 'when version parameter is invalid' do
let(:params) do let(:params) do
{ {
...@@ -1316,6 +1360,114 @@ describe Projects::FeatureFlagsController do ...@@ -1316,6 +1360,114 @@ describe Projects::FeatureFlagsController do
expect(strategy_json['scopes']).to eq([]) expect(strategy_json['scopes']).to eq([])
end end
it 'creates a gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
name: 'gitlabUserList',
parameters: {},
user_list_id: user_list.id
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to match([a_hash_including({
'id' => an_instance_of(Integer),
'name' => 'gitlabUserList',
'parameters' => {},
'user_list' => {
'id' => user_list.id,
'iid' => user_list.iid,
'name' => 'My List',
'user_xids' => 'user1,user2'
},
'scopes' => []
})])
end
it 'supports switching the associated user list for an existing gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
other_user_list = create(:operations_feature_flag_user_list, project: project, name: 'Other List', user_xids: 'user3')
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
user_list_id: other_user_list.id
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([{
'id' => strategy.id,
'name' => 'gitlabUserList',
'parameters' => {},
'user_list' => {
'id' => other_user_list.id,
'iid' => other_user_list.iid,
'name' => 'Other List',
'user_xids' => 'user3'
},
'scopes' => []
}])
end
it 'does not delete a user list when deleting a gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
_destroy: true
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([])
expect(::Operations::FeatureFlags::Strategy.count).to eq(0)
expect(::Operations::FeatureFlags::StrategyUserList.count).to eq(0)
expect(::Operations::FeatureFlags::UserList.first).to eq(user_list)
end
it 'returns not found when trying to create a gitlabUserList strategy with an invalid user list id' do
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
name: 'gitlabUserList',
parameters: {},
user_list_id: 1
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'updates an existing strategy' do it 'updates an existing strategy' do
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {}) strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {})
params = { params = {
......
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