This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from alphagov/add-migrator
Add a TagMigrator to migrate tags for an app
- Loading branch information
Showing
5 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# TaggingMigrator | ||
# | ||
# We are migrating the taggings from panopticon/content-api to publishing-api. | ||
# This class takes the taggings for a certain app and sends it to the | ||
# publishing-api so that it contains the current taggings. This functionality | ||
# will be removed after the migration. | ||
|
||
require 'gds_api/publishing_api_v2' | ||
|
||
class TaggingMigrator | ||
def initialize(app_name) | ||
@app_name = app_name | ||
end | ||
|
||
def migrate! | ||
artefacts_owned_by_app.each do |artefact| | ||
next unless artefact.content_id | ||
migrate_tags_for_artefact(artefact) | ||
end | ||
end | ||
|
||
private | ||
|
||
def migrate_tags_for_artefact(artefact) | ||
link_payload = { | ||
mainstream_browse_pages: [], | ||
topics: [], | ||
organisations: [], | ||
} | ||
|
||
artefact.tags.each do |tag| | ||
if tag.tag_type == 'section' | ||
link_payload[:mainstream_browse_pages] << tag.content_id | ||
end | ||
|
||
if tag.tag_type == 'specialist_sector' | ||
link_payload[:topics] << tag.content_id | ||
end | ||
|
||
if tag.tag_type == 'organisation' | ||
link_payload[:organisations] << tag.content_id | ||
end | ||
end | ||
|
||
publishing_api.put_links( | ||
artefact.content_id, | ||
links: link_payload | ||
) | ||
end | ||
|
||
def artefacts_owned_by_app | ||
Artefact.where(owning_app: @app_name) | ||
end | ||
|
||
def publishing_api | ||
@publishing_api ||= GdsApi::PublishingApiV2.new(Plek.new.find('publishing-api')) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
desc "Migrate taggings for an app (rake migrate_taggings OWNING_APP=smartanswers)" | ||
task migrate_taggings: [:environment] do | ||
TaggingMigrator.new(ENV.fetch('OWNING_APP')).migrate! | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative "../test_helper" | ||
|
||
class TaggingMigratorTest < ActiveSupport::TestCase | ||
test "sends current migrations to publishing-api" do | ||
create(:live_tag, tag_id: "a-topic", tag_type: "specialist_sector", content_id: "A-TOPIC") | ||
create(:live_tag, tag_id: "a-browse-page", tag_type: "section", content_id: "A-BROWSE-PAGE") | ||
create(:live_tag, tag_id: "an-organisation", tag_type: "organisation", content_id: "AN-ORGANISATION") | ||
|
||
create(:artefact, | ||
content_id: "A", | ||
slug: "item-a", | ||
owning_app: "smartanswers", | ||
sections: ["a-browse-page"], | ||
specialist_sectors: ["a-topic"], | ||
) | ||
create(:artefact, | ||
content_id: "B", | ||
slug: "item-b", | ||
owning_app: "smartanswers", | ||
sections: ["a-browse-page"], | ||
organisations: ["an-organisation"], | ||
) | ||
create(:artefact, | ||
content_id: "C", | ||
slug: "item-c", | ||
owning_app: "smartanswers", | ||
) | ||
create(:artefact, | ||
content_id: "D", | ||
slug: "item-d", | ||
owning_app: "whitehall", | ||
) | ||
|
||
stub_request(:put, %r[#{Plek.find('publishing-api')}/v2/links/*]). | ||
to_return(body: {}.to_json) | ||
|
||
TaggingMigrator.new("smartanswers").migrate! | ||
|
||
assert_requested :put, "http://publishing-api.dev.gov.uk/v2/links/A", | ||
body: '{"links":{"mainstream_browse_pages":["A-BROWSE-PAGE"],"topics":["A-TOPIC"],"organisations":[]}}' | ||
assert_requested :put, "http://publishing-api.dev.gov.uk/v2/links/B", | ||
body: '{"links":{"mainstream_browse_pages":["A-BROWSE-PAGE"],"topics":[],"organisations":["AN-ORGANISATION"]}}' | ||
assert_requested :put, "http://publishing-api.dev.gov.uk/v2/links/C", | ||
body: '{"links":{"mainstream_browse_pages":[],"topics":[],"organisations":[]}}' | ||
end | ||
end |