2018-06-27 07:31:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-13 13:56:05 +00:00
|
|
|
module RepositoryCheck
|
2020-02-19 18:09:10 +00:00
|
|
|
class SingleRepositoryWorker # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2016-10-21 16:13:41 +00:00
|
|
|
include RepositoryCheckQueue
|
2016-04-26 14:56:14 +00:00
|
|
|
|
2016-04-13 13:56:05 +00:00
|
|
|
def perform(project_id)
|
|
|
|
project = Project.find(project_id)
|
2018-05-03 19:15:12 +00:00
|
|
|
healthy = project_healthy?(project)
|
2018-04-24 12:38:08 +00:00
|
|
|
|
2018-05-03 19:15:12 +00:00
|
|
|
update_repository_check_status(project, healthy)
|
2018-04-24 12:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-05-03 19:15:12 +00:00
|
|
|
def update_repository_check_status(project, healthy)
|
2016-04-13 13:56:05 +00:00
|
|
|
project.update_columns(
|
2018-05-03 19:15:12 +00:00
|
|
|
last_repository_check_failed: !healthy,
|
2020-06-24 06:09:01 +00:00
|
|
|
last_repository_check_at: Time.current
|
2016-04-13 13:56:05 +00:00
|
|
|
)
|
|
|
|
end
|
2016-04-26 14:56:14 +00:00
|
|
|
|
2018-05-03 19:15:12 +00:00
|
|
|
def project_healthy?(project)
|
|
|
|
repo_healthy?(project) && wiki_repo_healthy?(project)
|
2018-04-23 10:09:38 +00:00
|
|
|
end
|
|
|
|
|
2018-05-03 19:15:12 +00:00
|
|
|
def repo_healthy?(project)
|
2018-05-07 12:21:44 +00:00
|
|
|
return true unless has_changes?(project)
|
2018-04-23 10:09:38 +00:00
|
|
|
|
|
|
|
git_fsck(project.repository)
|
|
|
|
end
|
|
|
|
|
2018-05-03 19:15:12 +00:00
|
|
|
def wiki_repo_healthy?(project)
|
2018-05-07 12:21:44 +00:00
|
|
|
return true unless has_wiki_changes?(project)
|
2018-04-23 10:09:38 +00:00
|
|
|
|
|
|
|
git_fsck(project.wiki.repository)
|
2016-04-13 13:56:05 +00:00
|
|
|
end
|
2016-04-26 14:56:14 +00:00
|
|
|
|
|
|
|
def git_fsck(repository)
|
2017-12-04 09:31:13 +00:00
|
|
|
return false unless repository.exists?
|
2016-04-26 14:56:14 +00:00
|
|
|
|
2017-12-04 09:31:13 +00:00
|
|
|
repository.raw_repository.fsck
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gitlab::Git::Repository::GitError => e
|
|
|
|
Gitlab::RepositoryCheckLogger.error(e.message)
|
|
|
|
false
|
2016-04-13 13:56:05 +00:00
|
|
|
end
|
2016-06-15 17:11:25 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-05-07 12:21:44 +00:00
|
|
|
def has_changes?(project)
|
2016-06-15 17:11:25 +00:00
|
|
|
Project.with_push.exists?(project.id)
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-05-07 12:21:44 +00:00
|
|
|
|
|
|
|
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
|
2016-04-13 13:56:05 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-12 21:09:39 +00:00
|
|
|
|
|
|
|
RepositoryCheck::SingleRepositoryWorker.prepend_if_ee('::EE::RepositoryCheck::SingleRepositoryWorker')
|