From: Jean-Philippe Lang Date: Mon, 19 Oct 2015 16:56:13 +0000 (+0000) Subject: Makes project selection by subaddress optional (#20732). X-Git-Tag: 3.2.0~170 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3795c22730f470146b02110a168276dce45755a4;p=redmine.git Makes project selection by subaddress optional (#20732). git-svn-id: http://svn.redmine.org/redmine/trunk@14690 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 770610427..e016dacfe 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -65,7 +65,7 @@ class MailHandler < ActionMailer::Base %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| + %w(allow_override unknown_user no_permission_check no_account_notice default_group project_from_subaddress).each do |option| options[option.to_sym] = env[option] if env[option] end if env['private'] @@ -365,11 +365,15 @@ class MailHandler < ActionMailer::Base end def get_project_from_receiver_addresses + local, domain = handler_options[:project_from_subaddress].to_s.split("@") + return nil unless local && domain + local = Regexp.escape(local) + [:to, :cc, :bcc].each do |field| header = @email[field] next if header.blank? || header.field.blank? || !header.field.respond_to?(:addrs) header.field.addrs.each do |addr| - if addr.local.to_s =~ /\+([^+]+)\z/ + if addr.domain.to_s.casecmp(domain)==0 && addr.local.to_s =~ /\A#{local}\+([^+]+)\z/ if project = Project.find_by_identifier($1) return project end diff --git a/extra/mail_handler/rdm-mailhandler.rb b/extra/mail_handler/rdm-mailhandler.rb index dde50547e..a604355f1 100644 --- a/extra/mail_handler/rdm-mailhandler.rb +++ b/extra/mail_handler/rdm-mailhandler.rb @@ -45,7 +45,7 @@ class RedmineMailHandler VERSION = '0.2.3' attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :default_group, :no_permission_check, - :url, :key, :no_check_certificate, :certificate_bundle, :no_account_notice, :no_notification + :url, :key, :no_check_certificate, :certificate_bundle, :no_account_notice, :no_notification, :project_from_subaddress def initialize self.issue_attributes = {} @@ -86,6 +86,8 @@ class RedmineMailHandler "user") { |v| self.no_notification = '1'} opts.separator("") opts.separator("Issue attributes control options:") + opts.on( "--project-from-subaddress ADDR", "select project from subadress of ADDR found", + "in To, Cc, Bcc headers") {|v| self.project_from_subaddress['project'] = v} opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} @@ -104,7 +106,6 @@ Overrides: * 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 @@ -113,15 +114,24 @@ Overrides: You can use --allow_override=all to allow all attributes to be overridable. Examples: - No project specified, emails MUST contain the 'Project' keyword: - rdm-mailhandler.rb --url http://redmine.domain.foo --key secret + No project specified, emails MUST contain the 'Project' keyword, otherwise + they will be dropped (not recommanded): + + rdm-mailhandler.rb --url http://redmine.domain.foo --key secret Fixed project and default tracker specified, but emails can override both tracker and priority attributes using keywords: - rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\ - --project foo \\ - --tracker bug \\ - --allow-override tracker,priority + + rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\ + --project myproject \\ + --tracker bug \\ + --allow-override tracker,priority + + Project selected by subaddress of redmine@example.net. Sending the email + to redmine+myproject@example.net will add the issue to myproject: + + rdm-mailhandler.rb --url http://redmine.domain.foo --key secret \\ + --project-from-subaddress redmine@example.net END_DESC opts.summary_width = 27 @@ -145,7 +155,8 @@ END_DESC 'default_group' => default_group, 'no_account_notice' => no_account_notice, 'no_notification' => no_notification, - 'no_permission_check' => no_permission_check} + 'no_permission_check' => no_permission_check, + 'project_from_subaddress' => project_from_subaddress} issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } debug "Posting to #{uri}..." diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 5d392c6f4..1ec35adce 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -107,7 +107,8 @@ class MailHandlerTest < ActiveSupport::TestCase # This email has redmine+onlinestore@somenet.foo as 'To' header issue = submit_email( 'ticket_on_project_given_by_to_header.eml', - :issue => {:tracker => 'Support request'} + :issue => {:tracker => 'Support request'}, + :project_from_subaddress => 'redmine@somenet.foo' ) assert issue.is_a?(Issue) assert !issue.new_record?