]> source.dussan.org Git - redmine.git/commitdiff
Time Entry Import fails to import custom fields with "User" format (#38254).
authorGo MAEDA <maeda@farend.jp>
Thu, 16 Feb 2023 00:31:29 +0000 (00:31 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 16 Feb 2023 00:31:29 +0000 (00:31 +0000)
Patch by Jens Krämer.

git-svn-id: https://svn.redmine.org/redmine/trunk@22112 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/time_entry_import.rb
test/unit/time_entry_import_test.rb

index 1659e6efa8d687c084334ea27d6a9fc407022049..a6dfee53cc8f8488217242c9bcb5c9060696fbcf 100644 (file)
@@ -116,8 +116,10 @@ class TimeEntryImport < Import
 
     if issue_id = row_value(row, 'issue_id').presence
       attributes[:issue_id] = issue_id
+      object.project = issue_project(issue_id)
     else
       attributes[:project_id] = project.id
+      object.project = project
     end
 
     attributes['custom_field_values'] = object.custom_field_values.inject({}) do |h, v|
@@ -137,4 +139,10 @@ class TimeEntryImport < Import
     object.send(:safe_attributes=, attributes, user)
     object
   end
+
+  def issue_project(issue_id)
+    if issue_project_id = Issue.where(id: issue_id).limit(1).pick(:project_id)
+      (@projects_cache ||= {})[issue_project_id] ||= allowed_target_projects.find_by_id(issue_project_id)
+    end
+  end
 end
index 3766f12cb7d841a2525df9dfc05d9a20acdb9ab4..f2feb275c2e30052188d685d1adec15efa54b10c 100644 (file)
@@ -187,6 +187,33 @@ class TimeEntryImportTest < ActiveSupport::TestCase
     assert_equal 1, fourth.project_id
   end
 
+  def test_imports_custom_field_with_user_format
+    cf = TimeEntryCustomField.create! name: 'User Field', field_format: 'user'
+    import = generate_import
+    import.settings = {
+      'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
+      'mapping' => {
+        'project_id' => '1',
+        'activity'   => 'value:10',
+        'issue_id'   => '1',
+        'spent_on'   => '2',
+        'hours'      => '3',
+        'comments'   => '4',
+        'user'       => '7',
+        "cf_#{cf.id}" => '7'
+      }
+    }
+    import.save!
+    first, second, third, fourth = new_records(TimeEntry, 4) {import.run}
+    jsmith = User.find_by_login 'jsmith'
+    dlopper = User.find_by_login 'dlopper'
+
+    assert_equal dlopper.id, third.custom_values.where(custom_field: cf.id).first.value.to_i
+    assert_equal jsmith.id, fourth.custom_values.where(custom_field: cf.id).first.value.to_i
+    assert_equal jsmith.id, first.custom_values.where(custom_field: cf.id).first.value.to_i
+    assert_equal jsmith.id, second.custom_values.where(custom_field: cf.id).first.value.to_i
+  end
+
   protected
 
   def generate_import(fixture_name='import_time_entries.csv')