annotate .svn/pristine/0b/0b97b483eed443a57933ffcafedb4eb971db5cf5.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 cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class ScmFetchError < Exception; end
Chris@909 19
Chris@909 20 class Repository < ActiveRecord::Base
Chris@909 21 include Redmine::Ciphering
Chris@909 22
Chris@909 23 belongs_to :project
Chris@909 24 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
Chris@909 25 has_many :changes, :through => :changesets
Chris@909 26
Chris@909 27 serialize :extra_info
Chris@909 28
Chris@909 29 # Raw SQL to delete changesets and changes in the database
Chris@909 30 # has_many :changesets, :dependent => :destroy is too slow for big repositories
Chris@909 31 before_destroy :clear_changesets
Chris@909 32
Chris@909 33 validates_length_of :password, :maximum => 255, :allow_nil => true
Chris@909 34 # Checks if the SCM is enabled when creating a repository
Chris@909 35 validate :repo_create_validation, :on => :create
Chris@909 36
Chris@909 37 def repo_create_validation
Chris@909 38 unless Setting.enabled_scm.include?(self.class.name.demodulize)
Chris@909 39 errors.add(:type, :invalid)
Chris@909 40 end
Chris@909 41 end
Chris@909 42
Chris@909 43 def self.human_attribute_name(attribute_key_name)
Chris@909 44 attr_name = attribute_key_name
Chris@909 45 if attr_name == "log_encoding"
Chris@909 46 attr_name = "commit_logs_encoding"
Chris@909 47 end
Chris@909 48 super(attr_name)
Chris@909 49 end
Chris@909 50
Chris@909 51 # Removes leading and trailing whitespace
Chris@909 52 def url=(arg)
Chris@909 53 write_attribute(:url, arg ? arg.to_s.strip : nil)
Chris@909 54 end
Chris@909 55
Chris@909 56 # Removes leading and trailing whitespace
Chris@909 57 def root_url=(arg)
Chris@909 58 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
Chris@909 59 end
Chris@909 60
Chris@909 61 def password
Chris@909 62 read_ciphered_attribute(:password)
Chris@909 63 end
Chris@909 64
Chris@909 65 def password=(arg)
Chris@909 66 write_ciphered_attribute(:password, arg)
Chris@909 67 end
Chris@909 68
Chris@909 69 def scm_adapter
Chris@909 70 self.class.scm_adapter_class
Chris@909 71 end
Chris@909 72
Chris@909 73 def scm
Chris@909 74 @scm ||= self.scm_adapter.new(url, root_url,
Chris@909 75 login, password, path_encoding)
Chris@909 76 update_attribute(:root_url, @scm.root_url) if root_url.blank?
Chris@909 77 @scm
Chris@909 78 end
Chris@909 79
Chris@909 80 def scm_name
Chris@909 81 self.class.scm_name
Chris@909 82 end
Chris@909 83
Chris@909 84 def merge_extra_info(arg)
Chris@909 85 h = extra_info || {}
Chris@909 86 return h if arg.nil?
Chris@909 87 h.merge!(arg)
Chris@909 88 write_attribute(:extra_info, h)
Chris@909 89 end
Chris@909 90
Chris@909 91 def report_last_commit
Chris@909 92 true
Chris@909 93 end
Chris@909 94
Chris@909 95 def supports_cat?
Chris@909 96 scm.supports_cat?
Chris@909 97 end
Chris@909 98
Chris@909 99 def supports_annotate?
Chris@909 100 scm.supports_annotate?
Chris@909 101 end
Chris@909 102
Chris@909 103 def supports_all_revisions?
Chris@909 104 true
Chris@909 105 end
Chris@909 106
Chris@909 107 def supports_directory_revisions?
Chris@909 108 false
Chris@909 109 end
Chris@909 110
Chris@909 111 def supports_revision_graph?
Chris@909 112 false
Chris@909 113 end
Chris@909 114
Chris@909 115 def entry(path=nil, identifier=nil)
Chris@909 116 scm.entry(path, identifier)
Chris@909 117 end
Chris@909 118
Chris@909 119 def entries(path=nil, identifier=nil)
Chris@909 120 scm.entries(path, identifier)
Chris@909 121 end
Chris@909 122
Chris@909 123 def branches
Chris@909 124 scm.branches
Chris@909 125 end
Chris@909 126
Chris@909 127 def tags
Chris@909 128 scm.tags
Chris@909 129 end
Chris@909 130
Chris@909 131 def default_branch
Chris@909 132 nil
Chris@909 133 end
Chris@909 134
Chris@909 135 def properties(path, identifier=nil)
Chris@909 136 scm.properties(path, identifier)
Chris@909 137 end
Chris@909 138
Chris@909 139 def cat(path, identifier=nil)
Chris@909 140 scm.cat(path, identifier)
Chris@909 141 end
Chris@909 142
Chris@909 143 def diff(path, rev, rev_to)
Chris@909 144 scm.diff(path, rev, rev_to)
Chris@909 145 end
Chris@909 146
Chris@909 147 def diff_format_revisions(cs, cs_to, sep=':')
Chris@909 148 text = ""
Chris@909 149 text << cs_to.format_identifier + sep if cs_to
Chris@909 150 text << cs.format_identifier if cs
Chris@909 151 text
Chris@909 152 end
Chris@909 153
Chris@909 154 # Returns a path relative to the url of the repository
Chris@909 155 def relative_path(path)
Chris@909 156 path
Chris@909 157 end
Chris@909 158
Chris@909 159 # Finds and returns a revision with a number or the beginning of a hash
Chris@909 160 def find_changeset_by_name(name)
Chris@909 161 return nil if name.blank?
Chris@909 162 changesets.find(:first, :conditions => (name.match(/^\d*$/) ?
Chris@909 163 ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%']))
Chris@909 164 end
Chris@909 165
Chris@909 166 def latest_changeset
Chris@909 167 @latest_changeset ||= changesets.find(:first)
Chris@909 168 end
Chris@909 169
Chris@909 170 # Returns the latest changesets for +path+
Chris@909 171 # Default behaviour is to search in cached changesets
Chris@909 172 def latest_changesets(path, rev, limit=10)
Chris@909 173 if path.blank?
Chris@909 174 changesets.find(
Chris@909 175 :all,
Chris@909 176 :include => :user,
Chris@909 177 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@909 178 :limit => limit)
Chris@909 179 else
Chris@909 180 changes.find(
Chris@909 181 :all,
Chris@909 182 :include => {:changeset => :user},
Chris@909 183 :conditions => ["path = ?", path.with_leading_slash],
Chris@909 184 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@909 185 :limit => limit
Chris@909 186 ).collect(&:changeset)
Chris@909 187 end
Chris@909 188 end
Chris@909 189
Chris@909 190 def scan_changesets_for_issue_ids
Chris@909 191 self.changesets.each(&:scan_comment_for_issue_ids)
Chris@909 192 end
Chris@909 193
Chris@909 194 # Returns an array of committers usernames and associated user_id
Chris@909 195 def committers
Chris@909 196 @committers ||= Changeset.connection.select_rows(
Chris@909 197 "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
Chris@909 198 end
Chris@909 199
Chris@909 200 # Maps committers username to a user ids
Chris@909 201 def committer_ids=(h)
Chris@909 202 if h.is_a?(Hash)
Chris@909 203 committers.each do |committer, user_id|
Chris@909 204 new_user_id = h[committer]
Chris@909 205 if new_user_id && (new_user_id.to_i != user_id.to_i)
Chris@909 206 new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
Chris@909 207 Changeset.update_all(
Chris@909 208 "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }",
Chris@909 209 ["repository_id = ? AND committer = ?", id, committer])
Chris@909 210 end
Chris@909 211 end
Chris@909 212 @committers = nil
Chris@909 213 @found_committer_users = nil
Chris@909 214 true
Chris@909 215 else
Chris@909 216 false
Chris@909 217 end
Chris@909 218 end
Chris@909 219
Chris@909 220 # Returns the Redmine User corresponding to the given +committer+
Chris@909 221 # It will return nil if the committer is not yet mapped and if no User
Chris@909 222 # with the same username or email was found
Chris@909 223 def find_committer_user(committer)
Chris@909 224 unless committer.blank?
Chris@909 225 @found_committer_users ||= {}
Chris@909 226 return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
Chris@909 227
Chris@909 228 user = nil
Chris@909 229 c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
Chris@909 230 if c && c.user
Chris@909 231 user = c.user
Chris@909 232 elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
Chris@909 233 username, email = $1.strip, $3
Chris@909 234 u = User.find_by_login(username)
Chris@909 235 u ||= User.find_by_mail(email) unless email.blank?
Chris@909 236 user = u
Chris@909 237 end
Chris@909 238 @found_committer_users[committer] = user
Chris@909 239 user
Chris@909 240 end
Chris@909 241 end
Chris@909 242
Chris@909 243 def repo_log_encoding
Chris@909 244 encoding = log_encoding.to_s.strip
Chris@909 245 encoding.blank? ? 'UTF-8' : encoding
Chris@909 246 end
Chris@909 247
Chris@909 248 # Fetches new changesets for all repositories of active projects
Chris@909 249 # Can be called periodically by an external script
Chris@909 250 # eg. ruby script/runner "Repository.fetch_changesets"
Chris@909 251 def self.fetch_changesets
Chris@909 252 Project.active.has_module(:repository).find(:all, :include => :repository).each do |project|
Chris@909 253 if project.repository
Chris@909 254 begin
Chris@909 255 project.repository.fetch_changesets
Chris@909 256 rescue Redmine::Scm::Adapters::CommandFailed => e
Chris@909 257 logger.error "scm: error during fetching changesets: #{e.message}"
Chris@909 258 end
Chris@909 259 end
Chris@909 260 end
Chris@909 261 end
Chris@909 262
Chris@909 263 # scan changeset comments to find related and fixed issues for all repositories
Chris@909 264 def self.scan_changesets_for_issue_ids
Chris@909 265 find(:all).each(&:scan_changesets_for_issue_ids)
Chris@909 266 end
Chris@909 267
Chris@909 268 def self.scm_name
Chris@909 269 'Abstract'
Chris@909 270 end
Chris@909 271
Chris@909 272 def self.available_scm
Chris@909 273 subclasses.collect {|klass| [klass.scm_name, klass.name]}
Chris@909 274 end
Chris@909 275
Chris@909 276 def self.factory(klass_name, *args)
Chris@909 277 klass = "Repository::#{klass_name}".constantize
Chris@909 278 klass.new(*args)
Chris@909 279 rescue
Chris@909 280 nil
Chris@909 281 end
Chris@909 282
Chris@909 283 def self.scm_adapter_class
Chris@909 284 nil
Chris@909 285 end
Chris@909 286
Chris@909 287 def self.scm_command
Chris@909 288 ret = ""
Chris@909 289 begin
Chris@909 290 ret = self.scm_adapter_class.client_command if self.scm_adapter_class
Chris@909 291 rescue Exception => e
Chris@909 292 logger.error "scm: error during get command: #{e.message}"
Chris@909 293 end
Chris@909 294 ret
Chris@909 295 end
Chris@909 296
Chris@909 297 def self.scm_version_string
Chris@909 298 ret = ""
Chris@909 299 begin
Chris@909 300 ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
Chris@909 301 rescue Exception => e
Chris@909 302 logger.error "scm: error during get version string: #{e.message}"
Chris@909 303 end
Chris@909 304 ret
Chris@909 305 end
Chris@909 306
Chris@909 307 def self.scm_available
Chris@909 308 ret = false
Chris@909 309 begin
Chris@909 310 ret = self.scm_adapter_class.client_available if self.scm_adapter_class
Chris@909 311 rescue Exception => e
Chris@909 312 logger.error "scm: error during get scm available: #{e.message}"
Chris@909 313 end
Chris@909 314 ret
Chris@909 315 end
Chris@909 316
Chris@909 317 private
Chris@909 318
Chris@909 319 def clear_changesets
Chris@909 320 cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}"
Chris@909 321 connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@909 322 connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@909 323 connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
Chris@909 324 end
Chris@909 325 end