annotate .svn/pristine/20/205eb718e22b02ea59cca1358f48bb87e5bd7f8d.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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