##// END OF EJS Templates
Remove invalid utf8 sequences from commit comments and author name (#4773)....
Jean-Philippe Lang -
r3352:46aa855572a2
parent child
Show More
@@ -0,0 +1,1
1 Texte encod� en ISO-8859-1. No newline at end of file
@@ -1,190 +1,195
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 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.revision}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
26 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.revision}" + (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.revision}}
29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.revision}}
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 def comments=(comment)
51 def comments=(comment)
52 write_attribute(:comments, Changeset.normalize_comments(comment))
52 write_attribute(:comments, Changeset.normalize_comments(comment))
53 end
53 end
54
54
55 def committed_on=(date)
55 def committed_on=(date)
56 self.commit_date = date
56 self.commit_date = date
57 super
57 super
58 end
58 end
59
59
60 def committer=(arg)
61 write_attribute(:committer, self.class.to_utf8(arg.to_s))
62 end
63
60 def project
64 def project
61 repository.project
65 repository.project
62 end
66 end
63
67
64 def author
68 def author
65 user || committer.to_s.split('<').first
69 user || committer.to_s.split('<').first
66 end
70 end
67
71
68 def before_create
72 def before_create
69 self.user = repository.find_committer_user(committer)
73 self.user = repository.find_committer_user(committer)
70 end
74 end
71
75
72 def after_create
76 def after_create
73 scan_comment_for_issue_ids
77 scan_comment_for_issue_ids
74 end
78 end
75 require 'pp'
79 require 'pp'
76
80
77 def scan_comment_for_issue_ids
81 def scan_comment_for_issue_ids
78 return if comments.blank?
82 return if comments.blank?
79 # keywords used to reference issues
83 # keywords used to reference issues
80 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
84 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
81 # keywords used to fix issues
85 # keywords used to fix issues
82 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
86 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
83 # status and optional done ratio applied
87 # status and optional done ratio applied
84 fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
88 fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
85 done_ratio = Setting.commit_fix_done_ratio.blank? ? nil : Setting.commit_fix_done_ratio.to_i
89 done_ratio = Setting.commit_fix_done_ratio.blank? ? nil : Setting.commit_fix_done_ratio.to_i
86
90
87 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
91 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
88 return if kw_regexp.blank?
92 return if kw_regexp.blank?
89
93
90 referenced_issues = []
94 referenced_issues = []
91
95
92 if ref_keywords.delete('*')
96 if ref_keywords.delete('*')
93 # find any issue ID in the comments
97 # find any issue ID in the comments
94 target_issue_ids = []
98 target_issue_ids = []
95 comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] }
99 comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] }
96 referenced_issues += find_referenced_issues_by_id(target_issue_ids)
100 referenced_issues += find_referenced_issues_by_id(target_issue_ids)
97 end
101 end
98
102
99 comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match|
103 comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match|
100 action = match[0]
104 action = match[0]
101 target_issue_ids = match[1].scan(/\d+/)
105 target_issue_ids = match[1].scan(/\d+/)
102 target_issues = find_referenced_issues_by_id(target_issue_ids)
106 target_issues = find_referenced_issues_by_id(target_issue_ids)
103 if fix_status && fix_keywords.include?(action.downcase)
107 if fix_status && fix_keywords.include?(action.downcase)
104 # update status of issues
108 # update status of issues
105 logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
109 logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
106 target_issues.each do |issue|
110 target_issues.each do |issue|
107 # the issue may have been updated by the closure of another one (eg. duplicate)
111 # the issue may have been updated by the closure of another one (eg. duplicate)
108 issue.reload
112 issue.reload
109 # don't change the status is the issue is closed
113 # don't change the status is the issue is closed
110 next if issue.status.is_closed?
114 next if issue.status.is_closed?
111 csettext = "r#{self.revision}"
115 csettext = "r#{self.revision}"
112 if self.scmid && (! (csettext =~ /^r[0-9]+$/))
116 if self.scmid && (! (csettext =~ /^r[0-9]+$/))
113 csettext = "commit:\"#{self.scmid}\""
117 csettext = "commit:\"#{self.scmid}\""
114 end
118 end
115 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
119 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
116 issue.status = fix_status
120 issue.status = fix_status
117 issue.done_ratio = done_ratio if done_ratio
121 issue.done_ratio = done_ratio if done_ratio
118 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
122 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
119 { :changeset => self, :issue => issue })
123 { :changeset => self, :issue => issue })
120 issue.save
124 issue.save
121 end
125 end
122 end
126 end
123 referenced_issues += target_issues
127 referenced_issues += target_issues
124 end
128 end
125
129
126 self.issues = referenced_issues.uniq
130 self.issues = referenced_issues.uniq
127 end
131 end
128
132
129 def short_comments
133 def short_comments
130 @short_comments || split_comments.first
134 @short_comments || split_comments.first
131 end
135 end
132
136
133 def long_comments
137 def long_comments
134 @long_comments || split_comments.last
138 @long_comments || split_comments.last
135 end
139 end
136
140
137 # Returns the previous changeset
141 # Returns the previous changeset
138 def previous
142 def previous
139 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
143 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
140 end
144 end
141
145
142 # Returns the next changeset
146 # Returns the next changeset
143 def next
147 def next
144 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
148 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
145 end
149 end
146
150
147 # Strips and reencodes a commit log before insertion into the database
151 # Strips and reencodes a commit log before insertion into the database
148 def self.normalize_comments(str)
152 def self.normalize_comments(str)
149 to_utf8(str.to_s.strip)
153 to_utf8(str.to_s.strip)
150 end
154 end
151
155
152 # Creates a new Change from it's common parameters
156 # Creates a new Change from it's common parameters
153 def create_change(change)
157 def create_change(change)
154 Change.create(:changeset => self,
158 Change.create(:changeset => self,
155 :action => change[:action],
159 :action => change[:action],
156 :path => change[:path],
160 :path => change[:path],
157 :from_path => change[:from_path],
161 :from_path => change[:from_path],
158 :from_revision => change[:from_revision])
162 :from_revision => change[:from_revision])
159 end
163 end
160
164
161 private
165 private
162
166
163 # Finds issues that can be referenced by the commit message
167 # Finds issues that can be referenced by the commit message
164 # i.e. issues that belong to the repository project, a subproject or a parent project
168 # i.e. issues that belong to the repository project, a subproject or a parent project
165 def find_referenced_issues_by_id(ids)
169 def find_referenced_issues_by_id(ids)
166 Issue.find_all_by_id(ids, :include => :project).select {|issue|
170 Issue.find_all_by_id(ids, :include => :project).select {|issue|
167 project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
171 project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
168 }
172 }
169 end
173 end
170
174
171 def split_comments
175 def split_comments
172 comments =~ /\A(.+?)\r?\n(.*)$/m
176 comments =~ /\A(.+?)\r?\n(.*)$/m
173 @short_comments = $1 || comments
177 @short_comments = $1 || comments
174 @long_comments = $2.to_s.strip
178 @long_comments = $2.to_s.strip
175 return @short_comments, @long_comments
179 return @short_comments, @long_comments
176 end
180 end
177
181
178 def self.to_utf8(str)
182 def self.to_utf8(str)
179 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
183 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
180 encoding = Setting.commit_logs_encoding.to_s.strip
184 encoding = Setting.commit_logs_encoding.to_s.strip
181 unless encoding.blank? || encoding == 'UTF-8'
185 unless encoding.blank? || encoding == 'UTF-8'
182 begin
186 begin
183 return Iconv.conv('UTF-8', encoding, str)
187 str = Iconv.conv('UTF-8', encoding, str)
184 rescue Iconv::Failure
188 rescue Iconv::Failure
185 # do nothing here
189 # do nothing here
186 end
190 end
187 end
191 end
188 str
192 # removes invalid UTF8 sequences
193 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
189 end
194 end
190 end
195 end
@@ -1,120 +1,136
1 # redMine - project management software
1 # encoding: utf-8
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 #
3 # Redmine - project management software
4 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 #
5 #
4 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
8 #
10 #
9 # 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,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # GNU General Public License for more details.
13 #
15 #
14 # 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
15 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
16 # 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.
17
19
18 require File.dirname(__FILE__) + '/../test_helper'
20 require File.dirname(__FILE__) + '/../test_helper'
19
21
20 class ChangesetTest < ActiveSupport::TestCase
22 class ChangesetTest < ActiveSupport::TestCase
21 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
22
24
23 def setup
25 def setup
24 end
26 end
25
27
26 def test_ref_keywords_any
28 def test_ref_keywords_any
27 ActionMailer::Base.deliveries.clear
29 ActionMailer::Base.deliveries.clear
28 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
29 Setting.commit_fix_done_ratio = '90'
31 Setting.commit_fix_done_ratio = '90'
30 Setting.commit_ref_keywords = '*'
32 Setting.commit_ref_keywords = '*'
31 Setting.commit_fix_keywords = 'fixes , closes'
33 Setting.commit_fix_keywords = 'fixes , closes'
32
34
33 c = Changeset.new(:repository => Project.find(1).repository,
35 c = Changeset.new(:repository => Project.find(1).repository,
34 :committed_on => Time.now,
36 :committed_on => Time.now,
35 :comments => 'New commit (#2). Fixes #1')
37 :comments => 'New commit (#2). Fixes #1')
36 c.scan_comment_for_issue_ids
38 c.scan_comment_for_issue_ids
37
39
38 assert_equal [1, 2], c.issue_ids.sort
40 assert_equal [1, 2], c.issue_ids.sort
39 fixed = Issue.find(1)
41 fixed = Issue.find(1)
40 assert fixed.closed?
42 assert fixed.closed?
41 assert_equal 90, fixed.done_ratio
43 assert_equal 90, fixed.done_ratio
42 assert_equal 1, ActionMailer::Base.deliveries.size
44 assert_equal 1, ActionMailer::Base.deliveries.size
43 end
45 end
44
46
45 def test_ref_keywords_any_line_start
47 def test_ref_keywords_any_line_start
46 Setting.commit_ref_keywords = '*'
48 Setting.commit_ref_keywords = '*'
47
49
48 c = Changeset.new(:repository => Project.find(1).repository,
50 c = Changeset.new(:repository => Project.find(1).repository,
49 :committed_on => Time.now,
51 :committed_on => Time.now,
50 :comments => '#1 is the reason of this commit')
52 :comments => '#1 is the reason of this commit')
51 c.scan_comment_for_issue_ids
53 c.scan_comment_for_issue_ids
52
54
53 assert_equal [1], c.issue_ids.sort
55 assert_equal [1], c.issue_ids.sort
54 end
56 end
55
57
56 def test_ref_keywords_allow_brackets_around_a_issue_number
58 def test_ref_keywords_allow_brackets_around_a_issue_number
57 Setting.commit_ref_keywords = '*'
59 Setting.commit_ref_keywords = '*'
58
60
59 c = Changeset.new(:repository => Project.find(1).repository,
61 c = Changeset.new(:repository => Project.find(1).repository,
60 :committed_on => Time.now,
62 :committed_on => Time.now,
61 :comments => '[#1] Worked on this issue')
63 :comments => '[#1] Worked on this issue')
62 c.scan_comment_for_issue_ids
64 c.scan_comment_for_issue_ids
63
65
64 assert_equal [1], c.issue_ids.sort
66 assert_equal [1], c.issue_ids.sort
65 end
67 end
66
68
67 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
69 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
68 Setting.commit_ref_keywords = '*'
70 Setting.commit_ref_keywords = '*'
69
71
70 c = Changeset.new(:repository => Project.find(1).repository,
72 c = Changeset.new(:repository => Project.find(1).repository,
71 :committed_on => Time.now,
73 :committed_on => Time.now,
72 :comments => '[#1 #2, #3] Worked on these')
74 :comments => '[#1 #2, #3] Worked on these')
73 c.scan_comment_for_issue_ids
75 c.scan_comment_for_issue_ids
74
76
75 assert_equal [1,2,3], c.issue_ids.sort
77 assert_equal [1,2,3], c.issue_ids.sort
76 end
78 end
77
79
78 def test_commit_referencing_a_subproject_issue
80 def test_commit_referencing_a_subproject_issue
79 c = Changeset.new(:repository => Project.find(1).repository,
81 c = Changeset.new(:repository => Project.find(1).repository,
80 :committed_on => Time.now,
82 :committed_on => Time.now,
81 :comments => 'refs #5, a subproject issue')
83 :comments => 'refs #5, a subproject issue')
82 c.scan_comment_for_issue_ids
84 c.scan_comment_for_issue_ids
83
85
84 assert_equal [5], c.issue_ids.sort
86 assert_equal [5], c.issue_ids.sort
85 assert c.issues.first.project != c.project
87 assert c.issues.first.project != c.project
86 end
88 end
87
89
88 def test_commit_referencing_a_parent_project_issue
90 def test_commit_referencing_a_parent_project_issue
89 # repository of child project
91 # repository of child project
90 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
92 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
91
93
92 c = Changeset.new(:repository => r,
94 c = Changeset.new(:repository => r,
93 :committed_on => Time.now,
95 :committed_on => Time.now,
94 :comments => 'refs #2, an issue of a parent project')
96 :comments => 'refs #2, an issue of a parent project')
95 c.scan_comment_for_issue_ids
97 c.scan_comment_for_issue_ids
96
98
97 assert_equal [2], c.issue_ids.sort
99 assert_equal [2], c.issue_ids.sort
98 assert c.issues.first.project != c.project
100 assert c.issues.first.project != c.project
99 end
101 end
100
102
101 def test_previous
103 def test_previous
102 changeset = Changeset.find_by_revision('3')
104 changeset = Changeset.find_by_revision('3')
103 assert_equal Changeset.find_by_revision('2'), changeset.previous
105 assert_equal Changeset.find_by_revision('2'), changeset.previous
104 end
106 end
105
107
106 def test_previous_nil
108 def test_previous_nil
107 changeset = Changeset.find_by_revision('1')
109 changeset = Changeset.find_by_revision('1')
108 assert_nil changeset.previous
110 assert_nil changeset.previous
109 end
111 end
110
112
111 def test_next
113 def test_next
112 changeset = Changeset.find_by_revision('2')
114 changeset = Changeset.find_by_revision('2')
113 assert_equal Changeset.find_by_revision('3'), changeset.next
115 assert_equal Changeset.find_by_revision('3'), changeset.next
114 end
116 end
115
117
116 def test_next_nil
118 def test_next_nil
117 changeset = Changeset.find_by_revision('10')
119 changeset = Changeset.find_by_revision('10')
118 assert_nil changeset.next
120 assert_nil changeset.next
119 end
121 end
122
123 def test_comments_should_be_converted_to_utf8
124 with_settings :commit_logs_encoding => 'ISO-8859-1' do
125 c = Changeset.new
126 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
127 assert_equal "Texte encodé en ISO-8859-1.", c.comments
128 end
129 end
130
131 def test_invalid_utf8_sequences_in_comments_should_be_stripped
132 c = Changeset.new
133 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
134 assert_equal "Texte encod en ISO-8859-1.", c.comments
135 end
120 end
136 end
General Comments 0
You need to be logged in to leave comments. Login now