Mercurial > hg > soundsoftware-site
comparison .svn/pristine/4c/4ce47e5153ddf18249e25aad233433aafed78ea8.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 class Changeset < ActiveRecord::Base | |
19 belongs_to :repository | |
20 belongs_to :user | |
21 has_many :filechanges, :class_name => 'Change', :dependent => :delete_all | |
22 has_and_belongs_to_many :issues | |
23 has_and_belongs_to_many :parents, | |
24 :class_name => "Changeset", | |
25 :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", | |
26 :association_foreign_key => 'parent_id', :foreign_key => 'changeset_id' | |
27 has_and_belongs_to_many :children, | |
28 :class_name => "Changeset", | |
29 :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", | |
30 :association_foreign_key => 'changeset_id', :foreign_key => 'parent_id' | |
31 | |
32 acts_as_event :title => Proc.new {|o| o.title}, | |
33 :description => :long_comments, | |
34 :datetime => :committed_on, | |
35 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :repository_id => o.repository.identifier_param, :rev => o.identifier}} | |
36 | |
37 acts_as_searchable :columns => 'comments', | |
38 :include => {:repository => :project}, | |
39 :project_key => "#{Repository.table_name}.project_id", | |
40 :date_column => 'committed_on' | |
41 | |
42 acts_as_activity_provider :timestamp => "#{table_name}.committed_on", | |
43 :author_key => :user_id, | |
44 :find_options => {:include => [:user, {:repository => :project}]} | |
45 | |
46 validates_presence_of :repository_id, :revision, :committed_on, :commit_date | |
47 validates_uniqueness_of :revision, :scope => :repository_id | |
48 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true | |
49 | |
50 scope :visible, lambda {|*args| | |
51 includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args)) | |
52 } | |
53 | |
54 after_create :scan_for_issues | |
55 before_create :before_create_cs | |
56 | |
57 def revision=(r) | |
58 write_attribute :revision, (r.nil? ? nil : r.to_s) | |
59 end | |
60 | |
61 # Returns the identifier of this changeset; depending on repository backends | |
62 def identifier | |
63 if repository.class.respond_to? :changeset_identifier | |
64 repository.class.changeset_identifier self | |
65 else | |
66 revision.to_s | |
67 end | |
68 end | |
69 | |
70 def committed_on=(date) | |
71 self.commit_date = date | |
72 super | |
73 end | |
74 | |
75 # Returns the readable identifier | |
76 def format_identifier | |
77 if repository.class.respond_to? :format_changeset_identifier | |
78 repository.class.format_changeset_identifier self | |
79 else | |
80 identifier | |
81 end | |
82 end | |
83 | |
84 def project | |
85 repository.project | |
86 end | |
87 | |
88 def author | |
89 user || committer.to_s.split('<').first | |
90 end | |
91 | |
92 def before_create_cs | |
93 self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding) | |
94 self.comments = self.class.normalize_comments( | |
95 self.comments, repository.repo_log_encoding) | |
96 self.user = repository.find_committer_user(self.committer) | |
97 end | |
98 | |
99 def scan_for_issues | |
100 scan_comment_for_issue_ids | |
101 end | |
102 | |
103 TIMELOG_RE = / | |
104 ( | |
105 ((\d+)(h|hours?))((\d+)(m|min)?)? | |
106 | | |
107 ((\d+)(h|hours?|m|min)) | |
108 | | |
109 (\d+):(\d+) | |
110 | | |
111 (\d+([\.,]\d+)?)h? | |
112 ) | |
113 /x | |
114 | |
115 def scan_comment_for_issue_ids | |
116 return if comments.blank? | |
117 # keywords used to reference issues | |
118 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) | |
119 ref_keywords_any = ref_keywords.delete('*') | |
120 # keywords used to fix issues | |
121 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) | |
122 | |
123 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") | |
124 | |
125 referenced_issues = [] | |
126 | |
127 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match| | |
128 action, refs = match[2], match[3] | |
129 next unless action.present? || ref_keywords_any | |
130 | |
131 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m| | |
132 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2] | |
133 if issue | |
134 referenced_issues << issue | |
135 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase) | |
136 log_time(issue, hours) if hours && Setting.commit_logtime_enabled? | |
137 end | |
138 end | |
139 end | |
140 | |
141 referenced_issues.uniq! | |
142 self.issues = referenced_issues unless referenced_issues.empty? | |
143 end | |
144 | |
145 def short_comments | |
146 @short_comments || split_comments.first | |
147 end | |
148 | |
149 def long_comments | |
150 @long_comments || split_comments.last | |
151 end | |
152 | |
153 def text_tag(ref_project=nil) | |
154 tag = if scmid? | |
155 "commit:#{scmid}" | |
156 else | |
157 "r#{revision}" | |
158 end | |
159 if repository && repository.identifier.present? | |
160 tag = "#{repository.identifier}|#{tag}" | |
161 end | |
162 if ref_project && project && ref_project != project | |
163 tag = "#{project.identifier}:#{tag}" | |
164 end | |
165 tag | |
166 end | |
167 | |
168 # Returns the title used for the changeset in the activity/search results | |
169 def title | |
170 repo = (repository && repository.identifier.present?) ? " (#{repository.identifier})" : '' | |
171 comm = short_comments.blank? ? '' : (': ' + short_comments) | |
172 "#{l(:label_revision)} #{format_identifier}#{repo}#{comm}" | |
173 end | |
174 | |
175 # Returns the previous changeset | |
176 def previous | |
177 @previous ||= Changeset.where(["id < ? AND repository_id = ?", id, repository_id]).order('id DESC').first | |
178 end | |
179 | |
180 # Returns the next changeset | |
181 def next | |
182 @next ||= Changeset.where(["id > ? AND repository_id = ?", id, repository_id]).order('id ASC').first | |
183 end | |
184 | |
185 # Creates a new Change from it's common parameters | |
186 def create_change(change) | |
187 Change.create(:changeset => self, | |
188 :action => change[:action], | |
189 :path => change[:path], | |
190 :from_path => change[:from_path], | |
191 :from_revision => change[:from_revision]) | |
192 end | |
193 | |
194 # Finds an issue that can be referenced by the commit message | |
195 def find_referenced_issue_by_id(id) | |
196 return nil if id.blank? | |
197 issue = Issue.find_by_id(id.to_i, :include => :project) | |
198 if Setting.commit_cross_project_ref? | |
199 # all issues can be referenced/fixed | |
200 elsif issue | |
201 # issue that belong to the repository project, a subproject or a parent project only | |
202 unless issue.project && | |
203 (project == issue.project || project.is_ancestor_of?(issue.project) || | |
204 project.is_descendant_of?(issue.project)) | |
205 issue = nil | |
206 end | |
207 end | |
208 issue | |
209 end | |
210 | |
211 private | |
212 | |
213 def fix_issue(issue) | |
214 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i) | |
215 if status.nil? | |
216 logger.warn("No status matches commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger | |
217 return issue | |
218 end | |
219 | |
220 # the issue may have been updated by the closure of another one (eg. duplicate) | |
221 issue.reload | |
222 # don't change the status is the issue is closed | |
223 return if issue.status && issue.status.is_closed? | |
224 | |
225 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag(issue.project))) | |
226 issue.status = status | |
227 unless Setting.commit_fix_done_ratio.blank? | |
228 issue.done_ratio = Setting.commit_fix_done_ratio.to_i | |
229 end | |
230 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update, | |
231 { :changeset => self, :issue => issue }) | |
232 unless issue.save | |
233 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger | |
234 end | |
235 issue | |
236 end | |
237 | |
238 def log_time(issue, hours) | |
239 time_entry = TimeEntry.new( | |
240 :user => user, | |
241 :hours => hours, | |
242 :issue => issue, | |
243 :spent_on => commit_date, | |
244 :comments => l(:text_time_logged_by_changeset, :value => text_tag(issue.project), | |
245 :locale => Setting.default_language) | |
246 ) | |
247 time_entry.activity = log_time_activity unless log_time_activity.nil? | |
248 | |
249 unless time_entry.save | |
250 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger | |
251 end | |
252 time_entry | |
253 end | |
254 | |
255 def log_time_activity | |
256 if Setting.commit_logtime_activity_id.to_i > 0 | |
257 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i) | |
258 end | |
259 end | |
260 | |
261 def split_comments | |
262 comments =~ /\A(.+?)\r?\n(.*)$/m | |
263 @short_comments = $1 || comments | |
264 @long_comments = $2.to_s.strip | |
265 return @short_comments, @long_comments | |
266 end | |
267 | |
268 public | |
269 | |
270 # Strips and reencodes a commit log before insertion into the database | |
271 def self.normalize_comments(str, encoding) | |
272 Changeset.to_utf8(str.to_s.strip, encoding) | |
273 end | |
274 | |
275 def self.to_utf8(str, encoding) | |
276 Redmine::CodesetUtil.to_utf8(str, encoding) | |
277 end | |
278 end |