Commit ff421f1e authored by Alexandru Croitor's avatar Alexandru Croitor Committed by Jarka Košanová

Store statistics on jira imported issues counts

Store counts of successfully imported issues, failed to import issues
and total issues count to be imported.
parent 090f4d1f
......@@ -116,6 +116,7 @@ module Issuable
# rubocop:enable GitlabSecurity/SqlInjection
scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) }
scope :with_label_ids, ->(label_ids) { joins(:label_links).where(label_links: { label_id: label_ids }) }
scope :any_label, -> { joins(:label_links).group(:id) }
scope :join_project, -> { joins(:project) }
scope :inc_notes_with_associations, -> { includes(notes: [:project, :author, :award_emoji]) }
......
......@@ -53,6 +53,7 @@ class JiraImportState < ApplicationRecord
before_transition any => :finished do |state, _|
InternalId.flush_records!(project: state.project)
state.project.update_project_counter_caches
state.store_issue_counts
end
after_transition any => :finished do |state, _|
......@@ -80,4 +81,20 @@ class JiraImportState < ApplicationRecord
def non_initial?
!initial?
end
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
end
......@@ -10,7 +10,7 @@ module Gitlab
def import(project)
JiraImport.cache_cleanup(project.id)
project.latest_jira_import&.finish!
project.latest_jira_import.finish!
end
end
end
......
---
title: Track statistics per project for jira imported issues
merge_request: 29406
author:
type: changed
......@@ -34,6 +34,10 @@ module Gitlab
cache_class.increment(self.failed_issues_counter_cache_key(project_id))
end
def self.issue_failures(project_id)
cache_class.read(self.failed_issues_counter_cache_key(project_id)).to_i
end
def self.get_issues_next_start_at(project_id)
cache_class.read(self.jira_issues_next_page_cache_key(project_id)).to_i
end
......
......@@ -130,8 +130,10 @@ describe JiraImportState do
context 'after transition to finished' do
let!(:jira_import) { build(:jira_import_state, :started, jid: 'some-other-jid', project: project)}
subject { jira_import.finish }
it 'triggers the import job' do
jira_import.finish
subject
expect(jira_import.jid).to be_nil
end
......@@ -139,11 +141,25 @@ describe JiraImportState do
it 'triggers the import job' do
jira_import.update!(status: :scheduled)
jira_import.finish
subject
expect(jira_import.status).to eq('scheduled')
expect(jira_import.jid).to eq('some-other-jid')
end
it 'updates the record with imported issues counts' do
import_label = create(:label, project: project, title: 'jira-import')
create_list(:labeled_issue, 3, project: project, labels: [import_label])
expect(Gitlab::JiraImport).to receive(:get_import_label_id).and_return(import_label.id)
expect(Gitlab::JiraImport).to receive(:issue_failures).and_return(2)
subject
expect(jira_import.total_issue_count).to eq(5)
expect(jira_import.failed_to_import_count).to eq(2)
expect(jira_import.imported_issues_count).to eq(3)
end
end
end
end
......@@ -20,7 +20,7 @@ describe Gitlab::JiraImport::Stage::FinishImportWorker do
end
context 'when feature flag enabled' do
let_it_be(:jira_import) { create(:jira_import_state, :scheduled, project: project) }
let_it_be(:jira_import, reload: true) { create(:jira_import_state, :scheduled, project: project) }
before do
stub_feature_flags(jira_issue_import: true)
......@@ -31,15 +31,27 @@ describe Gitlab::JiraImport::Stage::FinishImportWorker do
end
context 'when import started' do
let_it_be(:import_label) { create(:label, project: project, title: 'jira-import') }
let_it_be(:imported_issues) { create_list(:labeled_issue, 3, project: project, labels: [import_label]) }
before do
expect(Gitlab::JiraImport).to receive(:get_import_label_id).and_return(import_label.id)
expect(Gitlab::JiraImport).to receive(:issue_failures).and_return(2)
jira_import.start!
worker.perform(project.id)
end
it 'changes import state to finished' do
worker.perform(project.id)
expect(project.jira_import_status).to eq('finished')
end
it 'saves imported issues counts' do
latest_jira_import = project.latest_jira_import
expect(latest_jira_import.total_issue_count).to eq(5)
expect(latest_jira_import.failed_to_import_count).to eq(2)
expect(latest_jira_import.imported_issues_count).to eq(3)
end
end
end
end
......
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