summaryrefslogtreecommitdiff
path: root/app/workers/repository_check/single_repository_worker.rb
blob: f44e5693b259e03324bca6dfa446d2234db0e713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

module RepositoryCheck
  class SingleRepositoryWorker
    include ApplicationWorker
    include RepositoryCheckQueue

    def perform(project_id)
      project = Project.find(project_id)
      healthy = project_healthy?(project)

      update_repository_check_status(project, healthy)
    end

    private

    def update_repository_check_status(project, healthy)
      project.update_columns(
        last_repository_check_failed: !healthy,
        last_repository_check_at: Time.now
      )
    end

    def project_healthy?(project)
      repo_healthy?(project) && wiki_repo_healthy?(project)
    end

    def repo_healthy?(project)
      return true unless has_changes?(project)

      git_fsck(project.repository)
    end

    def wiki_repo_healthy?(project)
      return true unless has_wiki_changes?(project)

      git_fsck(project.wiki.repository)
    end

    def git_fsck(repository)
      return false unless repository.exists?

      repository.raw_repository.fsck

      true
    rescue Gitlab::Git::Repository::GitError => e
      Gitlab::RepositoryCheckLogger.error(e.message)
      false
    end

    def has_changes?(project)
      Project.with_push.exists?(project.id)
    end

    def has_wiki_changes?(project)
      return false unless project.wiki_enabled?

      # Historically some projects never had their wiki repos initialized;
      # this happens on project creation now. Let's initialize an empty repo
      # if it is not already there.
      return false unless project.create_wiki

      has_changes?(project)
    end
  end
end