Skip to content

Commit

Permalink
WIP- PR changes, still needs dry run implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
beccapearce committed Dec 13, 2024
1 parent 2dfbf34 commit 82aafa3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/govspeak/embedded_content_patterns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 20 additions & 18 deletions lib/govspeak/remove_advisory_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
51 changes: 40 additions & 11 deletions lib/tasks/remove_advisory.rake
Original file line number Diff line number Diff line change
@@ -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 = []
Expand All @@ -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
Expand All @@ -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 = []
Expand All @@ -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

0 comments on commit 82aafa3

Please sign in to comment.