destroy_service.rb 1.8 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1
module Projects
2
  class DestroyService < BaseService
3 4 5 6 7 8
    include Gitlab::ShellAdapter

    class DestroyError < StandardError; end

    DELETED_FLAG = '+deleted'

9
    def execute
D
Dmitriy Zaporozhets 已提交
10 11
      return false unless can?(current_user, :remove_project, project)

12
      project.team.truncate
D
Dmitriy Zaporozhets 已提交
13 14
      project.repository.expire_cache unless project.empty_repo?

15 16 17 18 19
      repo_path = project.path_with_namespace
      wiki_path = repo_path + '.wiki'

      Project.transaction do
        project.destroy!
D
Dmitriy Zaporozhets 已提交
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34
        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

      project.satellite.destroy
      log_info("Project \"#{project.name}\" was removed")
      system_hook_service.execute_hooks_for(project, :destroy)
      true
    end
D
Dmitriy Zaporozhets 已提交
35

36
    private
D
Dmitriy Zaporozhets 已提交
37

38 39 40
    def remove_repository(path)
      unless gitlab_shell.exists?(path + '.git')
        return true
D
Dmitriy Zaporozhets 已提交
41
      end
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

      new_path = removal_path(path)

      if gitlab_shell.mv_repository(path, new_path)
        log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
        GitlabShellWorker.perform_in(30.seconds, :remove_repository, new_path)
      else
        false
      end
    end

    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}"
D
Dmitriy Zaporozhets 已提交
65 66 67
    end
  end
end