annotate .svn/pristine/da/daba4fcb45f422512e01be77f3b55b7ccc6f33ba.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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