2020-04-06 02:09:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class JiraImportState < ApplicationRecord
|
|
|
|
include AfterCommitQueue
|
2020-04-06 08:10:44 -04:00
|
|
|
include ImportState::SidekiqJobTracker
|
2020-04-30 02:10:03 -04:00
|
|
|
include UsageStatistics
|
2020-04-06 02:09:19 -04:00
|
|
|
|
|
|
|
self.table_name = 'jira_imports'
|
|
|
|
|
2020-05-29 14:08:26 -04:00
|
|
|
ERROR_MESSAGE_SIZE = 1000 # 1000 characters limit
|
2020-04-06 02:09:19 -04:00
|
|
|
STATUSES = { initial: 0, scheduled: 1, started: 2, failed: 3, finished: 4 }.freeze
|
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :label
|
|
|
|
|
2020-04-14 14:09:54 -04:00
|
|
|
scope :by_jira_project_key, -> (jira_project_key) { where(jira_project_key: jira_project_key) }
|
2020-05-29 14:08:26 -04:00
|
|
|
scope :with_status, ->(statuses) { where(status: statuses) }
|
2020-04-14 14:09:54 -04:00
|
|
|
|
2020-04-06 02:09:19 -04:00
|
|
|
validates :project, presence: true
|
|
|
|
validates :jira_project_key, presence: true
|
|
|
|
validates :jira_project_name, presence: true
|
|
|
|
validates :jira_project_xid, presence: true
|
|
|
|
|
|
|
|
validates :project, uniqueness: {
|
|
|
|
conditions: -> { where.not(status: STATUSES.values_at(:failed, :finished)) },
|
|
|
|
message: _('Cannot have multiple Jira imports running at the same time')
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:08:26 -04:00
|
|
|
before_save :ensure_error_message_size
|
|
|
|
|
2020-04-09 08:09:24 -04:00
|
|
|
alias_method :scheduled_by, :user
|
|
|
|
|
2020-04-06 02:09:19 -04:00
|
|
|
state_machine :status, initial: :initial do
|
|
|
|
event :schedule do
|
|
|
|
transition initial: :scheduled
|
|
|
|
end
|
|
|
|
|
|
|
|
event :start do
|
|
|
|
transition scheduled: :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
|
|
|
end
|
|
|
|
|
|
|
|
event :do_fail do
|
|
|
|
transition [:initial, :scheduled, :started] => :failed
|
|
|
|
end
|
|
|
|
|
|
|
|
after_transition initial: :scheduled do |state, _|
|
|
|
|
state.run_after_commit do
|
|
|
|
job_id = Gitlab::JiraImport::Stage::StartImportWorker.perform_async(project.id)
|
2020-05-22 05:08:09 -04:00
|
|
|
state.update(jid: job_id, scheduled_at: Time.current) if job_id
|
2020-04-06 02:09:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 08:09:24 -04:00
|
|
|
before_transition any => :finished do |state, _|
|
|
|
|
InternalId.flush_records!(project: state.project)
|
|
|
|
state.project.update_project_counter_caches
|
2020-04-21 11:21:10 -04:00
|
|
|
state.store_issue_counts
|
2020-04-09 08:09:24 -04:00
|
|
|
end
|
|
|
|
|
2020-04-06 02:09:19 -04:00
|
|
|
after_transition any => :finished do |state, _|
|
|
|
|
if state.jid.present?
|
|
|
|
Gitlab::SidekiqStatus.unset(state.jid)
|
|
|
|
|
|
|
|
state.update_column(:jid, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-29 14:08:26 -04:00
|
|
|
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
|
|
|
|
|
2020-04-06 02:09:19 -04:00
|
|
|
# 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)
|
|
|
|
def owner_class_attribute_default
|
|
|
|
'initial'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
enum status: STATUSES
|
|
|
|
|
|
|
|
def in_progress?
|
|
|
|
scheduled? || started?
|
|
|
|
end
|
2020-04-09 08:09:24 -04:00
|
|
|
|
|
|
|
def non_initial?
|
|
|
|
!initial?
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
def store_issue_counts
|
|
|
|
import_label_id = Gitlab::JiraImport.get_import_label_id(project.id)
|
|
|
|
|
|
|
|
failed_to_import_count = Gitlab::JiraImport.issue_failures(project.id)
|
|
|
|
successfully_imported_count = project.issues.with_label_ids(import_label_id).count
|
|
|
|
total_issue_count = successfully_imported_count + failed_to_import_count
|
|
|
|
|
|
|
|
update(
|
|
|
|
{
|
|
|
|
failed_to_import_count: failed_to_import_count,
|
|
|
|
imported_issues_count: successfully_imported_count,
|
|
|
|
total_issue_count: total_issue_count
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2020-04-30 02:10:03 -04:00
|
|
|
|
|
|
|
def self.finished_imports_count
|
|
|
|
finished.sum(:imported_issues_count)
|
|
|
|
end
|
2020-05-29 14:08:26 -04:00
|
|
|
|
|
|
|
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
|
2020-04-06 02:09:19 -04:00
|
|
|
end
|