Skip to content

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

Fix message-id regex in mail_handler

Oliver Güntherc358b071c536
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index fe587dbd52b..cc77349acb3 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -132,22 +132,16 @@ class MailHandler < ActionMailer::Base
private
- MESSAGE_ID_RE = %r{^<?openproject\.([a-z0-9_]+)-(\d+)\.\d+@}
+ MESSAGE_ID_RE = %r{^<?openproject\.([a-z0-9_]+)-(\d+)-(\d+)\.\d+@}
ISSUE_REPLY_SUBJECT_RE = %r{.+? - .+ #(\d+):}
MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
def dispatch
- headers = [email.in_reply_to, email.references].flatten.compact
- if headers.detect { |h| h.to_s =~ MESSAGE_ID_RE }
- klass = $1
- object_id = $2.to_i
- method_name = "receive_#{klass}_reply"
- if self.class.private_instance_methods.map(&:to_s).include?(method_name)
- send method_name, object_id
- end
- elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
+ if (m, object_id = dispatch_target_from_message_id)
+ 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)
+ elsif (m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE))
receive_message_reply(m[1].to_i)
else
dispatch_to_default
@@ -172,6 +166,20 @@ class MailHandler < ActionMailer::Base
receive_work_package
end
+ ##
+ # Find a matching method to dispatch to given the mail's message ID
+ def dispatch_target_from_message_id
+ headers = [email.references, email.in_reply_to].flatten.compact
+ if headers.detect { |h| h.to_s =~ MESSAGE_ID_RE }
+ klass = $1
+ object_id = $3.to_i
+ method_name = :"receive_#{klass}_reply"
+ if self.class.private_instance_methods.include?(method_name)
+ return method(method_name), object_id
+ end
+ end
+ end
+
# Creates a new work package
def receive_work_package
project = target_project
diff --git a/spec/fixtures/mail_handler/message_reply.eml b/spec/fixtures/mail_handler/message_reply.eml
new file mode 100644
index 00000000000..6785d78197a
--- /dev/null
+++ b/spec/fixtures/mail_handler/message_reply.eml
@@ -0,0 +1,60 @@
+Date: Tue, 16 Mar 2021 16:24:54 +0100
+Message-ID: <[email protected]>
+Subject: [OpenProject - General discussion - msg12559] Test
+ fields in user stories
+Mime-Version: 1.0
+Content-Type: multipart/alternative;
+ boundary="--==_mimepart_6050cdc2e4db_2257939a87567a";
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+X-OpenProject-Project: openproject
+X-OpenProject-Wiki-Page-Id: 12559
+X-OpenProject-Type: Forum
+
+
+----==_mimepart_6050cdc2e4db_2257939a87567a
+Content-Type: text/plain;
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+
+
+
+
+
+http://localhost:3000/topics/12559?r=12559#message-12559
+Test User
+
+Test message
+
+
+--
+You have received this notification because you have either subscribed to it, or are involved in it.
+To change your notification preferences, please click here: https://community.openproject.org/my/mail_notifications
+
+----==_mimepart_6050cdc2e4db_2257939a87567a
+Content-Type: text/html;
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+
+<html>
+ <body>
+ <span class="header"></span>
+
+
+<h1>
+ OpenProject - General discussion: <a href="http://localhost:3000/topics/12559?r=12559#message-12559">Ready-If and Done-If fields in user stories</a>
+</h1>
+<em>Test user</em>
+
+<p class="op-uc-p">Test message</p>
+
+
+ <hr />
+ <span class="footer"><p class="op-uc-p">You have received this notification because you have either subscribed to it, or are involved in it.<br>
+To change your notification preferences, please click here: <a href="https://community.openproject.org/my/mail_notifications" class="op-uc-link">https://community.openproject.org/my/mail_notifications</a></p></span>
+ </body>
+</html>
+
+----==_mimepart_6050cdc2e4db_2257939a87567a--
diff --git a/spec/fixtures/mail_handler/work_package_reply.eml b/spec/fixtures/mail_handler/work_package_reply.eml
new file mode 100644
index 00000000000..c1aa58ab534
--- /dev/null
+++ b/spec/fixtures/mail_handler/work_package_reply.eml
@@ -0,0 +1,108 @@
+Date: Tue, 16 Mar 2021 15:17:04 +0100
+Message-ID: <openproject.work_package-70917-34540.20210302144105@openproject.com>
+References: <openproject.work_package-70917-34540.20210215121742@openproject.com>
+Subject: test - new Task #34540: asdf
+Mime-Version: 1.0
+Content-Type: multipart/alternative;
+ boundary="--==_mimepart_6050bdd0cb97_2257939a8755e5";
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+X-OpenProject-Project: test
+X-OpenProject-Issue-Id: 34540
+X-OpenProject-Issue-Author: admin
+X-OpenProject-Type: WorkPackage
+X-OpenProject-Issue-Assignee: reader
+
+
+----==_mimepart_6050bdd0cb97_2257939a8755e5
+Content-Type: text/plain;
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+
+
+
+
+
+Work package #34540 has been updated by admin lastname.
+
+ Assignee set to reader reader
+
+
+----------------------------------------
+
+Task #34540: asdf
+http://localhost:3000/work_packages/34540
+
+Author: admin lastname
+Status: new
+Priority: Medium
+Assignee: reader reader
+Accountable:
+Category:
+Version: test version
+Start date:
+Finish date:
+Estimated time:
+Module:
+Onboarding Activity:
+Ordered by:
+Prio board:
+
+
+
+
+
+--
+You have received this notification because you have either subscribed to it, or are involved in it.
+To change your notification preferences, please click here: https://community.openproject.org/my/mail_notifications
+
+----==_mimepart_6050bdd0cb97_2257939a8755e5
+Content-Type: text/html;
+ charset=UTF-8
+Content-Transfer-Encoding: 7bit
+
+<html>
+ <body>
+ <span class="header"></span>
+
+ Work package #34540 has been updated by admin lastname.
+<ul>
+ <li><strong>Assignee</strong> set to <i title="reader reader">reader reader</i></li>
+</ul>
+
+<hr />
+
+<h1><a href="http://localhost:3000/work_packages/34540">Task #34540: asdf</a></h1>
+
+<ul>
+ <li>Author: admin lastname</li>
+ <li>Status: new</li>
+ <li>Priority: Medium</li>
+ <li>Assignee: reader reader</li>
+ <li>Accountable: </li>
+ <li>Category: </li>
+ <li>Version: test version</li>
+ <li>Start date: </li>
+ <li>Finish date: </li>
+ <li>Estimated time: </li>
+
+
+ <li>Module: </li>
+ <li>Onboarding Activity: </li>
+ <li>Ordered by: </li>
+ <li>Prio board: </li>
+</ul>
+
+
+
+
+
+ <hr />
+ <span class="footer"><p class="op-uc-p">You have received this notification because you have either subscribed to it, or are involved in it.<br>
+To change your notification preferences, please click here: <a href="https://community.openproject.org/my/mail_notifications" class="op-uc-link">https://community.openproject.org/my/mail_notifications</a></p></span>
+ </body>
+</html>
+
+----==_mimepart_6050bdd0cb97_2257939a8755e5--
diff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rb
index f0e6d841d98..d9d28dae052 100644
--- a/spec/models/mail_handler_spec.rb
+++ b/spec/models/mail_handler_spec.rb
@@ -605,13 +605,46 @@ describe MailHandler, type: :model do
end
end
+ describe '#dispatch_target_from_message_id' do
+ let!(:mail_user) { FactoryBot.create :admin, mail: '[email protected]' }
+ let(:instance) do
+ mh = MailHandler.new
+ mh.options = {}
+ mh
+ end
+ subject { instance.receive mail }
+
+ context 'receiving reply from work package' do
+ let(:mail) { Mail.new(read_email('work_package_reply.eml')) }
+
+ it 'calls the work package reply' do
+ expect(instance).to receive(:receive_work_package_reply).with(34540)
+
+ subject
+ end
+ end
+
+ context 'receiving reply from message' do
+ let(:mail) { Mail.new(read_email('message_reply.eml')) }
+
+ it 'calls the work package reply' do
+ expect(instance).to receive(:receive_message_reply).with(12559)
+
+ subject
+ end
+ end
+ end
+
private
FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
+ def read_email(filename)
+ IO.read(File.join(FIXTURES_PATH, filename))
+ end
+
def submit_email(filename, options = {})
- raw = IO.read(File.join(FIXTURES_PATH, filename))
- MailHandler.receive(raw, options)
+ MailHandler.receive(read_email(filename), options)
end
def work_package_created(work_package)