Welcome to mirror list, hosted at ThFree Co, Russian Federation.

label_finder.rb « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2479a8f565844be7cbf06cd327d33a9626b5902 (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
# frozen_string_literal: true

module Gitlab
  module GithubImport
    class LabelFinder
      attr_reader :project

      # The base cache key to use for storing/retrieving label IDs.
      CACHE_KEY = 'github-import/label-finder/%{project}/%{name}'.freeze

      # project - An instance of `Project`.
      def initialize(project)
        @project = project
      end

      # Returns the label ID for the given name.
      def id_for(name)
        Caching.read_integer(cache_key_for(name))
      end

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

        Caching.write_multiple(mapping)
      end
      # rubocop: enable CodeReuse/ActiveRecord

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