redmine_tags icon indicating copy to clipboard operation
redmine_tags copied to clipboard

Broken issue copy functionality

Open martincizek opened this issue 4 years ago • 2 comments

When an issue with children is copied, the parent-child relation is lost.

Steps to reproduce:

  • Install redmine_tags
  • Create issue A with child issue B
  • Use "Copy" on issue A and submit
  • New issues A2 and B2 are created, but A2 is not a parent of B2

This issue is also detected by two of core Redmine tests:

  1. Error:
    IssuesControllerTest#test_create_as_copy_to_a_different_project_should_copy_subtask_custom_fields:
    ActiveRecord::StaleObjectError: Attempted to update a stale object: Issue.
        app/models/issue.rb:222:in `create_or_update'
        plugins/redmine_tags/lib/redmine_tags/hooks/model_issue_hook.rb:18:in `controller_issues_new_after_save'
        lib/redmine/hook.rb:66:in `block (2 levels) in call_hook'
        lib/redmine/hook.rb:66:in `each'
        lib/redmine/hook.rb:66:in `block in call_hook'
        lib/redmine/hook.rb:63:in `tap'
        lib/redmine/hook.rb:63:in `call_hook'
        lib/redmine/hook.rb:96:in `call_hook'
        app/controllers/issues_controller.rb:150:in `create'
        lib/redmine/sudo_mode.rb:61:in `sudo_mode'
        test/functional/issues_controller_test.rb:5383:in `block in test_create_as_copy_to_a_different_project_should_copy_subtask_custom_fields'
        test/functional/issues_controller_test.rb:5382:in `test_create_as_copy_to_a_different_project_should_copy_subtask_custom_fields'
    
    
    bin/rails test test/functional/issues_controller_test.rb:5378
    
  2. Error:
    IssuesControllerTest#test_create_as_copy_should_copy_subtasks:
    ActiveRecord::StaleObjectError: Attempted to update a stale object: Issue.
        app/models/issue.rb:222:in `create_or_update'
        plugins/redmine_tags/lib/redmine_tags/hooks/model_issue_hook.rb:18:in `controller_issues_new_after_save'
        lib/redmine/hook.rb:66:in `block (2 levels) in call_hook'
        lib/redmine/hook.rb:66:in `each'
        lib/redmine/hook.rb:66:in `block in call_hook'
        lib/redmine/hook.rb:63:in `tap'
        lib/redmine/hook.rb:63:in `call_hook'
        lib/redmine/hook.rb:96:in `call_hook'
        app/controllers/issues_controller.rb:150:in `create'
        lib/redmine/sudo_mode.rb:61:in `sudo_mode'
        test/functional/issues_controller_test.rb:5358:in `block in test_create_as_copy_should_copy_subtasks'
        test/functional/issues_controller_test.rb:5357:in `test_create_as_copy_should_copy_subtasks'
    
    
    bin/rails test test/functional/issues_controller_test.rb:5353
    

martincizek avatar May 03 '21 22:05 martincizek

A no-brainer fix would be as follows it works.

diff --git a/lib/redmine_tags/hooks/model_issue_hook.rb b/lib/redmine_tags/hooks/model_issue_hook.rb
index 4eb157f..abbc964 100755
--- a/lib/redmine_tags/hooks/model_issue_hook.rb
+++ b/lib/redmine_tags/hooks/model_issue_hook.rb
@@ -16,4 +16,8 @@ module RedmineTags
       def controller_issues_new_after_save(context = {})
         save_tags_to_issue context, false
         context[:issue].save
+      rescue ActiveRecord::StaleObjectError
+        context[:issue].reload
+        save_tags_to_issue context, false
+        context[:issue].save
       end

But it seems that the problematic controller_issues_new_after_save method is no more needed, so it would be wiser to remove it.

martincizek avatar May 03 '21 23:05 martincizek

I got similar error and PR #229 fixes it. Thanks!!

iquiw avatar Dec 07 '21 00:12 iquiw