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