2014-09-02 18:07:02 +05:30
|
|
|
module Projects
|
|
|
|
class DestroyService < BaseService
|
2015-09-11 14:41:01 +05:30
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
class DestroyError < StandardError; end
|
|
|
|
|
|
|
|
DELETED_FLAG = '+deleted'
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def pending_delete!
|
2016-06-02 11:05:42 +05:30
|
|
|
project.schedule_delete!(current_user.id, params)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def execute
|
|
|
|
return false unless can?(current_user, :remove_project, project)
|
|
|
|
|
|
|
|
project.team.truncate
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
repo_path = project.path_with_namespace
|
|
|
|
wiki_path = repo_path + '.wiki'
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
# 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)
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
Project.transaction do
|
|
|
|
project.destroy!
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
unless remove_registry_tags
|
|
|
|
raise_error('Failed to remove project container registry. Please try again or contact administrator')
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
unless remove_repository(repo_path)
|
|
|
|
raise_error('Failed to remove project repository. Please try again or contact administrator')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
unless remove_repository(wiki_path)
|
|
|
|
raise_error('Failed to remove wiki repository. Please try again or contact administrator')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
log_info("Project \"#{project.path_with_namespace}\" was removed")
|
2015-09-11 14:41:01 +05:30
|
|
|
system_hook_service.execute_hooks_for(project, :destroy)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def remove_repository(path)
|
|
|
|
# Skip repository removal. We use this flag when remove user or group
|
|
|
|
return true if params[:skip_repo] == true
|
|
|
|
|
|
|
|
# There is a possibility project does not have repository or wiki
|
|
|
|
return true unless gitlab_shell.exists?(path + '.git')
|
|
|
|
|
|
|
|
new_path = removal_path(path)
|
|
|
|
|
|
|
|
if gitlab_shell.mv_repository(path, new_path)
|
|
|
|
log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
|
|
|
|
GitlabShellWorker.perform_in(5.minutes, :remove_repository, new_path)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def remove_registry_tags
|
|
|
|
return true unless Gitlab.config.registry.enabled
|
|
|
|
|
|
|
|
project.container_registry_repository.delete_tags
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def raise_error(message)
|
|
|
|
raise DestroyError.new(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build a path for removing repositories
|
|
|
|
# We use `+` because its not allowed by GitLab so user can not create
|
|
|
|
# project with name cookies+119+deleted and capture someone stalled repository
|
|
|
|
#
|
|
|
|
# gitlab/cookies.git -> gitlab/cookies+119+deleted.git
|
|
|
|
#
|
|
|
|
def removal_path(path)
|
|
|
|
"#{path}+#{project.id}#{DELETED_FLAG}"
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
|
|
|
def flush_caches(project, wiki_path)
|
2016-06-02 11:05:42 +05:30
|
|
|
project.repository.before_delete
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
Repository.new(wiki_path, project).before_delete
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|