Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

copy dashboards with all widgets and updated references where necessary

Markus Kahl59f6de21c6f0
diff --git a/app/services/copy/dependency.rb b/app/services/copy/dependency.rb
index c3c6e44234b..25de6e1607a 100644
--- a/app/services/copy/dependency.rb
+++ b/app/services/copy/dependency.rb
@@ -63,7 +63,15 @@ module Copy
##
# Merge some other model's errors with the result errors
def add_error!(model, errors)
- result.errors.add(:base, "#{model.class.model_name.human} '#{model}': #{errors.full_messages.join(". ")}")
+ result.errors.add(:base, "#{human_model_name(model)}: #{error_messages(errors)}")
+ end
+
+ def human_model_name(model)
+ "#{model.class.model_name.human} '#{model}'"
+ end
+
+ def error_messages(errors)
+ errors.full_messages.join(". ")
end
def perform(params:)
diff --git a/app/services/grids/copy/widgets_dependent_service.rb b/app/services/grids/copy/widgets_dependent_service.rb
new file mode 100644
index 00000000000..4357500ef82
--- /dev/null
+++ b/app/services/grids/copy/widgets_dependent_service.rb
@@ -0,0 +1,115 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Grids::Copy
+ class WidgetsDependentService < ::Copy::Dependency
+ protected
+
+ def copy_dependency(params:)
+ copy_widgets source, target, params
+ end
+
+ def copy_widgets(grid, new_grid, params)
+ grid.widgets.find_each do |widget|
+ new_widget = duplicate_widget widget, new_grid, params
+
+ if new_widget && !new_widget.save
+ add_error! new_widget, new_widget.errors
+ end
+ end
+ end
+
+ def duplicate_widget(widget, new_grid, params)
+ new_widget = widget.dup
+ new_widget.grid = new_grid
+
+ references = map_references(widget, params).each do |result|
+ if result.success?
+ result.each do |option, value|
+ new_widget.options[option] = value
+ end
+ else
+ add_error! widget, result.errors
+ end
+ end
+
+ new_widget if references.all?(&:success?)
+ end
+
+ def map_references(widget, params)
+ widget.options.map do |option, value|
+ map_reference option, value, params
+ end
+ end
+
+ def map_reference(option, value, params)
+ mapper = find_mapper option
+
+ if mapper
+ mapper.call(value, params).map { |id| [option, id] }
+ else
+ ServiceResult.new success: true, result: [option, value]
+ end
+ end
+
+ def find_mapper(option)
+ _, mapper = reference_mappers.find do |key, _|
+ (key.is_a?(Regexp) && option.to_s =~ key) || key.to_s.downcase == option.to_s.downcase
+ end
+
+ mapper
+ end
+
+ def reference_mappers
+ {
+ /query_?id/i => method(:map_query_id)
+ }
+ end
+
+ def map_query_id(query_id, params)
+ existing_query_id = state.query_id_lookup[query_id.to_i] if state.query_id_lookup
+
+ if existing_query_id
+ ServiceResult.new(result: existing_query_id, success: true)
+ else
+ duplicate_query(query_id, params).map(&:id)
+ end
+ end
+
+ def duplicate_query(query_id, params)
+ query = Query.find query_id
+
+ ::Queries::CopyService
+ .new(user: user, source: query)
+ .with_state(state)
+ .call(params)
+ end
+ end
+end
diff --git a/app/services/grids/copy_service.rb b/app/services/grids/copy_service.rb
new file mode 100644
index 00000000000..602efac5234
--- /dev/null
+++ b/app/services/grids/copy_service.rb
@@ -0,0 +1,66 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Grids
+ ##
+ # Base class for any grid-based model's copy service.
+ class CopyService < ::BaseServices::Copy
+ def initialize(user:, source:, contract_class: ::EmptyContract)
+ super user: user, source: source, contract_class: contract_class
+ end
+
+ protected
+
+ ##
+ # DependentServices can be specialised through a class in the
+ # concrete model's namespace, e.g. Boards::Copy::WidgetsDependentService.
+ def copy_dependencies
+ [
+ widgets_dependency
+ ]
+ end
+
+ def widgets_dependency
+ self.class.module_parent::Copy::WidgetsDependentService
+ rescue NameError
+ Copy::WidgetsDependentService
+ end
+
+ def initialize_copy(source, params)
+ grid = source.dup
+
+ initialize_new_grid! grid, source, params
+
+ ServiceResult.new success: grid.save, result: grid
+ end
+
+ def initialize_new_grid!(_new_grid, _original_grid, _params); end
+ end
+end
diff --git a/app/services/projects/copy/boards_dependent_service.rb b/app/services/projects/copy/boards_dependent_service.rb
index bf2b41c173b..71d9abc80ff 100644
--- a/app/services/projects/copy/boards_dependent_service.rb
+++ b/app/services/projects/copy/boards_dependent_service.rb
@@ -49,10 +49,7 @@ module Projects::Copy
.new(source: board, user: user)
.with_state(state)
.call(params.merge)
- .on_failure do |result|
-
- add_error! board, result.errors
- end
+ .on_failure { |result| add_error! board, result.errors }
end
end
end
diff --git a/app/services/projects/copy/categories_dependent_service.rb b/app/services/projects/copy/categories_dependent_service.rb
index 77e7528ee31..b3acbf86e13 100644
--- a/app/services/projects/copy/categories_dependent_service.rb
+++ b/app/services/projects/copy/categories_dependent_service.rb
@@ -37,9 +37,15 @@ module Projects::Copy
protected
def copy_dependency(params:)
+ category_id_map = {}
+
source.categories.find_each do |category|
- target.categories.create category.attributes.dup.except('id', 'project_id')
+ new_category = target.categories.create category.attributes.dup.except('id', 'project_id')
+
+ category_id_map[category.id] = new_category.id
end
+
+ state.category_id_lookup = category_id_map
end
end
end
diff --git a/app/services/projects/copy/overview_dependent_service.rb b/app/services/projects/copy/overview_dependent_service.rb
new file mode 100644
index 00000000000..80360b71e0c
--- /dev/null
+++ b/app/services/projects/copy/overview_dependent_service.rb
@@ -0,0 +1,50 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Projects::Copy
+ class OverviewDependentService < Dependency
+ protected
+
+ # Copies the overview from +project+
+ def copy_dependency(params)
+ ::Grids::Overview.where(project: source).find_each do |overview|
+ duplicate_overview(overview, params)
+ end
+ end
+
+ def duplicate_overview(overview, params)
+ ::Overviews::CopyService
+ .new(source: overview, user: user)
+ .with_state(state)
+ .call(params.merge)
+ .on_failure { |result| add_error! overview, result.errors }
+ end
+ end
+end
diff --git a/app/services/projects/copy/queries_dependent_service.rb b/app/services/projects/copy/queries_dependent_service.rb
index bb9ea7ec942..e77c2e651ea 100644
--- a/app/services/projects/copy/queries_dependent_service.rb
+++ b/app/services/projects/copy/queries_dependent_service.rb
@@ -39,9 +39,20 @@ module Projects::Copy
# Copies queries from +project+
# Only includes the queries visible in the wp table view.
def copy_dependency(params:)
- source.queries.non_hidden.includes(:query_menu_item).each do |query|
- duplicate_query(query, params)
+ mapping = queries_to_copy.map do |query|
+ copy = duplicate_query(query, params)
+ # Either assign the succesfully copied query's ID or nil to indicate
+ # it could not be copied.
+ new_id = copy.map(&:id).to_a.first
+
+ [query.id, new_id]
end
+
+ state.query_id_lookup = Hash[mapping]
+ end
+
+ def queries_to_copy
+ source.queries.non_hidden.includes(:query_menu_item)
end
def duplicate_query(query, params)
@@ -49,10 +60,7 @@ module Projects::Copy
.new(source: query, user: user)
.with_state(state)
.call(params.merge)
- .on_failure do |result|
-
- add_error! query, result.errors
- end
+ .on_failure { |result| add_error! query, result.errors }
end
end
end
diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rb
index 6db7053ae35..f910b5aa7cf 100644
--- a/app/services/projects/copy_service.rb
+++ b/app/services/projects/copy_service.rb
@@ -56,7 +56,8 @@ module Projects
::Projects::Copy::WikiDependentService,
::Projects::Copy::ForumsDependentService,
::Projects::Copy::QueriesDependentService,
- ::Projects::Copy::BoardsDependentService
+ ::Projects::Copy::BoardsDependentService,
+ ::Projects::Copy::OverviewDependentService
]
end
@@ -76,6 +77,8 @@ module Projects
target.custom_field_values = source.custom_value_attributes
target.custom_values = source.custom_values.map(&:dup)
+ target.status = source.status.dup
+
# Additional input target params
target_project_params = params[:target_project_params].with_indifferent_access
diff --git a/app/services/queries/copy_service.rb b/app/services/queries/copy_service.rb
index 5b1acb0539d..dc1dd48c88f 100644
--- a/app/services/queries/copy_service.rb
+++ b/app/services/queries/copy_service.rb
@@ -44,9 +44,40 @@ module Queries
new_query.sort_criteria = source.sort_criteria if source.sort_criteria
new_query.project = state.project || source.project
+ map_filters new_query
+
ServiceResult.new(success: new_query.save, result: new_query)
end
+ def map_filters(query)
+ mappers = filter_mappers
+
+ query.filters.each do |filter|
+ mapper = mappers[filter.name.to_sym]
+
+ mapper&.call filter
+ end
+ end
+
+ def filter_mappers
+ {
+ version_id: method(:map_version_filter),
+ category_id: method(:map_category_filter)
+ }
+ end
+
+ def map_version_filter(filter)
+ return unless state.version_id_lookup
+
+ filter.values = filter.values.map { |id| state.version_id_lookup[id.to_i] || id }
+ end
+
+ def map_category_filter(filter)
+ return unless state.category_id_lookup
+
+ filter.values = filter.values.map { |id| state.category_id_lookup[id.to_i] || id }
+ end
+
def skipped_attributes
%w[id created_at updated_at project_id sort_criteria]
end
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index a51a02bf67d..0ff2b1ae690 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -127,10 +127,33 @@ class ServiceResult
def on_success
yield(self) if success?
+ self
end
def on_failure
yield(self) if failure?
+ self
+ end
+
+ def each
+ yield result if success?
+ self
+ end
+
+ def map
+ return self if failure?
+
+ dup.tap do |new_result|
+ new_result.result = yield result
+ end
+ end
+
+ def to_a
+ if success?
+ [result]
+ else
+ []
+ end
end
def message
diff --git a/app/views/copy_projects/copy_settings/_block_checkbox.html.erb b/app/views/copy_projects/copy_settings/_block_checkbox.html.erb
index d5aaa780880..7712e133efd 100644
--- a/app/views/copy_projects/copy_settings/_block_checkbox.html.erb
+++ b/app/views/copy_projects/copy_settings/_block_checkbox.html.erb
@@ -26,9 +26,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See docs/COPYRIGHT.rdoc for more details.
++#%>
-<% if count > 0 %>
+<% if count.nil? || count > 0 %>
<label class="block">
<%= check_box_tag 'only[]', name, checked, id: "only_#{name}" %>
- <%= "#{label} (#{count})" %>
+ <% count_label = count ? " (#{count})" : "" %>
+ <%= "#{label}#{count_label}" %>
</label>
<% end %>
diff --git a/app/views/copy_projects/copy_settings/_copy_associations.html.erb b/app/views/copy_projects/copy_settings/_copy_associations.html.erb
index 70b83436b65..4aea4bed6b7 100644
--- a/app/views/copy_projects/copy_settings/_copy_associations.html.erb
+++ b/app/views/copy_projects/copy_settings/_copy_associations.html.erb
@@ -30,6 +30,9 @@ See docs/COPYRIGHT.rdoc for more details.
<fieldset class="form--fieldset">
<legend class="form--fieldset-legend"><%= l(:button_copy) %></legend>
+ <%= render partial: "copy_projects/copy_settings/block_checkbox",
+ locals: { name: "overview", checked: true, label: t(:label_overview), count: nil } %>
+
<%= render partial: "copy_projects/copy_settings/block_checkbox",
locals: { name: "queries", checked: true, label: l(:label_query_plural),
count: project.queries.count } %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 59e4a6ace4b..58af9c3d7a7 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1818,8 +1818,8 @@ en:
mail_body_register_header_title: "Project member invitation email"
mail_body_register_user: "Dear %{name}, "
mail_body_register_links_html: |
- Please feel free to browse our youtube channel (%{youtube_link}) where we provide a webinar (%{webinar_link})
- and “Get started” videos (%{get_started_link}) to make your first steps in OpenProject as easy as possible.
+ Please feel free to browse our youtube channel (%{youtube_link}) where we provide a webinar (%{webinar_link})
+ and “Get started” videos (%{get_started_link}) to make your first steps in OpenProject as easy as possible.
<br />
If you have any further questions, consult our documentation (%{documentation_link}) or contact us (%{contact_us_link}).
mail_body_register_closing: "Your OpenProject team"
diff --git a/modules/boards/app/models/boards/grid.rb b/modules/boards/app/models/boards/grid.rb
index da9d0b7684d..f5e46fb12df 100644
--- a/modules/boards/app/models/boards/grid.rb
+++ b/modules/boards/app/models/boards/grid.rb
@@ -49,6 +49,10 @@ module Boards
::Query.where(id: contained_query_ids)
end
+ def to_s
+ "#{I18n.t('boards.label_board')} '#{name}'"
+ end
+
private
def delete_queries
diff --git a/modules/boards/app/services/boards/copy/widgets_dependent_service.rb b/modules/boards/app/services/boards/copy/widgets_dependent_service.rb
index 6776d4adabf..e95dc0cecd4 100644
--- a/modules/boards/app/services/boards/copy/widgets_dependent_service.rb
+++ b/modules/boards/app/services/boards/copy/widgets_dependent_service.rb
@@ -29,45 +29,15 @@
#++
module Boards::Copy
- class WidgetsDependentService < ::Copy::Dependency
+ class WidgetsDependentService < ::Grids::Copy::WidgetsDependentService
protected
- def copy_dependency(params:)
- copy_widgets(source, target, params)
- end
-
- def copy_widgets(board, new_board, params)
- board.widgets.find_each do |widget|
- unless widget.identifier == 'work_package_query'
- raise "Expected widget work_package_query, got #{widget.identifier}"
- end
-
- new_widget = duplicate_widget(widget, new_board, params)
-
- if new_widget && !new_widget.save
- add_error!(new_widget, new_widget.errors)
- end
+ def duplicate_widget(widget, _new_board, _params)
+ unless widget.identifier == 'work_package_query'
+ raise "Expected widget work_package_query, got #{widget.identifier}"
end
- end
- def duplicate_widget(widget, new_board, params)
- new_widget = widget.dup
- new_widget.grid = new_board
-
- query = Query.find widget.options['queryId']
-
- call = ::Queries::CopyService
- .new(user: user, source: query)
- .with_state(state)
- .call(params)
-
- if call.success?
- new_widget.options['queryId'] = call.result.id.to_s
- new_widget
- else
- add_error! widget, call.errors
- nil
- end
+ super
end
end
end
diff --git a/modules/boards/app/services/boards/copy_service.rb b/modules/boards/app/services/boards/copy_service.rb
index c5d59f05160..1ccd43d49ff 100644
--- a/modules/boards/app/services/boards/copy_service.rb
+++ b/modules/boards/app/services/boards/copy_service.rb
@@ -29,30 +29,11 @@
#++
module Boards
- class CopyService < ::BaseServices::Copy
-
- def initialize(user:, source:)
- super(user: user, source: source, contract_class: ::EmptyContract)
- end
-
+ class CopyService < ::Grids::CopyService
protected
- def copy_dependencies
- [
- ::Boards::Copy::WidgetsDependentService
- ]
- end
-
- def initialize_copy(source, params)
- new_board = ::Boards::Grid.new
- new_board.attributes = source.attributes.dup.except(*skipped_attributes)
- new_board.project = state.project || source.project
-
- ServiceResult.new(success: new_board.save, result: new_board)
- end
-
- def skipped_attributes
- %w[id created_at updated_at project_id sort_criteria]
+ def initialize_new_grid!(new_board, original_board, _params)
+ new_board.project = state.project || original_board.project
end
end
end
diff --git a/modules/boards/config/locales/en.yml b/modules/boards/config/locales/en.yml
index ca0145f29a1..40966ce4dad 100644
--- a/modules/boards/config/locales/en.yml
+++ b/modules/boards/config/locales/en.yml
@@ -5,4 +5,5 @@ en:
project_module_board_view: "Boards"
boards:
+ label_board: "Board"
label_boards: "Boards"
diff --git a/modules/boards/spec/services/copy_service_integration_spec.rb b/modules/boards/spec/services/copy_service_integration_spec.rb
index ef07cd3739e..b8e20370ab2 100644
--- a/modules/boards/spec/services/copy_service_integration_spec.rb
+++ b/modules/boards/spec/services/copy_service_integration_spec.rb
@@ -64,25 +64,37 @@ describe Projects::CopyService, 'integration', type: :model do
describe 'call' do
subject { instance.call(params) }
let(:project_copy) { subject.result }
- let(:copied_boards) { Boards::Grid.where(project: project_copy) }
- let(:copied_board) { copied_boards.first }
+ let(:board_copies) { Boards::Grid.where(project: project_copy) }
+ let(:board_copy) { board_copies.first }
it 'will copy the boards with the order correct' do
expect(subject).to be_success
- expect(copied_boards.count).to eq 1
+ expect(board_copies.count).to eq 1
# Expect board name to match
- expect(copied_board.name).to eq 'My Board'
+ expect(board_copy.name).to eq 'My Board'
# Expect query to differ
- query_id = copied_board.widgets.first.options['queryId']
+ query_id = board_copy.widgets.first.options['queryId']
expect(query_id.to_i).not_to eq(query.id)
# Expect query to be in correct project
query = Query.find(query_id)
expect(query.project).to eq project_copy
+ # Expect widgets have been copied (including updated query references)
+ widget = board_view.widgets.first
+ widget_copy = board_copy.widgets.first
+
+ different_attr = %w(id grid_id options)
+ expect(widget.attributes.except(*different_attr)).to eq widget_copy.attributes.except(*different_attr)
+
+ expect(widget_copy.grid_id).to eq board_copy.id
+ expect(widget_copy.options["queryId"]).to eq query.id
+ expect(widget_copy.options["filters"]).to eq widget.options["filters"]
+
+ # Expect work packages have been copied in the correct order
wps = query.ordered_work_packages
expect(wps.count).to eq 2
diff --git a/modules/overviews/app/models/grids/overview.rb b/modules/overviews/app/models/grids/overview.rb
index 596a7464a52..ed509ca29b5 100644
--- a/modules/overviews/app/models/grids/overview.rb
+++ b/modules/overviews/app/models/grids/overview.rb
@@ -35,5 +35,9 @@ module Grids
set_acts_as_attachable_options view_permission: :view_project,
delete_permission: :manage_overview,
add_permission: :manage_overview
+
+ def to_s
+ "Project '#{project&.name || 'missing'}' #{I18n.t('overviews.label')}"
+ end
end
end
diff --git a/modules/overviews/app/services/overviews/copy/widgets_dependent_service.rb b/modules/overviews/app/services/overviews/copy/widgets_dependent_service.rb
new file mode 100644
index 00000000000..4a1d5a00740
--- /dev/null
+++ b/modules/overviews/app/services/overviews/copy/widgets_dependent_service.rb
@@ -0,0 +1,34 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Overviews::Copy
+ class WidgetsDependentService < ::Grids::Copy::WidgetsDependentService
+ end
+end
diff --git a/modules/overviews/app/services/overviews/copy_service.rb b/modules/overviews/app/services/overviews/copy_service.rb
new file mode 100644
index 00000000000..c961dc86da9
--- /dev/null
+++ b/modules/overviews/app/services/overviews/copy_service.rb
@@ -0,0 +1,43 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Overviews
+ class CopyService < ::Grids::CopyService
+ protected
+
+ def initialize_new_grid!(new_overview, _original_overview, _params)
+ unless state.project
+ raise ArgumentError, "Overviews can only be copied as part of a project. Each project may only contain 1 overview itself."
+ end
+
+ new_overview.project = state.project
+ end
+ end
+end
diff --git a/modules/overviews/spec/services/copy_service_integration_spec.rb b/modules/overviews/spec/services/copy_service_integration_spec.rb
new file mode 100644
index 00000000000..10f38584fb6
--- /dev/null
+++ b/modules/overviews/spec/services/copy_service_integration_spec.rb
@@ -0,0 +1,182 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe Projects::CopyService, 'integration', type: :model do
+ shared_let(:source) { FactoryBot.create :project, enabled_module_names: %w[wiki work_package_tracking] }
+ shared_let(:source_category) { FactoryBot.create :category, project: source, name: 'Stock management' }
+ shared_let(:source_version) { FactoryBot.create :version, project: source, name: 'Version A' }
+
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: source,
+ member_through_role: role)
+ end
+ let(:role) { FactoryBot.create :role, permissions: %i[copy_projects] }
+ let(:instance) do
+ described_class.new(source: source, user: current_user)
+ end
+ let(:only_args) { nil }
+ let(:target_project_params) do
+ { name: 'Some name', identifier: 'some-identifier' }
+ end
+ let(:params) do
+ { target_project_params: target_project_params, only: only_args }
+ end
+
+ describe 'call' do
+ subject { instance.call(params) }
+ let(:project_copy) { subject.result }
+
+ describe 'overview' do
+ let(:widget_data) do
+ [
+ [[1, 1, 1, 2], "project_description", {}],
+ [[1, 1, 3, 4], "project_status", {}],
+ [[2, 2, 1, 2], "members", { "name" => "Members" }]
+ ]
+ end
+
+ let(:original_overview) do
+ widgets = widget_data.map do |layout, identifier, options|
+ FactoryBot.build(
+ :grid_widget,
+ identifier: identifier,
+ options: options,
+ start_row: layout[0],
+ end_row: layout[1],
+ start_column: layout[2],
+ end_column: layout[3]
+ )
+ end
+
+ FactoryBot.create :overview, project: source, widgets: widgets, column_count: 2, row_count: widgets.size / 2 + 1
+ end
+
+ let(:overview) { Grids::Overview.find_by(project: project_copy) }
+
+ context 'when requested' do
+ let(:only_args) { %i[work_packages overview] }
+
+ before do
+ original_overview
+
+ expect(subject).to be_success
+ end
+
+ it 'is copied' do
+ expect(overview.widgets.size).to eq original_overview.widgets.size
+ end
+ end
+
+ context 'when not requested' do
+ let(:only_args) { %i[work_packages] }
+
+ before do
+ original_overview
+
+ expect(subject).to be_success
+ end
+
+ it 'is ignored' do
+ expect(overview).to be_nil
+ end
+ end
+
+ describe 'copy' do
+ before do
+ original_overview
+
+ expect(subject).to be_success
+ end
+
+ it 'contains the same widgets' do
+ expect(overview.widgets.size).to eq original_overview.widgets.size
+
+ fields = %i(identifier options start_row end_row start_column end_column)
+
+ fields.each do |field|
+ expect(overview.widgets.map(&field)).to eq original_overview.widgets.map(&field)
+ end
+ end
+
+ context 'with references' do
+ describe 'to queries' do
+ let!(:query) do
+ FactoryBot.create(:public_query, project: source).tap do |query|
+ query.add_filter 'version_id', '=', [source_version.id.to_s]
+ query.add_filter 'category_id', '=', [source_category.id.to_s]
+ query.save!
+ end
+ end
+
+ let(:widget_data) do
+ [
+ [[1, 1, 1, 2], "work_packages_table", { "queryId" => query.id.to_s }]
+ ]
+ end
+
+ it 'copies the widgets and updates references to copied queries' do
+ expect(overview.widgets.size).to eq original_overview.widgets.size
+
+ overview.widgets.zip(original_overview.widgets).each do |widget, original_widget|
+ query = Query.find widget.options["queryId"]
+ original_query = Query.find original_widget.options["queryId"]
+
+ expect(query.id).not_to eq original_query.id
+ expect(query.name).to eq original_query.name
+
+ expect(query.filters.map(&:name)).to eq original_query.filters.map(&:name)
+ expect(query.filters.map(&:operator)).to eq original_query.filters.map(&:operator)
+ end
+ end
+
+ it 'updates references within the copied query' do
+ query_copy = Query.where(name: query.name, project: project_copy).first
+
+ expect(query_copy).to be_present
+
+ version_copy = Version.find query_copy.filters.find { |f| f.name == :version_id }.values.first
+
+ expect(version_copy.id).not_to eq source_version.id
+ expect(version_copy.name).to eq source_version.name
+
+ category_copy = Category.find query_copy.filters.find { |f| f.name == :category_id }.values.first
+
+ expect(category_copy.id).not_to eq source_category.id
+ expect(category_copy.name).to eq source_category.name
+ end
+ end
+ end
+ end
+ end
+ end
+end