Commit 4a68f2ce authored by Alexandru Croitor's avatar Alexandru Croitor

Allow to import different jira projects into gitlab project

Upated the code to allow for different projects to be imported
into same gitlab project by running the import multiple times.
parent 175ff2a2
...@@ -7,20 +7,16 @@ module Projects ...@@ -7,20 +7,16 @@ module Projects
before_action :jira_integration_configured? before_action :jira_integration_configured?
def show def show
prev_imported_project_key = @project.import_data&.data&.dig("jira", "project", "key") unless @project.import_state&.in_progress?
@jira_projects = [prev_imported_project_key].compact
unless @project.import_state&.in_progress? || prev_imported_project_key
jira_client = @project.jira_service.client jira_client = @project.jira_service.client
@jira_projects = jira_client.Project.all.map { |p| ["#{p.name}(#{p.key})", p.key] } @jira_projects = jira_client.Project.all.map { |p| ["#{p.name} (#{p.key})", p.key] }
end end
flash[:notice] = _("Import %{status}") % { status: @project.import_state.status } if @project.import_state.present? && !@project.import_state.none? flash[:notice] = _("Import %{status}") % { status: @project.import_state.status } if @project.import_state.present? && !@project.import_state.none?
end end
def import def import
import_state = @project.import_state import_state = @project.import_state || @project.create_import_state
import_state = @project.create_import_state unless import_state.present?
schedule_import(jira_import_params) unless import_state.in_progress? schedule_import(jira_import_params) unless import_state.in_progress?
...@@ -29,22 +25,29 @@ module Projects ...@@ -29,22 +25,29 @@ module Projects
private private
def jira_integration_configured? def jira_import_enabled?
unless @project.jira_service return if Feature.enabled?(:jira_issue_import, @project)
flash[:notice] = _("Configure Jira Integration first at Settings > Integrations > Jira")
redirect_to project_issues_path(@project) redirect_to project_issues_path(@project)
end
end end
def jira_import_enabled? def jira_integration_configured?
redirect_to project_issues_path(@project) unless Feature.enabled?(:jira_issue_import, @project) return if @project.jira_service
flash[:notice] = _("Configure the Jira integration first on your project's %{strong_start} Settings > Integrations > Jira%{strong_end} page." %
{ strong_start: '<strong>'.html_safe, strong_end: '</strong>'.html_safe })
redirect_to project_issues_path(@project)
end end
def schedule_import(params) def schedule_import(params)
return @project.import_state.schedule if @project.import_data import_data = @project.create_or_update_import_data(data: {}).becomes(JiraImportData)
import_data << JiraImportData::JiraProjectDetails.new(
params[:jira_project_key],
Time.now.strftime('%Y-%m-%d %H:%M:%S'),
{ user_id: current_user.id, name: current_user.name }
)
jira_data = { jira: { project: { key: params[:jira_project_key] } } }
@project.create_or_update_import_data(data: jira_data)
@project.import_type = 'jira' @project.import_type = 'jira'
@project.import_state.schedule if @project.save @project.import_state.schedule if @project.save
end end
......
# frozen_string_literal: true
class JiraImportData < ProjectImportData
JiraProjectDetails = Struct.new(:key, :scheduled_at, :scheduled_by)
def projects
return [] unless data
projects = data.dig('jira', 'projects').map do |p|
JiraProjectDetails.new(p['key'], p['scheduled_at'], p['scheduled_by'])
end
projects.sort_by { |jp| jp.scheduled_at }
end
def <<(project)
self.data ||= { jira: { projects: [] } }
self.data['jira']['projects'] << project.to_h.deep_stringify_keys!
end
end
...@@ -5,18 +5,20 @@ ...@@ -5,18 +5,20 @@
= render 'import/shared/errors' = render 'import/shared/errors'
- unless @project.import_state&.in_progress? - if @project.import_state&.in_progress?
%h3.page-title %h3.page-title.d-flex.align-items-center
= icon 'issues', text: _('Import Issues From Jira') = sprite_icon('issues', size: 16, css_class: 'mr-1')
= _('Import in progress')
- else
%h3.page-title.d-flex.align-items-center
= sprite_icon('issues', size: 16, css_class: 'mr-1')
= _('Import issues from Jira')
= form_tag import_project_import_jira_path(@project), method: :post do = form_tag import_project_import_jira_path(@project), method: :post do
.form-group.row .form-group.row
= label_tag :jira_project_key, _('From Project'), class: 'col-form-label col-md-2' = label_tag :jira_project_key, _('From project'), class: 'col-form-label col-md-2'
.col-md-4 .col-md-4
= select_tag :jira_project_key, options_for_select(@jira_projects, ''), { class: 'select2' } = select_tag :jira_project_key, options_for_select(@jira_projects, ''), { class: 'select2' }
.form-actions .form-actions
= submit_tag _('Import Issues'), class: 'btn btn-success' = submit_tag _('Import issues'), class: 'btn btn-success'
= link_to _('Cancel'), project_issues_path(@project), class: 'btn btn-cancel' = link_to _('Cancel'), project_issues_path(@project), class: 'btn btn-cancel'
- else
%h3.page-title
= icon 'issues', text: _('Import in progress')
...@@ -8,4 +8,4 @@ ...@@ -8,4 +8,4 @@
= _('Import CSV') = _('Import CSV')
- if Feature.enabled?(:jira_issue_import, @project) - if Feature.enabled?(:jira_issue_import, @project)
= link_to _("Import Jira Issues"), project_import_jira_path(@project), class: "btn btn-default" = link_to _("Import Jira issues"), project_import_jira_path(@project), class: "btn btn-default"
...@@ -5115,9 +5115,6 @@ msgstr "" ...@@ -5115,9 +5115,6 @@ msgstr ""
msgid "Configure Gitaly timeouts." msgid "Configure Gitaly timeouts."
msgstr "" msgstr ""
msgid "Configure Jira Integration first at Settings > Integrations > Jira"
msgstr ""
msgid "Configure Let's Encrypt" msgid "Configure Let's Encrypt"
msgstr "" msgstr ""
...@@ -5157,6 +5154,9 @@ msgstr "" ...@@ -5157,6 +5154,9 @@ msgstr ""
msgid "Configure the %{link} integration." msgid "Configure the %{link} integration."
msgstr "" msgstr ""
msgid "Configure the Jira integration first on your project's %{strong_start} Settings > Integrations > Jira%{strong_end} page."
msgstr ""
msgid "Configure the way a user creates a new account." msgid "Configure the way a user creates a new account."
msgstr "" msgstr ""
...@@ -8959,15 +8959,15 @@ msgstr "" ...@@ -8959,15 +8959,15 @@ msgstr ""
msgid "From Google Code" msgid "From Google Code"
msgstr "" msgstr ""
msgid "From Project"
msgstr ""
msgid "From issue creation until deploy to production" msgid "From issue creation until deploy to production"
msgstr "" msgstr ""
msgid "From merge request merge until deploy to production" msgid "From merge request merge until deploy to production"
msgstr "" msgstr ""
msgid "From project"
msgstr ""
msgid "From the Kubernetes cluster details view, install Runner from the applications list" msgid "From the Kubernetes cluster details view, install Runner from the applications list"
msgstr "" msgstr ""
...@@ -10620,13 +10620,7 @@ msgstr "" ...@@ -10620,13 +10620,7 @@ msgstr ""
msgid "Import CSV" msgid "Import CSV"
msgstr "" msgstr ""
msgid "Import Issues" msgid "Import Jira issues"
msgstr ""
msgid "Import Issues From Jira"
msgstr ""
msgid "Import Jira Issues"
msgstr "" msgstr ""
msgid "Import Projects from Gitea" msgid "Import Projects from Gitea"
...@@ -10650,6 +10644,9 @@ msgstr "" ...@@ -10650,6 +10644,9 @@ msgstr ""
msgid "Import issues" msgid "Import issues"
msgstr "" msgstr ""
msgid "Import issues from Jira"
msgstr ""
msgid "Import members" msgid "Import members"
msgstr "" msgstr ""
......
...@@ -64,7 +64,7 @@ describe Projects::Import::JiraController do ...@@ -64,7 +64,7 @@ describe Projects::Import::JiraController do
context 'when jira service is enabled for the project' do context 'when jira service is enabled for the project' do
let_it_be(:jira_service) { create(:jira_service, project: project) } let_it_be(:jira_service) { create(:jira_service, project: project) }
context 'when jira no import state before' do context 'when running jira import first time' do
context 'get show' do context 'get show' do
it 'renders show template' do it 'renders show template' do
allow(JIRA::Resource::Project).to receive(:all).and_return([]) allow(JIRA::Resource::Project).to receive(:all).and_return([])
...@@ -83,9 +83,11 @@ describe Projects::Import::JiraController do ...@@ -83,9 +83,11 @@ describe Projects::Import::JiraController do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' } post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
project.reload project.reload
jira_project = project.import_data.data.dig('jira', 'projects').first
expect(project.import_type).to eq 'jira' expect(project.import_type).to eq 'jira'
expect(project.import_state.status).to eq 'scheduled' expect(project.import_state.status).to eq 'scheduled'
expect(project.import_data.data.dig('jira', 'project', 'key')).to eq 'Test' expect(jira_project['key']).to eq 'Test'
expect(response).to redirect_to(project_import_jira_path(project)) expect(response).to redirect_to(project_import_jira_path(project))
end end
end end
...@@ -106,15 +108,61 @@ describe Projects::Import::JiraController do ...@@ -106,15 +108,61 @@ describe Projects::Import::JiraController do
context 'post import' do context 'post import' do
before do before do
project.reload project.reload
project.create_import_data(data: { 'jira': { 'project': { 'key': 'Test' } } }) project.create_import_data(
data: {
'jira': {
'projects': [{ 'key': 'Test', scheduled_at: 5.days.ago, scheduled_by: { user_id: user.id, name: user.name } }]
}
}
)
end end
it 'uses the existing import data' do it 'uses the existing import data' do
expect(controller).not_to receive(:schedule_import)
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
context 'when jira import ran before' do
let_it_be(:import_state) { create(:import_state, project: project, status: :finished) }
context 'get show' do
it 'renders import status' do
allow(JIRA::Resource::Project).to receive(:all).and_return([])
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
expect(project.import_state.status).to eq 'finished'
expect(flash.now[:notice]).to eq 'Import finished'
end
end
context 'post import' do
before do
project.reload
project.create_import_data(
data: {
'jira': {
'projects': [{ 'key': 'Test', scheduled_at: 5.days.ago, scheduled_by: { user_id: user.id, name: user.name } }]
}
}
)
end
it 'uses the existing import data' do
expect(controller).to receive(:schedule_import).and_call_original
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' } post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
project.reload project.reload
expect(project.import_state.status).to eq 'scheduled' expect(project.import_state.status).to eq 'scheduled'
expect(project.import_data.data.dig('jira', 'project', 'key')).to eq 'Test' jira_imported_projects = project.import_data.data.dig('jira', 'projects')
expect(jira_imported_projects.size).to eq 2
expect(jira_imported_projects.first['key']).to eq 'Test'
expect(jira_imported_projects.last['key']).to eq 'New Project'
expect(response).to redirect_to(project_import_jira_path(project)) expect(response).to redirect_to(project_import_jira_path(project))
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