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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Update work packages when changing to status-based mode
Christophe Bliarde15e00db1db3
diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rbindex 57803c14c93..e673ecd71b7 100644--- a/app/controllers/statuses_controller.rb+++ b/app/controllers/statuses_controller.rb@@ -110,8 +110,9 @@ class StatusesController < ApplicationControllerreturn unless WorkPackage.use_status_for_done_ratio?return unless @status.default_done_ratio_previously_changed?- WorkPackages::ApplyStatusPCompleteChangeJob- .perform_later(status_name: @status.name,+ WorkPackages::ApplyStatusesPCompleteJob+ .perform_later(cause_type: "status_p_complete_changed",+ status_name: @status.name,status_id: @status.id,change: @status.default_done_ratio_previous_change)enddiff --git a/app/models/journal.rb b/app/models/journal.rbindex faffeef1118..10c38d252fe 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -77,6 +77,7 @@ class Journal < ApplicationRecordprefix: trueVALID_CAUSE_TYPES = %w[default_attribute_written+ progress_mode_changed_to_status_basedstatus_p_complete_changedsystem_updatework_package_children_changed_timesdiff --git a/app/services/settings/update_service.rb b/app/services/settings/update_service.rbindex 617aa26ed1c..b959f235453 100644--- a/app/services/settings/update_service.rb+++ b/app/services/settings/update_service.rb@@ -42,7 +42,12 @@ class Settings::UpdateService < BaseServices::BaseContractedprivatedef set_setting_value(name, value)- Setting[name] = derive_value(value)+ old_value = Setting[name]+ new_value = derive_value(value)+ Setting[name] = new_value+ if name == :work_package_done_ratio && old_value != "status" && new_value == "status"+ WorkPackages::ApplyStatusesPCompleteJob.perform_later(cause_type: "progress_mode_changed_to_status_based")+ endenddef derive_value(value)diff --git a/app/workers/work_packages/apply_status_p_complete_change_job.rb b/app/workers/work_packages/apply_statuses_p_complete_job.rbsimilarity index 80%rename from app/workers/work_packages/apply_status_p_complete_change_job.rbrename to app/workers/work_packages/apply_statuses_p_complete_job.rbindex 86688519ed1..ccb51bc7636 100644--- a/app/workers/work_packages/apply_status_p_complete_change_job.rb+++ b/app/workers/work_packages/apply_statuses_p_complete_job.rb@@ -29,34 +29,64 @@#++# rubocop:disable Rails/SquishedSQLHeredocs-class WorkPackages::ApplyStatusPCompleteChangeJob < ApplicationJob+class WorkPackages::ApplyStatusesPCompleteJob < ApplicationJobqueue_with_priority :default- # Updates % complete and remaining work of all work packages after a status- # % complete value has been changed.+ VALID_CAUSE_TYPES = %w[+ progress_mode_changed_to_status_based+ status_p_complete_changed+ ].freeze++ # Updates % complete and remaining work of all work packages after a status %+ # complete value has been changed or the progress calculation mode was set to+ # status-based.## It creates a journal entry with the System user describing the change.#+ # @param status_name [String] The cause of the update to be put in the journal+ # entry. Must be one of `VALID_CAUSE_TYPES`.# @param status_name [String] The name of the status to apply.# @param status_id [Integer] The ID of the status to apply. Not used# currently, but here in case we need it in a later version.# @param change [Object] The change object containing an array of [old, new]# values of the change.# @return [void]- def perform(status_name:, status_id:, change:)+ def perform(cause_type:, status_name: nil, status_id: nil, change: nil)return if WorkPackage.use_field_for_done_ratio?+ journal_cause = journal_cause_from(cause_type, status_name:, status_id:, change:)+with_temporary_progress_table doset_p_complete_from_statusderive_remaining_work_from_work_and_p_completeupdate_totals- copy_progress_values_to_work_packages_and_update_journals(status_name:, status_id:, change:)+ copy_progress_values_to_work_packages_and_update_journals(journal_cause)endendprivate+ def journal_cause_from(cause_type, status_name:, status_id:, change:)+ if VALID_CAUSE_TYPES.exclude?(cause_type)+ raise ArgumentError, "Invalid cause type #{cause_type.inspect}. " \+ "Valid values are #{VALID_CAUSE_TYPES.inspect}"+ end++ case cause_type+ when "progress_mode_changed_to_status_based"+ { type: cause_type }+ when "status_p_complete_changed"+ raise ArgumentError, "status_name must be provided" if status_name.blank?+ raise ArgumentError, "status_id must be provided" if status_id.nil?+ raise ArgumentError, "change must be provided" if change.nil?++ { type: cause_type, status_name:, status_id:, status_p_complete_change: change }+ else+ raise "Unable to handle cause type #{cause_type.inspect}"+ end+ end+def with_temporary_progress_tablecreate_temporary_progress_tableyield@@ -125,9 +155,9 @@ class WorkPackages::ApplyStatusPCompleteChangeJob < ApplicationJobSQLend- def copy_progress_values_to_work_packages_and_update_journals(status_name:, status_id:, change:)+ def copy_progress_values_to_work_packages_and_update_journals(cause)updated_work_package_ids = copy_progress_values_to_work_packages- create_journals_for_updated_work_packages(updated_work_package_ids, status_name:, status_id:, change:)+ create_journals_for_updated_work_packages(updated_work_package_ids, cause:)enddef copy_progress_values_to_work_packages@@ -152,8 +182,7 @@ class WorkPackages::ApplyStatusPCompleteChangeJob < ApplicationJobresults.column_values(0)end- def create_journals_for_updated_work_packages(updated_work_package_ids, status_name:, status_id:, change:)- cause = { type: "status_p_complete_changed", status_name:, status_id:, status_p_complete_change: change }+ def create_journals_for_updated_work_packages(updated_work_package_ids, cause:)WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|Journals::CreateService.new(work_package, system_user).call(cause:)diff --git a/config/locales/en.yml b/config/locales/en.ymlindex c0f663b6b5e..3c930d65152 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1733,9 +1733,10 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}changes_retracted: "The changes were retracted."caused_changes:+ dates_changed: "Dates changed"default_attribute_written: "Read-only attributes written"+ progress_mode_changed_to_status_based: "Progress calculation updated"status_p_complete_changed: "Status % Complete changed"- dates_changed: "Dates changed"system_update: "OpenProject system update:"cause_descriptions:@@ -1752,6 +1753,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}dates:working: "%{date} is now working"non_working: "%{date} is now non-working"+ progress_mode_changed_to_status_based: Progress calculation mode set to status-basedstatus_p_complete_changed: >-% complete value for status '%{status_name}' changed from %{old_value}% to %{new_value}%system_update:diff --git a/lib/open_project/journal_formatter/cause.rb b/lib/open_project/journal_formatter/cause.rbindex cef5067f11c..88af8990230 100644--- a/lib/open_project/journal_formatter/cause.rb+++ b/lib/open_project/journal_formatter/cause.rb@@ -66,6 +66,8 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::Baseworking_days_changed_message(cause["changed_days"])when "status_p_complete_changed"status_p_complete_changed_message(cause, html)+ when "progress_mode_changed_to_status_based"+ progress_mode_changed_to_status_based_messageelserelated_work_package_changed_message(cause, html)end@@ -103,6 +105,10 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::BaseI18n.t("journals.cause_descriptions.status_p_complete_changed", status_name:, old_value:, new_value:)end+ def progress_mode_changed_to_status_based_message+ I18n.t("journals.cause_descriptions.progress_mode_changed_to_status_based")+ end+def related_work_package_changed_message(cause, html)related_work_package = WorkPackage.includes(:project).visible(User.current).find_by(id: cause["work_package_id"])diff --git a/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb b/spec/controllers/admin/settings/work_packages_settings_controller_spec.rbindex 03a3f0c9848..6670c46c77b 100644--- a/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb+++ b/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb@@ -31,10 +31,70 @@require "spec_helper"-RSpec.describe Admin::Settings::WorkPackagesSettingsController do # rubocop:disable RSpec/EmptyExampleGroup+RSpec.describe Admin::Settings::WorkPackagesSettingsController doshared_let(:user) { create(:admin) }current_user { user }require_admin_and_render_template("work_packages_settings")++ context "when changing progress calculation from work-based to status-based" do+ shared_let(:status) { create(:status, default_done_ratio: 42) }+ shared_let(:work_package) { create(:work_package, status:, done_ratio: 10, estimated_hours: 100) }++ before do+ Setting.work_package_done_ratio = "field"+ end++ it "starts a job to update work packages % complete and remaining work values" do+ patch "update",+ params: {+ settings: {+ work_package_done_ratio: "status"+ }+ }+ expect(Setting.work_package_done_ratio).to eq("status")+ expect(WorkPackages::ApplyStatusesPCompleteJob)+ .to have_been_enqueued.with(cause_type: "progress_mode_changed_to_status_based")++ perform_enqueued_jobs++ expect(work_package.reload.read_attribute(:done_ratio)).to eq(status.default_done_ratio)+ expect(work_package.last_journal.details["cause"]).to eq([nil, { "type" => "progress_mode_changed_to_status_based" }])+ end+ end++ context "when sending path request to change progress calculation from status-based to status-based" do+ before do+ Setting.work_package_done_ratio = "status"+ end++ it "does not start a job" do+ patch "update",+ params: {+ settings: {+ work_package_done_ratio: "status"+ }+ }+ expect(WorkPackages::ApplyStatusesPCompleteJob)+ .not_to have_been_enqueued+ end+ end++ context "when changing progress calculation from status-based to work-based" do+ before do+ Setting.work_package_done_ratio = "status"+ end++ it "does not start a job" do+ patch "update",+ params: {+ settings: {+ work_package_done_ratio: "field"+ }+ }+ expect(WorkPackages::ApplyStatusesPCompleteJob)+ .not_to have_been_enqueued+ end+ endenddiff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rbindex d76285c1a71..08c305fe872 100644--- a/spec/controllers/statuses_controller_spec.rb+++ b/spec/controllers/statuses_controller_spec.rb@@ -143,7 +143,7 @@ RSpec.describe StatusesController doit "does not start any jobs to update work packages % complete values" doexpect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)- expect(WorkPackages::ApplyStatusPCompleteChangeJob)+ expect(WorkPackages::ApplyStatusesPCompleteJob).not_to have_been_enqueuedendend@@ -158,8 +158,9 @@ RSpec.describe StatusesController doit "starts a job to update work packages % complete values" doold_default_done_ratio = status.default_done_ratioexpect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)- expect(WorkPackages::ApplyStatusPCompleteChangeJob)- .to have_been_enqueued.with(status_name: status.name,+ expect(WorkPackages::ApplyStatusesPCompleteJob)+ .to have_been_enqueued.with(cause_type: "status_p_complete_changed",+ status_name: status.name,status_id: status.id,change: [old_default_done_ratio, new_default_done_ratio])@@ -174,7 +175,7 @@ RSpec.describe StatusesController dolet(:status_params) { { default_done_ratio: status.default_done_ratio } }it "does not start any jobs" do- expect(WorkPackages::ApplyStatusPCompleteChangeJob)+ expect(WorkPackages::ApplyStatusesPCompleteJob).not_to have_been_enqueuedendend@@ -183,7 +184,7 @@ RSpec.describe StatusesController dolet(:status_params) { { name: "Another status name" } }it "does not start any jobs" do- expect(WorkPackages::ApplyStatusPCompleteChangeJob)+ expect(WorkPackages::ApplyStatusesPCompleteJob).not_to have_been_enqueuedendenddiff --git a/spec/lib/journal_formatter/cause_spec.rb b/spec/lib/journal_formatter/cause_spec.rbindex 67a16882d3a..1718a08d85a 100644--- a/spec/lib/journal_formatter/cause_spec.rb+++ b/spec/lib/journal_formatter/cause_spec.rb@@ -333,6 +333,32 @@ RSpec.describe OpenProject::JournalFormatter::Cause doendend+ context "when a change of progress calculation mode to status-based is the cause" do+ let(:cause) do+ {+ "type" => "progress_mode_changed_to_status_based"+ }+ end++ context "when rendering HTML variant" do+ let(:html) { true }++ it do+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.progress_mode_changed_to_status_based')}</strong> " \+ "Progress calculation mode set to status-based"+ end+ end++ context "when rendering raw variant" do+ let(:html) { false }++ it do+ expect(subject).to eq "#{I18n.t('journals.caused_changes.progress_mode_changed_to_status_based')} " \+ "Progress calculation mode set to status-based"+ end+ end+ end+context "when the change was caused by a system update" dolet(:cause) do{diff --git a/spec/workers/work_packages/apply_status_p_complete_change_job_spec.rb b/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rbsimilarity index 79%rename from spec/workers/work_packages/apply_status_p_complete_change_job_spec.rbrename to spec/workers/work_packages/apply_statuses_p_complete_job_spec.rbindex 32ae1c53582..530c3183bf7 100644--- a/spec/workers/work_packages/apply_status_p_complete_change_job_spec.rb+++ b/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rb@@ -28,7 +28,7 @@require "rails_helper"-RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob do+RSpec.describe WorkPackages::ApplyStatusesPCompleteJob doshared_let(:author) { create(:user) }shared_let(:priority) { create(:priority, name: "Normal") }shared_let(:project) { create(:project, name: "Main project") }@@ -48,10 +48,14 @@ RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob dosubject(:job) { described_class }- def expect_performing_job_changes(from:, to:, status_name: "New", status_id: 99, change: [33, 66])+ def expect_performing_job_changes(from:, to:,+ cause_type: "status_p_complete_changed",+ status_name: "New",+ status_id: 99,+ change: [33, 66])table = create_table(from)- job.perform_now(status_name:, status_id:, change:)+ job.perform_now(cause_type:, status_name:, status_id:, change:)table.work_packages.map(&:reload)expect_work_packages(table.work_packages, to)@@ -153,7 +157,7 @@ RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob dodescribe "journals" do# rubocop:disable RSpec/ExampleLength- it "creates journal entries for modified work packages" do+ it "creates journal entries for modified work packages on status % complete change" doparent, child1, child2 = expect_performing_job_changes(from: <<~TABLE,hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete@@ -167,6 +171,7 @@ RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob dochild 1 | Doing (40%) | 10h | 6h | 40% | 10h | 6h | 40%child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%TABLE+ cause_type: "status_p_complete_changed",status_name: status_40p_doing.name,status_id: status_40p_doing.id,change: [20, 40]@@ -185,6 +190,33 @@ RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob doexpect(child2.journals.count).to eq 1end# rubocop:enable RSpec/ExampleLength++ it "creates journal entries for modified work packages on progress calculation mode set to status-based" do+ parent, child1, child2 = expect_performing_job_changes(+ from: <<~TABLE,+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ parent | To do (0%) | | | 0% | 20h | 8h | 60%+ child 1 | Doing (40%) | 10h | 8h | 20% | 10h | 8h | 20%+ child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ TABLE+ to: <<~TABLE,+ subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ parent | To do (0%) | | | 0% | 20h | 6h | 70%+ child 1 | Doing (40%) | 10h | 6h | 40% | 10h | 6h | 40%+ child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ TABLE+ cause_type: "progress_mode_changed_to_status_based"+ )+ [parent, child1].each do |work_package|+ expect(work_package.journals.count).to eq 2+ last_journal = work_package.last_journal+ expect(last_journal.user).to eq(User.system)+ expect(last_journal.cause_type).to eq("progress_mode_changed_to_status_based")+ end++ # unchanged => no new journals+ expect(child2.journals.count).to eq 1+ endendendend