Project

General

Profile

Actions

Defect #41502

open

Custom field values for versions are not copied when copying a project that includes versions

Added by Minoru Maeda about 2 months ago. Updated about 1 month ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Affected version:

Description

When copying a project that includes versions, the versions themselves are copied, but any values entered in the custom fields for those versions are not.

Steps to Reproduce:

1. Go to "Custom fields » Versions » New custom field" and create a custom field (e.g., text format) for versions.
2. Create a version for a project.
3. Enter a value in the custom field for the version and save it.
4. Navigate to "Administration > Projects" and click "Copy" in the "Action" column for the project. In the "New project" screen, select "Versions (N) and Files" to include versions in the copy.
5. While the version itself is copied, the custom field values entered for the original version are not copied.

Redmine Infomation:

https://svn.redmine.org/redmine/trunk@23140

Environment:
  Redmine version                5.1.3.devel
  Ruby version                   3.2.5-p208 (2024-07-26) [aarch64-linux]
  Rails version                  7.2.1.1
  Environment                    development
  Database adapter               PostgreSQL
  Mailer queue                   ActiveJob::QueueAdapters::AsyncAdapter
  Mailer delivery                smtp
Redmine settings:
  Redmine theme                  Default
SCM:
  Subversion                     1.14.2
  Mercurial                      6.3.2
  Bazaar                         3.3.2
  Git                            2.46.1
  Filesystem                     
Redmine plugins:
  no plugin installed

Files

fix-41502.patch (1.55 KB) fix-41502.patch Takenori TAKAKI, 2024-10-29 06:15
Actions #1

Updated by Takenori TAKAKI about 2 months ago

I've created a patch to fix this problem and attach it.
Confirmed it works on the latest trunk.
Minoru Maeda, please review the patch!

Actions #2

Updated by Minoru Maeda about 1 month ago

Takenori TAKAKI wrote in #note-1:

I've created a patch to fix this problem and attach it.
Confirmed it works on the latest trunk.
Minoru Maeda, please review the patch!

Thank you for the patch. I applied the patch you provided to trunk@23170 and confirmed it works as expected.
When copying the project, I was able to confirm that custom field values in the "Text" and "List" formats for versions were also carried over! This is fantastic!

I think it works well as it is, but I noticed two points:

1. Custom fields in the "File" format were not copied.
2. For the "Version" and "User" formats, if there were values that do not exist in the target project, the version itself was not copied.

For the first point, it seems necessary to copy the Attachment record because the container is not matching in Redmine::FieldFormat::AttachmentFormat#set_custom_field_value .
As for the second point, I could only think of the idea of logging it, similar to Project.copy_issues .

Using TAKAKI's patch as a base, I created a response proposal for the points mentioned above, but I still think TAKAKI's original patch is preferable for its simplicity and sufficient functionality.

diff --git a/app/models/project.rb b/app/models/project.rb
index c438be16d..778c4d1cf 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1120,7 +1120,25 @@ class Project < ApplicationRecord
         attachment.copy(:container => new_version)
       end

+      new_version.custom_field_values = version.custom_field_values.inject({}) do |h, v|
+        if v.custom_field.field_format == 'attachment' && v.value.present?
+          attachment = v.custom_field.cast_value(v.value)
+
+          new_version_attachment = attachment.copy(container: new_version)
+          new_version_attachment.save
+          h[v.custom_field_id] = new_version_attachment.id
+        else
+          h[v.custom_field_id] = v.value
+        end
+
+        h
+      end
+
       self.versions << new_version
+
+      if new_version.new_record?
+        logger&.info("Project#copy_versions: #{new_version.name} could not be copied: #{new_version.errors.full_messages}")
+      end
     end
   end

Actions #3

Updated by Go MAEDA about 1 month ago

  • Target version set to Candidate for next minor release
Actions

Also available in: Atom PDF