Commit 175ff2a2 authored by Alexandru Croitor's avatar Alexandru Croitor

Add Import Jira Issues button to issues list page

Adding controller, routes for show and import actions for jira
importer.
parent 82994fe3
# frozen_string_literal: true
module Projects
module Import
class JiraController < Projects::ApplicationController
before_action :jira_import_enabled?
before_action :jira_integration_configured?
def show
prev_imported_project_key = @project.import_data&.data&.dig("jira", "project", "key")
@jira_projects = [prev_imported_project_key].compact
unless @project.import_state&.in_progress? || prev_imported_project_key
jira_client = @project.jira_service.client
@jira_projects = jira_client.Project.all.map { |p| ["#{p.name}(#{p.key})", p.key] }
end
flash[:notice] = _("Import %{status}") % { status: @project.import_state.status } if @project.import_state.present? && !@project.import_state.none?
end
def import
import_state = @project.import_state
import_state = @project.create_import_state unless import_state.present?
schedule_import(jira_import_params) unless import_state.in_progress?
redirect_to project_import_jira_path(@project)
end
private
def jira_integration_configured?
unless @project.jira_service
flash[:notice] = _("Configure Jira Integration first at Settings > Integrations > Jira")
redirect_to project_issues_path(@project)
end
end
def jira_import_enabled?
redirect_to project_issues_path(@project) unless Feature.enabled?(:jira_issue_import, @project)
end
def schedule_import(params)
return @project.import_state.schedule if @project.import_data
jira_data = { jira: { project: { key: params[:jira_project_key] } } }
@project.create_or_update_import_data(data: jira_data)
@project.import_type = 'jira'
@project.import_state.schedule if @project.save
end
def jira_import_params
params.permit(:jira_project_key)
end
end
end
end
- title = _('Jira Issue Import')
- page_title title
- breadcrumb_title title
- header_title _("Projects"), root_path
= render 'import/shared/errors'
- unless @project.import_state&.in_progress?
%h3.page-title
= icon 'issues', text: _('Import Issues From Jira')
= form_tag import_project_import_jira_path(@project), method: :post do
.form-group.row
= label_tag :jira_project_key, _('From Project'), class: 'col-form-label col-md-2'
.col-md-4
= select_tag :jira_project_key, options_for_select(@jira_projects, ''), { class: 'select2' }
.form-actions
= submit_tag _('Import Issues'), class: 'btn btn-success'
= link_to _('Cancel'), project_issues_path(@project), class: 'btn btn-cancel'
- else
%h3.page-title
= icon 'issues', text: _('Import in progress')
......@@ -7,3 +7,5 @@
- else
= _('Import CSV')
- if Feature.enabled?(:jira_issue_import, @project)
= link_to _("Import Jira Issues"), project_import_jira_path(@project), class: "btn btn-default"
......@@ -295,6 +295,12 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
draw :repository_scoped
draw :repository
draw :wiki
namespace :import do
resource :jira, only: [:show], controller: :jira do
post :import
end
end
end
# End of the /-/ scope.
......
......@@ -5115,6 +5115,9 @@ msgstr ""
msgid "Configure Gitaly timeouts."
msgstr ""
msgid "Configure Jira Integration first at Settings > Integrations > Jira"
msgstr ""
msgid "Configure Let's Encrypt"
msgstr ""
......@@ -8956,6 +8959,9 @@ msgstr ""
msgid "From Google Code"
msgstr ""
msgid "From Project"
msgstr ""
msgid "From issue creation until deploy to production"
msgstr ""
......@@ -10608,9 +10614,21 @@ msgstr ""
msgid "Import"
msgstr ""
msgid "Import %{status}"
msgstr ""
msgid "Import CSV"
msgstr ""
msgid "Import Issues"
msgstr ""
msgid "Import Issues From Jira"
msgstr ""
msgid "Import Jira Issues"
msgstr ""
msgid "Import Projects from Gitea"
msgstr ""
......@@ -11141,6 +11159,9 @@ msgstr ""
msgid "January"
msgstr ""
msgid "Jira Issue Import"
msgstr ""
msgid "JiraService|Events for %{noteable_model_name} are disabled."
msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Import::JiraController do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
context 'with anonymous user' do
before do
stub_feature_flags(jira_issue_import: true)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(new_user_session_path)
end
end
context 'post import' do
it 'redirects to issues page' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'with logged in user' do
before do
sign_in(user)
project.add_maintainer(user)
end
context 'when feature flag not enabled' do
before do
stub_feature_flags(jira_issue_import: false)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(project_issues_path(project))
end
end
context 'post import' do
it 'redirects to issues page' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
expect(response).to redirect_to(project_issues_path(project))
end
end
end
context 'when feature flag enabled' do
before do
stub_feature_flags(jira_issue_import: true)
end
context 'when jira service is enabled for the project' do
let_it_be(:jira_service) { create(:jira_service, project: project) }
context 'when jira no import state before' do
context 'get show' do
it 'renders show template' do
allow(JIRA::Resource::Project).to receive(:all).and_return([])
expect(project.import_state).to be_nil
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
expect(response).to render_template :show
end
end
context 'post import' do
it 'creates import state' do
expect(project.import_state).to be_nil
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
project.reload
expect(project.import_type).to eq 'jira'
expect(project.import_state.status).to eq 'scheduled'
expect(project.import_data.data.dig('jira', 'project', 'key')).to eq 'Test'
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
context 'when import state is scheduled' do
let_it_be(:import_state) { create(:import_state, project: project, status: :scheduled) }
context 'get show' do
it 'renders import status' do
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
expect(project.import_state.status).to eq 'scheduled'
expect(flash.now[:notice]).to eq 'Import scheduled'
end
end
context 'post import' do
before do
project.reload
project.create_import_data(data: { 'jira': { 'project': { 'key': 'Test' } } })
end
it 'uses the existing import data' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
project.reload
expect(project.import_state.status).to eq 'scheduled'
expect(project.import_data.data.dig('jira', 'project', 'key')).to eq 'Test'
expect(response).to redirect_to(project_import_jira_path(project))
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