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