##// END OF EJS Templates
Test failure (#23410)....
Jean-Philippe Lang -
r15366:0925ce756c30
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,545 +1,551
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 default_search_scope :issues
20 20
21 21 before_action :find_issue, :only => [:show, :edit, :update]
22 22 before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
23 23 before_action :authorize, :except => [:index, :new, :create]
24 24 before_action :find_optional_project, :only => [:index, :new, :create]
25 25 before_action :build_new_issue_from_params, :only => [:new, :create]
26 26 accept_rss_auth :index, :show
27 27 accept_api_auth :index, :show, :create, :update, :destroy
28 28
29 29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
30 30
31 31 helper :journals
32 32 helper :projects
33 33 helper :custom_fields
34 34 helper :issue_relations
35 35 helper :watchers
36 36 helper :attachments
37 37 helper :queries
38 38 include QueriesHelper
39 39 helper :repositories
40 40 helper :sort
41 41 include SortHelper
42 42 helper :timelog
43 43
44 44 def index
45 45 retrieve_query
46 46 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
47 47 sort_update(@query.sortable_columns)
48 48 @query.sort_criteria = sort_criteria.to_a
49 49
50 50 if @query.valid?
51 51 case params[:format]
52 52 when 'csv', 'pdf'
53 53 @limit = Setting.issues_export_limit.to_i
54 54 if params[:columns] == 'all'
55 55 @query.column_names = @query.available_inline_columns.map(&:name)
56 56 end
57 57 when 'atom'
58 58 @limit = Setting.feeds_limit.to_i
59 59 when 'xml', 'json'
60 60 @offset, @limit = api_offset_and_limit
61 61 @query.column_names = %w(author)
62 62 else
63 63 @limit = per_page_option
64 64 end
65 65
66 66 @issue_count = @query.issue_count
67 67 @issue_pages = Paginator.new @issue_count, @limit, params['page']
68 68 @offset ||= @issue_pages.offset
69 69 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
70 70 :order => sort_clause,
71 71 :offset => @offset,
72 72 :limit => @limit)
73 73 @issue_count_by_group = @query.issue_count_by_group
74 74
75 75 respond_to do |format|
76 76 format.html { render :template => 'issues/index', :layout => !request.xhr? }
77 77 format.api {
78 78 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
79 79 }
80 80 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
81 81 format.csv { send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv') }
82 82 format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
83 83 end
84 84 else
85 85 respond_to do |format|
86 86 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
87 87 format.any(:atom, :csv, :pdf) { head 422 }
88 88 format.api { render_validation_errors(@query) }
89 89 end
90 90 end
91 91 rescue ActiveRecord::RecordNotFound
92 92 render_404
93 93 end
94 94
95 95 def show
96 96 @journals = @issue.journals.
97 97 preload(:details).
98 98 preload(:user => :email_address).
99 99 reorder(:created_on, :id).to_a
100 100 @journals.each_with_index {|j,i| j.indice = i+1}
101 101 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
102 102 Journal.preload_journals_details_custom_fields(@journals)
103 103 @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
104 104 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 105
106 106 @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
107 107 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
108 108
109 109 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
110 110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 111 @priorities = IssuePriority.active
112 112 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
113 113 @relation = IssueRelation.new
114 114
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf {
123 123 send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
124 124 }
125 125 end
126 126 end
127 127
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js
132 132 end
133 133 end
134 134
135 135 def create
136 136 unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
137 137 raise ::Unauthorized
138 138 end
139 139 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 140 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
141 141 if @issue.save
142 142 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
143 143 respond_to do |format|
144 144 format.html {
145 145 render_attachment_warning_if_needed(@issue)
146 146 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
147 147 redirect_after_create
148 148 }
149 149 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
150 150 end
151 151 return
152 152 else
153 153 respond_to do |format|
154 format.html { render :action => 'new' }
154 format.html {
155 if @issue.project.nil?
156 render_error :status => 422
157 else
158 render :action => 'new'
159 end
160 }
155 161 format.api { render_validation_errors(@issue) }
156 162 end
157 163 end
158 164 end
159 165
160 166 def edit
161 167 return unless update_issue_from_params
162 168
163 169 respond_to do |format|
164 170 format.html { }
165 171 format.js
166 172 end
167 173 end
168 174
169 175 def update
170 176 return unless update_issue_from_params
171 177 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
172 178 saved = false
173 179 begin
174 180 saved = save_issue_with_child_records
175 181 rescue ActiveRecord::StaleObjectError
176 182 @conflict = true
177 183 if params[:last_journal_id]
178 184 @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
179 185 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
180 186 end
181 187 end
182 188
183 189 if saved
184 190 render_attachment_warning_if_needed(@issue)
185 191 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
186 192
187 193 respond_to do |format|
188 194 format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) }
189 195 format.api { render_api_ok }
190 196 end
191 197 else
192 198 respond_to do |format|
193 199 format.html { render :action => 'edit' }
194 200 format.api { render_validation_errors(@issue) }
195 201 end
196 202 end
197 203 end
198 204
199 205 # Bulk edit/copy a set of issues
200 206 def bulk_edit
201 207 @issues.sort!
202 208 @copy = params[:copy].present?
203 209 @notes = params[:notes]
204 210
205 211 if @copy
206 212 unless User.current.allowed_to?(:copy_issues, @projects)
207 213 raise ::Unauthorized
208 214 end
209 215 else
210 216 unless @issues.all?(&:attributes_editable?)
211 217 raise ::Unauthorized
212 218 end
213 219 end
214 220
215 221 @allowed_projects = Issue.allowed_target_projects
216 222 if params[:issue]
217 223 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
218 224 if @target_project
219 225 target_projects = [@target_project]
220 226 end
221 227 end
222 228 target_projects ||= @projects
223 229
224 230 if @copy
225 231 # Copied issues will get their default statuses
226 232 @available_statuses = []
227 233 else
228 234 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
229 235 end
230 236 @custom_fields = @issues.map{|i|i.editable_custom_fields}.reduce(:&)
231 237 @assignables = target_projects.map(&:assignable_users).reduce(:&)
232 238 @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&)
233 239 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
234 240 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
235 241 if @copy
236 242 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
237 243 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
238 244 end
239 245
240 246 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
241 247
242 248 @issue_params = params[:issue] || {}
243 249 @issue_params[:custom_field_values] ||= {}
244 250 end
245 251
246 252 def bulk_update
247 253 @issues.sort!
248 254 @copy = params[:copy].present?
249 255
250 256 attributes = parse_params_for_bulk_update(params[:issue])
251 257 copy_subtasks = (params[:copy_subtasks] == '1')
252 258 copy_attachments = (params[:copy_attachments] == '1')
253 259
254 260 if @copy
255 261 unless User.current.allowed_to?(:copy_issues, @projects)
256 262 raise ::Unauthorized
257 263 end
258 264 target_projects = @projects
259 265 if attributes['project_id'].present?
260 266 target_projects = Project.where(:id => attributes['project_id']).to_a
261 267 end
262 268 unless User.current.allowed_to?(:add_issues, target_projects)
263 269 raise ::Unauthorized
264 270 end
265 271 else
266 272 unless @issues.all?(&:attributes_editable?)
267 273 raise ::Unauthorized
268 274 end
269 275 end
270 276
271 277 unsaved_issues = []
272 278 saved_issues = []
273 279
274 280 if @copy && copy_subtasks
275 281 # Descendant issues will be copied with the parent task
276 282 # Don't copy them twice
277 283 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
278 284 end
279 285
280 286 @issues.each do |orig_issue|
281 287 orig_issue.reload
282 288 if @copy
283 289 issue = orig_issue.copy({},
284 290 :attachments => copy_attachments,
285 291 :subtasks => copy_subtasks,
286 292 :link => link_copy?(params[:link_copy])
287 293 )
288 294 else
289 295 issue = orig_issue
290 296 end
291 297 journal = issue.init_journal(User.current, params[:notes])
292 298 issue.safe_attributes = attributes
293 299 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
294 300 if issue.save
295 301 saved_issues << issue
296 302 else
297 303 unsaved_issues << orig_issue
298 304 end
299 305 end
300 306
301 307 if unsaved_issues.empty?
302 308 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
303 309 if params[:follow]
304 310 if @issues.size == 1 && saved_issues.size == 1
305 311 redirect_to issue_path(saved_issues.first)
306 312 elsif saved_issues.map(&:project).uniq.size == 1
307 313 redirect_to project_issues_path(saved_issues.map(&:project).first)
308 314 end
309 315 else
310 316 redirect_back_or_default _project_issues_path(@project)
311 317 end
312 318 else
313 319 @saved_issues = @issues
314 320 @unsaved_issues = unsaved_issues
315 321 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
316 322 bulk_edit
317 323 render :action => 'bulk_edit'
318 324 end
319 325 end
320 326
321 327 def destroy
322 328 raise Unauthorized unless @issues.all?(&:deletable?)
323 329 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
324 330 if @hours > 0
325 331 case params[:todo]
326 332 when 'destroy'
327 333 # nothing to do
328 334 when 'nullify'
329 335 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
330 336 when 'reassign'
331 337 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
332 338 if reassign_to.nil?
333 339 flash.now[:error] = l(:error_issue_not_found_in_project)
334 340 return
335 341 else
336 342 TimeEntry.where(['issue_id IN (?)', @issues]).
337 343 update_all("issue_id = #{reassign_to.id}")
338 344 end
339 345 else
340 346 # display the destroy form if it's a user request
341 347 return unless api_request?
342 348 end
343 349 end
344 350 @issues.each do |issue|
345 351 begin
346 352 issue.reload.destroy
347 353 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
348 354 # nothing to do, issue was already deleted (eg. by a parent)
349 355 end
350 356 end
351 357 respond_to do |format|
352 358 format.html { redirect_back_or_default _project_issues_path(@project) }
353 359 format.api { render_api_ok }
354 360 end
355 361 end
356 362
357 363 # Overrides Redmine::MenuManager::MenuController::ClassMethods for
358 364 # when the "New issue" tab is enabled
359 365 def current_menu_item
360 366 if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
361 367 :new_issue
362 368 else
363 369 super
364 370 end
365 371 end
366 372
367 373 private
368 374
369 375 def retrieve_previous_and_next_issue_ids
370 376 if params[:prev_issue_id].present? || params[:next_issue_id].present?
371 377 @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
372 378 @next_issue_id = params[:next_issue_id].presence.try(:to_i)
373 379 @issue_position = params[:issue_position].presence.try(:to_i)
374 380 @issue_count = params[:issue_count].presence.try(:to_i)
375 381 else
376 382 retrieve_query_from_session
377 383 if @query
378 384 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
379 385 sort_update(@query.sortable_columns, 'issues_index_sort')
380 386 limit = 500
381 387 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
382 388 if (idx = issue_ids.index(@issue.id)) && idx < limit
383 389 if issue_ids.size < 500
384 390 @issue_position = idx + 1
385 391 @issue_count = issue_ids.size
386 392 end
387 393 @prev_issue_id = issue_ids[idx - 1] if idx > 0
388 394 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
389 395 end
390 396 end
391 397 end
392 398 end
393 399
394 400 def previous_and_next_issue_ids_params
395 401 {
396 402 :prev_issue_id => params[:prev_issue_id],
397 403 :next_issue_id => params[:next_issue_id],
398 404 :issue_position => params[:issue_position],
399 405 :issue_count => params[:issue_count]
400 406 }.reject {|k,v| k.blank?}
401 407 end
402 408
403 409 # Used by #edit and #update to set some common instance variables
404 410 # from the params
405 411 def update_issue_from_params
406 412 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
407 413 if params[:time_entry]
408 414 @time_entry.safe_attributes = params[:time_entry]
409 415 end
410 416
411 417 @issue.init_journal(User.current)
412 418
413 419 issue_attributes = params[:issue]
414 420 if issue_attributes && params[:conflict_resolution]
415 421 case params[:conflict_resolution]
416 422 when 'overwrite'
417 423 issue_attributes = issue_attributes.dup
418 424 issue_attributes.delete(:lock_version)
419 425 when 'add_notes'
420 426 issue_attributes = issue_attributes.slice(:notes, :private_notes)
421 427 when 'cancel'
422 428 redirect_to issue_path(@issue)
423 429 return false
424 430 end
425 431 end
426 432 @issue.safe_attributes = issue_attributes
427 433 @priorities = IssuePriority.active
428 434 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
429 435 true
430 436 end
431 437
432 438 # Used by #new and #create to build a new issue from the params
433 439 # The new issue will be copied from an existing one if copy_from parameter is given
434 440 def build_new_issue_from_params
435 441 @issue = Issue.new
436 442 if params[:copy_from]
437 443 begin
438 444 @issue.init_journal(User.current)
439 445 @copy_from = Issue.visible.find(params[:copy_from])
440 446 unless User.current.allowed_to?(:copy_issues, @copy_from.project)
441 447 raise ::Unauthorized
442 448 end
443 449 @link_copy = link_copy?(params[:link_copy]) || request.get?
444 450 @copy_attachments = params[:copy_attachments].present? || request.get?
445 451 @copy_subtasks = params[:copy_subtasks].present? || request.get?
446 452 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy)
447 453 @issue.parent_issue_id = @copy_from.parent_id
448 454 rescue ActiveRecord::RecordNotFound
449 455 render_404
450 456 return
451 457 end
452 458 end
453 459 @issue.project = @project
454 460 if request.get?
455 461 @issue.project ||= @issue.allowed_target_projects.first
456 462 end
457 463 @issue.author ||= User.current
458 464 @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date?
459 465
460 466 attrs = (params[:issue] || {}).deep_dup
461 467 if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
462 468 attrs.delete(:status_id)
463 469 end
464 470 if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
465 471 # Discard submitted version when changing the project on the issue form
466 472 # so we can use the default version for the new project
467 473 attrs.delete(:fixed_version_id)
468 474 end
469 475 @issue.safe_attributes = attrs
470 476
471 477 if @issue.project
472 478 @issue.tracker ||= @issue.allowed_target_trackers.first
473 479 if @issue.tracker.nil?
474 480 if @issue.project.trackers.any?
475 481 # None of the project trackers is allowed to the user
476 482 render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403
477 483 else
478 484 # Project has no trackers
479 485 render_error l(:error_no_tracker_in_project)
480 486 end
481 487 return false
482 488 end
483 489 if @issue.status.nil?
484 490 render_error l(:error_no_default_issue_status)
485 491 return false
486 492 end
487 else
493 elsif request.get?
488 494 render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403
489 495 return false
490 496 end
491 497
492 498 @priorities = IssuePriority.active
493 499 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
494 500 end
495 501
496 502 # Saves @issue and a time_entry from the parameters
497 503 def save_issue_with_child_records
498 504 Issue.transaction do
499 505 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
500 506 time_entry = @time_entry || TimeEntry.new
501 507 time_entry.project = @issue.project
502 508 time_entry.issue = @issue
503 509 time_entry.user = User.current
504 510 time_entry.spent_on = User.current.today
505 511 time_entry.attributes = params[:time_entry]
506 512 @issue.time_entries << time_entry
507 513 end
508 514
509 515 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
510 516 if @issue.save
511 517 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
512 518 else
513 519 raise ActiveRecord::Rollback
514 520 end
515 521 end
516 522 end
517 523
518 524 # Returns true if the issue copy should be linked
519 525 # to the original issue
520 526 def link_copy?(param)
521 527 case Setting.link_copied_issue
522 528 when 'yes'
523 529 true
524 530 when 'no'
525 531 false
526 532 when 'ask'
527 533 param == '1'
528 534 end
529 535 end
530 536
531 537 # Redirects user after a successful issue creation
532 538 def redirect_after_create
533 539 if params[:continue]
534 540 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
535 541 if params[:project_id]
536 542 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
537 543 else
538 544 attrs.merge! :project_id => @issue.project_id
539 545 redirect_to new_issue_path(:issue => attrs)
540 546 end
541 547 else
542 548 redirect_to issue_path(@issue)
543 549 end
544 550 end
545 551 end
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now