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

Allow mail_handler to find case insensitive keys

Oliver Günther456f7b05ca87
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 60a4e3f68ab..c95dfe86b1f 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -334,13 +334,13 @@ class MailHandler < ActionMailer::Base
project = issue.project
attrs = {
- 'type_id' => (k = get_keyword(:type)) && project.types.find_by(name: k).try(:id),
- 'status_id' => (k = get_keyword(:status)) && Status.find_by(name: k).try(:id),
+ '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' => (k = get_keyword(:priority)) && IssuePriority.find_by(name: k).try(:id),
- 'category_id' => (k = get_keyword(:category)) && project.categories.find_by(name: k).try(:id),
+ 'priority_id' => lookup_case_insensitive_key(IssuePriority, :priority),
+ 'category_id' => lookup_case_insensitive_key(project.categories, :category),
'assigned_to_id' => assigned_to.try(:id),
- 'fixed_version_id' => (k = get_keyword(:fixed_version)) && project.shared_versions.find_by(name: k).try(:id),
+ 'fixed_version_id' => lookup_case_insensitive_key(project.shared_versions, :fixed_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),
@@ -363,6 +363,12 @@ class MailHandler < ActionMailer::Base
end
end
+ def lookup_case_insensitive_key(scope, attribute, column_name = Arel.sql('name'))
+ if k = get_keyword(attribute)
+ scope.find_by("lower(#{column_name}) = ?", k.downcase).try(:id)
+ end
+ end
+
# Returns the text/plain part of the email
# If not found (eg. HTML-only email), returns the body with tags removed
def plain_text_body
diff --git a/spec/fixtures/mail_handler/wp_on_given_project_case_insensitive.eml b/spec/fixtures/mail_handler/wp_on_given_project_case_insensitive.eml
new file mode 100644
index 00000000000..b77e3dfe520
--- /dev/null
+++ b/spec/fixtures/mail_handler/wp_on_given_project_case_insensitive.eml
@@ -0,0 +1,61 @@
+Return-Path: <[email protected]>
+Received: from osiris ([127.0.0.1])
+ by OSIRIS
+ with hMailServer ; Sun, 22 Jun 2008 12:28:07 +0200
+Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris>
+From: "John Smith" <[email protected]>
+Subject: New ticket on a given project
+Date: Sun, 22 Jun 2008 12:28:07 +0200
+MIME-Version: 1.0
+Content-Type: text/plain;
+ format=flowed;
+ charset="iso-8859-1";
+ reply-type=original
+Content-Transfer-Encoding: 7bit
+X-Priority: 3
+X-MSMail-Priority: Normal
+X-Mailer: Microsoft Outlook Express 6.00.2900.2869
+X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
+
+Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas imperdiet
+turpis et odio. Integer eget pede vel dolor euismod varius. Phasellus
+blandit eleifend augue. Nulla facilisi. Duis id diam. Class aptent taciti
+sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In
+in urna sed tellus aliquet lobortis. Morbi scelerisque tortor in dolor. Cras
+sagittis odio eu lacus. Aliquam sem tortor, consequat sit amet, vestibulum
+id, iaculis at, lectus. Fusce tortor libero, congue ut, euismod nec, luctus
+eget, eros. Pellentesque tortor enim, feugiat in, dignissim eget, tristique
+sed, mauris --- Pellentesque habitant morbi tristique senectus et netus et
+malesuada fames ac turpis egestas. Quisque sit amet libero. In hac habitasse
+platea dictumst.
+
+--- This line starts with a delimiter and should not be stripped
+
+This paragraph is before delimiters.
+
+BREAK
+
+This paragraph is between delimiters.
+
+---
+
+This paragraph is after the delimiter so it shouldn't appear.
+
+Nulla et nunc. Duis pede. Donec et ipsum. Nam ut dui tincidunt neque
+sollicitudin iaculis. Duis vitae dolor. Vestibulum eget massa. Sed lorem.
+Nullam volutpat cursus erat. Cras felis dolor, lacinia quis, rutrum et,
+dictum et, ligula. Sed erat nibh, gravida in, accumsan non, placerat sed,
+massa. Sed sodales, ante fermentum ultricies sollicitudin, massa leo
+pulvinar dui, a gravida orci mi eget odio. Nunc a lacus.
+
+Project: onlinestore
+status: resolved
+due date: 2010-12-31
+Start Date:2010-01-01
+Assigned to: John Smith
+fixed version: ALPHA
+priority: low
+estimated hours: 2.5
+done ratio: 30
+
diff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rb
index e0316c862ad..2c7c0d81c7c 100644
--- a/spec/models/mail_handler_spec.rb
+++ b/spec/models/mail_handler_spec.rb
@@ -66,6 +66,23 @@ describe MailHandler, type: :model do
end
end
+ shared_context 'wp_on_given_project_case_insensitive' do
+ let(:permissions) { %i[add_work_packages assign_versions] }
+ let!(:user) do
+ FactoryBot.create(:user,
+ firstname: 'John',
+ lastname: 'Smith',
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:submit_options) { {} }
+
+ subject do
+ submit_email('wp_on_given_project_case_insensitive.eml', **submit_options)
+ end
+ end
+
shared_context 'wp_update_with_quoted_reply_above' do
let(:permissions) { %i[edit_work_packages view_work_packages] }
let!(:user) do
@@ -344,6 +361,24 @@ describe MailHandler, type: :model do
.to eql(status)
end
end
+
+ context 'wp with status case insensitive' do
+ let!(:status) { FactoryBot.create(:status, name: 'Resolved') }
+ let!(:priority_low) { FactoryBot.create(:priority_low, name: 'Low', is_default: true) }
+ let!(:version) { FactoryBot.create(:version, name: 'alpha', project: project) }
+ let(:submit_options) { {allow_override: 'fixed_version'} }
+
+ # This email contains: 'Project: onlinestore' and 'Status: resolved'
+ include_context 'wp_on_given_project_case_insensitive'
+
+ it_behaves_like 'work package created'
+
+ it 'assigns the status to the created work package' do
+ expect(subject.status).to eq(status)
+ expect(subject.fixed_version).to eq(version)
+ expect(subject.priority).to eq priority_low
+ end
+ end
end
describe 'update work package' do