diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-07-18 18:09:14 +0300 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-07-26 14:47:50 +0300 |
commit | b5bdc55d239f3e19f8fe1e59b118da05ac81a0dd (patch) | |
tree | 507f7b46386d518cdb7b77fa4048d7f4d77eec37 /app/services/projects | |
parent | 0aa8249e484ca97cfc28c7301d69077919032c08 (diff) |
Move exception handling to execute
Diffstat (limited to 'app/services/projects')
-rw-r--r-- | app/services/projects/destroy_service.rb | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/app/services/projects/destroy_service.rb b/app/services/projects/destroy_service.rb index 7e38aacc91a..f6e8b6655f2 100644 --- a/app/services/projects/destroy_service.rb +++ b/app/services/projects/destroy_service.rb @@ -15,29 +15,48 @@ module Projects def execute return false unless can?(current_user, :remove_project, project) - repo_path = project.path_with_namespace - wiki_path = repo_path + '.wiki' - # Flush the cache for both repositories. This has to be done _before_ # removing the physical repositories as some expiration code depends on # Git data (e.g. a list of branch names). - flush_caches(project, wiki_path) + flush_caches(project) Projects::UnlinkForkService.new(project, current_user).execute - attempt_destroy_transaction(project, repo_path, wiki_path) + attempt_destroy_transaction(project) system_hook_service.execute_hooks_for(project, :destroy) - log_info("Project \"#{project.full_path}\" was removed") + true - rescue Projects::DestroyService::DestroyError => error - Rails.logger.error("Deletion failed on #{project.full_path} with the following message: #{error.message}") + rescue => error + attempt_rollback(project, error.message) false + rescue Exception => error # rubocop:disable Lint/RescueException + # Project.transaction can raise Exception + attempt_rollback(project, error.message) + raise end private + def repo_path + project.path_with_namespace + end + + def wiki_path + repo_path + '.wiki' + end + + def trash_repositories! + unless remove_repository(repo_path) + raise_error('Failed to remove project repository. Please try again or contact administrator.') + end + + unless remove_repository(wiki_path) + raise_error('Failed to remove wiki repository. Please try again or contact administrator.') + end + end + def remove_repository(path) # Skip repository removal. We use this flag when remove user or group return true if params[:skip_repo] == true @@ -59,26 +78,24 @@ module Projects end end - def attempt_destroy_transaction(project, repo_path, wiki_path) + def attempt_rollback(project, message) + return unless project + + project.update_attributes(delete_error: message, pending_delete: false) + log_error("Deletion failed on #{project.full_path} with the following message: #{message}") + end + + def attempt_destroy_transaction(project) Project.transaction do unless remove_legacy_registry_tags raise_error('Failed to remove some tags in project container registry. Please try again or contact administrator.') end - unless remove_repository(repo_path) - raise_error('Failed to remove project repository. Please try again or contact administrator.') - end - - unless remove_repository(wiki_path) - raise_error('Failed to remove wiki repository. Please try again or contact administrator.') - end + trash_repositories! project.team.truncate project.destroy! end - rescue Exception => error # rubocop:disable Lint/RescueException - project.update_attributes(delete_error: error.message, pending_delete: false) - raise end ## @@ -107,7 +124,7 @@ module Projects "#{path}+#{project.id}#{DELETED_FLAG}" end - def flush_caches(project, wiki_path) + def flush_caches(project) project.repository.before_delete Repository.new(wiki_path, project).before_delete |