From 3e987468606c4897ba44fd4f8c3d112401c628a6 Mon Sep 17 00:00:00 2001 From: Mikhail Voronyuk Date: Fri, 1 May 2015 23:45:41 +0300 Subject: [PATCH] receiving emails with ISSUE-id issue number style [#PROJECT-123]; fix Loofah error "NameError: uninitialized constant Loofah::Helpers" --- app/models/mail_handler.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index c03b0bf..486cfe5 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -155,6 +155,12 @@ class MailHandler < ActionMailer::Base # the subject may contains alien issue IDs. Let's check if one of the issue IDs is proper issue_id = get_issue_id_from_subject(subject) receive_issue_reply(issue_id) + elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE_ISSUE_ID) + # the subject may contains alien issue IDs. Let's check if one of the issue IDs is proper + logger.info "vmn Analyze the subject" if logger + issue = get_issue_from_subject_issue_id(subject) + logger.info "vmn issue found: " + issue.to_yaml if logger + receive_issue_reply(issue) elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE) receive_message_reply(m[1].to_i) else @@ -186,6 +192,21 @@ class MailHandler < ActionMailer::Base return end + ISSUE_REPLY_SUBJECT_RE_ISSUE_ID = %r{\[[^\]]*#([A-Z][A-Z0-9]*-\d+)\]} + def get_issue_from_subject_issue_id(subject) + scan_result = subject.scan(ISSUE_REPLY_SUBJECT_RE_ISSUE_ID) + scan_result.each do |match| + issue_id = match[0] + issue = Issue.find(issue_id) + if issue + # we can get an email with more than 1 proper issue ID. Let's return 1-st as it was in the original code. + return issue + end + end + # no matches + return + end + def dispatch_to_default receive_issue end @@ -217,7 +238,11 @@ class MailHandler < ActionMailer::Base # Adds a note to an existing issue def receive_issue_reply(issue_id, from_journal=nil) - issue = Issue.find_by_id(issue_id) + if issue_id.is_a?(Issue) + issue = issue_id + else + issue = Issue.find_by_id(issue_id) + end return unless issue # check permission unless @@handler_options[:no_permission_check] @@ -449,6 +474,7 @@ class MailHandler < ActionMailer::Base #Loofah works bad with nested div contains br, so let's help to it and preparse the body body.gsub! /<[bB][rR]>/, "\n" # convert html parts to text + require 'loofah/helpers' p.mime_type == 'text/html' ? Loofah.document(body).to_text : body end.join("\r\n") -- 1.9.5 (Apple Git-50.3)