Commit 4800446a authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch 'namespace-onboarding-action-add-user' into 'master'

Add user added namespace onboarding action

See merge request gitlab-org/gitlab!49383
parents 95069124 ba032b0d
...@@ -9,7 +9,8 @@ class NamespaceOnboardingAction < ApplicationRecord ...@@ -9,7 +9,8 @@ class NamespaceOnboardingAction < ApplicationRecord
subscription_created: 1, subscription_created: 1,
git_write: 2, git_write: 2,
merge_request_created: 3, merge_request_created: 3,
git_read: 4 git_read: 4,
user_added: 6
}.freeze }.freeze
enum action: ACTIONS enum action: ACTIONS
......
...@@ -38,6 +38,8 @@ module Members ...@@ -38,6 +38,8 @@ module Members
end end
end end
enqueue_onboarding_progress_action(source) if members.size > errors.size
return success unless errors.any? return success unless errors.any?
error(errors.to_sentence) error(errors.to_sentence)
...@@ -50,6 +52,10 @@ module Members ...@@ -50,6 +52,10 @@ module Members
limit && limit < 0 ? nil : limit limit && limit < 0 ? nil : limit
end end
def enqueue_onboarding_progress_action(source)
Namespaces::OnboardingUserAddedWorker.perform_async(source.id)
end
end end
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
class OnboardingProgressService class OnboardingProgressService
def initialize(namespace) def initialize(namespace)
@namespace = namespace @namespace = namespace.root_ancestor
end end
def execute(action:) def execute(action:)
......
...@@ -1787,6 +1787,14 @@ ...@@ -1787,6 +1787,14 @@
:weight: 1 :weight: 1
:idempotent: :idempotent:
:tags: [] :tags: []
- :name: namespaces_onboarding_user_added
:feature_category: :users
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: new_issue - :name: new_issue
:feature_category: :issue_tracking :feature_category: :issue_tracking
:has_external_dependencies: :has_external_dependencies:
......
# frozen_string_literal: true
module Namespaces
class OnboardingUserAddedWorker
include ApplicationWorker
feature_category :users
urgency :low
idempotent!
def perform(namespace_id)
namespace = Namespace.find(namespace_id)
OnboardingProgressService.new(namespace).execute(action: :user_added)
end
end
end
...@@ -198,6 +198,8 @@ ...@@ -198,6 +198,8 @@
- 1 - 1
- - namespaceless_project_destroy - - namespaceless_project_destroy
- 1 - 1
- - namespaces_onboarding_user_added
- 1
- - new_epic - - new_epic
- 2 - 2
- - new_issue - - new_issue
......
...@@ -3,59 +3,68 @@ ...@@ -3,59 +3,68 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Members::CreateService do RSpec.describe Members::CreateService do
let(:project) { create(:project) } let_it_be(:project) { create(:project) }
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:project_user) { create(:user) } let_it_be(:project_user) { create(:user) }
let_it_be(:user_ids) { project_user.id.to_s }
let_it_be(:access_level) { Gitlab::Access::GUEST }
let(:params) { { user_ids: user_ids, access_level: access_level } }
subject(:execute_service) { described_class.new(user, params).execute(project) }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
allow(Namespaces::OnboardingUserAddedWorker).to receive(:idempotent?).and_return(false)
end end
it 'adds user to members' do context 'when passing valid parameters' do
params = { user_ids: project_user.id.to_s, access_level: Gitlab::Access::GUEST } it 'adds a user to members' do
result = described_class.new(user, params).execute(project) expect(execute_service[:status]).to eq(:success)
expect(project.users).to include project_user
expect(result[:status]).to eq(:success) expect(Namespaces::OnboardingUserAddedWorker.jobs.last['args'][0]).to eq(project.id)
expect(project.users).to include project_user end
end end
it 'adds no user to members' do context 'when passing no user ids' do
params = { user_ids: '', access_level: Gitlab::Access::GUEST } let(:user_ids) { '' }
result = described_class.new(user, params).execute(project)
expect(result[:status]).to eq(:error) it 'does not add a member' do
expect(result[:message]).to be_present expect(execute_service[:status]).to eq(:error)
expect(project.users).not_to include project_user expect(execute_service[:message]).to be_present
expect(project.users).not_to include project_user
expect(Namespaces::OnboardingUserAddedWorker.jobs.size).to eq(0)
end
end end
it 'limits the number of users to 100' do context 'when passing many user ids' do
user_ids = 1.upto(101).to_a.join(',') let(:user_ids) { 1.upto(101).to_a.join(',') }
params = { user_ids: user_ids, access_level: Gitlab::Access::GUEST }
result = described_class.new(user, params).execute(project) it 'limits the number of users to 100' do
expect(execute_service[:status]).to eq(:error)
expect(result[:status]).to eq(:error) expect(execute_service[:message]).to be_present
expect(result[:message]).to be_present expect(project.users).not_to include project_user
expect(project.users).not_to include project_user expect(Namespaces::OnboardingUserAddedWorker.jobs.size).to eq(0)
end
end end
it 'does not add an invalid member' do context 'when passing an invalid access level' do
params = { user_ids: project_user.id.to_s, access_level: -1 } let(:access_level) { -1 }
result = described_class.new(user, params).execute(project)
expect(result[:status]).to eq(:error) it 'does not add a member' do
expect(result[:message]).to include("#{project_user.username}: Access level is not included in the list") expect(execute_service[:status]).to eq(:error)
expect(project.users).not_to include project_user expect(execute_service[:message]).to include("#{project_user.username}: Access level is not included in the list")
expect(project.users).not_to include project_user
expect(Namespaces::OnboardingUserAddedWorker.jobs.size).to eq(0)
end
end end
it 'does not add a member with an existing invite' do context 'when passing an existing invite user id' do
invited_member = create(:project_member, :invited, project: project) let(:user_ids) { create(:project_member, :invited, project: project).invite_email }
params = { user_ids: invited_member.invite_email,
access_level: Gitlab::Access::GUEST }
result = described_class.new(user, params).execute(project)
expect(result[:status]).to eq(:error) it 'does not add a member' do
expect(result[:message]).to eq('Invite email has already been taken') expect(execute_service[:status]).to eq(:error)
expect(execute_service[:message]).to eq('Invite email has already been taken')
expect(Namespaces::OnboardingUserAddedWorker.jobs.size).to eq(0)
end
end end
end end
...@@ -4,16 +4,30 @@ require 'spec_helper' ...@@ -4,16 +4,30 @@ require 'spec_helper'
RSpec.describe OnboardingProgressService do RSpec.describe OnboardingProgressService do
describe '#execute' do describe '#execute' do
let_it_be(:namespace) { build(:namespace) } let(:namespace) { create(:namespace, parent: root_namespace) }
let(:action) { :namespace_action }
subject(:execute_service) { described_class.new(namespace).execute(action: action) } subject(:execute_service) { described_class.new(namespace).execute(action: :subscription_created) }
it 'records a namespace onboarding progress action' do context 'when the namespace is a root' do
expect(NamespaceOnboardingAction).to receive(:create_action) let(:root_namespace) { nil }
.with(namespace, :namespace_action)
subject it 'records a namespace onboarding progress action for the given namespace' do
expect(NamespaceOnboardingAction).to receive(:create_action)
.with(namespace, :subscription_created).and_call_original
expect { execute_service }.to change(NamespaceOnboardingAction, :count).by(1)
end
end
context 'when the namespace is not the root' do
let_it_be(:root_namespace) { build(:namespace) }
it 'records a namespace onboarding progress action for the root namespace' do
expect(NamespaceOnboardingAction).to receive(:create_action)
.with(root_namespace, :subscription_created).and_call_original
expect { execute_service }.to change(NamespaceOnboardingAction, :count).by(1)
end
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::OnboardingUserAddedWorker, '#perform' do
include AfterNextHelpers
let_it_be(:group) { create(:group) }
it 'records the event' do
expect_next(OnboardingProgressService, group)
.to receive(:execute).with(action: :user_added).and_call_original
expect { subject.perform(group.id) }.to change(NamespaceOnboardingAction, :count).by(1)
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