From 1ccdf38fdde16421905f8419bb83f0e923bd16bc Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 17 Oct 2015 09:08:18 +0000 Subject: [PATCH] Mail Handler: add support for allow_override=all (#20543). git-svn-id: http://svn.redmine.org/redmine/trunk@14681 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/mail_handler.rb | 13 +++--- lib/tasks/email.rake | 78 ++++++++++++++-------------------- test/unit/mail_handler_test.rb | 49 ++++++++++++++++++--- 3 files changed, 83 insertions(+), 57 deletions(-) diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 007df6f2d..1364fdb97 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -29,11 +29,11 @@ class MailHandler < ActionMailer::Base options[:issue] ||= {} + options[:allow_override] ||= [] if options[:allow_override].is_a?(String) - options[:allow_override] = options[:allow_override].split(',').collect(&:strip) + options[:allow_override] = options[:allow_override].split(',') end - options[:allow_override] ||= [] - options[:allow_override].map!(&:downcase) + options[:allow_override].map! {|s| s.strip.downcase.gsub(/\s+/, '_')} # Project needs to be overridable if not specified options[:allow_override] << 'project' unless options[:issue].has_key?(:project) @@ -327,8 +327,11 @@ class MailHandler < ActionMailer::Base @keywords[attr] else @keywords[attr] = begin - if (options[:override] || handler_options[:allow_override].include?(attr.to_s.downcase)) && - (v = extract_keyword!(cleaned_up_text_body, attr, options[:format])) + override = options.key?(:override) ? + options[:override] : + (handler_options[:allow_override] & [attr.to_s.downcase.gsub(/\s+/, '_'), 'all']).present? + + if override && (v = extract_keyword!(cleaned_up_text_body, attr, options[:format])) v elsif !handler_options[:issue][attr].blank? handler_options[:issue][attr] diff --git a/lib/tasks/email.rake b/lib/tasks/email.rake index 82b84788c..51ca25b2e 100644 --- a/lib/tasks/email.rake +++ b/lib/tasks/email.rake @@ -21,37 +21,7 @@ namespace :redmine do desc <<-END_DESC Read an email from standard input. -General options: - unknown_user=ACTION how to handle emails from an unknown user - ACTION can be one of the following values: - ignore: email is ignored (default) - accept: accept as anonymous user - create: create a user account - no_permission_check=1 disable permission checking when receiving - the email - no_account_notice=1 disable new user account notification - default_group=foo,bar adds created user to foo and bar groups - -Issue attributes control options: - project=PROJECT identifier of the target project - status=STATUS name of the target status - tracker=TRACKER name of the target tracker - category=CATEGORY name of the target category - priority=PRIORITY name of the target priority - allow_override=ATTRS allow email content to override attributes - specified by previous options - ATTRS is a comma separated list of attributes - -Examples: - # No project specified. Emails MUST contain the 'Project' keyword: - rake redmine:email:read RAILS_ENV="production" < raw_email - - # Fixed project and default tracker specified, but emails can override - # both tracker and priority attributes: - rake redmine:email:read RAILS_ENV="production" \\ - project=foo \\ - tracker=bug \\ - allow_override=tracker,priority < raw_email +See redmine:email:receive_imap for more options and examples. END_DESC task :read => :environment do @@ -63,7 +33,21 @@ END_DESC desc <<-END_DESC Read emails from an IMAP server. -General options: +Available IMAP options: + host=HOST IMAP server host (default: 127.0.0.1) + port=PORT IMAP server port (default: 143) + ssl=SSL Use SSL/TLS? (default: false) + starttls=STARTTLS Use STARTTLS? (default: false) + username=USERNAME IMAP account + password=PASSWORD IMAP password + folder=FOLDER IMAP folder to read (default: INBOX) + +Processed emails control options: + move_on_success=MAILBOX move emails that were successfully received + to MAILBOX instead of deleting them + move_on_failure=MAILBOX move emails that were ignored to MAILBOX + +User and permissions options: unknown_user=ACTION how to handle emails from an unknown user ACTION can be one of the following values: ignore: email is ignored (default) @@ -74,15 +58,6 @@ General options: no_account_notice=1 disable new user account notification default_group=foo,bar adds created user to foo and bar groups -Available IMAP options: - host=HOST IMAP server host (default: 127.0.0.1) - port=PORT IMAP server port (default: 143) - ssl=SSL Use SSL/TLS? (default: false) - starttls=STARTTLS Use STARTTLS? (default: false) - username=USERNAME IMAP account - password=PASSWORD IMAP password - folder=FOLDER IMAP folder to read (default: INBOX) - Issue attributes control options: project=PROJECT identifier of the target project status=STATUS name of the target status @@ -90,14 +65,23 @@ Issue attributes control options: category=CATEGORY name of the target category priority=PRIORITY name of the target priority private create new issues as private - allow_override=ATTRS allow email content to override attributes - specified by previous options + allow_override=ATTRS allow email content to set attributes values ATTRS is a comma separated list of attributes + or 'all' to allow all attributes to be overridable + (see below for details) -Processed emails control options: - move_on_success=MAILBOX move emails that were successfully received - to MAILBOX instead of deleting them - move_on_failure=MAILBOX move emails that were ignored to MAILBOX +Overrides: + ATTRS is a comma separated list of attributes among: + * project, tracker, status, priority, category, assigned_to, fixed_version, + start_date, due_date, estimated_hours, done_ratio + * custom fields names with underscores instead of spaces (case insensitive) + + Example: allow_override=project,priority,my_custom_field + + If the project option is not set, project is overridable by default for + emails that create new issues. + + You can use allow_override=all to allow all attributes to be overridable. Examples: # No project specified. Emails MUST contain the 'Project' keyword: diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 40026b2d1..4a16eb7f9 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -40,10 +40,9 @@ class MailHandlerTest < ActiveSupport::TestCase Setting.clear_cache end - def test_add_issue + def test_add_issue_with_specific_overrides ActionMailer::Base.deliveries.clear lft1 = new_issue_lft - # This email contains: 'Project: onlinestore' issue = submit_email('ticket_on_given_project.eml', :allow_override => ['status', 'start_date', 'due_date', 'assigned_to', 'fixed_version', 'estimated_hours', 'done_ratio'] ) @@ -74,6 +73,46 @@ class MailHandlerTest < ActiveSupport::TestCase assert mail.subject.include?('New ticket on a given project') end + def test_add_issue_with_all_overrides + ActionMailer::Base.deliveries.clear + lft1 = new_issue_lft + issue = submit_email('ticket_on_given_project.eml', :allow_override => 'all') + assert issue.is_a?(Issue) + assert !issue.new_record? + issue.reload + assert_equal Project.find(2), issue.project + assert_equal issue.project.trackers.first, issue.tracker + assert_equal IssueStatus.find_by_name('Resolved'), issue.status + assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') + assert_equal '2010-01-01', issue.start_date.to_s + assert_equal '2010-12-31', issue.due_date.to_s + assert_equal User.find_by_login('jsmith'), issue.assigned_to + assert_equal Version.find_by_name('Alpha'), issue.fixed_version + assert_equal 2.5, issue.estimated_hours + assert_equal 30, issue.done_ratio + end + + def test_add_issue_without_overrides_should_ignore_attributes + WorkflowRule.delete_all + issue = submit_email('ticket_on_given_project.eml') + assert issue.is_a?(Issue) + assert !issue.new_record? + issue.reload + assert_equal Project.find(2), issue.project + assert_equal 'New ticket on a given project', issue.subject + assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') + assert_equal User.find_by_login('jsmith'), issue.author + + assert_equal issue.project.trackers.first, issue.tracker + assert_equal 'New', issue.status.name + assert_not_equal '2010-01-01', issue.start_date.to_s + assert_nil issue.due_date + assert_nil issue.assigned_to + assert_nil issue.fixed_version + assert_nil issue.estimated_hours + assert_equal 0, issue.done_ratio + end + def test_add_issue_with_default_tracker # This email contains: 'Project: onlinestore' issue = submit_email( @@ -86,9 +125,9 @@ class MailHandlerTest < ActiveSupport::TestCase assert_equal 'Support request', issue.tracker.name end - def test_add_issue_with_status + def test_add_issue_with_status_override # This email contains: 'Project: onlinestore' and 'Status: Resolved' - issue = submit_email('ticket_on_given_project.eml') + issue = submit_email('ticket_on_given_project.eml', :allow_override => ['status']) assert issue.is_a?(Issue) assert !issue.new_record? issue.reload @@ -185,7 +224,7 @@ class MailHandlerTest < ActiveSupport::TestCase def test_add_issue_with_custom_fields issue = submit_email('ticket_with_custom_fields.eml', - :issue => {:project => 'onlinestore'}, :allow_override => ['database', 'Searchable field'] + :issue => {:project => 'onlinestore'}, :allow_override => ['database', 'Searchable_field'] ) assert issue.is_a?(Issue) assert !issue.new_record? -- 2.39.5