Commit 9ebf05b5 authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '213225-adjust-issues-label-on-jira-import' into 'master'

Create project label for jira imported issues

See merge request gitlab-org/gitlab!29246
parents 2b3ffe79 29576e36
...@@ -12,6 +12,8 @@ class JiraImportState < ApplicationRecord ...@@ -12,6 +12,8 @@ class JiraImportState < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :label belongs_to :label
scope :by_jira_project_key, -> (jira_project_key) { where(jira_project_key: jira_project_key) }
validates :project, presence: true validates :project, presence: true
validates :jira_project_key, presence: true validates :jira_project_key, presence: true
validates :jira_project_name, presence: true validates :jira_project_name, presence: true
......
...@@ -33,8 +33,10 @@ module JiraImport ...@@ -33,8 +33,10 @@ module JiraImport
end end
def build_jira_import def build_jira_import
label = create_import_label(project)
project.jira_imports.build( project.jira_imports.build(
user: user, user: user,
label: label,
jira_project_key: jira_project_key, jira_project_key: jira_project_key,
# we do not have the jira_project_name or jira_project_xid yet so just set a mock value, # we do not have the jira_project_name or jira_project_xid yet so just set a mock value,
# we will once https://gitlab.com/gitlab-org/gitlab/-/merge_requests/28190 # we will once https://gitlab.com/gitlab-org/gitlab/-/merge_requests/28190
...@@ -43,6 +45,22 @@ module JiraImport ...@@ -43,6 +45,22 @@ module JiraImport
) )
end end
def create_import_label(project)
label = ::Labels::CreateService.new(build_label_attrs(project)).execute(project: project)
raise Projects::ImportService::Error, _('Failed to create import label for jira import.') if label.blank?
label
end
def build_label_attrs(project)
import_start_time = Time.zone.now
jira_imports_for_project = project.jira_imports.by_jira_project_key(jira_project_key).size + 1
title = "jira-import::#{jira_project_key}-#{jira_imports_for_project}"
description = "Label for issues that were imported from jira on #{import_start_time.strftime('%Y-%m-%d %H:%M:%S')}"
color = "#{Label.color_for(title)}"
{ title: title, description: description, color: color }
end
def validate def validate
return build_error_response(_('Jira import feature is disabled.')) unless project.jira_issues_import_feature_flag_enabled? return build_error_response(_('Jira import feature is disabled.')) unless project.jira_issues_import_feature_flag_enabled?
return build_error_response(_('You do not have permissions to run the import.')) unless user.can?(:admin_project, project) return build_error_response(_('You do not have permissions to run the import.')) unless user.can?(:admin_project, project)
......
---
title: Adjust label title applied to issues on import from Jira
merge_request: 29246
author:
type: changed
...@@ -11,28 +11,19 @@ module Gitlab ...@@ -11,28 +11,19 @@ module Gitlab
end end
def execute def execute
create_import_label(project) cache_import_label(project)
import_jira_labels import_jira_labels
end end
private private
def create_import_label(project) def cache_import_label(project)
label = Labels::CreateService.new(build_label_attrs(project)).execute(project: project) label = project.jira_imports.by_jira_project_key(jira_project_key).last.label
raise Projects::ImportService::Error, _('Failed to create import label for jira import.') unless label raise Projects::ImportService::Error, _('Failed to find import label for jira import.') unless label
JiraImport.cache_import_label_id(project.id, label.id) JiraImport.cache_import_label_id(project.id, label.id)
end end
def build_label_attrs(project)
import_start_time = project&.import_state&.last_update_started_at || Time.now
title = "jira-import-#{import_start_time.strftime('%Y-%m-%d-%H-%M-%S')}"
description = "Label for issues that were imported from jira on #{import_start_time.strftime('%Y-%m-%d %H:%M:%S')}"
color = "#{Label.color_for(title)}"
{ title: title, description: description, color: color }
end
def import_jira_labels def import_jira_labels
# todo: import jira labels, see https://gitlab.com/gitlab-org/gitlab/-/issues/212651 # todo: import jira labels, see https://gitlab.com/gitlab-org/gitlab/-/issues/212651
job_waiter job_waiter
......
...@@ -8670,6 +8670,9 @@ msgstr "" ...@@ -8670,6 +8670,9 @@ msgstr ""
msgid "Failed to enqueue the rebase operation, possibly due to a long-lived transaction. Try again later." msgid "Failed to enqueue the rebase operation, possibly due to a long-lived transaction. Try again later."
msgstr "" msgstr ""
msgid "Failed to find import label for jira import."
msgstr ""
msgid "Failed to get ref." msgid "Failed to get ref."
msgstr "" msgstr ""
......
...@@ -5,7 +5,6 @@ require 'spec_helper' ...@@ -5,7 +5,6 @@ require 'spec_helper'
describe Gitlab::JiraImport::LabelsImporter do describe Gitlab::JiraImport::LabelsImporter do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:jira_import) { create(:jira_import_state, project: project) }
let_it_be(:jira_service) { create(:jira_service, project: project) } let_it_be(:jira_service) { create(:jira_service, project: project) }
subject { described_class.new(project).execute } subject { described_class.new(project).execute }
...@@ -15,29 +14,24 @@ describe Gitlab::JiraImport::LabelsImporter do ...@@ -15,29 +14,24 @@ describe Gitlab::JiraImport::LabelsImporter do
end end
describe '#execute', :clean_gitlab_redis_cache do describe '#execute', :clean_gitlab_redis_cache do
context 'when label creation failes' do context 'when label is missing from jira import' do
before do let_it_be(:no_label_jira_import) { create(:jira_import_state, label: nil, project: project) }
allow_next_instance_of(Labels::CreateService) do |instance|
allow(instance).to receive(:execute).and_return(nil)
end
end
it 'raises error' do it 'raises error' do
expect { subject }.to raise_error(Projects::ImportService::Error, 'Failed to create import label for jira import.') expect { subject }.to raise_error(Projects::ImportService::Error, 'Failed to find import label for jira import.')
end end
end end
context 'when label is created successfully' do context 'when label exists' do
it 'creates import label' do let_it_be(:label) { create(:label) }
expect { subject }.to change { Label.count }.by(1) let_it_be(:jira_import_with_label) { create(:jira_import_state, label: label, project: project) }
end
it 'caches import label' do it 'caches import label' do
expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.import_label_cache_key(project.id))).to be nil expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.import_label_cache_key(project.id))).to be nil
subject subject
expect(Gitlab::JiraImport.get_import_label_id(project.id).to_i).to be > 0 expect(Gitlab::JiraImport.get_import_label_id(project.id).to_i).to eq(label.id)
end end
end end
end end
......
...@@ -81,6 +81,28 @@ describe JiraImport::StartImportService do ...@@ -81,6 +81,28 @@ describe JiraImport::StartImportService do
expect(jira_import.jira_project_key).to eq(fake_key) expect(jira_import.jira_project_key).to eq(fake_key)
expect(jira_import.user).to eq(user) expect(jira_import.user).to eq(user)
end end
it 'creates jira import label' do
expect { subject }.to change { Label.count }.by(1)
end
it 'creates jira label title with correct number' do
jira_import = subject.payload[:import_data]
label_title = "jira-import::#{jira_import.jira_project_key}-1"
expect(jira_import.label.title).to eq(label_title)
end
context 'when multiple jira imports for same jira project' do
let!(:jira_imports) { create_list(:jira_import_state, 3, :finished, project: project, jira_project_key: fake_key)}
it 'creates jira label title with correct number' do
jira_import = subject.payload[:import_data]
label_title = "jira-import::#{jira_import.jira_project_key}-4"
expect(jira_import.label.title).to eq(label_title)
end
end
end end
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