Class: Snippets::BulkDestroyService

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

Constant Summary collapse

NO_ACCESS_ERROR =
{
  reason: :no_access_error,
  message: "You don't have access to delete these snippets."
}.freeze
SNIPPET_REPOSITORIES_DELETE_ERROR =
{ reason: :snippet_repositories_delete_error,
message: 'Failed to delete snippet repositories.' }.freeze
SNIPPETS_DELETE_ERROR =
{
  reason: :snippet_delete_error,
  message: 'Failed to remove snippets.'
}.freeze
DeleteRepositoryError =
Class.new(StandardError)
SnippetAccessError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

#initialize(user, snippets) ⇒ BulkDestroyService

Returns a new instance of BulkDestroyService.



25
26
27
28
# File 'app/services/snippets/bulk_destroy_service.rb', line 25

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

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



20
21
22
# File 'app/services/snippets/bulk_destroy_service.rb', line 20

def current_user
  @current_user
end

#snippetsObject (readonly)

Returns the value of attribute snippets.



20
21
22
# File 'app/services/snippets/bulk_destroy_service.rb', line 20

def snippets
  @snippets
end

Instance Method Details

#execute(skip_authorization: false) ⇒ Object



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
# File 'app/services/snippets/bulk_destroy_service.rb', line 30

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
  ServiceResponse.error(
    reason: NO_ACCESS_ERROR[:reason],
    message: NO_ACCESS_ERROR[:message]
  )
rescue DeleteRepositoryError
  ServiceResponse.error(
    reason: SNIPPET_REPOSITORIES_DELETE_ERROR[:reason],
    message: SNIPPET_REPOSITORIES_DELETE_ERROR[:message]
  )
rescue StandardError
  # In case the delete operation fails
  ServiceResponse.error(
    reason: SNIPPETS_DELETE_ERROR[:reason],
    message: SNIPPETS_DELETE_ERROR[:message]
  )
end