Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/tasks/unpublish_statistics_announcements.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace :statistics_announcements do
desc "Unpublish statistics announcements and redirect them"
# Usage:
# 1. Create a file with one StatisticsAnnouncement ID per line
# 2. Copy the file to a pod:
# $ kubectl cp ids.txt whitehall-admin-xxxxx:/tmp/ids.txt
# 3. Run the task:
# $ kubectl exec whitehall-admin-xxxxx -- rake "statistics_announcements:unpublish_and_redirect[/tmp/ids.txt,https://www.gov.uk/government/statistics/redirect-page]"
#
task :unpublish_and_redirect, %i[file redirect_url] => :environment do |_task, args|
file = args[:file]
redirect_url = args[:redirect_url]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what we want, to redirect all the announcements to a single URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. No, we need different redirect URL for each announcement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't checked all of them, but is it possible to unpublish without providing a redirect url?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can send an unpublish request to Publishing API with a type value of "gone". See https://docs.publishing.service.gov.uk/repos/publishing-api/api.html#post-v2contentcontent_idunpublish

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked over the announcements and all of them have a publication. So I just tweaked a bit the task to accept a redirect url for each statistics announcement id


unless file && File.exist?(file) && redirect_url
puts "Usage: rake statistics_announcements:unpublish_and_redirect[file.txt,https://www.gov.uk/redirect]"
next
end

ids = File.readlines(file).map(&:strip).reject(&:empty?)

puts "Found #{ids.count} announcements to unpublish"
puts "Will redirect to: #{redirect_url}"
print "Continue? (y/n): "
next unless $stdin.gets.chomp.downcase == "y"

success_count = 0
failure_count = 0

ids.each do |id|
announcement = StatisticsAnnouncement.unscoped.find(id)
announcement.update!(publishing_state: "unpublished", redirect_url: redirect_url)
puts "#{id} - #{announcement.title} - OK"
success_count += 1
rescue ActiveRecord::RecordNotFound
puts "#{id} - NOT FOUND"
failure_count += 1
rescue StandardError => e
puts "#{id} - ERROR: #{e.message}"
failure_count += 1
end

puts "\nDone: #{success_count} success, #{failure_count} failed"
end
end