Commit cfff0a4f authored by Bob Van Landuyt's avatar Bob Van Landuyt Committed by Imre Farkas

This adds a context around expiring members

The context will be different depending on the type of member that is
being expired:

For a `GroupMember` the `source` relation will be a group, so the
`meta.root_namespace` will the root ancestor of that group.

For a `ProjectMember` the `source` relation will be a project. So the
`meta.project` will be set to that project, and the
`meta.root_namespace` will become the root namespace of that project.

The `meta.user` will always be set to the username of the user for
which the membership is being expired.
parent 172b3826
...@@ -2,20 +2,29 @@ ...@@ -2,20 +2,29 @@
class RemoveExpiredMembersWorker # rubocop:disable Scalability/IdempotentWorker class RemoveExpiredMembersWorker # rubocop:disable Scalability/IdempotentWorker
include ApplicationWorker include ApplicationWorker
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext include CronjobQueue
feature_category :authentication_and_authorization feature_category :authentication_and_authorization
worker_resource_boundary :cpu worker_resource_boundary :cpu
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def perform def perform
Member.expired.preload(:user).find_each do |member| Member.expired.preload(:user, :source).find_each do |member|
Members::DestroyService.new.execute(member, skip_authorization: true) context = {
user: member.user,
# The ApplicationContext will reject type-mismatches. So a GroupMemeber will only populate `namespace`.
# while a `ProjectMember` will populate `project
project: member.source,
namespace: member.source
}
with_context(context) do
Members::DestroyService.new.execute(member, skip_authorization: true)
expired_user = member.user expired_user = member.user
if expired_user.project_bot? if expired_user.project_bot?
Users::DestroyService.new(nil).execute(expired_user, skip_authorization: true) Users::DestroyService.new(nil).execute(expired_user, skip_authorization: true)
end
end end
rescue => ex rescue => ex
logger.error("Expired Member ID=#{member.id} cannot be removed - #{ex}") logger.error("Expired Member ID=#{member.id} cannot be removed - #{ex}")
......
...@@ -29,6 +29,15 @@ RSpec.describe RemoveExpiredMembersWorker do ...@@ -29,6 +29,15 @@ RSpec.describe RemoveExpiredMembersWorker do
worker.perform worker.perform
expect(non_expiring_project_member.reload).to be_present expect(non_expiring_project_member.reload).to be_present
end end
it 'adds context to resulting jobs' do
worker.perform
new_job = Sidekiq::Worker.jobs.last
expect(new_job).to include('meta.project' => expired_project_member.project.full_path,
'meta.user' => expired_project_member.user.username)
end
end end
context 'project bots' do context 'project bots' do
...@@ -98,6 +107,15 @@ RSpec.describe RemoveExpiredMembersWorker do ...@@ -98,6 +107,15 @@ RSpec.describe RemoveExpiredMembersWorker do
worker.perform worker.perform
expect(non_expiring_group_member.reload).to be_present expect(non_expiring_group_member.reload).to be_present
end end
it 'adds context to resulting jobs' do
worker.perform
new_job = Sidekiq::Worker.jobs.last
expect(new_job).to include('meta.root_namespace' => expired_group_member.group.full_path,
'meta.user' => expired_group_member.user.username)
end
end end
context 'when the last group owner expires' do context 'when the last group owner expires' do
......
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