comparison app/models/changeset.rb @ 119:8661b858af72

* Update to Redmine trunk rev 4705
author Chris Cannam
date Thu, 13 Jan 2011 14:12:06 +0000
parents 1d32c0a0efbf
children cd2282d2aa55 051f544170fe
comparison
equal deleted inserted replaced
39:150ceac17a8d 119:8661b858af72
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.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.revision}} 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'
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
51 # Returns the identifier of this changeset; depending on repository backends
52 def identifier
53 if repository.class.respond_to? :changeset_identifier
54 repository.class.changeset_identifier self
55 else
56 revision.to_s
57 end
58 end
50 59
51 def comments=(comment) 60 def comments=(comment)
52 write_attribute(:comments, Changeset.normalize_comments(comment)) 61 write_attribute(:comments, Changeset.normalize_comments(comment))
53 end 62 end
54 63
55 def committed_on=(date) 64 def committed_on=(date)
56 self.commit_date = date 65 self.commit_date = date
57 super 66 super
58 end 67 end
68
69 # Returns the readable identifier
70 def format_identifier
71 if repository.class.respond_to? :format_changeset_identifier
72 repository.class.format_changeset_identifier self
73 else
74 identifier
75 end
76 end
59 77
60 def committer=(arg) 78 def committer=(arg)
61 write_attribute(:committer, self.class.to_utf8(arg.to_s)) 79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
62 end 80 end
63 81
74 end 92 end
75 93
76 def after_create 94 def after_create
77 scan_comment_for_issue_ids 95 scan_comment_for_issue_ids
78 end 96 end
97
98 TIMELOG_RE = /
99 (
100 (\d+([.,]\d+)?)h?
101 |
102 (\d+):(\d+)
103 |
104 ((\d+)(h|hours?))?((\d+)(m|min)?)?
105 )
106 /x
79 107
80 def scan_comment_for_issue_ids 108 def scan_comment_for_issue_ids
81 return if comments.blank? 109 return if comments.blank?
82 # keywords used to reference issues 110 # keywords used to reference issues
83 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) 111 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
112 ref_keywords_any = ref_keywords.delete('*')
84 # keywords used to fix issues 113 # keywords used to fix issues
85 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) 114 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
86 115
87 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") 116 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
88 return if kw_regexp.blank?
89 117
90 referenced_issues = [] 118 referenced_issues = []
91 119
92 if ref_keywords.delete('*') 120 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
93 # find any issue ID in the comments 121 action, refs = match[2], match[3]
94 target_issue_ids = [] 122 next unless action.present? || ref_keywords_any
95 comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] } 123
96 referenced_issues += find_referenced_issues_by_id(target_issue_ids) 124 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
97 end 125 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
98 126 if issue
99 comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match| 127 referenced_issues << issue
100 action = match[0] 128 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
101 target_issue_ids = match[1].scan(/\d+/) 129 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
102 target_issues = find_referenced_issues_by_id(target_issue_ids)
103 if fix_keywords.include?(action.downcase) && fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
104 # update status of issues
105 logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
106 target_issues.each do |issue|
107 # the issue may have been updated by the closure of another one (eg. duplicate)
108 issue.reload
109 # don't change the status is the issue is closed
110 next if issue.status.is_closed?
111 csettext = "r#{self.revision}"
112 if self.scmid && (! (csettext =~ /^r[0-9]+$/))
113 csettext = "commit:\"#{self.scmid}\""
114 end
115 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
116 issue.status = fix_status
117 unless Setting.commit_fix_done_ratio.blank?
118 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
119 end
120 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
121 { :changeset => self, :issue => issue })
122 issue.save
123 end 130 end
124 end 131 end
125 referenced_issues += target_issues
126 end 132 end
127 133
128 referenced_issues.uniq! 134 referenced_issues.uniq!
129 self.issues = referenced_issues unless referenced_issues.empty? 135 self.issues = referenced_issues unless referenced_issues.empty?
130 end 136 end
133 @short_comments || split_comments.first 139 @short_comments || split_comments.first
134 end 140 end
135 141
136 def long_comments 142 def long_comments
137 @long_comments || split_comments.last 143 @long_comments || split_comments.last
144 end
145
146 def text_tag
147 if scmid?
148 "commit:#{scmid}"
149 else
150 "r#{revision}"
151 end
138 end 152 end
139 153
140 # Returns the previous changeset 154 # Returns the previous changeset
141 def previous 155 def previous
142 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC') 156 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
161 :from_revision => change[:from_revision]) 175 :from_revision => change[:from_revision])
162 end 176 end
163 177
164 private 178 private
165 179
166 # Finds issues that can be referenced by the commit message 180 # Finds an issue that can be referenced by the commit message
167 # i.e. issues that belong to the repository project, a subproject or a parent project 181 # i.e. an issue that belong to the repository project, a subproject or a parent project
168 def find_referenced_issues_by_id(ids) 182 def find_referenced_issue_by_id(id)
169 return [] if ids.compact.empty? 183 return nil if id.blank?
170 Issue.find_all_by_id(ids, :include => :project).select {|issue| 184 issue = Issue.find_by_id(id.to_i, :include => :project)
171 project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project) 185 if issue
172 } 186 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
187 issue = nil
188 end
189 end
190 issue
191 end
192
193 def fix_issue(issue)
194 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
195 if status.nil?
196 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
197 return issue
198 end
199
200 # the issue may have been updated by the closure of another one (eg. duplicate)
201 issue.reload
202 # don't change the status is the issue is closed
203 return if issue.status && issue.status.is_closed?
204
205 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
206 issue.status = status
207 unless Setting.commit_fix_done_ratio.blank?
208 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
209 end
210 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
211 { :changeset => self, :issue => issue })
212 unless issue.save
213 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
214 end
215 issue
216 end
217
218 def log_time(issue, hours)
219 time_entry = TimeEntry.new(
220 :user => user,
221 :hours => hours,
222 :issue => issue,
223 :spent_on => commit_date,
224 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
225 )
226 time_entry.activity = log_time_activity unless log_time_activity.nil?
227
228 unless time_entry.save
229 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
230 end
231 time_entry
232 end
233
234 def log_time_activity
235 if Setting.commit_logtime_activity_id.to_i > 0
236 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
237 end
173 end 238 end
174 239
175 def split_comments 240 def split_comments
176 comments =~ /\A(.+?)\r?\n(.*)$/m 241 comments =~ /\A(.+?)\r?\n(.*)$/m
177 @short_comments = $1 || comments 242 @short_comments = $1 || comments
178 @long_comments = $2.to_s.strip 243 @long_comments = $2.to_s.strip
179 return @short_comments, @long_comments 244 return @short_comments, @long_comments
180 end 245 end
181 246
182 def self.to_utf8(str) 247 def self.to_utf8(str)
183 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii 248 if str.respond_to?(:force_encoding)
249 str.force_encoding('UTF-8')
250 return str if str.valid_encoding?
251 else
252 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
253 end
254
184 encoding = Setting.commit_logs_encoding.to_s.strip 255 encoding = Setting.commit_logs_encoding.to_s.strip
185 unless encoding.blank? || encoding == 'UTF-8' 256 unless encoding.blank? || encoding == 'UTF-8'
186 begin 257 begin
187 str = Iconv.conv('UTF-8', encoding, str) 258 str = Iconv.conv('UTF-8', encoding, str)
188 rescue Iconv::Failure 259 rescue Iconv::Failure