From 56cfad3cf79f3e5997e9b4c2a6b2202f5f104786 Mon Sep 17 00:00:00 2001 From: david hamp-gonsalves Date: Fri, 25 Jul 2025 13:20:43 -0300 Subject: [PATCH] Move trial creation to background fixes https://gitlab.com/gitlab-org/gitlab/-/issues/538417 EE: true --- .../trials/welcome_create_service.rb | 16 ++---- .../trials/welcome_create_service_spec.rb | 56 +++++-------------- ee/spec/support/helpers/trial_helpers.rb | 18 ++++++ 3 files changed, 36 insertions(+), 54 deletions(-) diff --git a/ee/app/services/gitlab_subscriptions/trials/welcome_create_service.rb b/ee/app/services/gitlab_subscriptions/trials/welcome_create_service.rb index 6af2bc2cfbbd2d..cf69778d1e8a75 100644 --- a/ee/app/services/gitlab_subscriptions/trials/welcome_create_service.rb +++ b/ee/app/services/gitlab_subscriptions/trials/welcome_create_service.rb @@ -78,22 +78,14 @@ def submit_lead GitlabSubscriptions::CreateLeadService.new.execute(trial_user: lead_params) end - def submit_trial - result = GitlabSubscriptions::Trials::ApplyTrialService.new(uid: user.id, - trial_user_information: trial_params).execute - result[:add_on_purchase] - end - def setup_trial @lead_created = submit_lead.success? unless lead_created return error unless lead_created - add_on_purchase = submit_trial - - return error unless add_on_purchase.present? + GitlabSubscriptions::Trials::ApplyTrialWorker.perform_async(user.id, trial_params.deep_stringify_keys) - success(add_on_purchase) + success end def trial_params @@ -157,10 +149,10 @@ def error ) end - def success(add_on_purchase) + def success ServiceResponse.success( message: 'Trial applied', - payload: { namespace_id: namespace.id, add_on_purchase: add_on_purchase } + payload: { namespace_id: namespace.id } ) end end diff --git a/ee/spec/services/gitlab_subscriptions/trials/welcome_create_service_spec.rb b/ee/spec/services/gitlab_subscriptions/trials/welcome_create_service_spec.rb index 4aadef6bec6f97..aadcdcfc754401 100644 --- a/ee/spec/services/gitlab_subscriptions/trials/welcome_create_service_spec.rb +++ b/ee/spec/services/gitlab_subscriptions/trials/welcome_create_service_spec.rb @@ -11,7 +11,6 @@ let_it_be(:existing_project) { create(:project, namespace: existing_group) } let_it_be(:unrelated_group) { create(:group_with_plan, plan: :free_plan) } let_it_be(:unrelated_project) { create(:project, namespace: unrelated_group) } - let_it_be(:add_on_purchase) { build(:gitlab_subscription_add_on_purchase) } let(:glm_params) { { glm_source: 'some-source', glm_content: 'some-content' } } let(:group) { build(:group) } @@ -67,7 +66,7 @@ let(:retry_params) { {} } let(:lead_service_class) { GitlabSubscriptions::CreateLeadService } - let(:apply_trial_service_class) { GitlabSubscriptions::Trials::ApplyTrialService } + let(:apply_trial_worker_class) { GitlabSubscriptions::Trials::ApplyTrialService } subject(:execute) { described_class.new(params: params, user: user, **retry_params).execute } @@ -75,12 +74,12 @@ context 'when successful' do it 'creates lead and applies trial successfully', :aggregate_failures do expect_create_lead_success(lead_params) - expect_apply_trial_success(user, group, extra_params: group_params) + expect_apply_trial_async(user, namespace: nil, extra_params: glm_params) expect { execute }.to change { Group.count }.by(1).and change { Project.count }.by(1) expect(execute).to be_success expect(execute.message).to eq('Trial applied') - expect(execute.payload).to eq({ namespace_id: Group.last.id, add_on_purchase: add_on_purchase }) + expect(execute.payload).to eq({ namespace_id: Group.last.id }) end context 'when retrying' do @@ -94,12 +93,12 @@ it 'uses existing group' do expect_create_lead_success(lead_params) - expect_apply_trial_success(user, existing_group, extra_params: existing_group_params) + expect_apply_trial_async(user, namespace: existing_group, extra_params: glm_params) expect { execute }.to not_change { Group.count }.and change { Project.count }.by(1) expect(execute).to be_success expect(execute.message).to eq('Trial applied') - expect(execute.payload).to eq({ namespace_id: existing_group.id, add_on_purchase: add_on_purchase }) + expect(execute.payload).to eq({ namespace_id: existing_group.id }) end end @@ -110,28 +109,12 @@ it 'uses existing group and project' do expect_create_lead_success(lead_params) - expect_apply_trial_success(user, existing_group, extra_params: existing_group_params) + expect_apply_trial_async(user, namespace: existing_group, extra_params: glm_params) expect { execute }.to not_change { Group.count }.and not_change { Project.count } expect(execute).to be_success expect(execute.message).to eq('Trial applied') - expect(execute.payload).to eq({ namespace_id: existing_group.id, add_on_purchase: add_on_purchase }) - end - end - - context "when trial creation failed" do - let(:retry_params) do - { namespace_id: existing_group.id, project_id: existing_project.id, lead_created: true } - end - - it 'uses existing group, project and lead' do - expect(lead_service_class).not_to receive(:new) - expect_apply_trial_success(user, existing_group, extra_params: existing_group_params) - - expect { execute }.to not_change { Group.count }.and not_change { Project.count } - expect(execute).to be_success - expect(execute.message).to eq('Trial applied') - expect(execute.payload).to eq({ namespace_id: existing_group.id, add_on_purchase: add_on_purchase }) + expect(execute.payload).to eq({ namespace_id: existing_group.id }) end end @@ -142,7 +125,8 @@ it 'returns not found error and lead/trial is not submitted' do expect(lead_service_class).not_to receive(:new) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) + # TODO expect(execute).to be_error expect(execute.message).to eq('Not found') @@ -161,7 +145,7 @@ it 'returns not found error and lead/trial is not submitted' do expect(lead_service_class).not_to receive(:new) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) expect(execute).to be_error expect(execute.message).to eq('Not found') @@ -181,7 +165,7 @@ it 'returns not found error and lead/trial is not submitted' do expect(lead_service_class).not_to receive(:new) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) expect(execute).to be_error expect(execute.message).to eq('Not found') @@ -196,7 +180,7 @@ it 'returns model error and does not attempt to execute next steps' do expect(lead_service_class).not_to receive(:new) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) expect(Projects::CreateService).not_to receive(:new) expect(execute).to be_error @@ -211,7 +195,7 @@ it 'returns model error and does not attempt to execute next steps' do expect(lead_service_class).not_to receive(:new) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) expect(execute).to be_error expect(execute.message).to eq("Trial creation failed in project stage") @@ -223,7 +207,7 @@ context 'when lead creation fails' do it 'returns error with lead failure reason and does not attempt to submit trial' do expect_create_lead_fail(lead_params) - expect(apply_trial_service_class).not_to receive(:new) + expect(apply_trial_worker_class).not_to receive(:perform_async) expect(execute).to be_error expect(execute.message).to eq("Trial creation failed in lead stage") @@ -231,17 +215,5 @@ model_errors: {} }) end end - - context 'when trial creation fails' do - it 'returns error with trial failure reason' do - expect_create_lead_success(lead_params) - expect_apply_trial_fail(user, group, extra_params: existing_group_params) - - expect(execute).to be_error - expect(execute.message).to eq("Trial creation failed in application stage") - expect(execute.payload).to eq({ namespace_id: Group.last.id, project_id: Project.last.id, lead_created: true, - model_errors: {} }) - end - end end end diff --git a/ee/spec/support/helpers/trial_helpers.rb b/ee/spec/support/helpers/trial_helpers.rb index a15ebb2bcce696..5bbd0f370abbfb 100644 --- a/ee/spec/support/helpers/trial_helpers.rb +++ b/ee/spec/support/helpers/trial_helpers.rb @@ -51,6 +51,24 @@ def stub_apply_trial(user, namespace_id: anything, success: true, extra_params: end end + def expect_apply_trial_async(user, namespace: nil, extra_params: {}) + trial_user_information = extra_params.merge({ + namespace_id: be_a(Integer), + gitlab_com_trial: true, + sync_to_gl: true, + namespace: anything + }) + + if namespace.present? + trial_user_information[:namespace_id] = namespace.id + trial_user_information[:namespace] = namespace.slice(:id, :name, :path, :kind, :trial_ends_on) + trial_user_information[:namespace][:plan] = namespace.actual_plan_name + end + + expect(GitlabSubscriptions::Trials::ApplyTrialWorker) + .to receive(:perform_async).with(user.id, trial_user_information.deep_stringify_keys).and_call_original + end + def expect_apply_trial_success(user, group, extra_params: {}) stub_apply_trial(user, namespace_id: group.id, success: true, extra_params: extra_params) end -- GitLab