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
if params[:glm_source] == 'about.gitlab.com'
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
redirect_to select_trials_url(url_params)
end
end
def apply
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
apply_trial_and_redirect
end
def extend_reactivate
......@@ -203,4 +184,29 @@ class TrialsController < ApplicationController
def discover_group_security_flow?
%w(discover-group-security discover-project-security).include?(params[:glm_content])
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
......@@ -24,7 +24,7 @@ module EE
end
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.')
else
s_('Trials|Create a new group to start your GitLab Ultimate trial.')
......@@ -32,13 +32,13 @@ module EE
end
def show_trial_namespace_select?
any_trial_group_namespaces?
any_trialable_group_namespaces?
end
def namespace_options_for_select(selected = nil)
grouped_options = {
'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'))
......@@ -84,16 +84,20 @@ module EE
end
end
def only_trialable_group_namespace
trialable_group_namespaces.first if trialable_group_namespaces.count == 1
end
private
def trial_group_namespaces
strong_memoize(:trial_group_namespaces) do
def trialable_group_namespaces
strong_memoize(:trialable_group_namespaces) do
current_user.manageable_groups_eligible_for_trial
end
end
def any_trial_group_namespaces?
trial_group_namespaces.any?
def any_trialable_group_namespaces?
trialable_group_namespaces.any?
end
end
end
......@@ -91,7 +91,7 @@ RSpec.describe TrialsController do
end
end
subject do
subject(:post_create_lead) do
post :create_lead, params: post_params
response
end
......@@ -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))
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
context 'on failure' do
......@@ -152,7 +196,7 @@ RSpec.describe TrialsController do
expect(lead_service).to receive(:execute).with({ trial_user: expected_params }).and_return({ success: true })
end
subject
post_create_lead
end
end
end
......
......@@ -65,13 +65,13 @@ RSpec.describe EE::TrialHelper do
let_it_be(:group1) { 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(:groups_optgroup_regex) { /<optgroup label="Groups"><option/ }
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
subject { helper.namespace_options_for_select }
......@@ -84,7 +84,7 @@ RSpec.describe EE::TrialHelper do
end
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
is_expected.to match(new_optgroup_regex)
......@@ -93,7 +93,7 @@ RSpec.describe EE::TrialHelper do
end
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
is_expected.to match(new_optgroup_regex)
......@@ -104,7 +104,7 @@ RSpec.describe EE::TrialHelper do
describe '#trial_selection_intro_text' 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
subject { helper.trial_selection_intro_text }
......@@ -123,7 +123,7 @@ RSpec.describe EE::TrialHelper do
let_it_be(:have_group_namespace) { false }
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
subject { helper.show_trial_namespace_select? }
......@@ -288,4 +288,33 @@ RSpec.describe EE::TrialHelper do
it { is_expected.to eq(result) }
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
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