OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
remove scanning subjects to identify reply email
ulferts46c31f717d7a
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rbindex 0694c28fbce..25aad008bca 100644--- a/app/models/mail_handler.rb+++ b/app/models/mail_handler.rb@@ -136,17 +136,9 @@ class MailHandler < ActionMailer::Baseprivate- REFERENCES_RE = %r{^<?op\.([a-z_]+)-(\d+)@}.freeze- ISSUE_REPLY_SUBJECT_RE = %r{.+? - .+ #(\d+):}.freeze- MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}.freeze-def dispatchif (m, object_id = dispatch_target_from_header)m.call(object_id)- elsif (m = email.subject.match(ISSUE_REPLY_SUBJECT_RE))- receive_work_package_reply(m[1].to_i)- elsif (m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE))- receive_message_reply(m[1].to_i)elsedispatch_to_defaultend@@ -170,6 +162,8 @@ class MailHandler < ActionMailer::Basereceive_work_packageend+ REFERENCES_RE = %r{^<?op\.([a-z_]+)-(\d+)@}.freeze+### Find a matching method to dispatch to given the mail's references header.# We set this header in outgoing emails to include an encoded reference to the object@@ -347,28 +341,20 @@ class MailHandler < ActionMailer::Baseend# Returns a Hash of issue attributes extracted from keywords in the email body- def wp_attributes_from_keywords(issue)- assigned_to = (k = get_keyword(:assigned_to, override: true)) && find_assignee_from_keyword(k, issue)- project = issue.project-- attrs = {- 'type_id' => lookup_case_insensitive_key(project.types, :type),- 'status_id' => lookup_case_insensitive_key(Status, :status),- 'parent_id' => (k = get_keyword(:parent)),- 'priority_id' => lookup_case_insensitive_key(IssuePriority, :priority),- 'category_id' => lookup_case_insensitive_key(project.categories, :category),- 'assigned_to_id' => assigned_to.try(:id),- 'version_id' => lookup_case_insensitive_key(project.shared_versions, :version, Arel.sql("#{Version.table_name}.name")),- 'start_date' => get_keyword(:start_date, override: true, format: '\d{4}-\d{2}-\d{2}'),- 'due_date' => get_keyword(:due_date, override: true, format: '\d{4}-\d{2}-\d{2}'),- 'estimated_hours' => get_keyword(:estimated_hours, override: true),- 'done_ratio' => get_keyword(:done_ratio, override: true, format: '(\d|10)?0')+ def wp_attributes_from_keywords(work_package)+ {+ 'type_id' => wp_type_from_keywords(work_package),+ 'status_id' => wp_status_from_keywords,+ 'parent_id' => wp_parent_from_keywords,+ 'priority_id' => wp_priority_from_keywords,+ 'category_id' => wp_category_from_keywords(work_package),+ 'assigned_to_id' => wp_assignee_from_keywords(work_package),+ 'version_id' => wp_version_from_keywords(work_package),+ 'start_date' => wp_start_date_from_keywords,+ 'due_date' => wp_due_date_from_keywords,+ 'estimated_hours' => wp_estimated_hours_from_keywords,+ 'done_ratio' => wp_done_ratio_from_keyword}.delete_if { |_, v| v.blank? }-- if issue.new_record? && attrs['type_id'].nil?- attrs['type_id'] = issue.project.types.first.try(:id)- end- attrsend# Returns a Hash of issue custom field values extracted from keywords in the email body@@ -495,27 +481,6 @@ class MailHandler < ActionMailer::Baseendend- def find_assignee_from_keyword(keyword, work_package)- keyword = keyword.to_s.downcase- assignable = Principal.possible_assignee(work_package.project)- assignee = nil- assignee ||= assignable.detect do |a|- [a.mail.to_s.downcase, a.login.to_s.downcase].include?(keyword)- end- if assignee.nil? && keyword.match(/ /)- firstname, lastname = *keyword.split # "First Last Throwaway"- assignee ||= assignable.detect do |a|- a.is_a?(User) && a.firstname.to_s.downcase == firstname &&- a.lastname.to_s.downcase == lastname- end- end- if assignee.nil?- assignee ||= assignable.detect { |a| a.is_a?(Group) && a.name.downcase == keyword }- end-- assignee- end-def create_work_package(project)work_package = WorkPackage.new(project: project)attributes = collect_wp_attributes_from_email_on_create(work_package)@@ -569,6 +534,55 @@ class MailHandler < ActionMailer::Base'journal_notes' => cleaned_up_text_body)end+ def wp_type_from_keywords(work_package)+ lookup_case_insensitive_key(work_package.project.types, :type) ||+ (work_package.new_record? && work_package.project.types.first.try(:id))+ end++ def wp_status_from_keywords+ lookup_case_insensitive_key(Status, :status)+ end++ def wp_parent_from_keywords+ get_keyword(:parent)+ end++ def wp_priority_from_keywords+ lookup_case_insensitive_key(IssuePriority, :priority)+ end++ def wp_category_from_keywords(work_package)+ lookup_case_insensitive_key(work_package.project.categories, :category)+ end++ def wp_assignee_from_keywords(work_package)+ keyword = get_keyword(:assigned_to, override: true)++ return nil if keyword.blank?++ Principal.possible_assignee(work_package.project).where(id: Principal.like(keyword)).first.try(:id)+ end++ def wp_version_from_keywords(work_package)+ lookup_case_insensitive_key(work_package.project.shared_versions, :version, Arel.sql("#{Version.table_name}.name"))+ end++ def wp_start_date_from_keywords+ get_keyword(:start_date, override: true, format: '\d{4}-\d{2}-\d{2}')+ end++ def wp_due_date_from_keywords+ get_keyword(:due_date, override: true, format: '\d{4}-\d{2}-\d{2}')+ end++ def wp_estimated_hours_from_keywords+ get_keyword(:estimated_hours, override: true)+ end++ def wp_done_ratio_from_keyword+ get_keyword(:done_ratio, override: true, format: '(\d|10)?0')+ end+def log(message, level = :info)message = "MailHandler: #{message}"logger.public_send(level, message)diff --git a/spec/fixtures/mail_handler/update_ticket_with_attachment_and_sig.eml b/spec/fixtures/mail_handler/update_ticket_with_attachment_and_sig.emlindex 651c37240cd..8a0321783b0 100644--- a/spec/fixtures/mail_handler/update_ticket_with_attachment_and_sig.eml+++ b/spec/fixtures/mail_handler/update_ticket_with_attachment_and_sig.eml@@ -1,6 +1,7 @@Mime-Version: 1.0 (Apple Message framework v730)Content-Type: multipart/mixed; boundary=Apple-Mail-13-196941151Message-Id: <[email protected]>+References: <[email protected]From: [email protected]Subject: OnlineStore - new Bug #123: Some new bugDate: Mon, 6 Jun 2005 22:21:22 +0200diff --git a/spec/fixtures/mail_handler/work_package_with_list_custom_field.eml b/spec/fixtures/mail_handler/wp_reply_with_list_custom_field.emlsimilarity index 92%rename from spec/fixtures/mail_handler/work_package_with_list_custom_field.emlrename to spec/fixtures/mail_handler/wp_reply_with_list_custom_field.emlindex 74a4044cc36..674fa2d74e2 100644--- a/spec/fixtures/mail_handler/work_package_with_list_custom_field.eml+++ b/spec/fixtures/mail_handler/wp_reply_with_list_custom_field.eml@@ -3,6 +3,7 @@ Received: from osiris ([127.0.0.1])by OSIRISwith hMailServer ; Sun, 22 Jun 2008 12:28:07 +0200Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris>+References: <[email protected]From: "Hans Wurst" <[email protected]>To: <[email protected]>Subject: OnlineStore - new Bug #42: Work package with list custom fielddiff --git a/spec/fixtures/mail_handler/work_package_with_text_custom_field.eml b/spec/fixtures/mail_handler/wp_reply_with_text_custom_field.emlsimilarity index 93%rename from spec/fixtures/mail_handler/work_package_with_text_custom_field.emlrename to spec/fixtures/mail_handler/wp_reply_with_text_custom_field.emlindex a188a435ee2..15d5ba8d34e 100644--- a/spec/fixtures/mail_handler/work_package_with_text_custom_field.eml+++ b/spec/fixtures/mail_handler/wp_reply_with_text_custom_field.eml@@ -3,6 +3,7 @@ Received: from osiris ([127.0.0.1])by OSIRISwith hMailServer ; Sun, 22 Jun 2008 12:28:07 +0200Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris>+References: <[email protected]From: "Hans Wurst" <[email protected]>To: <[email protected]>Subject: OnlineStore - new Bug #42: Work package with text custom fielddiff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rbindex 584c76cf560..3fcb1932441 100644--- a/spec/models/mail_handler_spec.rb+++ b/spec/models/mail_handler_spec.rb@@ -102,7 +102,7 @@ describe MailHandler, type: :model doendend- shared_context 'wp create with cc' do+ shared_context 'with wp create with cc' dolet(:permissions) { %i[add_work_packages view_work_packages add_work_package_watchers] }let!(:user) doFactoryBot.create(:user,@@ -390,12 +390,13 @@ describe MailHandler, type: :model doit 'rejects if unknown_user=accept and permission check is present' doexpected ='MailHandler: work_package could not be created by Anonymous due to ' \- '#["may not be accessed.", "Type was attempted to be written but is not writable.", ' \- '"Project was attempted to be written but is not writable.", ' \- '"Subject was attempted to be written but is not writable.", ' \- '"Description was attempted to be written but is not writable."]'+ '#["may not be accessed.", ' \+ '"Type was attempted to be written but is not writable.", ' \+ '"Project was attempted to be written but is not writable.", ' \+ '"Subject was attempted to be written but is not writable.", ' \+ '"Description was attempted to be written but is not writable."]'- expect(Rails.logger)+ allow(Rails.logger).to receive(:error).with(expected)@@ -404,6 +405,10 @@ describe MailHandler, type: :model dounknown_user: 'accept'expect(result).to eq false++ expect(Rails.logger)+ .to have_received(:error)+ .with(expected)endit 'accepts if unknown_user=accept and no_permission_check' do@@ -475,7 +480,7 @@ describe MailHandler, type: :model doendcontext 'wp with cc' do- include_context 'wp create with cc'+ include_context 'with wp create with cc'it_behaves_like 'work package created'@@ -521,7 +526,7 @@ describe MailHandler, type: :model dowork_package.reloadallow(WorkPackage).to receive(:find_by).with(id: 123).and_return(work_package)- # Mail with two attachemnts, one of which is skipped by signature.asc filename match+ # Mail with two attachments, one of which is skipped by signature.asc filename matchsubmit_email 'update_ticket_with_attachment_and_sig.eml', issue: { project: 'onlinestore' }expect(work_package.attachments.length).to eq 2@@ -620,15 +625,15 @@ describe MailHandler, type: :model doallow_any_instance_of(WorkPackage).to receive(:available_custom_fields).and_return([custom_field])- expect(WorkPackage).to receive(:find_by).with(id: 42).and_return(work_package)- expect(User).to receive(:find_by_mail).with("[email protected]").and_return(mail_user)+ allow(WorkPackage).to receive(:find_by).with(id: 42).and_return(work_package)+ allow(User).to receive(:find_by_mail).with("[email protected]").and_return(mail_user)endcontext 'of type text' dolet(:custom_field) { FactoryBot.create :text_wp_custom_field, name: "Notes" }before do- submit_email 'work_package_with_text_custom_field.eml', issue: { project: project.identifier }+ submit_email 'wp_reply_with_text_custom_field.eml', issue: { project: project.identifier }work_package.reloadend@@ -644,7 +649,7 @@ describe MailHandler, type: :model dolet(:custom_field) { FactoryBot.create :list_wp_custom_field, name: "Letters", possible_values: %w(A B C) }before do- submit_email 'work_package_with_list_custom_field.eml', issue: { project: project.identifier }+ submit_email 'wp_reply_with_list_custom_field.eml', issue: { project: project.identifier }work_package.reloadenddiff --git a/spec_legacy/unit/mail_handler_spec.rb b/spec_legacy/unit/mail_handler_spec.rbindex ca9c6477a73..872c5fd4e67 100644--- a/spec_legacy/unit/mail_handler_spec.rb+++ b/spec_legacy/unit/mail_handler_spec.rb@@ -242,7 +242,7 @@ describe MailHandler, type: :model doendit 'should ignore emails from locked users' do- User.find(2).lock!+ User.find(2).locked!expect_any_instance_of(MailHandler).to receive(:dispatch).neverassert_no_difference 'WorkPackage.count' do