##// END OF EJS Templates
Fixes syntax for time logging in commit messages (#7630, #7718)....
Jean-Philippe Lang -
r4831:2d115bbe7071
parent child
Show More
@@ -1,272 +1,274
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'iconv'
19 19
20 20 class Changeset < ActiveRecord::Base
21 21 belongs_to :repository
22 22 belongs_to :user
23 23 has_many :changes, :dependent => :delete_all
24 24 has_and_belongs_to_many :issues
25 25
26 26 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.format_identifier}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
27 27 :description => :long_comments,
28 28 :datetime => :committed_on,
29 29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}}
30 30
31 31 acts_as_searchable :columns => 'comments',
32 32 :include => {:repository => :project},
33 33 :project_key => "#{Repository.table_name}.project_id",
34 34 :date_column => 'committed_on'
35 35
36 36 acts_as_activity_provider :timestamp => "#{table_name}.committed_on",
37 37 :author_key => :user_id,
38 38 :find_options => {:include => [:user, {:repository => :project}]}
39 39
40 40 validates_presence_of :repository_id, :revision, :committed_on, :commit_date
41 41 validates_uniqueness_of :revision, :scope => :repository_id
42 42 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
43 43
44 44 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
45 45 :conditions => Project.allowed_to_condition(args.first || User.current, :view_changesets) } }
46 46
47 47 def revision=(r)
48 48 write_attribute :revision, (r.nil? ? nil : r.to_s)
49 49 end
50 50
51 51 # Returns the identifier of this changeset; depending on repository backends
52 52 def identifier
53 53 if repository.class.respond_to? :changeset_identifier
54 54 repository.class.changeset_identifier self
55 55 else
56 56 revision.to_s
57 57 end
58 58 end
59 59
60 60 def comments=(comment)
61 61 write_attribute(:comments, Changeset.normalize_comments(comment))
62 62 end
63 63
64 64 def committed_on=(date)
65 65 self.commit_date = date
66 66 super
67 67 end
68 68
69 69 # Returns the readable identifier
70 70 def format_identifier
71 71 if repository.class.respond_to? :format_changeset_identifier
72 72 repository.class.format_changeset_identifier self
73 73 else
74 74 identifier
75 75 end
76 76 end
77 77
78 78 def committer=(arg)
79 79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
80 80 end
81 81
82 82 def project
83 83 repository.project
84 84 end
85 85
86 86 def author
87 87 user || committer.to_s.split('<').first
88 88 end
89 89
90 90 def before_create
91 91 self.user = repository.find_committer_user(committer)
92 92 end
93 93
94 94 def after_create
95 95 scan_comment_for_issue_ids
96 96 end
97 97
98 98 TIMELOG_RE = /
99 99 (
100 (\d+([.,]\d+)?)h?
100 ((\d+)(h|hours?))((\d+)(m|min)?)?
101 |
102 ((\d+)(h|hours?|m|min))
101 103 |
102 104 (\d+):(\d+)
103 105 |
104 ((\d+)(h|hours?))?((\d+)(m|min)?)?
106 (\d+([\.,]\d+)?)h?
105 107 )
106 108 /x
107 109
108 110 def scan_comment_for_issue_ids
109 111 return if comments.blank?
110 112 # keywords used to reference issues
111 113 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
112 114 ref_keywords_any = ref_keywords.delete('*')
113 115 # keywords used to fix issues
114 116 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
115 117
116 118 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
117 119
118 120 referenced_issues = []
119 121
120 122 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
121 123 action, refs = match[2], match[3]
122 124 next unless action.present? || ref_keywords_any
123 125
124 126 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
125 127 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
126 128 if issue
127 129 referenced_issues << issue
128 130 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
129 131 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
130 132 end
131 133 end
132 134 end
133 135
134 136 referenced_issues.uniq!
135 137 self.issues = referenced_issues unless referenced_issues.empty?
136 138 end
137 139
138 140 def short_comments
139 141 @short_comments || split_comments.first
140 142 end
141 143
142 144 def long_comments
143 145 @long_comments || split_comments.last
144 146 end
145 147
146 148 def text_tag
147 149 if scmid?
148 150 "commit:#{scmid}"
149 151 else
150 152 "r#{revision}"
151 153 end
152 154 end
153 155
154 156 # Returns the previous changeset
155 157 def previous
156 158 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
157 159 end
158 160
159 161 # Returns the next changeset
160 162 def next
161 163 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
162 164 end
163 165
164 166 # Strips and reencodes a commit log before insertion into the database
165 167 def self.normalize_comments(str)
166 168 to_utf8(str.to_s.strip)
167 169 end
168 170
169 171 # Creates a new Change from it's common parameters
170 172 def create_change(change)
171 173 Change.create(:changeset => self,
172 174 :action => change[:action],
173 175 :path => change[:path],
174 176 :from_path => change[:from_path],
175 177 :from_revision => change[:from_revision])
176 178 end
177 179
178 180 private
179 181
180 182 # Finds an issue that can be referenced by the commit message
181 183 # i.e. an issue that belong to the repository project, a subproject or a parent project
182 184 def find_referenced_issue_by_id(id)
183 185 return nil if id.blank?
184 186 issue = Issue.find_by_id(id.to_i, :include => :project)
185 187 if issue
186 188 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
187 189 issue = nil
188 190 end
189 191 end
190 192 issue
191 193 end
192 194
193 195 def fix_issue(issue)
194 196 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
195 197 if status.nil?
196 198 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
197 199 return issue
198 200 end
199 201
200 202 # the issue may have been updated by the closure of another one (eg. duplicate)
201 203 issue.reload
202 204 # don't change the status is the issue is closed
203 205 return if issue.status && issue.status.is_closed?
204 206
205 207 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
206 208 issue.status = status
207 209 unless Setting.commit_fix_done_ratio.blank?
208 210 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
209 211 end
210 212 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
211 213 { :changeset => self, :issue => issue })
212 214 unless issue.save
213 215 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
214 216 end
215 217 issue
216 218 end
217 219
218 220 def log_time(issue, hours)
219 221 time_entry = TimeEntry.new(
220 222 :user => user,
221 223 :hours => hours,
222 224 :issue => issue,
223 225 :spent_on => commit_date,
224 226 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
225 227 )
226 228 time_entry.activity = log_time_activity unless log_time_activity.nil?
227 229
228 230 unless time_entry.save
229 231 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
230 232 end
231 233 time_entry
232 234 end
233 235
234 236 def log_time_activity
235 237 if Setting.commit_logtime_activity_id.to_i > 0
236 238 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
237 239 end
238 240 end
239 241
240 242 def split_comments
241 243 comments =~ /\A(.+?)\r?\n(.*)$/m
242 244 @short_comments = $1 || comments
243 245 @long_comments = $2.to_s.strip
244 246 return @short_comments, @long_comments
245 247 end
246 248
247 249 def self.to_utf8(str)
248 250 encoding = Setting.commit_logs_encoding.to_s.strip
249 251 unless encoding.blank? || encoding == 'UTF-8'
250 252 begin
251 253 str = Iconv.conv('UTF-8', encoding, str)
252 254 rescue Iconv::Failure
253 255 # do nothing here
254 256 end
255 257 end
256 258 if str.respond_to?(:force_encoding)
257 259 str.force_encoding('UTF-8')
258 260 if ! str.valid_encoding?
259 261 str = str.encode("US-ASCII", :invalid => :replace,
260 262 :undef => :replace, :replace => '?').encode("UTF-8")
261 263 end
262 264 else
263 265 # removes invalid UTF8 sequences
264 266 begin
265 267 str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
266 268 rescue Iconv::InvalidEncoding
267 269 # "UTF-8//IGNORE" is not supported on some OS
268 270 end
269 271 end
270 272 str
271 273 end
272 274 end
@@ -1,252 +1,268
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2010 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class ChangesetTest < ActiveSupport::TestCase
23 23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
24 24
25 25 def setup
26 26 end
27 27
28 28 def test_ref_keywords_any
29 29 ActionMailer::Base.deliveries.clear
30 30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
31 31 Setting.commit_fix_done_ratio = '90'
32 32 Setting.commit_ref_keywords = '*'
33 33 Setting.commit_fix_keywords = 'fixes , closes'
34 34
35 35 c = Changeset.new(:repository => Project.find(1).repository,
36 36 :committed_on => Time.now,
37 37 :comments => 'New commit (#2). Fixes #1')
38 38 c.scan_comment_for_issue_ids
39 39
40 40 assert_equal [1, 2], c.issue_ids.sort
41 41 fixed = Issue.find(1)
42 42 assert fixed.closed?
43 43 assert_equal 90, fixed.done_ratio
44 44 assert_equal 1, ActionMailer::Base.deliveries.size
45 45 end
46 46
47 47 def test_ref_keywords
48 48 Setting.commit_ref_keywords = 'refs'
49 49 Setting.commit_fix_keywords = ''
50 50
51 51 c = Changeset.new(:repository => Project.find(1).repository,
52 52 :committed_on => Time.now,
53 53 :comments => 'Ignores #2. Refs #1')
54 54 c.scan_comment_for_issue_ids
55 55
56 56 assert_equal [1], c.issue_ids.sort
57 57 end
58 58
59 59 def test_ref_keywords_any_only
60 60 Setting.commit_ref_keywords = '*'
61 61 Setting.commit_fix_keywords = ''
62 62
63 63 c = Changeset.new(:repository => Project.find(1).repository,
64 64 :committed_on => Time.now,
65 65 :comments => 'Ignores #2. Refs #1')
66 66 c.scan_comment_for_issue_ids
67 67
68 68 assert_equal [1, 2], c.issue_ids.sort
69 69 end
70 70
71 71 def test_ref_keywords_any_with_timelog
72 72 Setting.commit_ref_keywords = '*'
73 73 Setting.commit_logtime_enabled = '1'
74 74
75 {
76 '2' => 2.0,
77 '2h' => 2.0,
78 '2hours' => 2.0,
79 '15m' => 0.25,
80 '15min' => 0.25,
81 '3h15' => 3.25,
82 '3h15m' => 3.25,
83 '3h15min' => 3.25,
84 '3:15' => 3.25,
85 '3.25' => 3.25,
86 '3.25h' => 3.25,
87 '3,25' => 3.25,
88 '3,25h' => 3.25,
89 }.each do |syntax, expected_hours|
75 90 c = Changeset.new(:repository => Project.find(1).repository,
76 91 :committed_on => 24.hours.ago,
77 :comments => 'Worked on this issue #1 @2h',
92 :comments => "Worked on this issue #1 @#{syntax}",
78 93 :revision => '520',
79 94 :user => User.find(2))
80 95 assert_difference 'TimeEntry.count' do
81 96 c.scan_comment_for_issue_ids
82 97 end
83 98 assert_equal [1], c.issue_ids.sort
84 99
85 100 time = TimeEntry.first(:order => 'id desc')
86 101 assert_equal 1, time.issue_id
87 102 assert_equal 1, time.project_id
88 103 assert_equal 2, time.user_id
89 assert_equal 2.0, time.hours
104 assert_equal expected_hours, time.hours, "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
90 105 assert_equal Date.yesterday, time.spent_on
91 106 assert time.activity.is_default?
92 107 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
93 108 end
109 end
94 110
95 111 def test_ref_keywords_closing_with_timelog
96 112 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
97 113 Setting.commit_ref_keywords = '*'
98 114 Setting.commit_fix_keywords = 'fixes , closes'
99 115 Setting.commit_logtime_enabled = '1'
100 116
101 117 c = Changeset.new(:repository => Project.find(1).repository,
102 118 :committed_on => Time.now,
103 :comments => 'This is a comment. Fixes #1 @2.5, #2 @1',
119 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
104 120 :user => User.find(2))
105 121 assert_difference 'TimeEntry.count', 2 do
106 122 c.scan_comment_for_issue_ids
107 123 end
108 124
109 125 assert_equal [1, 2], c.issue_ids.sort
110 126 assert Issue.find(1).closed?
111 127 assert Issue.find(2).closed?
112 128
113 129 times = TimeEntry.all(:order => 'id desc', :limit => 2)
114 130 assert_equal [1, 2], times.collect(&:issue_id).sort
115 131 end
116 132
117 133 def test_ref_keywords_any_line_start
118 134 Setting.commit_ref_keywords = '*'
119 135
120 136 c = Changeset.new(:repository => Project.find(1).repository,
121 137 :committed_on => Time.now,
122 138 :comments => '#1 is the reason of this commit')
123 139 c.scan_comment_for_issue_ids
124 140
125 141 assert_equal [1], c.issue_ids.sort
126 142 end
127 143
128 144 def test_ref_keywords_allow_brackets_around_a_issue_number
129 145 Setting.commit_ref_keywords = '*'
130 146
131 147 c = Changeset.new(:repository => Project.find(1).repository,
132 148 :committed_on => Time.now,
133 149 :comments => '[#1] Worked on this issue')
134 150 c.scan_comment_for_issue_ids
135 151
136 152 assert_equal [1], c.issue_ids.sort
137 153 end
138 154
139 155 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
140 156 Setting.commit_ref_keywords = '*'
141 157
142 158 c = Changeset.new(:repository => Project.find(1).repository,
143 159 :committed_on => Time.now,
144 160 :comments => '[#1 #2, #3] Worked on these')
145 161 c.scan_comment_for_issue_ids
146 162
147 163 assert_equal [1,2,3], c.issue_ids.sort
148 164 end
149 165
150 166 def test_commit_referencing_a_subproject_issue
151 167 c = Changeset.new(:repository => Project.find(1).repository,
152 168 :committed_on => Time.now,
153 169 :comments => 'refs #5, a subproject issue')
154 170 c.scan_comment_for_issue_ids
155 171
156 172 assert_equal [5], c.issue_ids.sort
157 173 assert c.issues.first.project != c.project
158 174 end
159 175
160 176 def test_commit_referencing_a_parent_project_issue
161 177 # repository of child project
162 178 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
163 179
164 180 c = Changeset.new(:repository => r,
165 181 :committed_on => Time.now,
166 182 :comments => 'refs #2, an issue of a parent project')
167 183 c.scan_comment_for_issue_ids
168 184
169 185 assert_equal [2], c.issue_ids.sort
170 186 assert c.issues.first.project != c.project
171 187 end
172 188
173 189 def test_text_tag_revision
174 190 c = Changeset.new(:revision => '520')
175 191 assert_equal 'r520', c.text_tag
176 192 end
177 193
178 194 def test_text_tag_hash
179 195 c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
180 196 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
181 197 end
182 198
183 199 def test_text_tag_hash_all_number
184 200 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
185 201 assert_equal 'commit:0123456789', c.text_tag
186 202 end
187 203
188 204 def test_previous
189 205 changeset = Changeset.find_by_revision('3')
190 206 assert_equal Changeset.find_by_revision('2'), changeset.previous
191 207 end
192 208
193 209 def test_previous_nil
194 210 changeset = Changeset.find_by_revision('1')
195 211 assert_nil changeset.previous
196 212 end
197 213
198 214 def test_next
199 215 changeset = Changeset.find_by_revision('2')
200 216 assert_equal Changeset.find_by_revision('3'), changeset.next
201 217 end
202 218
203 219 def test_next_nil
204 220 changeset = Changeset.find_by_revision('10')
205 221 assert_nil changeset.next
206 222 end
207 223
208 224 def test_comments_should_be_converted_to_utf8
209 225 with_settings :commit_logs_encoding => 'ISO-8859-1' do
210 226 c = Changeset.new
211 227 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
212 228 assert_equal "Texte encodé en ISO-8859-1.", c.comments
213 229 end
214 230 end
215 231
216 232 def test_invalid_utf8_sequences_in_comments_should_be_stripped
217 233 with_settings :commit_logs_encoding => 'UTF-8' do
218 234 c = Changeset.new
219 235 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
220 236 c.comments = str
221 237 if str.respond_to?(:force_encoding)
222 238 assert_equal "Texte encod? en ISO-8859-1.", c.comments
223 239 else
224 240 assert_equal "Texte encod en ISO-8859-1.", c.comments
225 241 end
226 242 end
227 243 end
228 244
229 245 def test_comments_should_be_converted_all_latin1_to_utf8
230 246 with_settings :commit_logs_encoding => 'ISO-8859-1' do
231 247 c = Changeset.new
232 248 s1 = "\xC2\x80"
233 249 s2 = "\xc3\x82\xc2\x80"
234 250 if s1.respond_to?(:force_encoding)
235 251 s3 = s1
236 252 s4 = s2
237 253 s1.force_encoding('ASCII-8BIT')
238 254 s2.force_encoding('ASCII-8BIT')
239 255 s3.force_encoding('ISO-8859-1')
240 256 s4.force_encoding('UTF-8')
241 257 assert_equal s3.encode('UTF-8'), s4
242 258 end
243 259 c.comments = s1
244 260 assert_equal s2, c.comments
245 261 end
246 262 end
247 263
248 264 def test_identifier
249 265 c = Changeset.find_by_revision('1')
250 266 assert_equal c.revision, c.identifier
251 267 end
252 268 end
General Comments 0
You need to be logged in to leave comments. Login now