Class: Gitlab::GithubImport::MilestoneFinder
- Inherits:
-
Object
- Object
- Gitlab::GithubImport::MilestoneFinder
- Defined in:
- lib/gitlab/github_import/milestone_finder.rb
Constant Summary collapse
- CACHE_KEY =
The base cache key to use for storing/retrieving milestone IDs.
'github-import/milestone-finder/%{project}/%{iid}'- CACHE_OBJECT_NOT_FOUND =
-1
Instance Attribute Summary collapse
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
-
#build_cache ⇒ Object
rubocop: disable CodeReuse/ActiveRecord.
-
#cache_key_for(iid) ⇒ Object
rubocop: enable CodeReuse/ActiveRecord.
-
#id_for(issuable) ⇒ Object
issuable - An instance of
Gitlab::GithubImport::Representation::IssueorGitlab::GithubImport::Representation::PullRequest. -
#initialize(project) ⇒ MilestoneFinder
constructor
project - An instance of
Project.
Constructor Details
#initialize(project) ⇒ MilestoneFinder
project - An instance of Project
13 14 15 |
# File 'lib/gitlab/github_import/milestone_finder.rb', line 13 def initialize(project) @project = project end |
Instance Attribute Details
#project ⇒ Object (readonly)
Returns the value of attribute project.
6 7 8 |
# File 'lib/gitlab/github_import/milestone_finder.rb', line 6 def project @project end |
Instance Method Details
#build_cache ⇒ Object
rubocop: disable CodeReuse/ActiveRecord
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gitlab/github_import/milestone_finder.rb', line 37 def build_cache mapping = @project .milestones .pluck(:id, :iid) .each_with_object({}) do |(id, iid), hash| hash[cache_key_for(iid)] = id end Gitlab::Cache::Import::Caching.write_multiple(mapping) end |
#cache_key_for(iid) ⇒ Object
rubocop: enable CodeReuse/ActiveRecord
49 50 51 |
# File 'lib/gitlab/github_import/milestone_finder.rb', line 49 def cache_key_for(iid) format(CACHE_KEY, project: project.id, iid: iid) end |
#id_for(issuable) ⇒ Object
issuable - An instance of Gitlab::GithubImport::Representation::Issue
or `Gitlab::GithubImport::Representation::PullRequest`.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gitlab/github_import/milestone_finder.rb', line 19 def id_for(issuable) return unless issuable.milestone_number milestone_iid = issuable.milestone_number cache_key = cache_key_for(milestone_iid) val = Gitlab::Cache::Import::Caching.read_integer(cache_key) return if val == CACHE_OBJECT_NOT_FOUND return val if val.present? object_id = project.milestones.by_iid(milestone_iid).pick(:id) || CACHE_OBJECT_NOT_FOUND Gitlab::Cache::Import::Caching.write(cache_key, object_id) object_id == CACHE_OBJECT_NOT_FOUND ? nil : object_id end |