From ed18b3359b6d4fc4b9a8d81c310a6bf8d01ef9ce Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 4 May 2013 07:32:35 +0000 Subject: [PATCH] Adds a method for extracting MailHandler options from ENV. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11785 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/mail_handler.rb | 13 +++++++++++++ lib/tasks/email.rake | 30 +++--------------------------- test/unit/mail_handler_test.rb | 13 +++++++++++++ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 0b74c5ac8..ef67e575d 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -46,6 +46,19 @@ class MailHandler < ActionMailer::Base super(email) end + # Extracts MailHandler options from environment variables + # Use when receiving emails with rake tasks + def self.extract_options_from_env(env) + options = {:issue => {}} + %w(project status tracker category priority).each do |option| + options[:issue][option.to_sym] = env[option] if env[option] + end + %w(allow_override unknown_user no_permission_check no_account_notice default_group).each do |option| + options[option.to_sym] = env[option] if env[option] + end + options + end + def logger Rails.logger end diff --git a/lib/tasks/email.rake b/lib/tasks/email.rake index 934070e54..596b086d2 100644 --- a/lib/tasks/email.rake +++ b/lib/tasks/email.rake @@ -55,15 +55,7 @@ Examples: END_DESC task :read => :environment do - options = { :issue => {} } - %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] } - options[:allow_override] = ENV['allow_override'] if ENV['allow_override'] - options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user'] - options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check'] - options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice'] - options[:default_group] = ENV['default_group'] if ENV['default_group'] - - MailHandler.receive(STDIN.read, options) + MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV)) end desc <<-END_DESC @@ -130,15 +122,7 @@ END_DESC :move_on_success => ENV['move_on_success'], :move_on_failure => ENV['move_on_failure']} - options = { :issue => {} } - %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] } - options[:allow_override] = ENV['allow_override'] if ENV['allow_override'] - options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user'] - options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check'] - options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice'] - options[:default_group] = ENV['default_group'] if ENV['default_group'] - - Redmine::IMAP.check(imap_options, options) + Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV)) end desc <<-END_DESC @@ -165,15 +149,7 @@ END_DESC :password => ENV['password'], :delete_unprocessed => ENV['delete_unprocessed']} - options = { :issue => {} } - %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] } - options[:allow_override] = ENV['allow_override'] if ENV['allow_override'] - options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user'] - options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check'] - options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice'] - options[:default_group] = ENV['default_group'] if ENV['default_group'] - - Redmine::POP3.check(pop_options, options) + Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV)) end desc "Send a test email to the user with the provided login name" diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index b345683fe..d1eabca42 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -798,6 +798,19 @@ class MailHandlerTest < ActiveSupport::TestCase assert_equal str2, user.lastname end + def test_extract_options_from_env_should_return_options + options = MailHandler.extract_options_from_env({ + 'tracker' => 'defect', + 'project' => 'foo', + 'unknown_user' => 'create' + }) + + assert_equal({ + :issue => {:tracker => 'defect', :project => 'foo'}, + :unknown_user => 'create' + }, options) + end + private def submit_email(filename, options={})