Class: Gitlab::GithubImport::LabelFinder

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

Constant Summary collapse

CACHE_KEY =

The base cache key to use for storing/retrieving label IDs.

'github-import/label-finder/%{project}/%{name}'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ LabelFinder

project - An instance of ‘Project`.



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

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/gitlab/github_import/label_finder.rb', line 6

def project
  @project
end

Instance Method Details

#build_cacheObject

rubocop: disable CodeReuse/ActiveRecord



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

def build_cache
  mapping = @project
    .labels
    .pluck(:id, :name)
    .each_with_object({}) do |(id, name), hash|
      hash[cache_key_for(name)] = id
    end

  Gitlab::Cache::Import::Caching.write_multiple(mapping)
end

#cache_key_for(name) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord



34
35
36
# File 'lib/gitlab/github_import/label_finder.rb', line 34

def cache_key_for(name)
  CACHE_KEY % { project: project.id, name: name }
end

#id_for(name) ⇒ Object

Returns the label ID for the given name.



17
18
19
# File 'lib/gitlab/github_import/label_finder.rb', line 17

def id_for(name)
  Gitlab::Cache::Import::Caching.read_integer(cache_key_for(name))
end