annotate .svn/pristine/9b/9b73141dc59eaf9663debcda41c84f3c59a0d34a.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 ScmFetchError < Exception; end
Chris@1517 19
Chris@1517 20 class Repository < ActiveRecord::Base
Chris@1517 21 include Redmine::Ciphering
Chris@1517 22 include Redmine::SafeAttributes
Chris@1517 23
Chris@1517 24 # Maximum length for repository identifiers
Chris@1517 25 IDENTIFIER_MAX_LENGTH = 255
Chris@1517 26
Chris@1517 27 belongs_to :project
Chris@1517 28 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
Chris@1517 29 has_many :filechanges, :class_name => 'Change', :through => :changesets
Chris@1517 30
Chris@1517 31 serialize :extra_info
Chris@1517 32
Chris@1517 33 before_save :check_default
Chris@1517 34
Chris@1517 35 # Raw SQL to delete changesets and changes in the database
Chris@1517 36 # has_many :changesets, :dependent => :destroy is too slow for big repositories
Chris@1517 37 before_destroy :clear_changesets
Chris@1517 38
Chris@1517 39 validates_length_of :password, :maximum => 255, :allow_nil => true
Chris@1517 40 validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH, :allow_blank => true
Chris@1517 41 validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? }
Chris@1517 42 validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true
Chris@1517 43 validates_exclusion_of :identifier, :in => %w(browse show entry raw changes annotate diff statistics graph revisions revision)
Chris@1517 44 # donwcase letters, digits, dashes, underscores but not digits only
Chris@1517 45 validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :allow_blank => true
Chris@1517 46 # Checks if the SCM is enabled when creating a repository
Chris@1517 47 validate :repo_create_validation, :on => :create
Chris@1517 48
Chris@1517 49 safe_attributes 'identifier',
Chris@1517 50 'login',
Chris@1517 51 'password',
Chris@1517 52 'path_encoding',
Chris@1517 53 'log_encoding',
Chris@1517 54 'is_default'
Chris@1517 55
Chris@1517 56 safe_attributes 'url',
Chris@1517 57 :if => lambda {|repository, user| repository.new_record?}
Chris@1517 58
Chris@1517 59 def repo_create_validation
Chris@1517 60 unless Setting.enabled_scm.include?(self.class.name.demodulize)
Chris@1517 61 errors.add(:type, :invalid)
Chris@1517 62 end
Chris@1517 63 end
Chris@1517 64
Chris@1517 65 def self.human_attribute_name(attribute_key_name, *args)
Chris@1517 66 attr_name = attribute_key_name.to_s
Chris@1517 67 if attr_name == "log_encoding"
Chris@1517 68 attr_name = "commit_logs_encoding"
Chris@1517 69 end
Chris@1517 70 super(attr_name, *args)
Chris@1517 71 end
Chris@1517 72
Chris@1517 73 # Removes leading and trailing whitespace
Chris@1517 74 def url=(arg)
Chris@1517 75 write_attribute(:url, arg ? arg.to_s.strip : nil)
Chris@1517 76 end
Chris@1517 77
Chris@1517 78 # Removes leading and trailing whitespace
Chris@1517 79 def root_url=(arg)
Chris@1517 80 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
Chris@1517 81 end
Chris@1517 82
Chris@1517 83 def password
Chris@1517 84 read_ciphered_attribute(:password)
Chris@1517 85 end
Chris@1517 86
Chris@1517 87 def password=(arg)
Chris@1517 88 write_ciphered_attribute(:password, arg)
Chris@1517 89 end
Chris@1517 90
Chris@1517 91 def scm_adapter
Chris@1517 92 self.class.scm_adapter_class
Chris@1517 93 end
Chris@1517 94
Chris@1517 95 def scm
Chris@1517 96 unless @scm
Chris@1517 97 @scm = self.scm_adapter.new(url, root_url,
Chris@1517 98 login, password, path_encoding)
Chris@1517 99 if root_url.blank? && @scm.root_url.present?
Chris@1517 100 update_attribute(:root_url, @scm.root_url)
Chris@1517 101 end
Chris@1517 102 end
Chris@1517 103 @scm
Chris@1517 104 end
Chris@1517 105
Chris@1517 106 def scm_name
Chris@1517 107 self.class.scm_name
Chris@1517 108 end
Chris@1517 109
Chris@1517 110 def name
Chris@1517 111 if identifier.present?
Chris@1517 112 identifier
Chris@1517 113 elsif is_default?
Chris@1517 114 l(:field_repository_is_default)
Chris@1517 115 else
Chris@1517 116 scm_name
Chris@1517 117 end
Chris@1517 118 end
Chris@1517 119
Chris@1517 120 def identifier=(identifier)
Chris@1517 121 super unless identifier_frozen?
Chris@1517 122 end
Chris@1517 123
Chris@1517 124 def identifier_frozen?
Chris@1517 125 errors[:identifier].blank? && !(new_record? || identifier.blank?)
Chris@1517 126 end
Chris@1517 127
Chris@1517 128 def identifier_param
Chris@1517 129 if is_default?
Chris@1517 130 nil
Chris@1517 131 elsif identifier.present?
Chris@1517 132 identifier
Chris@1517 133 else
Chris@1517 134 id.to_s
Chris@1517 135 end
Chris@1517 136 end
Chris@1517 137
Chris@1517 138 def <=>(repository)
Chris@1517 139 if is_default?
Chris@1517 140 -1
Chris@1517 141 elsif repository.is_default?
Chris@1517 142 1
Chris@1517 143 else
Chris@1517 144 identifier.to_s <=> repository.identifier.to_s
Chris@1517 145 end
Chris@1517 146 end
Chris@1517 147
Chris@1517 148 def self.find_by_identifier_param(param)
Chris@1517 149 if param.to_s =~ /^\d+$/
Chris@1517 150 find_by_id(param)
Chris@1517 151 else
Chris@1517 152 find_by_identifier(param)
Chris@1517 153 end
Chris@1517 154 end
Chris@1517 155
Chris@1517 156 # TODO: should return an empty hash instead of nil to avoid many ||{}
Chris@1517 157 def extra_info
Chris@1517 158 h = read_attribute(:extra_info)
Chris@1517 159 h.is_a?(Hash) ? h : nil
Chris@1517 160 end
Chris@1517 161
Chris@1517 162 def merge_extra_info(arg)
Chris@1517 163 h = extra_info || {}
Chris@1517 164 return h if arg.nil?
Chris@1517 165 h.merge!(arg)
Chris@1517 166 write_attribute(:extra_info, h)
Chris@1517 167 end
Chris@1517 168
Chris@1517 169 def report_last_commit
Chris@1517 170 true
Chris@1517 171 end
Chris@1517 172
Chris@1517 173 def supports_cat?
Chris@1517 174 scm.supports_cat?
Chris@1517 175 end
Chris@1517 176
Chris@1517 177 def supports_annotate?
Chris@1517 178 scm.supports_annotate?
Chris@1517 179 end
Chris@1517 180
Chris@1517 181 def supports_all_revisions?
Chris@1517 182 true
Chris@1517 183 end
Chris@1517 184
Chris@1517 185 def supports_directory_revisions?
Chris@1517 186 false
Chris@1517 187 end
Chris@1517 188
Chris@1517 189 def supports_revision_graph?
Chris@1517 190 false
Chris@1517 191 end
Chris@1517 192
Chris@1517 193 def entry(path=nil, identifier=nil)
Chris@1517 194 scm.entry(path, identifier)
Chris@1517 195 end
Chris@1517 196
Chris@1517 197 def scm_entries(path=nil, identifier=nil)
Chris@1517 198 scm.entries(path, identifier)
Chris@1517 199 end
Chris@1517 200 protected :scm_entries
Chris@1517 201
Chris@1517 202 def entries(path=nil, identifier=nil)
Chris@1517 203 entries = scm_entries(path, identifier)
Chris@1517 204 load_entries_changesets(entries)
Chris@1517 205 entries
Chris@1517 206 end
Chris@1517 207
Chris@1517 208 def branches
Chris@1517 209 scm.branches
Chris@1517 210 end
Chris@1517 211
Chris@1517 212 def tags
Chris@1517 213 scm.tags
Chris@1517 214 end
Chris@1517 215
Chris@1517 216 def default_branch
Chris@1517 217 nil
Chris@1517 218 end
Chris@1517 219
Chris@1517 220 def properties(path, identifier=nil)
Chris@1517 221 scm.properties(path, identifier)
Chris@1517 222 end
Chris@1517 223
Chris@1517 224 def cat(path, identifier=nil)
Chris@1517 225 scm.cat(path, identifier)
Chris@1517 226 end
Chris@1517 227
Chris@1517 228 def diff(path, rev, rev_to)
Chris@1517 229 scm.diff(path, rev, rev_to)
Chris@1517 230 end
Chris@1517 231
Chris@1517 232 def diff_format_revisions(cs, cs_to, sep=':')
Chris@1517 233 text = ""
Chris@1517 234 text << cs_to.format_identifier + sep if cs_to
Chris@1517 235 text << cs.format_identifier if cs
Chris@1517 236 text
Chris@1517 237 end
Chris@1517 238
Chris@1517 239 # Returns a path relative to the url of the repository
Chris@1517 240 def relative_path(path)
Chris@1517 241 path
Chris@1517 242 end
Chris@1517 243
Chris@1517 244 # Finds and returns a revision with a number or the beginning of a hash
Chris@1517 245 def find_changeset_by_name(name)
Chris@1517 246 return nil if name.blank?
Chris@1517 247 s = name.to_s
Chris@1517 248 if s.match(/^\d*$/)
Chris@1517 249 changesets.where("revision = ?", s).first
Chris@1517 250 else
Chris@1517 251 changesets.where("revision LIKE ?", s + '%').first
Chris@1517 252 end
Chris@1517 253 end
Chris@1517 254
Chris@1517 255 def latest_changeset
Chris@1517 256 @latest_changeset ||= changesets.first
Chris@1517 257 end
Chris@1517 258
Chris@1517 259 # Returns the latest changesets for +path+
Chris@1517 260 # Default behaviour is to search in cached changesets
Chris@1517 261 def latest_changesets(path, rev, limit=10)
Chris@1517 262 if path.blank?
Chris@1517 263 changesets.
Chris@1517 264 reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC").
Chris@1517 265 limit(limit).
Chris@1517 266 preload(:user).
Chris@1517 267 all
Chris@1517 268 else
Chris@1517 269 filechanges.
Chris@1517 270 where("path = ?", path.with_leading_slash).
Chris@1517 271 reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC").
Chris@1517 272 limit(limit).
Chris@1517 273 preload(:changeset => :user).
Chris@1517 274 collect(&:changeset)
Chris@1517 275 end
Chris@1517 276 end
Chris@1517 277
Chris@1517 278 def scan_changesets_for_issue_ids
Chris@1517 279 self.changesets.each(&:scan_comment_for_issue_ids)
Chris@1517 280 end
Chris@1517 281
Chris@1517 282 # Returns an array of committers usernames and associated user_id
Chris@1517 283 def committers
Chris@1517 284 @committers ||= Changeset.connection.select_rows(
Chris@1517 285 "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
Chris@1517 286 end
Chris@1517 287
Chris@1517 288 # Maps committers username to a user ids
Chris@1517 289 def committer_ids=(h)
Chris@1517 290 if h.is_a?(Hash)
Chris@1517 291 committers.each do |committer, user_id|
Chris@1517 292 new_user_id = h[committer]
Chris@1517 293 if new_user_id && (new_user_id.to_i != user_id.to_i)
Chris@1517 294 new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
Chris@1517 295 Changeset.where(["repository_id = ? AND committer = ?", id, committer]).
Chris@1517 296 update_all("user_id = #{new_user_id.nil? ? 'NULL' : new_user_id}")
Chris@1517 297 end
Chris@1517 298 end
Chris@1517 299 @committers = nil
Chris@1517 300 @found_committer_users = nil
Chris@1517 301 true
Chris@1517 302 else
Chris@1517 303 false
Chris@1517 304 end
Chris@1517 305 end
Chris@1517 306
Chris@1517 307 # Returns the Redmine User corresponding to the given +committer+
Chris@1517 308 # It will return nil if the committer is not yet mapped and if no User
Chris@1517 309 # with the same username or email was found
Chris@1517 310 def find_committer_user(committer)
Chris@1517 311 unless committer.blank?
Chris@1517 312 @found_committer_users ||= {}
Chris@1517 313 return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
Chris@1517 314
Chris@1517 315 user = nil
Chris@1517 316 c = changesets.where(:committer => committer).includes(:user).first
Chris@1517 317 if c && c.user
Chris@1517 318 user = c.user
Chris@1517 319 elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
Chris@1517 320 username, email = $1.strip, $3
Chris@1517 321 u = User.find_by_login(username)
Chris@1517 322 u ||= User.find_by_mail(email) unless email.blank?
Chris@1517 323 user = u
Chris@1517 324 end
Chris@1517 325 @found_committer_users[committer] = user
Chris@1517 326 user
Chris@1517 327 end
Chris@1517 328 end
Chris@1517 329
Chris@1517 330 def repo_log_encoding
Chris@1517 331 encoding = log_encoding.to_s.strip
Chris@1517 332 encoding.blank? ? 'UTF-8' : encoding
Chris@1517 333 end
Chris@1517 334
Chris@1517 335 # Fetches new changesets for all repositories of active projects
Chris@1517 336 # Can be called periodically by an external script
Chris@1517 337 # eg. ruby script/runner "Repository.fetch_changesets"
Chris@1517 338 def self.fetch_changesets
Chris@1517 339 Project.active.has_module(:repository).all.each do |project|
Chris@1517 340 project.repositories.each do |repository|
Chris@1517 341 begin
Chris@1517 342 repository.fetch_changesets
Chris@1517 343 rescue Redmine::Scm::Adapters::CommandFailed => e
Chris@1517 344 logger.error "scm: error during fetching changesets: #{e.message}"
Chris@1517 345 end
Chris@1517 346 end
Chris@1517 347 end
Chris@1517 348 end
Chris@1517 349
Chris@1517 350 # scan changeset comments to find related and fixed issues for all repositories
Chris@1517 351 def self.scan_changesets_for_issue_ids
Chris@1517 352 all.each(&:scan_changesets_for_issue_ids)
Chris@1517 353 end
Chris@1517 354
Chris@1517 355 def self.scm_name
Chris@1517 356 'Abstract'
Chris@1517 357 end
Chris@1517 358
Chris@1517 359 def self.available_scm
Chris@1517 360 subclasses.collect {|klass| [klass.scm_name, klass.name]}
Chris@1517 361 end
Chris@1517 362
Chris@1517 363 def self.factory(klass_name, *args)
Chris@1517 364 klass = "Repository::#{klass_name}".constantize
Chris@1517 365 klass.new(*args)
Chris@1517 366 rescue
Chris@1517 367 nil
Chris@1517 368 end
Chris@1517 369
Chris@1517 370 def self.scm_adapter_class
Chris@1517 371 nil
Chris@1517 372 end
Chris@1517 373
Chris@1517 374 def self.scm_command
Chris@1517 375 ret = ""
Chris@1517 376 begin
Chris@1517 377 ret = self.scm_adapter_class.client_command if self.scm_adapter_class
Chris@1517 378 rescue Exception => e
Chris@1517 379 logger.error "scm: error during get command: #{e.message}"
Chris@1517 380 end
Chris@1517 381 ret
Chris@1517 382 end
Chris@1517 383
Chris@1517 384 def self.scm_version_string
Chris@1517 385 ret = ""
Chris@1517 386 begin
Chris@1517 387 ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
Chris@1517 388 rescue Exception => e
Chris@1517 389 logger.error "scm: error during get version string: #{e.message}"
Chris@1517 390 end
Chris@1517 391 ret
Chris@1517 392 end
Chris@1517 393
Chris@1517 394 def self.scm_available
Chris@1517 395 ret = false
Chris@1517 396 begin
Chris@1517 397 ret = self.scm_adapter_class.client_available if self.scm_adapter_class
Chris@1517 398 rescue Exception => e
Chris@1517 399 logger.error "scm: error during get scm available: #{e.message}"
Chris@1517 400 end
Chris@1517 401 ret
Chris@1517 402 end
Chris@1517 403
Chris@1517 404 def set_as_default?
Chris@1517 405 new_record? && project && Repository.where(:project_id => project.id).empty?
Chris@1517 406 end
Chris@1517 407
Chris@1517 408 protected
Chris@1517 409
Chris@1517 410 def check_default
Chris@1517 411 if !is_default? && set_as_default?
Chris@1517 412 self.is_default = true
Chris@1517 413 end
Chris@1517 414 if is_default? && is_default_changed?
Chris@1517 415 Repository.where(["project_id = ?", project_id]).update_all(["is_default = ?", false])
Chris@1517 416 end
Chris@1517 417 end
Chris@1517 418
Chris@1517 419 def load_entries_changesets(entries)
Chris@1517 420 if entries
Chris@1517 421 entries.each do |entry|
Chris@1517 422 if entry.lastrev && entry.lastrev.identifier
Chris@1517 423 entry.changeset = find_changeset_by_name(entry.lastrev.identifier)
Chris@1517 424 end
Chris@1517 425 end
Chris@1517 426 end
Chris@1517 427 end
Chris@1517 428
Chris@1517 429 private
Chris@1517 430
Chris@1517 431 # Deletes repository data
Chris@1517 432 def clear_changesets
Chris@1517 433 cs = Changeset.table_name
Chris@1517 434 ch = Change.table_name
Chris@1517 435 ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}"
Chris@1517 436 cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}"
Chris@1517 437
Chris@1517 438 connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@1517 439 connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@1517 440 connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@1517 441 connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
Chris@1517 442 clear_extra_info_of_changesets
Chris@1517 443 end
Chris@1517 444
Chris@1517 445 def clear_extra_info_of_changesets
Chris@1517 446 end
Chris@1517 447 end