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