debian-mirror-gitlab/lib/gitlab/github_import/issuable_finder.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.8 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
# IssuableFinder can be used for caching and retrieving database IDs for
# issuable objects such as issues and pull requests. By caching these IDs we
# remove the need for running a lot of database queries when importing
# GitHub projects.
class IssuableFinder
attr_reader :project, :object
# The base cache key to use for storing/retrieving issuable IDs.
2019-12-04 20:38:33 +05:30
CACHE_KEY = 'github-import/issuable-finder/%{project}/%{type}/%{iid}'
2018-03-17 18:26:18 +05:30
# project - An instance of `Project`.
# object - The object to look up or set a database ID for.
def initialize(project, object)
@project = project
@object = object
end
# Returns the database ID for the object.
#
# This method will return `nil` if no ID could be found.
def database_id
2021-11-11 11:23:49 +05:30
val = Gitlab::Cache::Import::Caching.read(cache_key, timeout: timeout)
2018-03-17 18:26:18 +05:30
val.to_i if val.present?
end
# Associates the given database ID with the current object.
#
# database_id - The ID of the corresponding database row.
def cache_database_id(database_id)
2021-11-11 11:23:49 +05:30
Gitlab::Cache::Import::Caching.write(cache_key, database_id, timeout: timeout)
2018-03-17 18:26:18 +05:30
end
private
def cache_key
CACHE_KEY % {
project: project.id,
type: cache_key_type,
iid: cache_key_iid
}
end
# Returns the identifier to use for cache keys.
#
# For issues and pull requests this will be "Issue" or "MergeRequest"
# respectively. For diff notes this will return "MergeRequest", for
# regular notes it will either return "Issue" or "MergeRequest" depending
# on what type of object the note belongs to.
def cache_key_type
if object.respond_to?(:issuable_type)
object.issuable_type
elsif object.respond_to?(:noteable_type)
object.noteable_type
else
raise(
TypeError,
"Instances of #{object.class} are not supported"
)
end
end
def cache_key_iid
if object.respond_to?(:noteable_id)
object.noteable_id
elsif object.respond_to?(:iid)
object.iid
2022-08-27 11:52:29 +05:30
elsif object.respond_to?(:issuable_id)
object.issuable_id
2018-03-17 18:26:18 +05:30
else
raise(
TypeError,
"Instances of #{object.class} are not supported"
)
end
end
2021-11-11 11:23:49 +05:30
def timeout
2022-11-25 23:54:43 +05:30
if import_settings.enabled?(:single_endpoint_notes_import)
2021-11-11 11:23:49 +05:30
Gitlab::Cache::Import::Caching::LONGER_TIMEOUT
else
Gitlab::Cache::Import::Caching::TIMEOUT
end
end
2022-11-25 23:54:43 +05:30
def import_settings
::Gitlab::GithubImport::Settings.new(project)
end
2018-03-17 18:26:18 +05:30
end
end
end