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

Add cause message for closing duplicate

Oliver Günther315ace522721
diff --git a/app/models/journal.rb b/app/models/journal.rb
index 6a004db89db..5c372671a6f 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -82,6 +82,7 @@ class Journal < ApplicationRecord
work_package_parent_changed_times
work_package_predecessor_changed_times
work_package_related_changed_times
+ work_package_duplicate_closed
working_days_changed
].freeze
diff --git a/app/models/journal/caused_by_duplicate_work_package_close.rb b/app/models/journal/caused_by_duplicate_work_package_close.rb
new file mode 100644
index 00000000000..2779cadde6a
--- /dev/null
+++ b/app/models/journal/caused_by_duplicate_work_package_close.rb
@@ -0,0 +1,36 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journal::CausedByDuplicateWorkPackageClose < CauseOfChange::Base
+ def initialize(work_package:)
+ additional = {
+ "work_package_id" => work_package.id
+ }
+ super("work_package_duplicate_closed", additional)
+ end
+end
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 51d387b21e6..e02d14aa81d 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -646,20 +646,16 @@ class WorkPackage < ApplicationRecord
# Don't re-close it if it's already closed
next if duplicate.closed?
- # Implicitly creates a new journal
- duplicate.update_attribute :status, status
-
- override_last_journal_notes_and_user_of!(duplicate)
+ # Close the duplicate
+ close_duplicate(duplicate)
end
end
- def override_last_journal_notes_and_user_of!(other_work_package)
- journal = other_work_package.journals.last
- # Same user and notes
- journal.user = last_journal.user
- journal.notes = last_journal.notes
-
- journal.save
+ def close_duplicate(duplicate)
+ WorkPackages::UpdateService
+ .new(user: User.system, model: duplicate, contract_class: EmptyContract)
+ .call(status:, journal_cause: Journal::CausedByDuplicateWorkPackageClose.new(work_package: self))
+ .on_failure { |res| Rails.logger.error "Failed to close duplicate ##{duplicate.id} of ##{id}: #{res.message}" }
end
# Query generator for selecting groups of issue counts for a project
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 81979583af5..17be67f7c20 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1927,6 +1927,7 @@ en:
progress_mode_changed_to_status_based: "Progress calculation updated"
status_changed: "Status '%{status_name}'"
system_update: "OpenProject system update:"
+ work_package_duplicate_closed: "Duplicate work package updated:"
total_percent_complete_mode_changed_to_work_weighted_average: "Calculation of % Complete totals now weighted by Work."
total_percent_complete_mode_changed_to_simple_average: "Calculation of % Complete totals now based on a simple average of only % Complete values."
cause_descriptions:
@@ -1934,6 +1935,7 @@ en:
work_package_parent_changed_times: by changes to parent %{link}
work_package_children_changed_times: by changes to child %{link}
work_package_related_changed_times: by changes to related %{link}
+ work_package_duplicate_closed: The status was automatically updated by the duplicated work package %{link}
unaccessable_work_package_changed: by changes to a related work package
working_days_changed:
changed: "by changes to working days (%{changes})"
diff --git a/spec/models/work_packages/work_package_duplicates_spec.rb b/spec/models/work_packages/work_package_duplicates_spec.rb
new file mode 100644
index 00000000000..e990fec9356
--- /dev/null
+++ b/spec/models/work_packages/work_package_duplicates_spec.rb
@@ -0,0 +1,60 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe WorkPackage, "duplicates/duplicated_by relations" do
+ create_shared_association_defaults_for_work_package_factory
+
+ shared_let(:work_package) { create(:work_package, subject: "original") }
+ shared_let(:duplicate) { create(:work_package, subject: "duplicate") }
+ shared_let(:relation) do
+ create(:relation,
+ from: duplicate,
+ to: work_package,
+ relation_type: Relation::TYPE_DUPLICATES)
+ end
+
+ context "when work package is closed" do
+ let(:closed_status) { create(:closed_status) }
+
+ it "updates the duplicate" do
+ expect(duplicate.status).not_to eq(closed_status)
+
+ work_package.status = closed_status
+ work_package.save!
+ duplicate.reload
+
+ expect(duplicate.status).to eq(closed_status)
+ journal = duplicate.journals.last
+
+ expect(journal.user).to eq(User.system)
+ expect(journal.cause).to eq("type" => "work_package_duplicate_closed", "work_package_id" => work_package.id)
+ end
+ end
+end