diff --git a/app/models/changeset.rb b/app/models/changeset.rb
index 063a4a48c..29679fa7b 100644
--- a/app/models/changeset.rb
+++ b/app/models/changeset.rb
@@ -77,52 +77,40 @@ class Changeset < ActiveRecord::Base
scan_comment_for_issue_ids
end
+ TIMELOG_RE = /
+ (
+ (\d+([.,]\d+)?)h?
+ |
+ (\d+):(\d+)
+ |
+ ((\d+)(h|hours?))?((\d+)(m|min)?)?
+ )
+ /x
+
def scan_comment_for_issue_ids
return if comments.blank?
# keywords used to reference issues
ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
+ ref_keywords_any = ref_keywords.delete('*')
# keywords used to fix issues
fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
- return if kw_regexp.blank?
referenced_issues = []
- if ref_keywords.delete('*')
- # find any issue ID in the comments
- target_issue_ids = []
- comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] }
- referenced_issues += find_referenced_issues_by_id(target_issue_ids)
- end
-
- comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match|
- action = match[0]
- target_issue_ids = match[1].scan(/\d+/)
- target_issues = find_referenced_issues_by_id(target_issue_ids)
- if fix_keywords.include?(action.downcase) && fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
- # update status of issues
- logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
- target_issues.each do |issue|
- # the issue may have been updated by the closure of another one (eg. duplicate)
- issue.reload
- # don't change the status is the issue is closed
- next if issue.status.is_closed?
- csettext = "r#{self.revision}"
- if self.scmid && (! (csettext =~ /^r[0-9]+$/))
- csettext = "commit:\"#{self.scmid}\""
- end
- journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
- issue.status = fix_status
- unless Setting.commit_fix_done_ratio.blank?
- issue.done_ratio = Setting.commit_fix_done_ratio.to_i
- end
- Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
- { :changeset => self, :issue => issue })
- issue.save
+ comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
+ action, refs = match[2], match[3]
+ next unless action.present? || ref_keywords_any
+
+ refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
+ issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
+ if issue
+ referenced_issues << issue
+ fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
+ log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
end
end
- referenced_issues += target_issues
end
referenced_issues.uniq!
@@ -136,6 +124,15 @@ class Changeset < ActiveRecord::Base
def long_comments
@long_comments || split_comments.last
end
+
+ def text_tag
+ c = scmid? ? scmid : revision
+ if c.to_s =~ /^[0-9]*$/
+ "r#{c}"
+ else
+ "commit:#{c}"
+ end
+ end
# Returns the previous changeset
def previous
@@ -163,13 +160,64 @@ class Changeset < ActiveRecord::Base
private
- # Finds issues that can be referenced by the commit message
- # i.e. issues that belong to the repository project, a subproject or a parent project
- def find_referenced_issues_by_id(ids)
- return [] if ids.compact.empty?
- Issue.find_all_by_id(ids, :include => :project).select {|issue|
- project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
- }
+ # Finds an issue that can be referenced by the commit message
+ # i.e. an issue that belong to the repository project, a subproject or a parent project
+ def find_referenced_issue_by_id(id)
+ return nil if id.blank?
+ issue = Issue.find_by_id(id.to_i, :include => :project)
+ if issue
+ unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
+ issue = nil
+ end
+ end
+ issue
+ end
+
+ def fix_issue(issue)
+ status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
+ if status.nil?
+ logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
+ return issue
+ end
+
+ # the issue may have been updated by the closure of another one (eg. duplicate)
+ issue.reload
+ # don't change the status is the issue is closed
+ return if issue.status && issue.status.is_closed?
+
+ journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
+ issue.status = status
+ unless Setting.commit_fix_done_ratio.blank?
+ issue.done_ratio = Setting.commit_fix_done_ratio.to_i
+ end
+ Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
+ { :changeset => self, :issue => issue })
+ unless issue.save
+ logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
+ end
+ issue
+ end
+
+ def log_time(issue, hours)
+ time_entry = TimeEntry.new(
+ :user => user,
+ :hours => hours,
+ :issue => issue,
+ :spent_on => commit_date,
+ :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
+ )
+ time_entry.activity = log_time_activity unless log_time_activity.nil?
+
+ unless time_entry.save
+ logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
+ end
+ 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
diff --git a/app/views/settings/_repositories.rhtml b/app/views/settings/_repositories.rhtml
index 198b83289..9f2b26d45 100644
--- a/app/views/settings/_repositories.rhtml
+++ b/app/views/settings/_repositories.rhtml
@@ -31,6 +31,13 @@
<%= l(:label_applied_status) %>: <%= setting_select :commit_fix_status_id, [["", 0]] + IssueStatus.find(:all).collect{|status| [status.name, status.id.to_s]}, :label => false %>
<%= l(:field_done_ratio) %>: <%= setting_select :commit_fix_done_ratio, (0..10).to_a.collect {|r| ["#{r*10} %", "#{r*10}"] }, :blank => :label_no_change_option, :label => false %>
<%= l(:text_comma_separated) %>
<%= setting_check_box :commit_logtime_enabled, + :onclick => "if (this.checked) { Form.Element.enable('settings_commit_logtime_activity_id'); } else { Form.Element.disable('settings_commit_logtime_activity_id'); }"%>
+ +<%= setting_select :commit_logtime_activity_id, + [[l(:label_default), 0]] + TimeEntryActivity.shared.all.collect{|activity| [activity.name, activity.id.to_s]}, + :disabled => !Setting.commit_logtime_enabled?%>
<%= submit_tag l(:button_save) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 78654b1fb..e559d01c2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -354,6 +354,8 @@ en: setting_rest_api_enabled: Enable REST web service setting_cache_formatted_text: Cache formatted text setting_default_notification_option: Default notification option + setting_commit_logtime_enabled: Enable time logging + setting_commit_logtime_activity_id: Activity for logged time permission_add_project: Create project permission_add_subprojects: Create subprojects @@ -875,6 +877,7 @@ en: text_no_configuration_data: "Roles, trackers, issue statuses and workflow have not been configured yet.\nIt is highly recommended to load the default configuration. You will be able to modify it once loaded." text_load_default_configuration: Load the default configuration text_status_changed_by_changeset: "Applied in changeset {{value}}." + text_time_logged_by_changeset: "Applied in changeset {{value}}." text_issues_destroy_confirmation: 'Are you sure you want to delete the selected issue(s) ?' text_select_project_modules: 'Select modules to enable for this project:' text_default_administrator_account_changed: Default administrator account changed diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 350c38dbd..897c6312f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -358,6 +358,8 @@ fr: setting_gravatar_default: Image Gravatar par défaut setting_start_of_week: Jour de début des calendriers setting_cache_formatted_text: Mettre en cache le texte formaté + setting_commit_logtime_enabled: Permettre la saisie de temps + setting_commit_logtime_activity_id: Activité pour le temps saisi permission_add_project: Créer un projet permission_add_subprojects: Créer des sous-projets @@ -857,6 +859,7 @@ fr: text_no_configuration_data: "Les rôles, trackers, statuts et le workflow ne sont pas encore paramétrés.\nIl est vivement recommandé de charger le paramétrage par defaut. Vous pourrez le modifier une fois chargé." text_load_default_configuration: Charger le paramétrage par défaut text_status_changed_by_changeset: "Appliqué par commit {{value}}." + text_time_logged_by_changeset: "Appliqué par commit {{value}}" text_issues_destroy_confirmation: 'Êtes-vous sûr de vouloir supprimer le(s) demandes(s) selectionnée(s) ?' text_select_project_modules: 'Sélectionner les modules à activer pour ce projet :' text_default_administrator_account_changed: Compte administrateur par défaut changé diff --git a/config/settings.yml b/config/settings.yml index 2d32decb3..74647eb5a 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -98,6 +98,11 @@ commit_fix_status_id: default: 0 commit_fix_done_ratio: default: 100 +commit_logtime_enabled: + default: 0 +commit_logtime_activity_id: + format: int + default: 0 # autologin duration in days # 0 means autologin is disabled autologin: diff --git a/test/fixtures/changesets.yml b/test/fixtures/changesets.yml index 41f077a3f..a7d168a6e 100644 --- a/test/fixtures/changesets.yml +++ b/test/fixtures/changesets.yml @@ -24,7 +24,7 @@ changesets_003: id: 102 comments: |- A commit with wrong issue ids - IssueID 666 3 + IssueID #666 #3 repository_id: 10 committer: dlopper user_id: 3 diff --git a/test/unit/changeset_test.rb b/test/unit/changeset_test.rb index 8010383fe..612a4299b 100644 --- a/test/unit/changeset_test.rb +++ b/test/unit/changeset_test.rb @@ -44,6 +44,77 @@ class ChangesetTest < ActiveSupport::TestCase assert_equal 1, ActionMailer::Base.deliveries.size end + def test_ref_keywords + Setting.commit_ref_keywords = 'refs' + Setting.commit_fix_keywords = '' + + c = Changeset.new(:repository => Project.find(1).repository, + :committed_on => Time.now, + :comments => 'Ignores #2. Refs #1') + c.scan_comment_for_issue_ids + + assert_equal [1], c.issue_ids.sort + end + + def test_ref_keywords_any_only + Setting.commit_ref_keywords = '*' + Setting.commit_fix_keywords = '' + + c = Changeset.new(:repository => Project.find(1).repository, + :committed_on => Time.now, + :comments => 'Ignores #2. Refs #1') + c.scan_comment_for_issue_ids + + assert_equal [1, 2], c.issue_ids.sort + end + + def test_ref_keywords_any_with_timelog + Setting.commit_ref_keywords = '*' + Setting.commit_logtime_enabled = '1' + + c = Changeset.new(:repository => Project.find(1).repository, + :committed_on => 24.hours.ago, + :comments => 'Worked on this issue #1 @2h', + :scmid => '520', + :revision => '520', + :user => User.find(2)) + assert_difference 'TimeEntry.count' do + c.scan_comment_for_issue_ids + end + assert_equal [1], c.issue_ids.sort + + time = TimeEntry.first(:order => 'id desc') + assert_equal 1, time.issue_id + assert_equal 1, time.project_id + assert_equal 2, time.user_id + assert_equal 2.0, time.hours + assert_equal Date.yesterday, time.spent_on + assert time.activity.is_default? + assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}" + end + + def test_ref_keywords_closing_with_timelog + Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id + Setting.commit_ref_keywords = '*' + Setting.commit_fix_keywords = 'fixes , closes' + Setting.commit_logtime_enabled = '1' + + c = Changeset.new(:repository => Project.find(1).repository, + :committed_on => Time.now, + :comments => 'This is a comment. Fixes #1 @2.5, #2 @1', + :user => User.find(2)) + assert_difference 'TimeEntry.count', 2 do + c.scan_comment_for_issue_ids + end + + assert_equal [1, 2], c.issue_ids.sort + assert Issue.find(1).closed? + assert Issue.find(2).closed? + + times = TimeEntry.all(:order => 'id desc', :limit => 2) + assert_equal [1, 2], times.collect(&:issue_id).sort + end + def test_ref_keywords_any_line_start Setting.commit_ref_keywords = '*' @@ -99,6 +170,16 @@ class ChangesetTest < ActiveSupport::TestCase assert_equal [2], c.issue_ids.sort assert c.issues.first.project != c.project end + + def test_text_tag_numeric + c = Changeset.new(:scmid => '520', :revision => '520') + assert_equal 'r520', c.text_tag + end + + def test_text_tag_hash + c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518') + assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag + end def test_previous changeset = Changeset.find_by_revision('3')