Commit cb5840cd authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '214808-store-users-mapping-prep1' into 'master'

Prepare Jira import for caching users mapping

See merge request gitlab-org/gitlab!34067
parents 725b7109 486c46d4
......@@ -113,15 +113,18 @@ module Gitlab
end
end
# Sets multiple keys to a given value.
# Sets multiple keys to given values.
#
# mapping - A Hash mapping the cache keys to their values.
# key_prefix - prefix inserted before each key
# timeout - The time after which the cache key should expire.
def self.write_multiple(mapping, timeout: TIMEOUT)
def self.write_multiple(mapping, key_prefix: nil, timeout: TIMEOUT)
Redis::Cache.with do |redis|
redis.multi do |multi|
redis.pipelined do |multi|
mapping.each do |raw_key, value|
multi.set(cache_key_for(raw_key), value, ex: timeout)
key = cache_key_for("#{key_prefix}#{raw_key}")
multi.set(key, value, ex: timeout)
end
end
end
......
......@@ -7,7 +7,8 @@ module Gitlab
FAILED_ISSUES_COUNTER_KEY = 'jira-import/failed/%{project_id}/%{collection_type}'
NEXT_ITEMS_START_AT_KEY = 'jira-import/paginator/%{project_id}/%{collection_type}'
JIRA_IMPORT_LABEL = 'jira-import/import-label/%{project_id}'
ITEMS_MAPPER_CACHE_KEY = 'jira-import/items-mapper/%{project_id}/%{collection_type}/%{jira_isssue_id}'
ITEMS_MAPPER_CACHE_KEY = 'jira-import/items-mapper/%{project_id}/%{collection_type}/%{jira_item_id}'
USERS_MAPPER_KEY_PREFIX = 'jira-import/items-mapper/%{project_id}/users/'
ALREADY_IMPORTED_ITEMS_CACHE_KEY = 'jira-importer/already-imported/%{project}/%{collection_type}'
def self.validate_project_settings!(project, user: nil, configuration_check: true)
......@@ -24,8 +25,12 @@ module Gitlab
raise Projects::ImportService::Error, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless jira_service&.valid_connection?
end
def self.jira_issue_cache_key(project_id, jira_issue_id)
ITEMS_MAPPER_CACHE_KEY % { project_id: project_id, collection_type: :issues, jira_isssue_id: jira_issue_id }
def self.jira_item_cache_key(project_id, jira_item_id, collection_type)
ITEMS_MAPPER_CACHE_KEY % { project_id: project_id, collection_type: collection_type, jira_item_id: jira_item_id }
end
def self.jira_user_key_prefix(project_id)
USERS_MAPPER_KEY_PREFIX % { project_id: project_id }
end
def self.already_imported_cache_key(collection_type, project_id)
......@@ -62,7 +67,7 @@ module Gitlab
end
def self.cache_issue_mapping(issue_id, jira_issue_id, project_id)
cache_key = JiraImport.jira_issue_cache_key(project_id, jira_issue_id)
cache_key = JiraImport.jira_item_cache_key(project_id, jira_issue_id, :issues)
cache_class.write(cache_key, issue_id)
end
......@@ -81,6 +86,19 @@ module Gitlab
cache_class.expire(self.already_imported_cache_key(:issues, project_id), JIRA_IMPORT_CACHE_TIMEOUT)
end
# Caches the mapping of jira_account_id -> gitlab user id
# project_id - id of a project
# mapping - hash in format of jira_account_id -> gitlab user id
def self.cache_users_mapping(project_id, mapping)
cache_class.write_multiple(mapping, key_prefix: jira_user_key_prefix(project_id))
end
def self.get_user_mapping(project_id, jira_account_id)
cache_key = JiraImport.jira_item_cache_key(project_id, jira_account_id, :users)
cache_class.read(cache_key)&.to_i
end
def self.cache_class
Gitlab::Cache::Import::Caching
end
......
......@@ -89,7 +89,7 @@ describe Gitlab::Cache::Import::Caching, :clean_gitlab_redis_cache do
end
describe '.write_multiple' do
it 'sets multiple keys' do
it 'sets multiple keys when key_prefix not set' do
mapping = { 'foo' => 10, 'bar' => 20 }
described_class.write_multiple(mapping)
......@@ -101,6 +101,19 @@ describe Gitlab::Cache::Import::Caching, :clean_gitlab_redis_cache do
expect(found).to eq(value.to_s)
end
end
it 'sets multiple keys with correct prefix' do
mapping = { 'foo' => 10, 'bar' => 20 }
described_class.write_multiple(mapping, key_prefix: 'pref/')
mapping.each do |key, value|
full_key = described_class.cache_key_for("pref/#{key}")
found = Gitlab::Redis::Cache.with { |r| r.get(full_key) }
expect(found).to eq(value.to_s)
end
end
end
describe '.expire' do
......
......@@ -107,7 +107,7 @@ describe Gitlab::JiraImport do
describe '.jira_issue_cache_key' do
it 'returns cache key for Jira issue imported to given project' do
expect(described_class.jira_issue_cache_key(project_id, 'DEMO-123')).to eq("jira-import/items-mapper/#{project_id}/issues/DEMO-123")
expect(described_class.jira_item_cache_key(project_id, 'DEMO-123', :issues)).to eq("jira-import/items-mapper/#{project_id}/issues/DEMO-123")
end
end
......@@ -144,6 +144,29 @@ describe Gitlab::JiraImport do
end
end
describe '.cache_users_mapping', :clean_gitlab_redis_cache do
let(:data) { { 'user1' => '456', 'user234' => '23' } }
it 'stores the data correctly' do
described_class.cache_users_mapping(project_id, data)
expect(Gitlab::Cache::Import::Caching.read("jira-import/items-mapper/#{project_id}/users/user1")).to eq('456')
expect(Gitlab::Cache::Import::Caching.read("jira-import/items-mapper/#{project_id}/users/user234")).to eq('23')
end
end
describe '.get_user_mapping', :clean_gitlab_redis_cache do
it 'reads the data correctly' do
Gitlab::Cache::Import::Caching.write("jira-import/items-mapper/#{project_id}/users/user-123", '456')
expect(described_class.get_user_mapping(project_id, 'user-123')).to eq(456)
end
it 'returns nil if value not found' do
expect(described_class.get_user_mapping(project_id, 'user-123')).to be_nil
end
end
describe '.store_issues_next_started_at', :clean_gitlab_redis_cache do
it 'stores nil value' do
described_class.store_issues_next_started_at(project_id, nil)
......
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