Commit 16e6aedd authored by Jay's avatar Jay Committed by Markus Koller

Assign trial to only eligible namespace

In the registration flow if a user only has 1 eligible namespace, then
assign the trial to that namespace.

part of:
https://gitlab.com/gitlab-org/gitlab/-/issues/339654

Changelog: changed
EE: true
parent 44cf50ef
...@@ -34,34 +34,15 @@ class TrialsController < ApplicationController ...@@ -34,34 +34,15 @@ class TrialsController < ApplicationController
if params[:glm_source] == 'about.gitlab.com' if params[:glm_source] == 'about.gitlab.com'
redirect_to(new_users_sign_up_group_path(url_params.merge(trial_onboarding_flow: true))) redirect_to(new_users_sign_up_group_path(url_params.merge(trial_onboarding_flow: true)))
elsif @namespace = helpers.only_trialable_group_namespace
apply_trial_and_redirect
else else
redirect_to select_trials_url(url_params) redirect_to select_trials_url(url_params)
end end
end end
def apply def apply
return render(:select) if @namespace.invalid? apply_trial_and_redirect
@result = GitlabSubscriptions::ApplyTrialService.new.execute(apply_trial_params)
if @result&.dig(:success)
record_experiment_user(:remove_known_trial_form_fields_welcoming, namespace_id: @namespace.id)
record_experiment_conversion_event(:remove_known_trial_form_fields_welcoming)
experiment(:trial_registration_with_reassurance, actor: current_user)
.track(:apply_trial, label: 'trials:apply', namespace: @namespace, user: current_user)
experiment(:force_company_trial, user: current_user).track(:create_trial, namespace: @namespace, user: current_user, label: 'trials_controller') if @namespace.created_at > 24.hours.ago
experiment(:combined_registration, user: current_user).track(:create_trial)
if discover_group_security_flow?
redirect_trial_user_to_feature_experiment_flow
else
redirect_to group_url(@namespace, { trial: true })
end
else
render :select
end
end end
def extend_reactivate def extend_reactivate
...@@ -203,4 +184,29 @@ class TrialsController < ApplicationController ...@@ -203,4 +184,29 @@ class TrialsController < ApplicationController
def discover_group_security_flow? def discover_group_security_flow?
%w(discover-group-security discover-project-security).include?(params[:glm_content]) %w(discover-group-security discover-project-security).include?(params[:glm_content])
end end
def apply_trial_and_redirect
return render(:select) if @namespace.invalid?
@result = GitlabSubscriptions::ApplyTrialService.new.execute(apply_trial_params)
if @result&.dig(:success)
record_experiment_user(:remove_known_trial_form_fields_welcoming, namespace_id: @namespace.id)
record_experiment_conversion_event(:remove_known_trial_form_fields_welcoming)
experiment(:trial_registration_with_reassurance, actor: current_user)
.track(:apply_trial, label: 'trials:apply', namespace: @namespace, user: current_user)
experiment(:force_company_trial, user: current_user).track(:create_trial, namespace: @namespace, user: current_user, label: 'trials_controller') if @namespace.created_at > 24.hours.ago
experiment(:combined_registration, user: current_user).track(:create_trial)
if discover_group_security_flow?
redirect_trial_user_to_feature_experiment_flow
else
redirect_to group_url(@namespace, { trial: true })
end
else
render :select
end
end
end end
...@@ -24,7 +24,7 @@ module EE ...@@ -24,7 +24,7 @@ module EE
end end
def trial_selection_intro_text def trial_selection_intro_text
if any_trial_group_namespaces? if any_trialable_group_namespaces?
s_('Trials|You can apply your trial to a new group or an existing group.') s_('Trials|You can apply your trial to a new group or an existing group.')
else else
s_('Trials|Create a new group to start your GitLab Ultimate trial.') s_('Trials|Create a new group to start your GitLab Ultimate trial.')
...@@ -32,13 +32,13 @@ module EE ...@@ -32,13 +32,13 @@ module EE
end end
def show_trial_namespace_select? def show_trial_namespace_select?
any_trial_group_namespaces? any_trialable_group_namespaces?
end end
def namespace_options_for_select(selected = nil) def namespace_options_for_select(selected = nil)
grouped_options = { grouped_options = {
'New' => [[_('Create group'), 0]], 'New' => [[_('Create group'), 0]],
'Groups' => trial_group_namespaces.map { |n| [n.name, n.id] } 'Groups' => trialable_group_namespaces.map { |n| [n.name, n.id] }
} }
grouped_options_for_select(grouped_options, selected, prompt: _('Please select')) grouped_options_for_select(grouped_options, selected, prompt: _('Please select'))
...@@ -84,16 +84,20 @@ module EE ...@@ -84,16 +84,20 @@ module EE
end end
end end
def only_trialable_group_namespace
trialable_group_namespaces.first if trialable_group_namespaces.count == 1
end
private private
def trial_group_namespaces def trialable_group_namespaces
strong_memoize(:trial_group_namespaces) do strong_memoize(:trialable_group_namespaces) do
current_user.manageable_groups_eligible_for_trial current_user.manageable_groups_eligible_for_trial
end end
end end
def any_trial_group_namespaces? def any_trialable_group_namespaces?
trial_group_namespaces.any? trialable_group_namespaces.any?
end end
end end
end end
...@@ -91,7 +91,7 @@ RSpec.describe TrialsController do ...@@ -91,7 +91,7 @@ RSpec.describe TrialsController do
end end
end end
subject do subject(:post_create_lead) do
post :create_lead, params: post_params post :create_lead, params: post_params
response response
end end
...@@ -111,6 +111,50 @@ RSpec.describe TrialsController do ...@@ -111,6 +111,50 @@ RSpec.describe TrialsController do
is_expected.to redirect_to(new_users_sign_up_group_path(glm_source: 'about.gitlab.com', trial_onboarding_flow: true)) is_expected.to redirect_to(new_users_sign_up_group_path(glm_source: 'about.gitlab.com', trial_onboarding_flow: true))
end end
end end
context 'when user has 1 trial eligible namespace', :experiment do
let_it_be(:namespace) { create(:group, path: 'namespace-test') }
let(:apply_trial_result) { true }
before do
namespace.add_owner(user)
allow_next_instance_of(GitlabSubscriptions::ApplyTrialService) do |service|
allow(service).to receive(:execute).and_return({ success: apply_trial_result })
end
end
context 'when the ApplyTrialService is successful' do
it 'applies a trial to the namespace' do
gl_com_params = { gitlab_com_trial: true, sync_to_gl: true }
apply_trial_params = {
uid: user.id,
trial_user: ActionController::Parameters.new(post_params).permit(:namespace_id).merge(gl_com_params)
}
expect_next_instance_of(GitlabSubscriptions::ApplyTrialService) do |service|
expect(service).to receive(:execute).with(apply_trial_params).and_return({ success: true })
end
post_create_lead
end
it "tracks for the combined_registration experiment" do
expect(experiment(:combined_registration)).to track(:create_trial).on_next_instance
post_create_lead
end
it { is_expected.to redirect_to(group_url(namespace, { trial: true })) }
end
context 'when the ApplyTrialService is unsuccessful' do
let(:apply_trial_result) { false }
it { is_expected.to render_template(:select) }
end
end
end end
context 'on failure' do context 'on failure' do
...@@ -152,7 +196,7 @@ RSpec.describe TrialsController do ...@@ -152,7 +196,7 @@ RSpec.describe TrialsController do
expect(lead_service).to receive(:execute).with({ trial_user: expected_params }).and_return({ success: true }) expect(lead_service).to receive(:execute).with({ trial_user: expected_params }).and_return({ success: true })
end end
subject post_create_lead
end end
end end
end end
......
...@@ -65,13 +65,13 @@ RSpec.describe EE::TrialHelper do ...@@ -65,13 +65,13 @@ RSpec.describe EE::TrialHelper do
let_it_be(:group1) { create :group } let_it_be(:group1) { create :group }
let_it_be(:group2) { create :group } let_it_be(:group2) { create :group }
let(:trial_group_namespaces) { [] } let(:trialable_group_namespaces) { [] }
let(:new_optgroup_regex) { /<optgroup label="New"><option/ } let(:new_optgroup_regex) { /<optgroup label="New"><option/ }
let(:groups_optgroup_regex) { /<optgroup label="Groups"><option/ } let(:groups_optgroup_regex) { /<optgroup label="Groups"><option/ }
before do before do
allow(helper).to receive(:trial_group_namespaces).and_return(trial_group_namespaces) allow(helper).to receive(:trialable_group_namespaces).and_return(trialable_group_namespaces)
end end
subject { helper.namespace_options_for_select } subject { helper.namespace_options_for_select }
...@@ -84,7 +84,7 @@ RSpec.describe EE::TrialHelper do ...@@ -84,7 +84,7 @@ RSpec.describe EE::TrialHelper do
end end
context 'when only group namespaces are eligible' do context 'when only group namespaces are eligible' do
let(:trial_group_namespaces) { [group1, group2] } let(:trialable_group_namespaces) { [group1, group2] }
it 'returns the "New" and "Groups" option groups', :aggregate_failures do it 'returns the "New" and "Groups" option groups', :aggregate_failures do
is_expected.to match(new_optgroup_regex) is_expected.to match(new_optgroup_regex)
...@@ -93,7 +93,7 @@ RSpec.describe EE::TrialHelper do ...@@ -93,7 +93,7 @@ RSpec.describe EE::TrialHelper do
end end
context 'when some group namespaces are eligible' do context 'when some group namespaces are eligible' do
let(:trial_group_namespaces) { [group1, group2] } let(:trialable_group_namespaces) { [group1, group2] }
it 'returns the "New", "Groups" option groups', :aggregate_failures do it 'returns the "New", "Groups" option groups', :aggregate_failures do
is_expected.to match(new_optgroup_regex) is_expected.to match(new_optgroup_regex)
...@@ -104,7 +104,7 @@ RSpec.describe EE::TrialHelper do ...@@ -104,7 +104,7 @@ RSpec.describe EE::TrialHelper do
describe '#trial_selection_intro_text' do describe '#trial_selection_intro_text' do
before do before do
allow(helper).to receive(:any_trial_group_namespaces?).and_return(have_group_namespace) allow(helper).to receive(:any_trialable_group_namespaces?).and_return(have_group_namespace)
end end
subject { helper.trial_selection_intro_text } subject { helper.trial_selection_intro_text }
...@@ -123,7 +123,7 @@ RSpec.describe EE::TrialHelper do ...@@ -123,7 +123,7 @@ RSpec.describe EE::TrialHelper do
let_it_be(:have_group_namespace) { false } let_it_be(:have_group_namespace) { false }
before do before do
allow(helper).to receive(:any_trial_group_namespaces?).and_return(have_group_namespace) allow(helper).to receive(:any_trialable_group_namespaces?).and_return(have_group_namespace)
end end
subject { helper.show_trial_namespace_select? } subject { helper.show_trial_namespace_select? }
...@@ -288,4 +288,33 @@ RSpec.describe EE::TrialHelper do ...@@ -288,4 +288,33 @@ RSpec.describe EE::TrialHelper do
it { is_expected.to eq(result) } it { is_expected.to eq(result) }
end end
end end
describe '#only_trialable_group_namespace' do
subject { helper.only_trialable_group_namespace }
let_it_be(:group1) { create :group }
let_it_be(:group2) { create :group }
let(:trialable_group_namespaces) { [group1] }
before do
allow(helper).to receive(:trialable_group_namespaces).and_return(trialable_group_namespaces)
end
context 'when there is 1 namespace group eligible' do
it { is_expected.to eq(group1) }
end
context 'when more than 1 namespace is eligible' do
let(:trialable_group_namespaces) { [group1, group2] }
it { is_expected.to be_nil }
end
context 'when there are 0 namespace groups eligible' do
let(:trialable_group_namespaces) { [] }
it { is_expected.to be_nil }
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