From 1659dcd5361eacadfa16e177cbbc4c98dfb14143 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Tue, 19 Nov 2024 15:04:50 +0000 Subject: [PATCH] Remove obsolete 'bulk_republish' rake tasks These all now have equivalents in forms under https://whitehall-admin.publishing.service.gov.uk/government/admin/republishing --- lib/tasks/publishing_api.rake | 116 --------- test/unit/lib/tasks/publishing_api_test.rb | 273 --------------------- 2 files changed, 389 deletions(-) diff --git a/lib/tasks/publishing_api.rake b/lib/tasks/publishing_api.rake index d69dabe434b..abf52707c59 100644 --- a/lib/tasks/publishing_api.rake +++ b/lib/tasks/publishing_api.rake @@ -123,122 +123,6 @@ namespace :publishing_api do end end - namespace :bulk_republish do - desc "Republish all About pages" - task all_about_pages: :environment do - about_us_pages = Organisation.all.map(&:about_us).compact - count = about_us_pages.count - puts "# Sending #{count} 'about us' pages to Publishing API" - about_us_pages.each_with_index do |about_us_page, i| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue( - "bulk_republishing", - about_us_page.document_id, - true, - ) - puts "Queuing #{i}-#{i + 99} of #{count} items" if (i % 100).zero? - end - puts "Finished queuing items for Publishing API" - end - - desc "Republish all documents with draft editions" - task all_drafts: :environment do - editions = Edition.in_pre_publication_state.includes(:document) - - puts "Enqueueing #{editions.count} documents" - editions.find_each do |edition| - document_id = edition.document.id - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document_id, true) - end - puts "Finished enqueueing items for Publishing API" - end - - desc "Republish all editions which have attachments to the Publishing API" - task editions_with_attachments: :environment do - editions = Edition.publicly_visible.where( - id: Attachment.where(attachable_type: "Edition").select("attachable_id"), - ) - - editions.joins(:document).distinct.pluck("documents.id").each do |document_id| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document_id, true) - end - end - - desc "⚠️ WARNING: this rake task republishes **all** documents with HTML attachments (this can block publishing for > 1 hour) ⚠️. Republish all documents with HTML attachments to the Publishing API." - task html_attachments: :environment do - document_ids = Edition - .publicly_visible - .where(id: HtmlAttachment.where(attachable_type: "Edition").select(:attachable_id)) - .pluck(:document_id) - document_ids.each do |document_id| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document_id, true) - end - end - - desc "Republish all draft editions with HTML attachments to the Publishing API" - task drafts_with_html_attachments: :environment do - document_ids = Edition - .in_pre_publication_state - .where(id: HtmlAttachment.where(attachable_type: "Edition").select(:attachable_id)) - .pluck(:document_id) - document_ids.each do |document_id| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document_id, true) - end - end - - desc "Republish all documents of a given type, e.g. 'NewsArticle'" - task :document_type, [:document_type] => :environment do |_, args| - begin - document_type = args[:document_type].constantize - rescue NameError - abort "Unknown document type #{args[:document_type]}\nCheck the GOV.UK developer documentation for a list of acceptable document types: https://docs.publishing.service.gov.uk/manual/republishing-content.html#whitehall" - end - - documents = document_type.all - puts "Enqueueing #{documents.count} documents" - documents.find_each do |document| - if document.respond_to?(:publish_to_publishing_api) - Whitehall::PublishingApi.bulk_republish_async(document) if document.can_publish_to_publishing_api? - else - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document.document_id, true) - end - end - puts "Finished enqueueing items for Publishing API" - end - - desc "Republish all documents of a given organisation" - task :by_organisation, [:organisation_slug] => :environment do |_, args| - org = Organisation.find_by(slug: args[:organisation_slug]) - editions = Edition.latest_edition.in_organisation(org) - puts "Enqueueing #{editions.count} documents" - editions.find_each do |edition| - document = edition.document - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document.id, true) - end - puts "Finished enqueueing items for Publishing API" - end - - desc "Republish documents by content id" - task :documents_by_content_ids, %w[content_ids] => :environment do |_, args| - content_ids = args[:content_ids].split - document_ids = Document.where(content_id: content_ids).pluck(:id) - - puts "Bulk republishing #{document_ids.count} documents" - - document_ids.each do |id| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", id, true) - end - end - - desc "Republish all documents" - task all_documents: :environment do - puts "Enqueueing #{Document.count} documents" - Document.find_each do |document| - PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document.id, true) - end - puts "Finished enqueueing items for Publishing API" - end - end - namespace :unpublish do desc "Manually unpublish content with a redirect" # This task is for unpublishing Whitehall managed content where diff --git a/test/unit/lib/tasks/publishing_api_test.rb b/test/unit/lib/tasks/publishing_api_test.rb index 27e55516ce5..d936d541b43 100644 --- a/test/unit/lib/tasks/publishing_api_test.rb +++ b/test/unit/lib/tasks/publishing_api_test.rb @@ -117,279 +117,6 @@ class PublishingApiRake < ActiveSupport::TestCase end end - describe "bulk_republish" do - describe "#all_about_pages" do - let(:task) { Rake::Task["publishing_api:bulk_republish:all_about_pages"] } - - test "republishes all about pages" do - corporate_info = create( - :published_corporate_information_page, - corporate_information_page_type_id: CorporateInformationPageType::AboutUs.id, - ) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - corporate_info.document_id, - true, - ) - - capture_io { task.invoke } - end - end - - describe "#all_drafts" do - let(:task) { Rake::Task["publishing_api:bulk_republish:all_drafts"] } - - test "republishes all draft editions" do - # A draft document which hasn't been published yet - draft_only = create(:draft_detailed_guide).document - - # A published document with no draft edition - # This document should *not* be republished - create(:published_detailed_guide).document - - # A published document with a newer draft edition - published_with_draft = create(:published_detailed_guide).document - create(:draft_detailed_guide, document: published_with_draft) - - # A scheduled document which isn't live yet - scheduled = create(:scheduled_detailed_guide).document - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - draft_only.id, - true, # Publishing API will queue as low priority - ) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - published_with_draft.id, - true, # Publishing API will queue as low priority - ) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - scheduled.id, - true, # Publishing API will queue as low priority - ) - - capture_io { task.invoke } - end - end - - describe "#editions_with_attachments" do - let(:task) { Rake::Task["publishing_api:bulk_republish:editions_with_attachments"] } - - test "republishes all live editions with attachments" do - live_editions = [ - create(:published_publication, :with_html_attachment), - create(:published_publication, :with_external_attachment), - create(:published_publication, :with_file_attachment), - ] - - other_editions = [ - create(:draft_publication, :with_file_attachment), - create(:published_news_article), # without attachments - ] - - live_editions.each do |edition| - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document_id, - true, - ).once - end - - other_editions.each do |edition| - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document_id, - true, - ).never - end - - capture_io { task.invoke } - end - end - - describe "#html_attachments" do - let(:task) { Rake::Task["publishing_api:bulk_republish:html_attachments"] } - - test "republishes all documents with HMTL attachments" do - edition = create(:published_publication, :with_html_attachment) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document_id, - true, - ) - capture_io { task.invoke } - end - end - - describe "#publication_drafts_with_html_attachments" do - let(:task) { Rake::Task["publishing_api:bulk_republish:drafts_with_html_attachments"] } - - test "republishes draft publication documents with HMTL attachments" do - edition = create(:draft_publication, :with_html_attachment) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document_id, - true, - ) - capture_io { task.invoke } - end - end - - describe "#consultation_drafts_with_html_attachments" do - let(:task) { Rake::Task["publishing_api:bulk_republish:drafts_with_html_attachments"] } - - test "republishes draft consultation documents with HMTL attachments" do - edition = create(:draft_consultation, :with_html_attachment) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document_id, - true, - ) - capture_io { task.invoke } - end - end - - describe "#document_type" do - let(:task) { Rake::Task["publishing_api:bulk_republish:document_type"] } - - describe "for editionable document types" do - document_types = %w[CaseStudy - Consultation - CorporateInformationPage - DetailedGuide - DocumentCollection - FatalityNotice - NewsArticle - Publication - Speech - StatisticalDataSet] - - document_types.each do |document_type| - test "republishes all #{document_type} documents" do - document = create(document_type.underscore.to_sym) # rubocop:disable Rails/SaveBang - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - document.document_id, - true, - ) - capture_io { task.invoke(document_type) } - end - end - end - - describe "for non-editionable document types" do - document_types = %w[Contact - Government - OperationalField - Organisation - Person - PolicyGroup - RoleAppointment - Role - StatisticsAnnouncement - TakePartPage - TopicalEventAboutPage - TopicalEvent] - - document_types.each do |document_type| - test "republishes all #{document_type} documents" do - document = create(document_type.underscore.to_sym) # rubocop:disable Rails/SaveBang - - Whitehall::PublishingApi.expects(:bulk_republish_async).with(document) - capture_io { task.invoke(document_type) } - end - end - end - - describe "for non-existent document types" do - test "it returns an error" do - document_type = "SomeDocumentTypeThatDoesntExist" - assert_raises(SystemExit, match: /Unknown document type #{document_type}/) do - capture_io { task.invoke(document_type) } - end - end - end - end - - describe "#by_organisation" do - let(:org) { create(:organisation) } - let(:task) { Rake::Task["publishing_api:bulk_republish:by_organisation"] } - - test "Republishes the latest edition for each document owned by the organisation" do - edition = create(:published_news_article, organisations: [org]) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document.id, - true, - ) - - capture_io { task.invoke(org.slug) } - end - - test "Ignores documents owned by other organisations" do - some_other_org = create(:organisation) - edition = create(:published_news_article, organisations: [some_other_org]) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document.id, - true, - ).never - - capture_io { task.invoke(org.slug) } - end - end - - describe "#documents_by_content_ids" do - let(:task) { Rake::Task["publishing_api:bulk_republish:documents_by_content_ids"] } - - test "Republishes documents by content ids" do - edition = create(:publication) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - edition.document.id, - true, - ) - - capture_io { task.invoke([edition.content_id]) } - end - end - - describe "#all_documents" do - let(:task) { Rake::Task["publishing_api:bulk_republish:all_documents"] } - - test "Republishes all documents" do - publication = create(:published_publication) - news_story = create(:published_news_story) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - news_story.document_id, - true, - ) - - PublishingApiDocumentRepublishingWorker.expects(:perform_async_in_queue).with( - "bulk_republishing", - publication.document_id, - true, - ) - - capture_io { task.invoke } - end - end - end - describe "unpublish" do describe "#by_content_id" do let(:task) { Rake::Task["publishing_api:unpublish:by_content_id"] }