comparison app/models/changeset.rb @ 118:b859cc0c4fa1 cannam

Merge from branch "redmine-1.1"
author Chris Cannam
date Thu, 13 Jan 2011 13:21:03 +0000
parents 9c6c72729d91 af80e5618e9b
children cd2282d2aa55
comparison
equal deleted inserted replaced
114:8a205cc048de 118:b859cc0c4fa1
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)} #{RepositoriesHelper.format_revision(o)}" + (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',
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. 51 # Returns the identifier of this changeset; depending on repository backends
52 # e.g. revision number for centralized system; hash id for DVCS
53 def identifier 52 def identifier
54 scmid || revision 53 if repository.class.respond_to? :changeset_identifier
55 end 54 repository.class.changeset_identifier self
56 55 else
57 # Returns the wiki identifier, "rN" or "commit:ABCDEF" 56 revision.to_s
58 def wiki_identifier 57 end
59 if scmid # hash-like 58 end
60 "commit:#{scmid}"
61 else # numeric
62 "r#{revision}"
63 end
64 end
65 private :wiki_identifier
66 59
67 def comments=(comment) 60 def comments=(comment)
68 write_attribute(:comments, Changeset.normalize_comments(comment)) 61 write_attribute(:comments, Changeset.normalize_comments(comment))
69 end 62 end
70 63
71 def committed_on=(date) 64 def committed_on=(date)
72 self.commit_date = date 65 self.commit_date = date
73 super 66 super
74 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
75 77
76 def committer=(arg) 78 def committer=(arg)
77 write_attribute(:committer, self.class.to_utf8(arg.to_s)) 79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
78 end 80 end
79 81
90 end 92 end
91 93
92 def after_create 94 def after_create
93 scan_comment_for_issue_ids 95 scan_comment_for_issue_ids
94 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
95 107
96 def scan_comment_for_issue_ids 108 def scan_comment_for_issue_ids
97 return if comments.blank? 109 return if comments.blank?
98 # keywords used to reference issues 110 # keywords used to reference issues
99 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('*')
100 # keywords used to fix issues 113 # keywords used to fix issues
101 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) 114 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
102 115
103 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("|")
104 return if kw_regexp.blank?
105 117
106 referenced_issues = [] 118 referenced_issues = []
107 119
108 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|
109 # find any issue ID in the comments 121 action, refs = match[2], match[3]
110 target_issue_ids = [] 122 next unless action.present? || ref_keywords_any
111 comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] } 123
112 referenced_issues += find_referenced_issues_by_id(target_issue_ids) 124 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
113 end 125 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
114 126 if issue
115 comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match| 127 referenced_issues << issue
116 action = match[0] 128 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
117 target_issue_ids = match[1].scan(/\d+/) 129 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
118 target_issues = find_referenced_issues_by_id(target_issue_ids)
119 if fix_keywords.include?(action.downcase) && fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
120 # update status of issues
121 logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
122 target_issues.each do |issue|
123 # the issue may have been updated by the closure of another one (eg. duplicate)
124 issue.reload
125 # don't change the status is the issue is closed
126 next if issue.status.is_closed?
127 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, wiki_identifier))
128 issue.status = fix_status
129 unless Setting.commit_fix_done_ratio.blank?
130 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
131 end
132 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
133 { :changeset => self, :issue => issue })
134 issue.save
135 end 130 end
136 end 131 end
137 referenced_issues += target_issues
138 end 132 end
139 133
140 referenced_issues.uniq! 134 referenced_issues.uniq!
141 self.issues = referenced_issues unless referenced_issues.empty? 135 self.issues = referenced_issues unless referenced_issues.empty?
142 end 136 end
145 @short_comments || split_comments.first 139 @short_comments || split_comments.first
146 end 140 end
147 141
148 def long_comments 142 def long_comments
149 @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
150 end 152 end
151 153
152 # Returns the previous changeset 154 # Returns the previous changeset
153 def previous 155 def previous
154 @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')
173 :from_revision => change[:from_revision]) 175 :from_revision => change[:from_revision])
174 end 176 end
175 177
176 private 178 private
177 179
178 # Finds issues that can be referenced by the commit message 180 # Finds an issue that can be referenced by the commit message
179 # 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
180 def find_referenced_issues_by_id(ids) 182 def find_referenced_issue_by_id(id)
181 return [] if ids.compact.empty? 183 return nil if id.blank?
182 Issue.find_all_by_id(ids, :include => :project).select {|issue| 184 issue = Issue.find_by_id(id.to_i, :include => :project)
183 project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project) 185 if issue
184 } 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
185 end 238 end
186 239
187 def split_comments 240 def split_comments
188 comments =~ /\A(.+?)\r?\n(.*)$/m 241 comments =~ /\A(.+?)\r?\n(.*)$/m
189 @short_comments = $1 || comments 242 @short_comments = $1 || comments