2020-03-26 08:07:48 -04:00
# frozen_string_literal: true
module Gitlab
module JiraImport
JIRA_IMPORT_CACHE_TIMEOUT = 10 . seconds . to_i
FAILED_ISSUES_COUNTER_KEY = 'jira-import/failed/%{project_id}/%{collection_type}'
NEXT_ITEMS_START_AT_KEY = 'jira-import/paginator/%{project_id}/%{collection_type}'
2020-04-01 05:07:45 -04:00
JIRA_IMPORT_LABEL = 'jira-import/import-label/%{project_id}'
2020-06-15 14:08:43 -04:00
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/'
2020-03-26 08:07:48 -04:00
ALREADY_IMPORTED_ITEMS_CACHE_KEY = 'jira-importer/already-imported/%{project}/%{collection_type}'
2020-06-08 14:08:27 -04:00
def self . validate_project_settings! ( project , user : nil , configuration_check : true )
2020-06-02 08:08:33 -04:00
if user
raise Projects :: ImportService :: Error , _ ( 'Cannot import because issues are not available in this project.' ) unless project . feature_available? ( :issues , user )
raise Projects :: ImportService :: Error , _ ( 'You do not have permissions to run the import.' ) unless user . can? ( :admin_project , project )
end
2020-06-08 14:08:27 -04:00
return unless configuration_check
2021-06-18 11:10:16 -04:00
jira_integration = project . jira_integration
2020-06-02 08:08:33 -04:00
2021-06-18 11:10:16 -04:00
raise Projects :: ImportService :: Error , _ ( 'Jira integration not configured.' ) unless jira_integration & . active?
raise Projects :: ImportService :: Error , _ ( 'Unable to connect to the Jira instance. Please check your Jira integration configuration.' ) unless jira_integration & . valid_connection?
2020-06-02 08:08:33 -04:00
end
2020-06-15 14:08:43 -04:00
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 }
2020-03-26 08:07:48 -04:00
end
def self . already_imported_cache_key ( collection_type , project_id )
ALREADY_IMPORTED_ITEMS_CACHE_KEY % { collection_type : collection_type , project : project_id }
end
def self . jira_issues_next_page_cache_key ( project_id )
NEXT_ITEMS_START_AT_KEY % { project_id : project_id , collection_type : :issues }
end
def self . failed_issues_counter_cache_key ( project_id )
FAILED_ISSUES_COUNTER_KEY % { project_id : project_id , collection_type : :issues }
end
2020-04-01 05:07:45 -04:00
def self . import_label_cache_key ( project_id )
JIRA_IMPORT_LABEL % { project_id : project_id }
end
2020-03-26 08:07:48 -04:00
def self . increment_issue_failures ( project_id )
2020-04-01 05:07:45 -04:00
cache_class . increment ( self . failed_issues_counter_cache_key ( project_id ) )
2020-03-26 08:07:48 -04:00
end
2020-04-21 11:21:10 -04:00
def self . issue_failures ( project_id )
cache_class . read ( self . failed_issues_counter_cache_key ( project_id ) ) . to_i
end
2020-03-26 08:07:48 -04:00
def self . get_issues_next_start_at ( project_id )
2020-04-01 05:07:45 -04:00
cache_class . read ( self . jira_issues_next_page_cache_key ( project_id ) ) . to_i
2020-03-26 08:07:48 -04:00
end
def self . store_issues_next_started_at ( project_id , value )
cache_key = self . jira_issues_next_page_cache_key ( project_id )
2020-04-01 05:07:45 -04:00
cache_class . write ( cache_key , value )
end
def self . cache_issue_mapping ( issue_id , jira_issue_id , project_id )
2020-06-15 14:08:43 -04:00
cache_key = JiraImport . jira_item_cache_key ( project_id , jira_issue_id , :issues )
2020-04-01 05:07:45 -04:00
cache_class . write ( cache_key , issue_id )
end
def self . get_import_label_id ( project_id )
cache_class . read ( JiraImport . import_label_cache_key ( project_id ) )
end
def self . cache_import_label_id ( project_id , label_id )
cache_class . write ( JiraImport . import_label_cache_key ( project_id ) , label_id )
2020-03-26 08:07:48 -04:00
end
def self . cache_cleanup ( project_id )
2020-04-01 05:07:45 -04:00
cache_class . expire ( self . import_label_cache_key ( project_id ) , JIRA_IMPORT_CACHE_TIMEOUT )
cache_class . expire ( self . failed_issues_counter_cache_key ( project_id ) , JIRA_IMPORT_CACHE_TIMEOUT )
cache_class . expire ( self . jira_issues_next_page_cache_key ( project_id ) , JIRA_IMPORT_CACHE_TIMEOUT )
cache_class . expire ( self . already_imported_cache_key ( :issues , project_id ) , JIRA_IMPORT_CACHE_TIMEOUT )
end
2020-06-15 14:08:43 -04:00
# 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
2020-04-01 05:07:45 -04:00
def self . cache_class
Gitlab :: Cache :: Import :: Caching
2020-03-26 08:07:48 -04:00
end
end
end