Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
beccapearce committed Dec 30, 2024
1 parent 9498741 commit 63fdaff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 68 deletions.
6 changes: 1 addition & 5 deletions lib/govspeak/remove_advisory_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ def advisory_match_group(body_content)
end

def regexp_for_advisory_markup
opening_at = "(^@)"
content_after_at = '([\s\S]*?)'
closing_at = "(@?)"
other_possible_line_ends = '(?:^\$CTA|\r?\n\r?\n|^@|$)'
Regexp.new("#{opening_at}#{content_after_at}#{closing_at}(?=#{other_possible_line_ends})", Regexp::MULTILINE)
Govspeak::EmbeddedContentPatterns::ADVISORY.to_s
end

def replace_advisory_with_information_callout(match, body_content)
Expand Down
67 changes: 4 additions & 63 deletions lib/tasks/remove_advisory.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace :remove_advisory do
desc "Process advisory govspeak in published editions"
task published_editions: :environment do
task :published_editions, %i[dry_run] => :environment do |_, args|
regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s
successes = []
failures = []
Expand All @@ -18,38 +18,7 @@ namespace :remove_advisory do

published_content_containing_advisory_govspeak.each do |document_id|
edition = Document.find(document_id).latest_edition
Govspeak::RemoveAdvisoryService.new(edition, dry_run: false).process!
successes << edition.content_id
print "S"
rescue StandardError => e
failures << { content_id: edition.content_id, error: e.message }
print "F"
end

summarize_results(successes, failures)
end

desc "Dry run to show which editions would have advisory govspeak processed"
task dry_run_published_editions: :environment do
regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s

successes = []
failures = []
published_content_containing_advisory_govspeak = []

puts "\nStarting dry run of published editions...\n"

Edition
.where(state: "published")
.joins("RIGHT JOIN edition_translations ON edition_translations.edition_id = editions.id")
.where("body REGEXP ?", regex)
.find_each do |object|
published_content_containing_advisory_govspeak << object.document_id
end

published_content_containing_advisory_govspeak.each do |document_id|
edition = Document.find(document_id).latest_edition
Govspeak::RemoveAdvisoryService.new(edition, dry_run: true).process!
Govspeak::RemoveAdvisoryService.new(edition, dry_run: args[:dry_run]).process!
successes << edition.content_id
print "S"
rescue StandardError => e
Expand All @@ -61,7 +30,7 @@ namespace :remove_advisory do
end

desc "Process advisory govspeak in published HTML attachments"
task published_html_attachments: :environment do
task :published_html_attachments, %i[dry_run] => :environment do |_, args|
regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s

successes = []
Expand All @@ -77,7 +46,7 @@ namespace :remove_advisory do
.find_each do |attachment|
next if attachment.attachable.respond_to?(:state) && attachment.attachable.state != "published"

Govspeak::RemoveAdvisoryService.new(attachment, dry_run: false).process!
Govspeak::RemoveAdvisoryService.new(attachment, dry_run: args[:dry_run]).process!
successes << attachment.content_id
print "S"
rescue StandardError => e
Expand All @@ -89,34 +58,6 @@ namespace :remove_advisory do
end
end

desc "Dry run to show which HTML publications would have advisory govspeak processed"
task dry_run_published_html_attachments: :environment do
regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s

successes = []
failures = []

puts "\nStarting dry run of published HTML attachments...\n"

HtmlAttachment
.joins(:govspeak_content)
.where(deleted: false)
.where.not(attachable: nil)
.where("govspeak_contents.body REGEXP ?", regex)
.find_each do |attachment|
next if attachment.attachable.respond_to?(:state) && attachment.attachable.state != "published"

Govspeak::RemoveAdvisoryService.new(attachment, dry_run: true).process!
successes << attachment.content_id
print "S"
rescue StandardError => e
failures << { content_id: attachment.content_id, error: e.message }
print "F"
end

summarize_results(successes, failures)
end

def summarize_results(successes, failures)
puts "\n\nSummary:\n"
puts "Successes: #{successes.count}"
Expand Down
41 changes: 41 additions & 0 deletions test/unit/lib/govspeak/remove_advisory_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,45 @@ class Govspeak::RemoveAdvisoryServiceTest < ActiveSupport::TestCase

assert_equal expected, service.replace_all_advisories(edition.body)
end

test "replace_all_advisories will replace advisories with no space after the @" do
body = "@This is a very important message or warning@"
edition = create(:published_edition, body:)
service = Govspeak::RemoveAdvisoryService.new(edition)

expected = "^This is a very important message or warning^"

assert_equal expected, service.replace_all_advisories(edition.body)
end

test "replace_all_advisories will replace advisories with no closing @" do
body = "\r\n@ New online safety legislation is coming which will aim to reduce online harms.\r\n\r\n"
edition = create(:published_edition, body:)
service = Govspeak::RemoveAdvisoryService.new(edition)

expected = "\r\n^ New online safety legislation is coming which will aim to reduce online harms.^\r\n\r\n"

assert_equal expected, service.replace_all_advisories(edition.body)
end

test "replace_all_advisories will not replace anything resembling an email address" do
body = "\r\nFor further information please get in touch at [email protected].\r\n\r\n"
edition = create(:published_edition, body:)
service = Govspeak::RemoveAdvisoryService.new(edition)

expected = "\r\nFor further information please get in touch at [email protected].\r\n\r\n"

assert_equal expected, service.replace_all_advisories(edition.body)
end

test "replace_all_advisories will not replace twitter handles" do
# NB any instances of twitter handles at the start of a line have been handled separately
body = "\r\nTo hear more you can follow us at on @foobar\r\n\r\n"
edition = create(:published_edition, body:)
service = Govspeak::RemoveAdvisoryService.new(edition)

expected = "\r\nTo hear more you can follow us at on @foobar\r\n\r\n"

assert_equal expected, service.replace_all_advisories(edition.body)
end
end

0 comments on commit 63fdaff

Please sign in to comment.