Class: Groups::DestroyService

Inherits:
BaseService show all
Defined in:
app/services/groups/destroy_service.rb

Constant Summary collapse

DestroyError =
Class.new(StandardError)

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #group, #params

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from Groups::BaseService

Instance Method Details

#async_executeObject



7
8
9
10
11
12
# File 'app/services/groups/destroy_service.rb', line 7

def async_execute
  mark_deletion_in_progress

  job_id = GroupDestroyWorker.perform_async(group.id, current_user.id)
  Gitlab::AppLogger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}")
end

#executeObject

rubocop: disable CodeReuse/ActiveRecord



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/groups/destroy_service.rb', line 15

def execute
  authorize_group_deletion
  mark_deletion_in_progress

  group.projects.includes(:project_feature).find_each do |project|
    # Execute the destruction of the models immediately to ensure atomic cleanup.
    success = ::Projects::DestroyService.new(project, current_user).execute

    raise DestroyError, "Project #{project.id} can't be deleted" unless success
  end

  # reload the relation to prevent triggering destroy hooks on the projects again
  group.projects.reset

  group.children.each do |group|
    # This needs to be synchronous since the namespace gets destroyed below
    DestroyService.new(group, current_user).execute
  end

  group.chat_team&.remove_mattermost_team(current_user)

  user_ids_for_project_authorizations_refresh = obtain_user_ids_for_project_authorizations_refresh

  destroy_associated_users
  ::Import::BulkImports::RemoveExportUploadsService.new(group).execute

  group.destroy

  if user_ids_for_project_authorizations_refresh.present?
    UserProjectAccessChangedService
      .new(user_ids_for_project_authorizations_refresh)
      .execute
  end

  publish_event

  group
rescue Exception => e # rubocop:disable Lint/RescueException -- Namespace.transaction can raise Exception
  log_payload = {
    group_id: group.id,
    current_user: current_user&.id,
    error_class: e.class,
    error_message: e.message,
    error_backtrace: e.backtrace
  }

  reschedule_deletion
  Gitlab::AppLogger.error(log_payload.merge(message: "Rescheduling group deletion"))

  raise e
end