Commit 8ac11f4e authored by Eugie Limpin's avatar Eugie Limpin Committed by Aleksei Lipniagov

Send account validation email when pipeline fails

parent b845112d
......@@ -461,7 +461,7 @@ module EE
end
def requires_credit_card?(project)
return false unless ::Gitlab.com?
return false unless ::Gitlab.dev_env_or_com?
return false unless created_after_credit_card_release_day?(project)
root_namespace = project.root_namespace
......
......@@ -3,6 +3,7 @@
module EE
module NotificationService
include ::Gitlab::Utils::UsageData
extend ::Gitlab::Utils::Override
# When we add approvers to a merge request we should send an email to:
#
......@@ -91,6 +92,13 @@ module EE
end
end
override :pipeline_finished
def pipeline_finished(pipeline, ref_status: nil, recipients: nil)
super
send_account_validation_email(pipeline)
end
private
def oncall_user_removed_recipients(rotation, removed_user)
......@@ -175,5 +183,20 @@ module EE
.deliver_later
end
end
def send_account_validation_email(pipeline)
return unless ::Feature.enabled?(:account_validation_email)
return unless ::Gitlab.dev_env_or_com?
return unless pipeline.failed?
return unless pipeline.user_not_verified?
user = pipeline.user
return if user.has_required_credit_card_to_run_pipelines?(pipeline.project)
return unless user.can?(:receive_notifications)
return unless user.email_opted_in?
email = user.notification_email_or_default
mailer.account_validation_email(pipeline, email).deliver_later
end
end
end
---
name: account_validation_email
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/75183
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/346884
milestone: '14.6'
type: development
group: group::activation
default_enabled: false
......@@ -1812,7 +1812,7 @@ RSpec.describe User do
with_them do
before do
allow(::Gitlab).to receive(:com?).and_return(saas == :saas)
allow(::Gitlab).to receive(:dev_env_or_com?).and_return(saas == :saas)
user.created_at = ::Users::CreditCardValidation::RELEASE_DAY + days_from_release.days
allow(user).to receive(:credit_card_validated_at).and_return(Time.current) if cc_present
allow(project.namespace).to receive(:free_plan?).and_return(plan == :free)
......@@ -1860,7 +1860,7 @@ RSpec.describe User do
with_them do
before do
allow(::Gitlab).to receive(:com?).and_return(saas == :saas)
allow(::Gitlab).to receive(:dev_env_or_com?).and_return(saas == :saas)
user.created_at = ::Users::CreditCardValidation::RELEASE_DAY + days_from_release.days
allow(user).to receive(:credit_card_validated_at).and_return(Time.current) if cc_present
allow(project.namespace).to receive(:free_plan?).and_return(plan == :free)
......
......@@ -986,4 +986,88 @@ RSpec.describe EE::NotificationService, :mailer do
end
end
end
describe '#pipeline_finished' do
let_it_be(:project, reload: true) { create(:project, :repository) }
let(:user) { create(:user, email_opted_in: true) }
let(:has_required_credit_card_to_run_pipelines) { false }
let(:failure_reason) { 'user_not_verified' }
let(:pipeline) do
create(
:ci_empty_pipeline,
project: project,
ref: 'master',
status: 'failed',
failure_reason: failure_reason,
sha: project.commit.id,
user: user
)
end
subject(:pipeline_finished) { NotificationService.new.pipeline_finished(pipeline) }
before do
allow(::Gitlab).to receive(:dev_env_or_com?).and_return(true)
allow(user).to receive(:has_required_credit_card_to_run_pipelines?).and_return(has_required_credit_card_to_run_pipelines)
project.add_maintainer(user)
end
shared_examples 'does not send account activation email' do
it 'does not send account activation email' do
expect(Notify).not_to receive(:account_validation_email)
pipeline_finished
end
end
context 'with a failed pipeline' do
it 'sends account activation email' do
expect(Notify).to receive(:account_validation_email).with(pipeline, user.notification_email_or_default).once.and_call_original
pipeline_finished
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(account_validation_email: false)
end
include_examples 'does not send account activation email'
end
context 'when not in dev env or gitlab.com' do
before do
allow(::Gitlab).to receive(:dev_env_or_com?).and_return(false)
end
include_examples 'does not send account activation email'
end
context 'when user is not opted in to marketing emails' do
let(:user) { create(:user, email_opted_in: false) }
include_examples 'does not send account activation email'
end
context "when failure reason is not user_not_verified" do
let(:failure_reason) { 'unknown_failure' }
include_examples 'does not send account activation email'
end
context 'when user account is validated' do
let(:has_required_credit_card_to_run_pipelines) { true }
include_examples 'does not send account activation email'
end
context 'when user cannot receive notifications' do
let(:user) { User.ghost }
include_examples 'does not send account activation email'
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