Use only one cache hash with with a hash initializer by default

parent 275f00ee
...@@ -29,17 +29,14 @@ module Github ...@@ -29,17 +29,14 @@ module Github
self.reset_callbacks :validate self.reset_callbacks :validate
end end
attr_reader :project, :repository, :repo, :options, :errors, attr_reader :project, :repository, :repo, :options, :errors, :cached
:cached_label_ids, :cached_gitlab_users, :cached_user_ids
def initialize(project, options) def initialize(project, options)
@project = project @project = project
@repository = project.repository @repository = project.repository
@repo = project.import_source @repo = project.import_source
@options = options @options = options
@cached_label_ids = {} @cached = Hash.new { |hash, key| hash[key] = Hash.new }
@cached_user_ids = {}
@cached_gitlab_users = {}
@errors = [] @errors = []
end end
...@@ -107,7 +104,7 @@ module Github ...@@ -107,7 +104,7 @@ module Github
# Cache labels # Cache labels
project.labels.select(:id, :title).find_each do |label| project.labels.select(:id, :title).find_each do |label|
@cached_label_ids[label.title] = label.id cached[:label_ids][label.title] = label.id
end end
end end
...@@ -325,7 +322,7 @@ module Github ...@@ -325,7 +322,7 @@ module Github
end end
def label_ids(issuable) def label_ids(issuable)
issuable.map { |attrs| cached_label_ids[attrs.fetch('name')] }.compact issuable.map { |attrs| cached[:label_ids][attrs.fetch('name')] }.compact
end end
def milestone_id(milestone) def milestone_id(milestone)
...@@ -336,12 +333,12 @@ module Github ...@@ -336,12 +333,12 @@ module Github
def user_id(user, fallback_id = nil) def user_id(user, fallback_id = nil)
return unless user.present? return unless user.present?
return cached_user_ids[user.id] if cached_user_ids.key?(user.id) return cached[:user_ids][user.id] if cached[:user_ids].key?(user.id)
gitlab_user_id = user_id_by_external_uid(user.id) || user_id_by_email(user.email) gitlab_user_id = user_id_by_external_uid(user.id) || user_id_by_email(user.email)
cached_gitlab_users[user.id] = gitlab_user_id.present? cached[:gitlab_user_ids][user.id] = gitlab_user_id.present?
cached_user_ids[user.id] = gitlab_user_id || fallback_id cached[:user_ids][user.id] = gitlab_user_id || fallback_id
end end
def user_id_by_email(email) def user_id_by_email(email)
...@@ -362,7 +359,7 @@ module Github ...@@ -362,7 +359,7 @@ module Github
end end
def format_description(body, author) def format_description(body, author)
return body if cached_gitlab_users[author.id] return body if cached[:gitlab_user_ids][author.id]
"*Created by: #{author.username}*\n\n#{body}" "*Created by: #{author.username}*\n\n#{body}"
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment