Class: Snippets::BulkDestroyService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Allowable
Defined in:
app/services/snippets/bulk_destroy_service.rb

Constant Summary collapse

DeleteRepositoryError =
Class.new(StandardError)
SnippetAccessError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(user, snippets) ⇒ BulkDestroyService

Returns a new instance of BulkDestroyService.



12
13
14
15
# File 'app/services/snippets/bulk_destroy_service.rb', line 12

def initialize(user, snippets)
  @current_user = user
  @snippets = snippets
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



7
8
9
# File 'app/services/snippets/bulk_destroy_service.rb', line 7

def current_user
  @current_user
end

#snippetsObject (readonly)

Returns the value of attribute snippets.



7
8
9
# File 'app/services/snippets/bulk_destroy_service.rb', line 7

def snippets
  @snippets
end

Instance Method Details

#execute(skip_authorization: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/snippets/bulk_destroy_service.rb', line 17

def execute(skip_authorization: false)
  return ServiceResponse.success(message: 'No snippets found.') if snippets.empty?

  user_can_delete_snippets! unless skip_authorization
  attempt_delete_repositories!
  snippets.destroy_all # rubocop: disable Cop/DestroyAll

  ServiceResponse.success(message: 'Snippets were deleted.')
rescue SnippetAccessError
  service_response_error("You don't have access to delete these snippets.", 403)
rescue DeleteRepositoryError
  service_response_error('Failed to delete snippet repositories.', 400)
rescue StandardError
  # In case the delete operation fails
  service_response_error('Failed to remove snippets.', 400)
end