2017-10-13 12:50:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
class MilestoneFinder
|
|
|
|
attr_reader :project
|
|
|
|
|
|
|
|
# The base cache key to use for storing/retrieving milestone IDs.
|
2019-08-31 15:25:25 -04:00
|
|
|
CACHE_KEY = 'github-import/milestone-finder/%{project}/%{iid}'
|
2017-10-13 12:50:36 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-10-13 12:50:36 -04:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-10-13 12:50:36 -04:00
|
|
|
|
|
|
|
def cache_key_for(iid)
|
|
|
|
CACHE_KEY % { project: project.id, iid: iid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|