annotate .svn/pristine/69/69267f9be067432ed3952ec606d218fa34373fd7.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 require 'SVG/Graph/Bar'
Chris@1517 19 require 'SVG/Graph/BarHorizontal'
Chris@1517 20 require 'digest/sha1'
Chris@1517 21 require 'redmine/scm/adapters'
Chris@1517 22
Chris@1517 23 class ChangesetNotFound < Exception; end
Chris@1517 24 class InvalidRevisionParam < Exception; end
Chris@1517 25
Chris@1517 26 class RepositoriesController < ApplicationController
Chris@1517 27 menu_item :repository
Chris@1517 28 menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers]
Chris@1517 29 default_search_scope :changesets
Chris@1517 30
Chris@1517 31 before_filter :find_project_by_project_id, :only => [:new, :create]
Chris@1517 32 before_filter :find_repository, :only => [:edit, :update, :destroy, :committers]
Chris@1517 33 before_filter :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers]
Chris@1517 34 before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue]
Chris@1517 35 before_filter :authorize
Chris@1517 36 accept_rss_auth :revisions
Chris@1517 37
Chris@1517 38 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
Chris@1517 39
Chris@1517 40 def new
Chris@1517 41 scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
Chris@1517 42 @repository = Repository.factory(scm)
Chris@1517 43 @repository.is_default = @project.repository.nil?
Chris@1517 44 @repository.project = @project
Chris@1517 45 end
Chris@1517 46
Chris@1517 47 def create
Chris@1517 48 attrs = pickup_extra_info
Chris@1517 49 @repository = Repository.factory(params[:repository_scm])
Chris@1517 50 @repository.safe_attributes = params[:repository]
Chris@1517 51 if attrs[:attrs_extra].keys.any?
Chris@1517 52 @repository.merge_extra_info(attrs[:attrs_extra])
Chris@1517 53 end
Chris@1517 54 @repository.project = @project
Chris@1517 55 if request.post? && @repository.save
Chris@1517 56 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@1517 57 else
Chris@1517 58 render :action => 'new'
Chris@1517 59 end
Chris@1517 60 end
Chris@1517 61
Chris@1517 62 def edit
Chris@1517 63 end
Chris@1517 64
Chris@1517 65 def update
Chris@1517 66 attrs = pickup_extra_info
Chris@1517 67 @repository.safe_attributes = attrs[:attrs]
Chris@1517 68 if attrs[:attrs_extra].keys.any?
Chris@1517 69 @repository.merge_extra_info(attrs[:attrs_extra])
Chris@1517 70 end
Chris@1517 71 @repository.project = @project
Chris@1517 72 if request.put? && @repository.save
Chris@1517 73 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@1517 74 else
Chris@1517 75 render :action => 'edit'
Chris@1517 76 end
Chris@1517 77 end
Chris@1517 78
Chris@1517 79 def pickup_extra_info
Chris@1517 80 p = {}
Chris@1517 81 p_extra = {}
Chris@1517 82 params[:repository].each do |k, v|
Chris@1517 83 if k =~ /^extra_/
Chris@1517 84 p_extra[k] = v
Chris@1517 85 else
Chris@1517 86 p[k] = v
Chris@1517 87 end
Chris@1517 88 end
Chris@1517 89 {:attrs => p, :attrs_extra => p_extra}
Chris@1517 90 end
Chris@1517 91 private :pickup_extra_info
Chris@1517 92
Chris@1517 93 def committers
Chris@1517 94 @committers = @repository.committers
Chris@1517 95 @users = @project.users
Chris@1517 96 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
Chris@1517 97 @users += User.where(:id => additional_user_ids).all unless additional_user_ids.empty?
Chris@1517 98 @users.compact!
Chris@1517 99 @users.sort!
Chris@1517 100 if request.post? && params[:committers].is_a?(Hash)
Chris@1517 101 # Build a hash with repository usernames as keys and corresponding user ids as values
Chris@1517 102 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
Chris@1517 103 flash[:notice] = l(:notice_successful_update)
Chris@1517 104 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@1517 105 end
Chris@1517 106 end
Chris@1517 107
Chris@1517 108 def destroy
Chris@1517 109 @repository.destroy if request.delete?
Chris@1517 110 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@1517 111 end
Chris@1517 112
Chris@1517 113 def show
Chris@1517 114 @repository.fetch_changesets if @project.active? && Setting.autofetch_changesets? && @path.empty?
Chris@1517 115
Chris@1517 116 @entries = @repository.entries(@path, @rev)
Chris@1517 117 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 118 if request.xhr?
Chris@1517 119 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
Chris@1517 120 else
Chris@1517 121 (show_error_not_found; return) unless @entries
Chris@1517 122 @changesets = @repository.latest_changesets(@path, @rev)
Chris@1517 123 @properties = @repository.properties(@path, @rev)
Chris@1517 124 @repositories = @project.repositories
Chris@1517 125 render :action => 'show'
Chris@1517 126 end
Chris@1517 127 end
Chris@1517 128
Chris@1517 129 alias_method :browse, :show
Chris@1517 130
Chris@1517 131 def changes
Chris@1517 132 @entry = @repository.entry(@path, @rev)
Chris@1517 133 (show_error_not_found; return) unless @entry
Chris@1517 134 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
Chris@1517 135 @properties = @repository.properties(@path, @rev)
Chris@1517 136 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 137 end
Chris@1517 138
Chris@1517 139 def revisions
Chris@1517 140 @changeset_count = @repository.changesets.count
Chris@1517 141 @changeset_pages = Paginator.new @changeset_count,
Chris@1517 142 per_page_option,
Chris@1517 143 params['page']
Chris@1517 144 @changesets = @repository.changesets.
Chris@1517 145 limit(@changeset_pages.per_page).
Chris@1517 146 offset(@changeset_pages.offset).
Chris@1517 147 includes(:user, :repository, :parents).
Chris@1517 148 all
Chris@1517 149
Chris@1517 150 respond_to do |format|
Chris@1517 151 format.html { render :layout => false if request.xhr? }
Chris@1517 152 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
Chris@1517 153 end
Chris@1517 154 end
Chris@1517 155
Chris@1517 156 def raw
Chris@1517 157 entry_and_raw(true)
Chris@1517 158 end
Chris@1517 159
Chris@1517 160 def entry
Chris@1517 161 entry_and_raw(false)
Chris@1517 162 end
Chris@1517 163
Chris@1517 164 def entry_and_raw(is_raw)
Chris@1517 165 @entry = @repository.entry(@path, @rev)
Chris@1517 166 (show_error_not_found; return) unless @entry
Chris@1517 167
Chris@1517 168 # If the entry is a dir, show the browser
Chris@1517 169 (show; return) if @entry.is_dir?
Chris@1517 170
Chris@1517 171 @content = @repository.cat(@path, @rev)
Chris@1517 172 (show_error_not_found; return) unless @content
Chris@1517 173 if is_raw ||
Chris@1517 174 (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
Chris@1517 175 ! is_entry_text_data?(@content, @path)
Chris@1517 176 # Force the download
Chris@1517 177 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
Chris@1517 178 send_type = Redmine::MimeType.of(@path)
Chris@1517 179 send_opt[:type] = send_type.to_s if send_type
Chris@1517 180 send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
Chris@1517 181 send_data @content, send_opt
Chris@1517 182 else
Chris@1517 183 # Prevent empty lines when displaying a file with Windows style eol
Chris@1517 184 # TODO: UTF-16
Chris@1517 185 # Is this needs? AttachmentsController reads file simply.
Chris@1517 186 @content.gsub!("\r\n", "\n")
Chris@1517 187 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 188 end
Chris@1517 189 end
Chris@1517 190 private :entry_and_raw
Chris@1517 191
Chris@1517 192 def is_entry_text_data?(ent, path)
Chris@1517 193 # UTF-16 contains "\x00".
Chris@1517 194 # It is very strict that file contains less than 30% of ascii symbols
Chris@1517 195 # in non Western Europe.
Chris@1517 196 return true if Redmine::MimeType.is_type?('text', path)
Chris@1517 197 # Ruby 1.8.6 has a bug of integer divisions.
Chris@1517 198 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
Chris@1517 199 return false if ent.is_binary_data?
Chris@1517 200 true
Chris@1517 201 end
Chris@1517 202 private :is_entry_text_data?
Chris@1517 203
Chris@1517 204 def annotate
Chris@1517 205 @entry = @repository.entry(@path, @rev)
Chris@1517 206 (show_error_not_found; return) unless @entry
Chris@1517 207
Chris@1517 208 @annotate = @repository.scm.annotate(@path, @rev)
Chris@1517 209 if @annotate.nil? || @annotate.empty?
Chris@1517 210 (render_error l(:error_scm_annotate); return)
Chris@1517 211 end
Chris@1517 212 ann_buf_size = 0
Chris@1517 213 @annotate.lines.each do |buf|
Chris@1517 214 ann_buf_size += buf.size
Chris@1517 215 end
Chris@1517 216 if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
Chris@1517 217 (render_error l(:error_scm_annotate_big_text_file); return)
Chris@1517 218 end
Chris@1517 219 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 220 end
Chris@1517 221
Chris@1517 222 def revision
Chris@1517 223 respond_to do |format|
Chris@1517 224 format.html
Chris@1517 225 format.js {render :layout => false}
Chris@1517 226 end
Chris@1517 227 end
Chris@1517 228
Chris@1517 229 # Adds a related issue to a changeset
Chris@1517 230 # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues
Chris@1517 231 def add_related_issue
Chris@1517 232 issue_id = params[:issue_id].to_s.sub(/^#/,'')
Chris@1517 233 @issue = @changeset.find_referenced_issue_by_id(issue_id)
Chris@1517 234 if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
Chris@1517 235 @issue = nil
Chris@1517 236 end
Chris@1517 237
Chris@1517 238 if @issue
Chris@1517 239 @changeset.issues << @issue
Chris@1517 240 end
Chris@1517 241 end
Chris@1517 242
Chris@1517 243 # Removes a related issue from a changeset
Chris@1517 244 # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
Chris@1517 245 def remove_related_issue
Chris@1517 246 @issue = Issue.visible.find_by_id(params[:issue_id])
Chris@1517 247 if @issue
Chris@1517 248 @changeset.issues.delete(@issue)
Chris@1517 249 end
Chris@1517 250 end
Chris@1517 251
Chris@1517 252 def diff
Chris@1517 253 if params[:format] == 'diff'
Chris@1517 254 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@1517 255 (show_error_not_found; return) unless @diff
Chris@1517 256 filename = "changeset_r#{@rev}"
Chris@1517 257 filename << "_r#{@rev_to}" if @rev_to
Chris@1517 258 send_data @diff.join, :filename => "#{filename}.diff",
Chris@1517 259 :type => 'text/x-patch',
Chris@1517 260 :disposition => 'attachment'
Chris@1517 261 else
Chris@1517 262 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@1517 263 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@1517 264
Chris@1517 265 # Save diff type as user preference
Chris@1517 266 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@1517 267 User.current.pref[:diff_type] = @diff_type
Chris@1517 268 User.current.preference.save
Chris@1517 269 end
Chris@1517 270 @cache_key = "repositories/diff/#{@repository.id}/" +
Chris@1517 271 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
Chris@1517 272 unless read_fragment(@cache_key)
Chris@1517 273 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@1517 274 show_error_not_found unless @diff
Chris@1517 275 end
Chris@1517 276
Chris@1517 277 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 278 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
Chris@1517 279 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
Chris@1517 280 end
Chris@1517 281 end
Chris@1517 282
Chris@1517 283 def stats
Chris@1517 284 end
Chris@1517 285
Chris@1517 286 def graph
Chris@1517 287 data = nil
Chris@1517 288 case params[:graph]
Chris@1517 289 when "commits_per_month"
Chris@1517 290 data = graph_commits_per_month(@repository)
Chris@1517 291 when "commits_per_author"
Chris@1517 292 data = graph_commits_per_author(@repository)
Chris@1517 293 end
Chris@1517 294 if data
Chris@1517 295 headers["Content-Type"] = "image/svg+xml"
Chris@1517 296 send_data(data, :type => "image/svg+xml", :disposition => "inline")
Chris@1517 297 else
Chris@1517 298 render_404
Chris@1517 299 end
Chris@1517 300 end
Chris@1517 301
Chris@1517 302 private
Chris@1517 303
Chris@1517 304 def find_repository
Chris@1517 305 @repository = Repository.find(params[:id])
Chris@1517 306 @project = @repository.project
Chris@1517 307 rescue ActiveRecord::RecordNotFound
Chris@1517 308 render_404
Chris@1517 309 end
Chris@1517 310
Chris@1517 311 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
Chris@1517 312
Chris@1517 313 def find_project_repository
Chris@1517 314 @project = Project.find(params[:id])
Chris@1517 315 if params[:repository_id].present?
Chris@1517 316 @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
Chris@1517 317 else
Chris@1517 318 @repository = @project.repository
Chris@1517 319 end
Chris@1517 320 (render_404; return false) unless @repository
Chris@1517 321 @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
Chris@1517 322 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
Chris@1517 323 @rev_to = params[:rev_to]
Chris@1517 324
Chris@1517 325 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
Chris@1517 326 if @repository.branches.blank?
Chris@1517 327 raise InvalidRevisionParam
Chris@1517 328 end
Chris@1517 329 end
Chris@1517 330 rescue ActiveRecord::RecordNotFound
Chris@1517 331 render_404
Chris@1517 332 rescue InvalidRevisionParam
Chris@1517 333 show_error_not_found
Chris@1517 334 end
Chris@1517 335
Chris@1517 336 def find_changeset
Chris@1517 337 if @rev.present?
Chris@1517 338 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1517 339 end
Chris@1517 340 show_error_not_found unless @changeset
Chris@1517 341 end
Chris@1517 342
Chris@1517 343 def show_error_not_found
Chris@1517 344 render_error :message => l(:error_scm_not_found), :status => 404
Chris@1517 345 end
Chris@1517 346
Chris@1517 347 # Handler for Redmine::Scm::Adapters::CommandFailed exception
Chris@1517 348 def show_error_command_failed(exception)
Chris@1517 349 render_error l(:error_scm_command_failed, exception.message)
Chris@1517 350 end
Chris@1517 351
Chris@1517 352 def graph_commits_per_month(repository)
Chris@1517 353 @date_to = Date.today
Chris@1517 354 @date_from = @date_to << 11
Chris@1517 355 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
Chris@1517 356 commits_by_day = Changeset.
Chris@1517 357 where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
Chris@1517 358 group(:commit_date).
Chris@1517 359 count
Chris@1517 360 commits_by_month = [0] * 12
Chris@1517 361 commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@1517 362
Chris@1517 363 changes_by_day = Change.
Chris@1517 364 joins(:changeset).
Chris@1517 365 where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
Chris@1517 366 group(:commit_date).
Chris@1517 367 count
Chris@1517 368 changes_by_month = [0] * 12
Chris@1517 369 changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@1517 370
Chris@1517 371 fields = []
Chris@1517 372 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
Chris@1517 373
Chris@1517 374 graph = SVG::Graph::Bar.new(
Chris@1517 375 :height => 300,
Chris@1517 376 :width => 800,
Chris@1517 377 :fields => fields.reverse,
Chris@1517 378 :stack => :side,
Chris@1517 379 :scale_integers => true,
Chris@1517 380 :step_x_labels => 2,
Chris@1517 381 :show_data_values => false,
Chris@1517 382 :graph_title => l(:label_commits_per_month),
Chris@1517 383 :show_graph_title => true
Chris@1517 384 )
Chris@1517 385
Chris@1517 386 graph.add_data(
Chris@1517 387 :data => commits_by_month[0..11].reverse,
Chris@1517 388 :title => l(:label_revision_plural)
Chris@1517 389 )
Chris@1517 390
Chris@1517 391 graph.add_data(
Chris@1517 392 :data => changes_by_month[0..11].reverse,
Chris@1517 393 :title => l(:label_change_plural)
Chris@1517 394 )
Chris@1517 395
Chris@1517 396 graph.burn
Chris@1517 397 end
Chris@1517 398
Chris@1517 399 def graph_commits_per_author(repository)
Chris@1517 400 commits_by_author = Changeset.where("repository_id = ?", repository.id).group(:committer).count
Chris@1517 401 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
Chris@1517 402
Chris@1517 403 changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", repository.id).group(:committer).count
Chris@1517 404 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
Chris@1517 405
Chris@1517 406 fields = commits_by_author.collect {|r| r.first}
Chris@1517 407 commits_data = commits_by_author.collect {|r| r.last}
Chris@1517 408 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
Chris@1517 409
Chris@1517 410 fields = fields + [""]*(10 - fields.length) if fields.length<10
Chris@1517 411 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
Chris@1517 412 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
Chris@1517 413
Chris@1517 414 # Remove email adress in usernames
Chris@1517 415 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
Chris@1517 416
Chris@1517 417 graph = SVG::Graph::BarHorizontal.new(
Chris@1517 418 :height => 30 * commits_data.length,
Chris@1517 419 :width => 800,
Chris@1517 420 :fields => fields,
Chris@1517 421 :stack => :side,
Chris@1517 422 :scale_integers => true,
Chris@1517 423 :show_data_values => false,
Chris@1517 424 :rotate_y_labels => false,
Chris@1517 425 :graph_title => l(:label_commits_per_author),
Chris@1517 426 :show_graph_title => true
Chris@1517 427 )
Chris@1517 428 graph.add_data(
Chris@1517 429 :data => commits_data,
Chris@1517 430 :title => l(:label_revision_plural)
Chris@1517 431 )
Chris@1517 432 graph.add_data(
Chris@1517 433 :data => changes_data,
Chris@1517 434 :title => l(:label_change_plural)
Chris@1517 435 )
Chris@1517 436 graph.burn
Chris@1517 437 end
Chris@1517 438 end