Class: Gitlab::LanguageDetection

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/language_detection.rb

Constant Summary collapse

MAX_LANGUAGES =
5

Instance Method Summary collapse

Constructor Details

#initialize(repository, repository_languages) ⇒ LanguageDetection

Returns a new instance of LanguageDetection.


7
8
9
10
# File 'lib/gitlab/language_detection.rb', line 7

def initialize(repository, repository_languages)
  @repository = repository
  @repository_languages = repository_languages
end

Instance Method Details

#deletionsObject

Returns the ids of the programming languages that do not occur in the detection as current repository languages


47
48
49
50
51
52
53
# File 'lib/gitlab/language_detection.rb', line 47

def deletions
  @repository_languages.map do |repo_lang|
    next if detection.key?(repo_lang.name)

    repo_lang.programming_language_id
  end.compact
end

#insertions(programming_languages) ⇒ Object

Newly detected languages, returned in a structure accepted by ApplicationRecord.legacy_bulk_insert


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/language_detection.rb', line 22

def insertions(programming_languages)
  lang_to_id = programming_languages.to_h { |p| [p.name, p.id] }

  (languages - previous_language_names).map do |new_lang|
    {
      project_id: @repository.project.id,
      share: detection[new_lang][:value],
      programming_language_id: lang_to_id[new_lang]
    }
  end
end

#language_color(name) ⇒ Object


16
17
18
# File 'lib/gitlab/language_detection.rb', line 16

def language_color(name)
  detection.dig(name, :color)
end

#languagesObject


12
13
14
# File 'lib/gitlab/language_detection.rb', line 12

def languages
  detection.keys
end

#updatesObject

updates analyses which records only require updating of their share


35
36
37
38
39
40
41
42
43
# File 'lib/gitlab/language_detection.rb', line 35

def updates
  to_update = @repository_languages.select do |lang|
    detection.key?(lang.name) && detection[lang.name][:value] != lang.share
  end

  to_update.map do |lang|
    { programming_language_id: lang.programming_language_id, share: detection[lang.name][:value] }
  end
end