debian-mirror-gitlab/app/services/projects/detect_repository_languages_service.rb

69 lines
2.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
module Projects
class DetectRepositoryLanguagesService < BaseService
2019-07-07 11:18:12 +05:30
attr_reader :programming_languages
2018-11-18 11:00:15 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def execute
repository_languages = project.repository_languages
detection = Gitlab::LanguageDetection.new(repository, repository_languages)
matching_programming_languages = ensure_programming_languages(detection)
RepositoryLanguage.transaction do
2020-03-13 15:44:24 +05:30
RepositoryLanguage.where(project_id: project.id, programming_language_id: detection.deletions).delete_all
2018-11-18 11:00:15 +05:30
detection.updates.each do |update|
RepositoryLanguage
.where(project_id: project.id)
.where(programming_language_id: update[:programming_language_id])
.update_all(share: update[:share])
end
2021-12-11 22:18:48 +05:30
ApplicationRecord.legacy_bulk_insert( # rubocop:disable Gitlab/BulkInsert
2018-11-18 11:00:15 +05:30
RepositoryLanguage.table_name,
detection.insertions(matching_programming_languages)
)
2019-07-07 11:18:12 +05:30
set_detected_repository_languages
2018-11-18 11:00:15 +05:30
end
2019-07-31 22:56:46 +05:30
project.repository_languages.reset
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def ensure_programming_languages(detection)
existing_languages = ProgrammingLanguage.where(name: detection.languages)
return existing_languages if detection.languages.size == existing_languages.size
missing_languages = detection.languages - existing_languages.map(&:name)
created_languages = missing_languages.map do |name|
create_language(name, detection.language_color(name))
end
existing_languages + created_languages
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def create_language(name, color)
ProgrammingLanguage.transaction do
ProgrammingLanguage.where(name: name).first_or_create(color: color)
end
rescue ActiveRecord::RecordNotUnique
retry
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-07-07 11:18:12 +05:30
def set_detected_repository_languages
return if project.detected_repository_languages?
project.update_column(:detected_repository_languages, true)
end
2018-11-18 11:00:15 +05:30
end
end