##// END OF EJS Templates
Allow setting an issue's notes via params[:issue][:notes]. (XML API)...
Eric Davis -
r3934:f92dcdf50a4c
parent child
Show More
@@ -1,325 +1,325
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class IssuesController < ApplicationController
18 class IssuesController < ApplicationController
19 menu_item :new_issue, :only => [:new, :create]
19 menu_item :new_issue, :only => [:new, :create]
20 default_search_scope :issues
20 default_search_scope :issues
21
21
22 before_filter :find_issue, :only => [:show, :edit, :update]
22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
24 before_filter :find_project, :only => [:new, :create]
24 before_filter :find_project, :only => [:new, :create]
25 before_filter :authorize, :except => [:index]
25 before_filter :authorize, :except => [:index]
26 before_filter :find_optional_project, :only => [:index]
26 before_filter :find_optional_project, :only => [:index]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 before_filter :build_new_issue_from_params, :only => [:new, :create]
28 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 accept_key_auth :index, :show
29 accept_key_auth :index, :show
30
30
31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32
32
33 helper :journals
33 helper :journals
34 helper :projects
34 helper :projects
35 include ProjectsHelper
35 include ProjectsHelper
36 helper :custom_fields
36 helper :custom_fields
37 include CustomFieldsHelper
37 include CustomFieldsHelper
38 helper :issue_relations
38 helper :issue_relations
39 include IssueRelationsHelper
39 include IssueRelationsHelper
40 helper :watchers
40 helper :watchers
41 include WatchersHelper
41 include WatchersHelper
42 helper :attachments
42 helper :attachments
43 include AttachmentsHelper
43 include AttachmentsHelper
44 helper :queries
44 helper :queries
45 include QueriesHelper
45 include QueriesHelper
46 helper :sort
46 helper :sort
47 include SortHelper
47 include SortHelper
48 include IssuesHelper
48 include IssuesHelper
49 helper :timelog
49 helper :timelog
50 include Redmine::Export::PDF
50 include Redmine::Export::PDF
51
51
52 verify :method => [:post, :delete],
52 verify :method => [:post, :delete],
53 :only => :destroy,
53 :only => :destroy,
54 :render => { :nothing => true, :status => :method_not_allowed }
54 :render => { :nothing => true, :status => :method_not_allowed }
55
55
56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
57 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
57 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
58 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
58 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
59
59
60 def index
60 def index
61 retrieve_query
61 retrieve_query
62 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
62 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
63 sort_update(@query.sortable_columns)
63 sort_update(@query.sortable_columns)
64
64
65 if @query.valid?
65 if @query.valid?
66 limit = case params[:format]
66 limit = case params[:format]
67 when 'csv', 'pdf'
67 when 'csv', 'pdf'
68 Setting.issues_export_limit.to_i
68 Setting.issues_export_limit.to_i
69 when 'atom'
69 when 'atom'
70 Setting.feeds_limit.to_i
70 Setting.feeds_limit.to_i
71 else
71 else
72 per_page_option
72 per_page_option
73 end
73 end
74
74
75 @issue_count = @query.issue_count
75 @issue_count = @query.issue_count
76 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
76 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
77 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
78 :order => sort_clause,
78 :order => sort_clause,
79 :offset => @issue_pages.current.offset,
79 :offset => @issue_pages.current.offset,
80 :limit => limit)
80 :limit => limit)
81 @issue_count_by_group = @query.issue_count_by_group
81 @issue_count_by_group = @query.issue_count_by_group
82
82
83 respond_to do |format|
83 respond_to do |format|
84 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
84 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
85 format.xml { render :layout => false }
85 format.xml { render :layout => false }
86 format.json { render :text => @issues.to_json, :layout => false }
86 format.json { render :text => @issues.to_json, :layout => false }
87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
88 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
90 end
90 end
91 else
91 else
92 # Send html if the query is not valid
92 # Send html if the query is not valid
93 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
93 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
94 end
94 end
95 rescue ActiveRecord::RecordNotFound
95 rescue ActiveRecord::RecordNotFound
96 render_404
96 render_404
97 end
97 end
98
98
99 def show
99 def show
100 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
100 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
101 @journals.each_with_index {|j,i| j.indice = i+1}
101 @journals.each_with_index {|j,i| j.indice = i+1}
102 @journals.reverse! if User.current.wants_comments_in_reverse_order?
102 @journals.reverse! if User.current.wants_comments_in_reverse_order?
103 @changesets = @issue.changesets.visible.all
103 @changesets = @issue.changesets.visible.all
104 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
104 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
105 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
105 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
106 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
106 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
107 @priorities = IssuePriority.all
107 @priorities = IssuePriority.all
108 @time_entry = TimeEntry.new
108 @time_entry = TimeEntry.new
109 respond_to do |format|
109 respond_to do |format|
110 format.html { render :template => 'issues/show.rhtml' }
110 format.html { render :template => 'issues/show.rhtml' }
111 format.xml { render :layout => false }
111 format.xml { render :layout => false }
112 format.json { render :text => @issue.to_json, :layout => false }
112 format.json { render :text => @issue.to_json, :layout => false }
113 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
113 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
114 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
114 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
115 end
115 end
116 end
116 end
117
117
118 # Add a new issue
118 # Add a new issue
119 # The new issue will be created from an existing one if copy_from parameter is given
119 # The new issue will be created from an existing one if copy_from parameter is given
120 def new
120 def new
121 respond_to do |format|
121 respond_to do |format|
122 format.html { render :action => 'new', :layout => !request.xhr? }
122 format.html { render :action => 'new', :layout => !request.xhr? }
123 format.js { render :partial => 'attributes' }
123 format.js { render :partial => 'attributes' }
124 end
124 end
125 end
125 end
126
126
127 def create
127 def create
128 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
128 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
129 if @issue.save
129 if @issue.save
130 attachments = Attachment.attach_files(@issue, params[:attachments])
130 attachments = Attachment.attach_files(@issue, params[:attachments])
131 render_attachment_warning_if_needed(@issue)
131 render_attachment_warning_if_needed(@issue)
132 flash[:notice] = l(:notice_successful_create)
132 flash[:notice] = l(:notice_successful_create)
133 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
133 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
134 respond_to do |format|
134 respond_to do |format|
135 format.html {
135 format.html {
136 redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
136 redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
137 { :action => 'show', :id => @issue })
137 { :action => 'show', :id => @issue })
138 }
138 }
139 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
139 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
140 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
140 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
141 end
141 end
142 return
142 return
143 else
143 else
144 respond_to do |format|
144 respond_to do |format|
145 format.html { render :action => 'new' }
145 format.html { render :action => 'new' }
146 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
146 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
147 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
147 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
148 end
148 end
149 end
149 end
150 end
150 end
151
151
152 # Attributes that can be updated on workflow transition (without :edit permission)
152 # Attributes that can be updated on workflow transition (without :edit permission)
153 # TODO: make it configurable (at least per role)
153 # TODO: make it configurable (at least per role)
154 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
154 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
155
155
156 def edit
156 def edit
157 update_issue_from_params
157 update_issue_from_params
158
158
159 @journal = @issue.current_journal
159 @journal = @issue.current_journal
160
160
161 respond_to do |format|
161 respond_to do |format|
162 format.html { }
162 format.html { }
163 format.xml { }
163 format.xml { }
164 end
164 end
165 end
165 end
166
166
167 def update
167 def update
168 update_issue_from_params
168 update_issue_from_params
169
169
170 if @issue.save_issue_with_child_records(params, @time_entry)
170 if @issue.save_issue_with_child_records(params, @time_entry)
171 render_attachment_warning_if_needed(@issue)
171 render_attachment_warning_if_needed(@issue)
172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
173
173
174 respond_to do |format|
174 respond_to do |format|
175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
176 format.xml { head :ok }
176 format.xml { head :ok }
177 format.json { head :ok }
177 format.json { head :ok }
178 end
178 end
179 else
179 else
180 render_attachment_warning_if_needed(@issue)
180 render_attachment_warning_if_needed(@issue)
181 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
181 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
182 @journal = @issue.current_journal
182 @journal = @issue.current_journal
183
183
184 respond_to do |format|
184 respond_to do |format|
185 format.html { render :action => 'edit' }
185 format.html { render :action => 'edit' }
186 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
186 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
187 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
187 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
188 end
188 end
189 end
189 end
190 end
190 end
191
191
192 # Bulk edit a set of issues
192 # Bulk edit a set of issues
193 def bulk_edit
193 def bulk_edit
194 @issues.sort!
194 @issues.sort!
195 @available_statuses = Workflow.available_statuses(@project)
195 @available_statuses = Workflow.available_statuses(@project)
196 @custom_fields = @project.all_issue_custom_fields
196 @custom_fields = @project.all_issue_custom_fields
197 end
197 end
198
198
199 def bulk_update
199 def bulk_update
200 @issues.sort!
200 @issues.sort!
201 attributes = parse_params_for_bulk_issue_attributes(params)
201 attributes = parse_params_for_bulk_issue_attributes(params)
202
202
203 unsaved_issue_ids = []
203 unsaved_issue_ids = []
204 @issues.each do |issue|
204 @issues.each do |issue|
205 issue.reload
205 issue.reload
206 journal = issue.init_journal(User.current, params[:notes])
206 journal = issue.init_journal(User.current, params[:notes])
207 issue.safe_attributes = attributes
207 issue.safe_attributes = attributes
208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
209 unless issue.save
209 unless issue.save
210 # Keep unsaved issue ids to display them in flash error
210 # Keep unsaved issue ids to display them in flash error
211 unsaved_issue_ids << issue.id
211 unsaved_issue_ids << issue.id
212 end
212 end
213 end
213 end
214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
216 end
216 end
217
217
218 def destroy
218 def destroy
219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
220 if @hours > 0
220 if @hours > 0
221 case params[:todo]
221 case params[:todo]
222 when 'destroy'
222 when 'destroy'
223 # nothing to do
223 # nothing to do
224 when 'nullify'
224 when 'nullify'
225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
226 when 'reassign'
226 when 'reassign'
227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
228 if reassign_to.nil?
228 if reassign_to.nil?
229 flash.now[:error] = l(:error_issue_not_found_in_project)
229 flash.now[:error] = l(:error_issue_not_found_in_project)
230 return
230 return
231 else
231 else
232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
233 end
233 end
234 else
234 else
235 unless params[:format] == 'xml' || params[:format] == 'json'
235 unless params[:format] == 'xml' || params[:format] == 'json'
236 # display the destroy form if it's a user request
236 # display the destroy form if it's a user request
237 return
237 return
238 end
238 end
239 end
239 end
240 end
240 end
241 @issues.each(&:destroy)
241 @issues.each(&:destroy)
242 respond_to do |format|
242 respond_to do |format|
243 format.html { redirect_to :action => 'index', :project_id => @project }
243 format.html { redirect_to :action => 'index', :project_id => @project }
244 format.xml { head :ok }
244 format.xml { head :ok }
245 format.json { head :ok }
245 format.json { head :ok }
246 end
246 end
247 end
247 end
248
248
249 private
249 private
250 def find_issue
250 def find_issue
251 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
251 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
252 @project = @issue.project
252 @project = @issue.project
253 rescue ActiveRecord::RecordNotFound
253 rescue ActiveRecord::RecordNotFound
254 render_404
254 render_404
255 end
255 end
256
256
257 def find_project
257 def find_project
258 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
258 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
259 @project = Project.find(project_id)
259 @project = Project.find(project_id)
260 rescue ActiveRecord::RecordNotFound
260 rescue ActiveRecord::RecordNotFound
261 render_404
261 render_404
262 end
262 end
263
263
264 # Used by #edit and #update to set some common instance variables
264 # Used by #edit and #update to set some common instance variables
265 # from the params
265 # from the params
266 # TODO: Refactor, not everything in here is needed by #edit
266 # TODO: Refactor, not everything in here is needed by #edit
267 def update_issue_from_params
267 def update_issue_from_params
268 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
268 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
269 @priorities = IssuePriority.all
269 @priorities = IssuePriority.all
270 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
270 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
271 @time_entry = TimeEntry.new
271 @time_entry = TimeEntry.new
272
272
273 @notes = params[:notes]
273 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
274 @issue.init_journal(User.current, @notes)
274 @issue.init_journal(User.current, @notes)
275 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
275 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
276 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
276 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
277 attrs = params[:issue].dup
277 attrs = params[:issue].dup
278 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
278 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
279 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
279 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
280 @issue.safe_attributes = attrs
280 @issue.safe_attributes = attrs
281 end
281 end
282
282
283 end
283 end
284
284
285 # TODO: Refactor, lots of extra code in here
285 # TODO: Refactor, lots of extra code in here
286 def build_new_issue_from_params
286 def build_new_issue_from_params
287 if params[:id].blank?
287 if params[:id].blank?
288 @issue = Issue.new
288 @issue = Issue.new
289 @issue.copy_from(params[:copy_from]) if params[:copy_from]
289 @issue.copy_from(params[:copy_from]) if params[:copy_from]
290 @issue.project = @project
290 @issue.project = @project
291 else
291 else
292 @issue = @project.issues.visible.find(params[:id])
292 @issue = @project.issues.visible.find(params[:id])
293 end
293 end
294
294
295 @issue.project = @project
295 @issue.project = @project
296 # Tracker must be set before custom field values
296 # Tracker must be set before custom field values
297 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
297 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
298 if @issue.tracker.nil?
298 if @issue.tracker.nil?
299 render_error l(:error_no_tracker_in_project)
299 render_error l(:error_no_tracker_in_project)
300 return false
300 return false
301 end
301 end
302 if params[:issue].is_a?(Hash)
302 if params[:issue].is_a?(Hash)
303 @issue.safe_attributes = params[:issue]
303 @issue.safe_attributes = params[:issue]
304 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
304 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
305 end
305 end
306 @issue.author = User.current
306 @issue.author = User.current
307 @issue.start_date ||= Date.today
307 @issue.start_date ||= Date.today
308 @priorities = IssuePriority.all
308 @priorities = IssuePriority.all
309 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
309 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
310 end
310 end
311
311
312 def check_for_default_issue_status
312 def check_for_default_issue_status
313 if IssueStatus.default.nil?
313 if IssueStatus.default.nil?
314 render_error l(:error_no_default_issue_status)
314 render_error l(:error_no_default_issue_status)
315 return false
315 return false
316 end
316 end
317 end
317 end
318
318
319 def parse_params_for_bulk_issue_attributes(params)
319 def parse_params_for_bulk_issue_attributes(params)
320 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
320 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
321 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
321 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
322 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
322 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
323 attributes
323 attributes
324 end
324 end
325 end
325 end
@@ -1,339 +1,349
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang
2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require "#{File.dirname(__FILE__)}/../test_helper"
18 require "#{File.dirname(__FILE__)}/../test_helper"
19
19
20 class IssuesApiTest < ActionController::IntegrationTest
20 class IssuesApiTest < ActionController::IntegrationTest
21 fixtures :projects,
21 fixtures :projects,
22 :users,
22 :users,
23 :roles,
23 :roles,
24 :members,
24 :members,
25 :member_roles,
25 :member_roles,
26 :issues,
26 :issues,
27 :issue_statuses,
27 :issue_statuses,
28 :versions,
28 :versions,
29 :trackers,
29 :trackers,
30 :projects_trackers,
30 :projects_trackers,
31 :issue_categories,
31 :issue_categories,
32 :enabled_modules,
32 :enabled_modules,
33 :enumerations,
33 :enumerations,
34 :attachments,
34 :attachments,
35 :workflows,
35 :workflows,
36 :custom_fields,
36 :custom_fields,
37 :custom_values,
37 :custom_values,
38 :custom_fields_projects,
38 :custom_fields_projects,
39 :custom_fields_trackers,
39 :custom_fields_trackers,
40 :time_entries,
40 :time_entries,
41 :journals,
41 :journals,
42 :journal_details,
42 :journal_details,
43 :queries
43 :queries
44
44
45 def setup
45 def setup
46 Setting.rest_api_enabled = '1'
46 Setting.rest_api_enabled = '1'
47 end
47 end
48
48
49 context "/index.xml" do
49 context "/index.xml" do
50 setup do
50 setup do
51 get '/issues.xml'
51 get '/issues.xml'
52 end
52 end
53
53
54 should_respond_with :success
54 should_respond_with :success
55 should_respond_with_content_type 'application/xml'
55 should_respond_with_content_type 'application/xml'
56 end
56 end
57
57
58 context "/index.json" do
58 context "/index.json" do
59 setup do
59 setup do
60 get '/issues.json'
60 get '/issues.json'
61 end
61 end
62
62
63 should_respond_with :success
63 should_respond_with :success
64 should_respond_with_content_type 'application/json'
64 should_respond_with_content_type 'application/json'
65
65
66 should 'return a valid JSON string' do
66 should 'return a valid JSON string' do
67 assert ActiveSupport::JSON.decode(response.body)
67 assert ActiveSupport::JSON.decode(response.body)
68 end
68 end
69 end
69 end
70
70
71 context "/index.xml with filter" do
71 context "/index.xml with filter" do
72 setup do
72 setup do
73 get '/issues.xml?status_id=5'
73 get '/issues.xml?status_id=5'
74 end
74 end
75
75
76 should_respond_with :success
76 should_respond_with :success
77 should_respond_with_content_type 'application/xml'
77 should_respond_with_content_type 'application/xml'
78 should "show only issues with the status_id" do
78 should "show only issues with the status_id" do
79 assert_tag :tag => 'issues',
79 assert_tag :tag => 'issues',
80 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
80 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
81 :only => { :tag => 'issue' } }
81 :only => { :tag => 'issue' } }
82 end
82 end
83 end
83 end
84
84
85 context "/index.json with filter" do
85 context "/index.json with filter" do
86 setup do
86 setup do
87 get '/issues.json?status_id=5'
87 get '/issues.json?status_id=5'
88 end
88 end
89
89
90 should_respond_with :success
90 should_respond_with :success
91 should_respond_with_content_type 'application/json'
91 should_respond_with_content_type 'application/json'
92
92
93 should 'return a valid JSON string' do
93 should 'return a valid JSON string' do
94 assert ActiveSupport::JSON.decode(response.body)
94 assert ActiveSupport::JSON.decode(response.body)
95 end
95 end
96
96
97 should "show only issues with the status_id" do
97 should "show only issues with the status_id" do
98 json = ActiveSupport::JSON.decode(response.body)
98 json = ActiveSupport::JSON.decode(response.body)
99 status_ids_used = json.collect {|j| j['status_id'] }
99 status_ids_used = json.collect {|j| j['status_id'] }
100 assert_equal 3, status_ids_used.length
100 assert_equal 3, status_ids_used.length
101 assert status_ids_used.all? {|id| id == 5 }
101 assert status_ids_used.all? {|id| id == 5 }
102 end
102 end
103
103
104 end
104 end
105
105
106 context "/issues/1.xml" do
106 context "/issues/1.xml" do
107 setup do
107 setup do
108 get '/issues/1.xml'
108 get '/issues/1.xml'
109 end
109 end
110
110
111 should_respond_with :success
111 should_respond_with :success
112 should_respond_with_content_type 'application/xml'
112 should_respond_with_content_type 'application/xml'
113 end
113 end
114
114
115 context "/issues/1.json" do
115 context "/issues/1.json" do
116 setup do
116 setup do
117 get '/issues/1.json'
117 get '/issues/1.json'
118 end
118 end
119
119
120 should_respond_with :success
120 should_respond_with :success
121 should_respond_with_content_type 'application/json'
121 should_respond_with_content_type 'application/json'
122
122
123 should 'return a valid JSON string' do
123 should 'return a valid JSON string' do
124 assert ActiveSupport::JSON.decode(response.body)
124 assert ActiveSupport::JSON.decode(response.body)
125 end
125 end
126 end
126 end
127
127
128 context "POST /issues.xml" do
128 context "POST /issues.xml" do
129 setup do
129 setup do
130 @issue_count = Issue.count
130 @issue_count = Issue.count
131 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
131 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
132 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
132 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
133 end
133 end
134
134
135 should_respond_with :created
135 should_respond_with :created
136 should_respond_with_content_type 'application/xml'
136 should_respond_with_content_type 'application/xml'
137
137
138 should "create an issue with the attributes" do
138 should "create an issue with the attributes" do
139 assert_equal Issue.count, @issue_count + 1
139 assert_equal Issue.count, @issue_count + 1
140
140
141 issue = Issue.first(:order => 'id DESC')
141 issue = Issue.first(:order => 'id DESC')
142 @attributes.each do |attribute, value|
142 @attributes.each do |attribute, value|
143 assert_equal value, issue.send(attribute)
143 assert_equal value, issue.send(attribute)
144 end
144 end
145 end
145 end
146 end
146 end
147
147
148 context "POST /issues.xml with failure" do
148 context "POST /issues.xml with failure" do
149 setup do
149 setup do
150 @attributes = {:project_id => 1}
150 @attributes = {:project_id => 1}
151 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
151 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
152 end
152 end
153
153
154 should_respond_with :unprocessable_entity
154 should_respond_with :unprocessable_entity
155 should_respond_with_content_type 'application/xml'
155 should_respond_with_content_type 'application/xml'
156
156
157 should "have an errors tag" do
157 should "have an errors tag" do
158 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
158 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
159 end
159 end
160 end
160 end
161
161
162 context "POST /issues.json" do
162 context "POST /issues.json" do
163 setup do
163 setup do
164 @issue_count = Issue.count
164 @issue_count = Issue.count
165 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
165 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
166 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
166 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
167 end
167 end
168
168
169 should_respond_with :created
169 should_respond_with :created
170 should_respond_with_content_type 'application/json'
170 should_respond_with_content_type 'application/json'
171
171
172 should "create an issue with the attributes" do
172 should "create an issue with the attributes" do
173 assert_equal Issue.count, @issue_count + 1
173 assert_equal Issue.count, @issue_count + 1
174
174
175 issue = Issue.first(:order => 'id DESC')
175 issue = Issue.first(:order => 'id DESC')
176 @attributes.each do |attribute, value|
176 @attributes.each do |attribute, value|
177 assert_equal value, issue.send(attribute)
177 assert_equal value, issue.send(attribute)
178 end
178 end
179 end
179 end
180 end
180 end
181
181
182 context "POST /issues.json with failure" do
182 context "POST /issues.json with failure" do
183 setup do
183 setup do
184 @attributes = {:project_id => 1}
184 @attributes = {:project_id => 1}
185 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
185 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
186 end
186 end
187
187
188 should_respond_with :unprocessable_entity
188 should_respond_with :unprocessable_entity
189 should_respond_with_content_type 'application/json'
189 should_respond_with_content_type 'application/json'
190
190
191 should "have an errors element" do
191 should "have an errors element" do
192 json = ActiveSupport::JSON.decode(response.body)
192 json = ActiveSupport::JSON.decode(response.body)
193 assert_equal "can't be blank", json.first['subject']
193 assert_equal "can't be blank", json.first['subject']
194 end
194 end
195 end
195 end
196
196
197 context "PUT /issues/1.xml" do
197 context "PUT /issues/1.xml" do
198 setup do
198 setup do
199 @issue_count = Issue.count
199 @issue_count = Issue.count
200 @journal_count = Journal.count
200 @journal_count = Journal.count
201 @attributes = {:subject => 'API update'}
201 @attributes = {:subject => 'API update', :notes => 'A new note'}
202
202
203 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
203 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
204 end
204 end
205
205
206 should_respond_with :ok
206 should_respond_with :ok
207 should_respond_with_content_type 'application/xml'
207 should_respond_with_content_type 'application/xml'
208
208
209 should "not create a new issue" do
209 should "not create a new issue" do
210 assert_equal Issue.count, @issue_count
210 assert_equal Issue.count, @issue_count
211 end
211 end
212
212
213 should "create a new journal" do
213 should "create a new journal" do
214 assert_equal Journal.count, @journal_count + 1
214 assert_equal Journal.count, @journal_count + 1
215 end
215 end
216
216
217 should "add the note to the journal" do
218 journal = Journal.last
219 assert_equal "A new note", journal.notes
220 end
221
217 should "update the issue" do
222 should "update the issue" do
218 issue = Issue.find(1)
223 issue = Issue.find(1)
219 @attributes.each do |attribute, value|
224 @attributes.each do |attribute, value|
220 assert_equal value, issue.send(attribute)
225 assert_equal value, issue.send(attribute) unless attribute == :notes
221 end
226 end
222 end
227 end
223
228
224 end
229 end
225
230
226 context "PUT /issues/1.xml with failed update" do
231 context "PUT /issues/1.xml with failed update" do
227 setup do
232 setup do
228 @attributes = {:subject => ''}
233 @attributes = {:subject => ''}
229 @issue_count = Issue.count
234 @issue_count = Issue.count
230 @journal_count = Journal.count
235 @journal_count = Journal.count
231
236
232 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
237 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
233 end
238 end
234
239
235 should_respond_with :unprocessable_entity
240 should_respond_with :unprocessable_entity
236 should_respond_with_content_type 'application/xml'
241 should_respond_with_content_type 'application/xml'
237
242
238 should "not create a new issue" do
243 should "not create a new issue" do
239 assert_equal Issue.count, @issue_count
244 assert_equal Issue.count, @issue_count
240 end
245 end
241
246
242 should "not create a new journal" do
247 should "not create a new journal" do
243 assert_equal Journal.count, @journal_count
248 assert_equal Journal.count, @journal_count
244 end
249 end
245
250
246 should "have an errors tag" do
251 should "have an errors tag" do
247 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
252 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
248 end
253 end
249 end
254 end
250
255
251 context "PUT /issues/1.json" do
256 context "PUT /issues/1.json" do
252 setup do
257 setup do
253 @issue_count = Issue.count
258 @issue_count = Issue.count
254 @journal_count = Journal.count
259 @journal_count = Journal.count
255 @attributes = {:subject => 'API update'}
260 @attributes = {:subject => 'API update', :notes => 'A new note'}
256
261
257 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
262 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
258 end
263 end
259
264
260 should_respond_with :ok
265 should_respond_with :ok
261 should_respond_with_content_type 'application/json'
266 should_respond_with_content_type 'application/json'
262
267
263 should "not create a new issue" do
268 should "not create a new issue" do
264 assert_equal Issue.count, @issue_count
269 assert_equal Issue.count, @issue_count
265 end
270 end
266
271
267 should "create a new journal" do
272 should "create a new journal" do
268 assert_equal Journal.count, @journal_count + 1
273 assert_equal Journal.count, @journal_count + 1
269 end
274 end
270
275
276 should "add the note to the journal" do
277 journal = Journal.last
278 assert_equal "A new note", journal.notes
279 end
280
271 should "update the issue" do
281 should "update the issue" do
272 issue = Issue.find(1)
282 issue = Issue.find(1)
273 @attributes.each do |attribute, value|
283 @attributes.each do |attribute, value|
274 assert_equal value, issue.send(attribute)
284 assert_equal value, issue.send(attribute) unless attribute == :notes
275 end
285 end
276 end
286 end
277
287
278 end
288 end
279
289
280 context "PUT /issues/1.json with failed update" do
290 context "PUT /issues/1.json with failed update" do
281 setup do
291 setup do
282 @attributes = {:subject => ''}
292 @attributes = {:subject => ''}
283 @issue_count = Issue.count
293 @issue_count = Issue.count
284 @journal_count = Journal.count
294 @journal_count = Journal.count
285
295
286 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
296 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
287 end
297 end
288
298
289 should_respond_with :unprocessable_entity
299 should_respond_with :unprocessable_entity
290 should_respond_with_content_type 'application/json'
300 should_respond_with_content_type 'application/json'
291
301
292 should "not create a new issue" do
302 should "not create a new issue" do
293 assert_equal Issue.count, @issue_count
303 assert_equal Issue.count, @issue_count
294 end
304 end
295
305
296 should "not create a new journal" do
306 should "not create a new journal" do
297 assert_equal Journal.count, @journal_count
307 assert_equal Journal.count, @journal_count
298 end
308 end
299
309
300 should "have an errors attribute" do
310 should "have an errors attribute" do
301 json = ActiveSupport::JSON.decode(response.body)
311 json = ActiveSupport::JSON.decode(response.body)
302 assert_equal "can't be blank", json.first['subject']
312 assert_equal "can't be blank", json.first['subject']
303 end
313 end
304 end
314 end
305
315
306 context "DELETE /issues/1.xml" do
316 context "DELETE /issues/1.xml" do
307 setup do
317 setup do
308 @issue_count = Issue.count
318 @issue_count = Issue.count
309 delete '/issues/1.xml', {}, :authorization => credentials('jsmith')
319 delete '/issues/1.xml', {}, :authorization => credentials('jsmith')
310 end
320 end
311
321
312 should_respond_with :ok
322 should_respond_with :ok
313 should_respond_with_content_type 'application/xml'
323 should_respond_with_content_type 'application/xml'
314
324
315 should "delete the issue" do
325 should "delete the issue" do
316 assert_equal Issue.count, @issue_count -1
326 assert_equal Issue.count, @issue_count -1
317 assert_nil Issue.find_by_id(1)
327 assert_nil Issue.find_by_id(1)
318 end
328 end
319 end
329 end
320
330
321 context "DELETE /issues/1.json" do
331 context "DELETE /issues/1.json" do
322 setup do
332 setup do
323 @issue_count = Issue.count
333 @issue_count = Issue.count
324 delete '/issues/1.json', {}, :authorization => credentials('jsmith')
334 delete '/issues/1.json', {}, :authorization => credentials('jsmith')
325 end
335 end
326
336
327 should_respond_with :ok
337 should_respond_with :ok
328 should_respond_with_content_type 'application/json'
338 should_respond_with_content_type 'application/json'
329
339
330 should "delete the issue" do
340 should "delete the issue" do
331 assert_equal Issue.count, @issue_count -1
341 assert_equal Issue.count, @issue_count -1
332 assert_nil Issue.find_by_id(1)
342 assert_nil Issue.find_by_id(1)
333 end
343 end
334 end
344 end
335
345
336 def credentials(user, password=nil)
346 def credentials(user, password=nil)
337 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
347 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
338 end
348 end
339 end
349 end
General Comments 0
You need to be logged in to leave comments. Login now