Skip to content

Commit

Permalink
Merge pull request #16949 from opf/fix/duplicate-cause
Browse files Browse the repository at this point in the history
Add cause message for closing duplicate
  • Loading branch information
cbliard authored Oct 15, 2024
2 parents c0d66f0 + 315ace5 commit 90b66bf
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/models/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions app/models/journal/caused_by_duplicate_work_package_close.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 7 additions & 11 deletions app/models/work_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,20 +646,16 @@ def close_duplicates
# 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
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1920,13 +1920,15 @@ 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:
work_package_predecessor_changed_times: by changes to predecessor %{link}
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})"
Expand Down
60 changes: 60 additions & 0 deletions spec/models/work_packages/work_package_duplicates_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 90b66bf

Please sign in to comment.