require 'gds_api/publishing_api'
require 'highline'

desc "Migrate the homepage"
task migrate_homepage: :environment do
  cli = HighLine.new
  cli.say cli.color(
    "Publishing the homepage will make the old service manual inaccessible. Continue?",
    :red
  )
  exit unless cli.agree "Are you sure you wish to continue?"

  publishing_api_v1 = GdsApi::PublishingApi.new(
    Plek.new.find('publishing-api'),
    bearer_token: ENV['PUBLISHING_API_BEARER_TOKEN'] || 'example'
  )

  # Take ownership of the path reservation
  publishing_api_v1.put_path('/service-manual',
    publishing_app: 'service-manual-publisher',
    override_existing: true
  )

  homepage = HomepagePresenter.new
  
  # Save and publish the homepage
  PUBLISHING_API.put_content(
    homepage.content_id,
    homepage.content_payload
  )

  PUBLISHING_API.publish(
    homepage.content_id,
    "major"
  )

  puts "Done."
end

Add a rake task to migrate the homepage
Add a rake task to migrate the homepage

Because the service manual base path was previously ‘owned’ by a different application, we can’t publish to /service-manual without first explicitly claiming that path reservation using the paths endpoint.

Note that running this rake task will make the old service manual inaccessible, and should only be run on production once the old manual is no longer required.
