From 64ede1f986e8bdbc5ce59515190f004de36151ba Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 22 Mar 2022 18:47:40 +0800 Subject: [PATCH] allow imported time entries to override the selected project with the actual project of their issue --- app/models/time_entry_import.rb | 8 ++++++-- test/unit/time_entry_import_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/time_entry_import.rb b/app/models/time_entry_import.rb index 27b9e040f..3f4fdbbc6 100644 --- a/app/models/time_entry_import.rb +++ b/app/models/time_entry_import.rb @@ -105,17 +105,21 @@ class TimeEntryImport < Import end attributes = { - :project_id => project.id, :activity_id => activity_id, :author_id => user.id, :user_id => user_id, - :issue_id => row_value(row, 'issue_id'), :spent_on => row_date(row, 'spent_on'), :hours => row_value(row, 'hours'), :comments => row_value(row, 'comments') } + if issue_id = row_value(row, 'issue_id').presence + attributes[:issue_id] = issue_id + else + attributes[:project_id] = project.id + end + attributes['custom_field_values'] = object.custom_field_values.inject({}) do |h, v| value = case v.custom_field.field_format diff --git a/test/unit/time_entry_import_test.rb b/test/unit/time_entry_import_test.rb index f0ec41cb7..36e7f574d 100644 --- a/test/unit/time_entry_import_test.rb +++ b/test/unit/time_entry_import_test.rb @@ -165,6 +165,28 @@ class TimeEntryImportTest < ActiveSupport::TestCase assert_equal 2, fourth.user_id end + def test_imports_timelogs_for_issues_in_other_project + import = generate_import + import.settings = { + 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8', + 'mapping' => { + 'project_id' => '3', + 'activity' => 'value:10', + 'issue_id' => '1', + 'spent_on' => '2', + 'hours' => '3', + 'comments' => '4', + 'user' => '7' + } + } + import.save! + first, second, third, fourth = new_records(TimeEntry, 4) {import.run} + assert_equal 3, first.project_id + assert_equal 3, second.project_id + assert_equal 1, third.project_id + assert_equal 1, fourth.project_id + end + protected def generate_import(fixture_name='import_time_entries.csv') -- 2.20.1