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