From 17fe7b28e8f3b482d22e7c43d8df8725b847aedf Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 23 Oct 2018 17:19:14 +0800 Subject: [PATCH] fix logging time via a commit message for project specific activities - in case the issue's project has a project specific activity which is a child of the configured activity to use for such time log entries, the timelog could not be saved. --- app/models/changeset.rb | 10 +++------- app/models/project.rb | 10 ++++++++++ test/unit/changeset_test.rb | 31 ++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/models/changeset.rb b/app/models/changeset.rb index 6a0b42df8..0907c6703 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -259,7 +259,9 @@ class Changeset < ActiveRecord::Base :comments => l(:text_time_logged_by_changeset, :value => text_tag(issue.project), :locale => Setting.default_language) ) - time_entry.activity = log_time_activity unless log_time_activity.nil? + if activity = issue.project.commit_logtime_activity + time_entry.activity = activity + end unless time_entry.save logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger @@ -267,12 +269,6 @@ class Changeset < ActiveRecord::Base time_entry end - def log_time_activity - if Setting.commit_logtime_activity_id.to_i > 0 - TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i) - end - end - def split_comments comments =~ /\A(.+?)\r?\n(.*)$/m @short_comments = $1 || comments diff --git a/app/models/project.rb b/app/models/project.rb index baeeb8e3e..d640d5df9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -295,6 +295,16 @@ class Project < ActiveRecord::Base end end + # returns the time log activity to be used when logging time via a changeset + def commit_logtime_activity + activity_id = Setting.commit_logtime_activity_id.to_i + if activity_id > 0 + activities + .where('id = ? OR parent_id = ?', activity_id, activity_id) + .first + end + end + # Returns a :conditions SQL string that can be used to find the issues associated with this project. # # Examples: diff --git a/test/unit/changeset_test.rb b/test/unit/changeset_test.rb index 982ab62d3..8f0f845c8 100644 --- a/test/unit/changeset_test.rb +++ b/test/unit/changeset_test.rb @@ -75,9 +75,38 @@ class ChangesetTest < ActiveSupport::TestCase assert_equal [1, 2], c.issue_ids.sort end + def test_project_specific_activity + project = Project.find 1 + activity = TimeEntryActivity.find 9 + + Setting.commit_ref_keywords = '*' + Setting.commit_logtime_enabled = '1' + Setting.commit_logtime_activity_id = activity.id + + project_specific_activity = TimeEntryActivity.create!( + name: activity.name, + parent_id: activity.id, + position: activity.position, + project_id: project.id + ) + + c = Changeset.new(:repository => project.repository, + :committed_on => 24.hours.ago, + :comments => "Worked on this issue #1 @8h", + :revision => '520', + :user => User.find(2)) + assert_difference 'TimeEntry.count' do + c.scan_comment_for_issue_ids + end + + time = TimeEntry.order('id desc').first + assert_equal project_specific_activity, time.activity + end + def test_ref_keywords_any_with_timelog Setting.commit_ref_keywords = '*' Setting.commit_logtime_enabled = '1' + Setting.commit_logtime_activity_id = 9 { '2' => 2.0, @@ -113,7 +142,7 @@ class ChangesetTest < ActiveSupport::TestCase "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}" ) assert_equal Date.yesterday, time.spent_on - assert time.activity.is_default? + assert_equal 9, time.activity_id assert( time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}" -- 2.20.1