diff options
Diffstat (limited to 'app/models/jira_import_state.rb')
-rw-r--r-- | app/models/jira_import_state.rb | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/app/models/jira_import_state.rb b/app/models/jira_import_state.rb index 92147794e88..2d952c552a8 100644 --- a/app/models/jira_import_state.rb +++ b/app/models/jira_import_state.rb @@ -7,6 +7,7 @@ class JiraImportState < ApplicationRecord self.table_name = 'jira_imports' + ERROR_MESSAGE_SIZE = 1000 # 1000 characters limit STATUSES = { initial: 0, scheduled: 1, started: 2, failed: 3, finished: 4 }.freeze belongs_to :project @@ -14,6 +15,7 @@ class JiraImportState < ApplicationRecord belongs_to :label scope :by_jira_project_key, -> (jira_project_key) { where(jira_project_key: jira_project_key) } + scope :with_status, ->(statuses) { where(status: statuses) } validates :project, presence: true validates :jira_project_key, presence: true @@ -25,6 +27,8 @@ class JiraImportState < ApplicationRecord message: _('Cannot have multiple Jira imports running at the same time') } + before_save :ensure_error_message_size + alias_method :scheduled_by, :user state_machine :status, initial: :initial do @@ -47,7 +51,7 @@ class JiraImportState < ApplicationRecord after_transition initial: :scheduled do |state, _| state.run_after_commit do job_id = Gitlab::JiraImport::Stage::StartImportWorker.perform_async(project.id) - state.update(jid: job_id, scheduled_at: Time.now) if job_id + state.update(jid: job_id, scheduled_at: Time.current) if job_id end end @@ -65,6 +69,13 @@ class JiraImportState < ApplicationRecord end end + after_transition any => :failed do |state, transition| + arguments_hash = transition.args.first + error_message = arguments_hash&.dig(:error_message) + + state.update_column(:error_message, error_message) if error_message.present? + end + # Supress warning: # both JiraImportState and its :status machine have defined a different default for "status". # although both have same value but represented in 2 ways: integer(0) and symbol(:initial) @@ -102,4 +113,18 @@ class JiraImportState < ApplicationRecord def self.finished_imports_count finished.sum(:imported_issues_count) end + + def mark_as_failed(error_message) + sanitized_message = Gitlab::UrlSanitizer.sanitize(error_message) + + do_fail(error_message: error_message) + rescue ActiveRecord::ActiveRecordError => e + Gitlab::AppLogger.error("Error setting import status to failed: #{e.message}. Original error: #{sanitized_message}") + end + + private + + def ensure_error_message_size + self.error_message = error_message&.truncate(ERROR_MESSAGE_SIZE) + end end |