diff --git a/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake b/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake
new file mode 100644
index 000000000..c411ced10
--- /dev/null
+++ b/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake
@@ -0,0 +1,54 @@
+require Rails.root.join("lib", "tasks", "task_helper")
+
+namespace :after_party do
+ # Matches "ne pas répondre", plus some content before and after:
+ # - try to remove dashes before the footer
+ # - try to remove line-breaks and empty HTML tags before the footer text
+ # - matches "Veuillez ne pas répondre" or "Merci de ne pas répondre"
+ # - once the footer text is found, extend the match to the end of the body
+ FOOTER_REGEXP = /(—|---|-)?( |\r|\n|
|
|<\/p>||<\/small>||<\/b>| )*(Veuillez)?(Merci)?( |\r|\n)*(de)? ne pas répondre(.*)$/m + # When the footer contains any of these words, it is kept untouched. + FOOTER_EXCEPTIONS = [ + 'PDF', + '@', + 'Hadrien', + 'Esther', + 'Sicoval', + 'a323', + 'SNC', + 'Polynésie', + 'drac', + 'theplatform' + ] + + desc 'Deployment task: remove_footer_from_email_templates' + task remove_footer_from_email_templates: :environment do + rake_puts "Running deploy task 'remove_footer_from_email_templates'" + + models = [ + Mails::ClosedMail, + Mails::InitiatedMail, + Mails::ReceivedMail, + Mails::RefusedMail, + Mails::WithoutContinuationMail + ] + + models.each do |model_class| + model_class.all.find_each do |template| + remove_footer(template) + end + end + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord.create version: '20181120133842' + end # task :remove_footer_from_email_templates + + def remove_footer(template) + matches = template.body.match(FOOTER_REGEXP) + if matches && FOOTER_EXCEPTIONS.none? { |exception| matches[0].include?(exception) } + rake_puts "#{template.model_name.to_s} \##{template.id}: removing footer" + template.update(body: matches.pre_match) + end + end +end # namespace :after_party diff --git a/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb b/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb new file mode 100644 index 000000000..d1a914240 --- /dev/null +++ b/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb @@ -0,0 +1,72 @@ +describe '20181120133842_remove_footer_from_email_templates.rake' do + let(:rake_task) { Rake::Task['after_party:remove_footer_from_email_templates'] } + let(:templates) do + bodies.map { |body| create(:received_mail, body: body) } + end + + before do + templates + end + + subject! do + rake_task.invoke + end + + after do + rake_task.reenable + end + + context 'when emails have "do not reply" footers' do + let(:bodies) do + [ + "
Some content
---Some content
-Some content
—Some content
---Some content
—Some content
---Some content
---\r\n\r\n
\r\n
Some content
---Some content
---\r\n\r\n
\r\n
Some content
---Some content
' + end + end + end + + context 'when emails don’t have the standard boilerplate in the footer' do + let(:bodies) do + [ + "Some content.
Merci, l'équipe demarches-simplifiees.fr.\r\n
", + "Some content.
Merci, l'équipe TPS.\r\n
" + ] + end + + it 'keeps the footer' do + templates.each do |template| + expect(bodies).to include(template.reload.body) + end + end + end + + context 'when the footer contains some excluded strings' do + let(:bodies) do + [ + "Some content
---Some content
---Some content
---