summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import/milestone_finder.rb
blob: 208d15dc1448e79af0a9de85537bfd267e2782fb (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
40
# frozen_string_literal: true

module Gitlab
  module GithubImport
    class MilestoneFinder
      attr_reader :project

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

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

      # issuable - An instance of `Gitlab::GithubImport::Representation::Issue`
      #            or `Gitlab::GithubImport::Representation::PullRequest`.
      def id_for(issuable)
        return unless issuable.milestone_number

        Caching.read_integer(cache_key_for(issuable.milestone_number))
      end

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

        Caching.write_multiple(mapping)
      end

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