Support import of priorities of BCF XML files

Treat unknown priorities
pull/7471/head
Wieland Lindenthal 5 years ago
parent f557042d8b
commit c727b1dc5b
  1. 30
      modules/bcf/app/controllers/bcf/issues_controller.rb
  2. 14
      modules/bcf/app/views/bcf/issues/_import_options_hidden_fields.html.erb
  3. 38
      modules/bcf/app/views/bcf/issues/configure_unknown_priorities.html.erb
  4. 8
      modules/bcf/lib/open_project/bcf/bcf_xml/aggregations.rb
  5. 2
      modules/bcf/lib/open_project/bcf/bcf_xml/importer.rb
  6. 30
      modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb

@ -102,15 +102,17 @@ module ::Bcf
def set_import_options
@import_options = {
unknown_types_action: params.dig(:import_options, :unknown_types_action).presence || "use_default",
unknown_statuses_action: params.dig(:import_options, :unknown_statuses_action).presence || "use_default",
invalid_people_action: params.dig(:import_options, :invalid_people_action).presence || "anonymize",
unknown_mails_action: params.dig(:import_options, :unknown_mails_action).presence || 'invite',
non_members_action: params.dig(:import_options, :non_members_action).presence || 'add',
unknown_types_chose_ids: params.dig(:import_options, :unknown_types_chose_ids) || [],
unknown_statuses_chose_ids: params.dig(:import_options, :unknown_statuses_chose_ids) || [],
unknown_mails_invite_role_ids: params.dig(:import_options, :unknown_mails_invite_role_ids) || [],
non_members_add_role_ids: params.dig(:import_options, :non_members_add_role_ids) || []
unknown_types_action: params.dig(:import_options, :unknown_types_action).presence || "use_default",
unknown_statuses_action: params.dig(:import_options, :unknown_statuses_action).presence || "use_default",
unknown_priorities_action: params.dig(:import_options, :unknown_priorities_action).presence || "use_default",
invalid_people_action: params.dig(:import_options, :invalid_people_action).presence || "anonymize",
unknown_mails_action: params.dig(:import_options, :unknown_mails_action).presence || 'invite',
non_members_action: params.dig(:import_options, :non_members_action).presence || 'add',
unknown_types_chose_ids: params.dig(:import_options, :unknown_types_chose_ids) || [],
unknown_statuses_chose_ids: params.dig(:import_options, :unknown_statuses_chose_ids) || [],
unknown_priorities_chose_ids: params.dig(:import_options, :unknown_priorities_chose_ids) || [],
unknown_mails_invite_role_ids: params.dig(:import_options, :unknown_mails_invite_role_ids) || [],
non_members_add_role_ids: params.dig(:import_options, :non_members_add_role_ids) || []
}
end
@ -119,6 +121,8 @@ module ::Bcf
render_config_unknown_types
elsif render_config_unknown_statuses?
render_config_unknown_statuses
elsif render_config_unknown_priorities?
render_config_unknown_priorities
elsif render_config_invalid_people?
render_config_invalid_people
elsif render_config_unknown_mails?
@ -166,6 +170,14 @@ module ::Bcf
@importer.aggregations.unknown_statuses.present? && !params.dig(:import_options, :unknown_statuses_action).present?
end
def render_config_unknown_priorities?
@importer.aggregations.unknown_priorities.present? && !params.dig(:import_options, :unknown_priorities_action).present?
end
def render_config_unknown_priorities
render 'bcf/issues/configure_unknown_priorities'
end
def render_config_unknown_mails
@roles = Role.givable
render 'bcf/issues/configure_unknown_mails'

@ -1,7 +1,8 @@
<%= hidden_field :import_options, :unknown_types_action, :value => params.dig(:import_options, :unknown_types_action) %>
<%= hidden_field :import_options, :unknown_statuses_action, :value => params.dig(:import_options, :unknown_statuses_action) %>
<%= hidden_field :import_options, :invalid_people_action, :value => params.dig(:import_options, :invalid_people_action) %>
<%= hidden_field :import_options, :unknown_mails_action, :value => params.dig(:import_options, :unknown_mails_action) %>
<%= hidden_field :import_options, :unknown_types_action, :value => params.dig(:import_options, :unknown_types_action) %>
<%= hidden_field :import_options, :unknown_statuses_action, :value => params.dig(:import_options, :unknown_statuses_action) %>
<%= hidden_field :import_options, :unknown_priorities_action, :value => params.dig(:import_options, :unknown_priorities_action) %>
<%= hidden_field :import_options, :invalid_people_action, :value => params.dig(:import_options, :invalid_people_action) %>
<%= hidden_field :import_options, :unknown_mails_action, :value => params.dig(:import_options, :unknown_mails_action) %>
<% if params.dig(:import_options, :unknown_types_chose_ids)&.any? %>
<% params.dig(:import_options, :unknown_types_chose_ids).each do |id| %>
@ -13,6 +14,11 @@
<%= hidden_field :import_options, 'unknown_statuses_chose_ids[]', :name => 'import_options[unknown_statuses_chose_ids][]', :value => id %>
<% end %>
<% end %>
<% if params.dig(:import_options, :unknown_priorities_chose_ids)&.any? %>
<% params.dig(:import_options, :unknown_priorities_chose_ids).each do |id| %>
<%= hidden_field :import_options, 'unknown_priorities_chose_ids[]', :name => 'import_options[unknown_priorities_chose_ids][]', :value => id %>
<% end %>
<% end %>
<% if params.dig(:import_options, :unknown_mails_invite_role_ids)&.any? %>
<% params.dig(:import_options, :unknown_mails_invite_role_ids).each do |id| %>
<%= hidden_field :import_options, 'unknown_mails_invite_role_ids[]', :name => 'import_options[unknown_mails_invite_role_ids][]', :value => id %>

