diff --git a/app/components/admin/editions/tags_component.rb b/app/components/admin/editions/tags_component.rb index ddbe606f3ab..5b7b8ad1881 100644 --- a/app/components/admin/editions/tags_component.rb +++ b/app/components/admin/editions/tags_component.rb @@ -34,11 +34,11 @@ def limited_access_tag end def broken_links_report_tag - return unless edition.link_check_reports.any? && edition.link_check_reports.last.completed? + return unless edition.link_check_report.present? && edition.link_check_report.completed? - return create_tag("Broken links") if edition.link_check_reports.last.broken_links.any? + return create_tag("Broken links") if edition.link_check_report.broken_links.any? - create_tag("Link warnings") if edition.link_check_reports.last.caution_links.any? + create_tag("Link warnings") if edition.link_check_report.caution_links.any? end def create_tag(label) diff --git a/app/controllers/admin/editions_controller.rb b/app/controllers/admin/editions_controller.rb index de8b4104b47..a0365af2b05 100644 --- a/app/controllers/admin/editions_controller.rb +++ b/app/controllers/admin/editions_controller.rb @@ -106,7 +106,7 @@ def update if updater.can_perform? && @edition.save_as(current_user) updater.perform! - if @edition.link_check_reports.last + if @edition.link_check_report LinkCheckerApiService.check_links(@edition, admin_link_checker_api_callback_url) end @@ -437,7 +437,7 @@ def edition_filter_options .symbolize_keys .merge( include_unpublishing: true, - include_link_check_reports: true, + include_link_check_report: true, include_last_author: true, ) .merge(per_page: Admin::EditionFilter::GOVUK_DESIGN_SYSTEM_PER_PAGE) diff --git a/app/models/admin/edition_filter.rb b/app/models/admin/edition_filter.rb index 0237cb31ca1..bb6a12b0169 100644 --- a/app/models/admin/edition_filter.rb +++ b/app/models/admin/edition_filter.rb @@ -124,7 +124,7 @@ def unpaginated_editions editions = editions.review_overdue if review_overdue editions = editions.includes(:unpublishing) if include_unpublishing? - editions = editions.includes(:link_check_reports) if include_link_check_reports? + editions = editions.includes(:link_check_report) if include_link_check_report? editions = editions.includes(:last_author) if include_last_author? @unpaginated_editions = editions @@ -269,8 +269,8 @@ def include_unpublishing? options.fetch(:include_unpublishing, false) end - def include_link_check_reports? - options.fetch(:include_link_check_reports, false) + def include_link_check_report? + options.fetch(:include_link_check_report, false) end def include_last_author? diff --git a/app/models/edition.rb b/app/models/edition.rb index 3e4325da6c3..78314196bd8 100644 --- a/app/models/edition.rb +++ b/app/models/edition.rb @@ -44,7 +44,7 @@ class Edition < ApplicationRecord has_many :edition_authors, dependent: :destroy has_many :authors, through: :edition_authors, source: :user has_many :topical_event_featurings, inverse_of: :edition - has_many :link_check_reports, as: :link_reportable, class_name: "LinkCheckerApiReport" + has_one :link_check_report, as: :link_reportable, class_name: "LinkCheckerApiReport" has_many :edition_dependencies, dependent: :destroy has_many :depended_upon_contacts, through: :edition_dependencies, source: :dependable, source_type: "Contact" diff --git a/app/services/link_reporter_csv_service.rb b/app/services/link_reporter_csv_service.rb index 47cc13935cb..5e4fd2dd6a2 100644 --- a/app/services/link_reporter_csv_service.rb +++ b/app/services/link_reporter_csv_service.rb @@ -69,9 +69,9 @@ def timestamp(edition) end def broken_links(edition) - return [] if edition.link_check_reports.blank? + return [] if edition.link_check_report.nil? - edition.link_check_reports.last.broken_links.map(&:uri) + edition.link_check_report.broken_links.map(&:uri) end def edition_organisation(edition) diff --git a/app/sidekiq/check_organisation_links_worker.rb b/app/sidekiq/check_organisation_links_worker.rb index 8ecb2ba125c..fcc6880fc0b 100644 --- a/app/sidekiq/check_organisation_links_worker.rb +++ b/app/sidekiq/check_organisation_links_worker.rb @@ -40,7 +40,7 @@ def find_organisation(organisation_id) end def public_editions(organisation) - least_recently_checked = Edition.includes(:link_check_reports).publicly_visible.with_translations.in_organisation(organisation).order("link_checker_api_reports.updated_at").limit(ORGANISATION_EDITION_LIMIT) + least_recently_checked = Edition.includes(:link_check_report).publicly_visible.with_translations.in_organisation(organisation).order("link_checker_api_reports.updated_at").limit(ORGANISATION_EDITION_LIMIT) Edition.where(id: least_recently_checked.pluck(:id)) end end diff --git a/app/views/admin/editions/edit.html.erb b/app/views/admin/editions/edit.html.erb index 10a7b0c4b04..bb990d5ab48 100644 --- a/app/views/admin/editions/edit.html.erb +++ b/app/views/admin/editions/edit.html.erb @@ -41,7 +41,7 @@ content: simple_formatting_sidebar( hide_inline_attachments_help: !@edition.allows_inline_attachments?, show_attachments_tab_help: true, - link_check_report: LinkCheckerApiService.has_links?(@edition, convert_admin_links: false) ? @edition.link_check_reports.last : nil, + link_check_report: LinkCheckerApiService.has_links?(@edition, convert_admin_links: false) ? @edition.link_check_report : nil, ), }, { diff --git a/app/views/admin/editions/show/_sidebar.html.erb b/app/views/admin/editions/show/_sidebar.html.erb index 76a33b6fca9..2ca19e742a6 100644 --- a/app/views/admin/editions/show/_sidebar.html.erb +++ b/app/views/admin/editions/show/_sidebar.html.erb @@ -4,7 +4,7 @@ <%= render( partial: "admin/link_check_reports/link_check_report", locals: { - report: (@edition.link_check_reports.last || @edition.link_check_reports.build), + report: (@edition.link_check_report || @edition.link_check_report.build), allow_new_report: true, }, ) if show_link_check_report?(@edition) %> diff --git a/db/data_migration/2025022610370000_remove_old_link_checker_reports.rb b/db/data_migration/2025022610370000_remove_old_link_checker_reports.rb new file mode 100644 index 00000000000..3a6b2ccacbd --- /dev/null +++ b/db/data_migration/2025022610370000_remove_old_link_checker_reports.rb @@ -0,0 +1,12 @@ +# To address https://github.com/alphagov/whitehall/issues/6011, we need +# to remove old link checker reports and switch to a 'has_one' relationship. + +Edition.find_each do |edition| + reports = edition.link_check_reports.order(created_at: :desc).to_a + next if reports.size <= 1 + + # Keep the most recent report and delete the rest + reports_to_delete = reports.drop(1) + reports_to_delete.each(&:destroy) +end +