diff --git a/lib/govspeak/embedded_content_patterns.rb b/lib/govspeak/embedded_content_patterns.rb index 784f51a4e8f..d938972e02f 100644 --- a/lib/govspeak/embedded_content_patterns.rb +++ b/lib/govspeak/embedded_content_patterns.rb @@ -4,5 +4,6 @@ module EmbeddedContentPatterns ADMIN_EDITION_PATH = %r{/admin/(?:#{Whitehall.edition_route_path_segments.join('|')})/(\d+)} ADMIN_ORGANISATION_CIP_PATH = %r{/admin/organisations/([\w-]+)/corporate_information_pages/(\d+)} ADMIN_WORLDWIDE_ORGANISATION_CIP_PATH = %r{/admin/worldwide_organisations/([\w-]+)/corporate_information_pages/(\d+)} + ADVISORY = /(^@)([\s\S]*?)(@?)(?=(?:^\$CTA|\r?\n\r?\n|^@|$))/m end end diff --git a/lib/govspeak/remove_advisory_service.rb b/lib/govspeak/remove_advisory_service.rb index 567f54cc9b1..ef7dda09ccf 100644 --- a/lib/govspeak/remove_advisory_service.rb +++ b/lib/govspeak/remove_advisory_service.rb @@ -2,60 +2,62 @@ module Govspeak class RemoveAdvisoryService attr_reader :body - def initialize(object) + def initialize(object, dry_run: true) @object = object @body = object.body || object.govspeak_content.body @whodunnit = User.find_by(name: "GDS Inside Government Team") + @dry_run = dry_run end def process! if @object.is_a?(Edition) AuditTrail.acting_as(@whodunnit) do - # Create a new draft of the edition draft = @object.create_draft(@whodunnit) # Replace advisories in the body of the new attachment new_body = replace_all_advisories(body) - # Update the draft edition and publish + # Update the draft edition with the new body and set to minor change draft.update!( body: new_body, minor_change: true, ) - draft.submit! - publish_reason = "Replacing deprecated advisory elements with information callouts" - edition_publisher = Whitehall.edition_services.publisher(draft, user: @whodunnit, remark: publish_reason) - edition_publisher.perform! + submit_and_publish!(draft) end elsif @object.is_a?(HtmlAttachment) AuditTrail.acting_as(@whodunnit) do - # Create a draft of the attachable edition + # Create a draft of the edition the attachment belongs to draft = @object.attachable.create_draft(@whodunnit) - # Find the attachment in the new draft + # Find the relevant attachment in the new draft new_attachment = draft.html_attachments.find_by(slug: @object.slug) # Replace advisories in the body of the new attachment new_body = replace_all_advisories(new_attachment.body) new_attachment.govspeak_content.update!(body: new_body) - # Update the draft edition and publish + # Set the owning draft edition to be a minor change draft.update!(minor_change: true) - draft.submit! - publish_reason = "Replacing deprecated advisory elements with information callouts" - edition_publisher = Whitehall.edition_services.publisher( - draft, - user: @whodunnit, - remark: publish_reason, - ) - edition_publisher.perform! + submit_and_publish!(draft) end else raise "Unsupported object type: #{@object.class.name}" end end + def submit_and_publish!(draft) + # Submit the draft so it is ready to be published + draft.submit! + + # Add a reason for force publishing + publish_reason = "Replacing deprecated advisory elements with information callouts" + + # Publish the edition + edition_publisher = Whitehall.edition_services.publisher(draft, user: @whodunnit, remark: publish_reason) + edition_publisher.perform! + end + def replace_all_advisories(body_content) match = advisory_match_group(body_content) return body_content if match.nil? diff --git a/lib/tasks/remove_advisory.rake b/lib/tasks/remove_advisory.rake index 2112c4313e5..b6c8e6a5b4c 100644 --- a/lib/tasks/remove_advisory.rake +++ b/lib/tasks/remove_advisory.rake @@ -1,7 +1,37 @@ namespace :remove_advisory_from_editions do desc "Process advisory govspeak in published editions" task published_editions: :environment do - regex = Regexp.new(/(^@)([\s\S]*?)(@?)(?=(?:^\$CTA|\r?\n\r?\n|^@|$))/m).to_s + regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s + successes = [] + failures = [] + published_content_containing_advisory_govspeak = [] + + puts "\nProcessing 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: 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 = [] @@ -19,8 +49,7 @@ namespace :remove_advisory_from_editions do published_content_containing_advisory_govspeak.each do |document_id| edition = Document.find(document_id).latest_edition - # should you be passing in the edition here rather than the document_id? - Govspeak::RemoveAdvisoryService.new(edition).process! + Govspeak::RemoveAdvisoryService.new(edition, dry_run: true).process! successes << edition.content_id print "S" rescue StandardError => e @@ -33,7 +62,7 @@ namespace :remove_advisory_from_editions do desc "Process advisory govspeak in published HTML attachments" task published_html_attachments: :environment do - regex = Regexp.new(/(^@)([\s\S]*?)(@?)(?=(?:^\$CTA|\r?\n\r?\n|^@|$))/m).to_s + regex = Govspeak::EmbeddedContentPatterns::ADVISORY.to_s successes = [] failures = [] @@ -56,13 +85,13 @@ namespace :remove_advisory_from_editions do summarize_results(successes, failures) end +end - def summarize_results(successes, failures) - puts "\n\nSummary:\n" - puts "Successes: #{successes.count}" - puts "Failures: #{failures.count}" - failures.each do |failure| - puts "Failed Content ID: #{failure[:content_id]}, Error: #{failure[:error]}" - end +def summarize_results(successes, failures) + puts "\n\nSummary:\n" + puts "Successes: #{successes.count}" + puts "Failures: #{failures.count}" + failures.each do |failure| + puts "Failed Content ID: #{failure[:content_id]}, Error: #{failure[:error]}" end end