@ -0,0 +1,38 @@
<%= stylesheet_link_tag 'bcf/bcf.css' %>
<%= toolbar title: t('bcf.bcf_xml.import_title') %>
<%= form_tag({ action: :configure_import }, multipart: true, method: :post) do %>
<%= render 'import_options_hidden_fields' %>
<h3>Invalid priority names found</h3>
<ul>
<% @importer.aggregations.unknown_priorities.each do |name| %>
<li> <%= name.present? ? name : '(No priority provided)' %></li>
<% end %>
</ul>
<div class="form--field -full-width">
<div class="form--field-container">
<label class="option-label">
<input type="radio" name="import_options[unknown_priorities_action]" value="cancel" checked>
Recommended: Correct the data first and try another import.
</label>
</div>
</div>
<label class="option-label">
<input type="radio" name="import_options[unknown_priorities_action]" value="chose">
Import them all, setting priority to
<% options = IssuePriority.all.collect { |priority| [priority.name, priority.id] } %>
<%= select_tag 'import_options[unknown_priorities_chose_ids][]', options_for_select(options, IssuePriority.default.id),
multiple: false,
no_label: true,
title: l(:label_priority),
tabIndex: 0,
class: 'form--select form--inline-select',
id: 'unknown_priorities_chose_ids' %>
</label>
<%= submit_tag t('bcf.bcf_xml.import.button_proceed'), class: 'button -highlight' %>
<%= link_to t(:button_cancel),
{ action: :index },
class: 'button' %>
<% end %>

@ -53,6 +53,14 @@ module OpenProject::Bcf::BcfXml
@instance_cache[:unknown_types] ||= all_types - Type.all.map(&:name)
end
def all_priorities
@instance_cache[:all_priorities] ||= listings.map { |entry| entry[:priority] }.flatten.uniq
end
def unknown_priorities
@instance_cache[:unknown_priorities] ||= all_priorities - IssuePriority.all.map(&:name)
end
def clear_instance_cache
@instance_cache = {}
end

@ -9,11 +9,13 @@ module OpenProject::Bcf::BcfXml
DEFAULT_IMPORT_OPTIONS = {
unknown_types_action: "use_default",
unknown_statuses_action: "use_default",
unknown_priorities_action: "use_default",
invalid_people_action: "anonymize",
unknown_mails_action: 'invite',
non_members_action: 'add',
unknown_types_chose_ids: [],
unknown_statuses_chose_ids: [],
unknown_priorities_chose_ids: [],
unknown_mails_invite_role_ids: [],
non_members_add_role_ids: []
}.freeze

@ -28,6 +28,7 @@ module OpenProject::Bcf::BcfXml
treat_unknown_types
treat_unknown_statuses
treat_unknown_priorities
extractor.doc = @doc
@ -78,7 +79,7 @@ module OpenProject::Bcf::BcfXml
elsif import_options[:unknown_statuses_action] == 'chose' && import_options[:unknown_statuses_chose_ids].any?
replace_status_with(::Status.find_by(id: import_options[:unknown_statuses_chose_ids].first)&.name)
else
raise StandardError.new 'Unknown topic statuses found in import. Use an existing status name.'
raise StandardError.new 'Unknown topic status found in import. Use an existing status name.'
end
end
end
@ -89,6 +90,33 @@ module OpenProject::Bcf::BcfXml
@doc.xpath('/Markup/Topic').first.set_attribute('TopicStatus', new_status_name)
end
##
# Handle unknown priorities during import
def treat_unknown_priorities
if aggregations.unknown_priorities.any?
if import_options[:unknown_priorities_action] == 'use_default'
# NOP The 'use_default' case gets already covered by OP.
elsif import_options[:unknown_priorities_action] == 'chose' && import_options[:unknown_priorities_chose_ids].any?
replace_priorities_with(::IssuePriority.find_by(id: import_options[:unknown_priorities_chose_ids].first)&.name)
else
raise StandardError.new 'Unknown topic priority found in import. Use an existing priority name.'
end
end
end
def replace_priorities_with(new_priority_name)
raise StandardError.new "New priority name can't be blank." unless new_priority_name.present?
priority_node = @doc.xpath('/Markup/Topic/Priority').first
if priority_node
priority_node.content = new_priority_name
else
# Valid BCF XML Topics must have a Title node. So we can add the Priority node just behind it and thus,
# maintain the schema's sequence compliance.
@doc.at('/Markup/Topic/Title').after("<Priority>#{new_priority_name}</Priority>")
end
end
def synchronize_with_work_package
self.is_update = issue.work_package.present?
self.wp_last_updated_at = issue.work_package&.updated_at

Loading…
Cancel
Save