destroy_service.rb 2.5 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 10 11 12 13 14
    def pending_delete!
      project.update_attribute(:pending_delete, true)

      ProjectDestroyWorker.perform_in(1.minute, project.id, current_user.id, params)
    end

15
    def execute
D
Dmitriy Zaporozhets 已提交
16 17
      return false unless can?(current_user, :remove_project, project)

18
      project.team.truncate
D
Dmitriy Zaporozhets 已提交
19

20 21 22
      repo_path = project.path_with_namespace
      wiki_path = repo_path + '.wiki'

23 24 25 26 27
      # 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)

28 29
      Project.transaction do
        project.destroy!
D
Dmitriy Zaporozhets 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43
        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

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

45
    private
D
Dmitriy Zaporozhets 已提交
46

47
    def remove_repository(path)
48 49 50 51 52
      # 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')
53 54 55 56 57

      new_path = removal_path(path)

      if gitlab_shell.mv_repository(path, new_path)
        log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
58
        GitlabShellWorker.perform_in(5.minutes, :remove_repository, new_path)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      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 已提交
76
    end
77 78 79 80 81 82 83 84

    def flush_caches(project, wiki_path)
      project.repository.expire_all_caches! if project.repository.exists?

      wiki_repo = Repository.new(wiki_path, project)

      wiki_repo.expire_all_caches! if wiki_repo.exists?
    end
D
Dmitriy Zaporozhets 已提交
85 86
  end
end