A platform for community discussion. Free, open, simple.
DEV: rake task to merge the manually installed Horizon (#33355)
Krzysztof Kotlarek39a43f7e76bb
diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rakeindex f36e128f2a1..a251560e261 100644--- a/lib/tasks/themes.rake+++ b/lib/tasks/themes.rake@@ -292,3 +292,112 @@ task "themes:qunit_all_official" => :environment do |task, args|Rake::Task["themes:qunit"].reenableRake::Task["themes:qunit"].invoke("ids", theme_ids_with_qunit_tests.join("|"))end++HORIZON_REMOTE_PATHS = %w[+ github.com/discourse-org/horizon+ github.com/discourse-org/nextgen-theme+ github.com/discourse/horizon+]++def get_horizon_themes+ Theme.joins(:remote_theme).where(<<~SQL)+ remote_themes.remote_url LIKE ANY (ARRAY[#{HORIZON_REMOTE_PATHS.map { |path| "'%#{path}%'" }.join(", ")}])+ SQL+end++desc "Remove duplicated Horizon themes"+task "themes:deduplicate_horizon" => :environment do |task, args|+ horizon_themes = get_horizon_themes++ abort("🟩 No Horizon themes found") if horizon_themes.empty?++ # v0 will handle only one horizon theme+ abort("⭕ More than one horizon theme installed") if horizon_themes.count > 1++ remote_horizon_theme = horizon_themes.first+ system_horizon_theme = Theme.horizon_theme++ Theme.transaction do+ # Enable hidden horizon theme if it is not enabled+ if !SiteSetting.experimental_system_themes_map.include?("horizon")+ SiteSetting.experimental_system_themes =+ (SiteSetting.experimental_system_themes_map << "horizon").join("|")+ end+ puts "Horizon is enabled"++ # Make the system horizon selectable if remote horizon is selectable+ system_horizon_theme.update!(user_selectable: remote_horizon_theme.user_selectable)+ puts "User selectable is updated"++ # Move all components of the remote horizon theme to the system horizon theme+ ChildTheme+ .where(parent_theme_id: remote_horizon_theme.id)+ .where.not(child_theme_id: system_horizon_theme.child_themes.pluck(:id))+ .find_each { |child_theme| child_theme.update!(parent_theme_id: system_horizon_theme.id) }+ puts "Components is moved"++ # Update user options to point to the system horizon theme+ UserOption+ .where(theme_ids: [remote_horizon_theme.id])+ .find_each { |user_option| user_option.update!(theme_ids: [system_horizon_theme.id]) }+ puts "User options theme id is updated"++ # Move color palettes+ color_schemes_map =+ remote_horizon_theme+ .color_schemes+ .reduce({}) do |map, color_scheme|+ map[color_scheme.id] = system_horizon_theme+ .color_schemes+ .find_by(name: color_scheme.name)+ .id+ map+ end+ if remote_horizon_theme.color_scheme.theme_id == remote_horizon_theme.id+ system_horizon_theme.update!(+ color_scheme_id: color_schemes_map[remote_horizon_theme.color_scheme_id],+ )+ elsif remote_horizon_theme.color_scheme_id+ system_horizon_theme.update!(color_scheme_id: remote_horizon_theme.color_scheme_id)+ end+ puts "Theme color palette is updated"+ UserOption+ .where(color_scheme_id: color_schemes_map.keys)+ .find_each do |user_option|+ user_option.update!(color_scheme_id: color_schemes_map[user_option.color_scheme_id])+ end+ UserOption+ .where(dark_scheme_id: color_schemes_map.keys)+ .find_each do |user_option|+ user_option.update!(dark_scheme_id: color_schemes_map[user_option.dark_scheme_id])+ end+ puts "User option color schemes are updated"++ # Move theme settings+ ThemeSetting+ .where(theme_id: remote_horizon_theme.id)+ .find_each { |theme_setting| theme_setting.update!(theme_id: system_horizon_theme.id) }+ puts "Theme settings are moved"++ # Move theme translations+ ThemeTranslationOverride+ .where(theme_id: remote_horizon_theme.id)+ .find_each { |translation| translation.update!(theme_id: system_horizon_theme.id) }+ puts "Theme translations are moved"++ # Change default theme to system horizon theme if remote horizon theme is default+ SiteSetting.default_theme_id = system_horizon_theme.id if remote_horizon_theme.default?+ puts "Default theme is updated to system horizon theme"++ # Delete the remote horizon theme and color palettes+ remote_horizon_theme.color_schemes.map(&:destroy!)+ puts "Remote horizon theme color palettes are deleted"++ StaffActionLogger.new(Discourse.system_user).log_theme_destroy(remote_horizon_theme)+ remote_horizon_theme.destroy!+ puts "Remote horizon theme is deleted"++ Theme.expire_site_cache!+ puts "Site cache is expired"+ end+enddiff --git a/spec/tasks/themes_spec.rb b/spec/tasks/themes_spec.rbindex d6a63c4fddb..0cf24a123ac 100644--- a/spec/tasks/themes_spec.rb+++ b/spec/tasks/themes_spec.rb@@ -118,4 +118,169 @@ RSpec.describe "tasks/themes" doexpect(theme.remote_theme.local_version).to eq(original_local_version)endend++ describe "themes:deduplicate_horizon" do+ fab!(:user)+ fab!(:remote_theme) do+ RemoteTheme.create!(+ remote_url: "https://github.com/discourse-org/horizon.git",+ minimum_discourse_version: "2.5.0",+ )+ end+ fab!(:remote_horizon_theme) do+ Fabricate(+ :theme,+ user: user,+ name: "Remote Horizon",+ remote_theme: remote_theme,+ user_selectable: true,+ )+ end+ fab!(:child_component) do+ Fabricate(:theme, user: user, component: true, name: "Child Component")+ end+ fab!(:theme_setting) do+ ThemeSetting.create!(+ theme: remote_horizon_theme,+ data_type: ThemeSetting.types[:bool],+ name: "enable_welcome_banner",+ value: "false",+ )+ end+ fab!(:manual_color_scheme) do+ remote_horizon_theme.color_schemes.create!(+ name: "Lily Dark",+ theme_id: remote_horizon_theme.id,+ )+ end+ fab!(:theme_translation_override) do+ ThemeTranslationOverride.create!(+ theme: remote_horizon_theme,+ locale: "en",+ translation_key: "test.key",+ value: "Test Value",+ )+ end++ let!(:system_horizon_theme) { Theme.horizon_theme }+ let!(:system_color_scheme) { system_horizon_theme.color_schemes.where(name: "Lily Dark").first }++ before do+ remote_horizon_theme.update!(color_scheme: manual_color_scheme)+ remote_horizon_theme.add_relative_theme!(:child, child_component)+ remote_horizon_theme.save!+ user.user_option.update!(+ theme_ids: [remote_horizon_theme.id],+ color_scheme_id: manual_color_scheme.id,+ dark_scheme_id: manual_color_scheme.id,+ )+ SiteSetting.default_theme_id = remote_horizon_theme.id+ end++ context "when no horizon themes exist" do+ before do+ remote_horizon_theme.remote_theme&.destroy!+ remote_horizon_theme.destroy!+ end++ it "aborts with no themes message" do+ output =+ capture_stderr do+ expect { invoke_rake_task("themes:deduplicate_horizon") }.to raise_error(SystemExit)+ end+ expect(output).to include("🟩 No Horizon themes found")+ end+ end++ context "when multiple horizon themes exist" do+ fab!(:remote_theme_2) do+ RemoteTheme.create!(+ remote_url: "https://github.com/discourse/horizon.git",+ minimum_discourse_version: "2.5.0",+ )+ end+ fab!(:remote_horizon_theme_2) do+ Fabricate(:theme, user: user, name: "Second Horizon", remote_theme: remote_theme_2)+ end++ it "aborts with multiple themes message" do+ output =+ capture_stderr do+ expect { invoke_rake_task("themes:deduplicate_horizon") }.to raise_error(SystemExit)+ end+ expect(output).to include("⭕ More than one horizon theme installed")+ end+ end++ context "when exactly one horizon theme exists" do+ it "successfully migrates theme data" do+ expect(system_horizon_theme.user_selectable).to be false+ expect(SiteSetting.default_theme_id).to eq(remote_horizon_theme.id)+ expect(system_horizon_theme.child_themes).to eq([])+ expect(system_horizon_theme.theme_settings.pluck(:name)).to eq([])+ expect(system_horizon_theme.theme_translation_overrides.pluck(:translation_key)).to eq([])+ expect(system_horizon_theme.color_scheme.name).to eq("Horizon")+ expect {+ capture_stdout { invoke_rake_task("themes:deduplicate_horizon") }+ }.not_to raise_error++ expect { remote_horizon_theme.reload }.to raise_error(ActiveRecord::RecordNotFound)++ system_horizon_theme.reload+ expect(system_horizon_theme.user_selectable).to be true+ expect(SiteSetting.default_theme_id).to eq(system_horizon_theme.id)+ expect(system_horizon_theme.child_themes).to include(child_component)+ expect(system_horizon_theme.theme_settings.pluck(:name)).to include(theme_setting.name)+ expect(system_horizon_theme.theme_translation_overrides.pluck(:translation_key)).to include(+ "test.key",+ )+ expect(system_horizon_theme.color_scheme).to eq(system_color_scheme)+ end++ it "logs that remote theme was deleted" do+ expect {+ capture_stderr { capture_stdout { invoke_rake_task("themes:deduplicate_horizon") } }+ }.to change { UserHistory.count }++ expect(UserHistory.last.action).to eq(UserHistory.actions[:delete_theme])+ expect(UserHistory.last.subject).to eq("Remote Horizon")+ end++ it "updates user theme and color scheme" do+ expect(user.user_option.theme_ids).to eq([remote_horizon_theme.id])+ expect(user.user_option.color_scheme_id).to eq(manual_color_scheme.id)+ expect(user.user_option.dark_scheme_id).to eq(manual_color_scheme.id)++ capture_stdout { invoke_rake_task("themes:deduplicate_horizon") }++ user.user_option.reload+ expect(user.user_option.theme_ids).to eq([system_horizon_theme.id])+ expect(user.user_option.color_scheme_id).to eq(system_color_scheme.id)+ expect(user.user_option.dark_scheme_id).to eq(system_color_scheme.id)+ end++ it "does not update user color scheme if custom" do+ custom_color_scheme = Fabricate(:color_scheme)+ user.user_option.update!(color_scheme_id: custom_color_scheme.id)++ expect { capture_stdout { invoke_rake_task("themes:deduplicate_horizon") } }.not_to change {+ user.user_option.color_scheme_id+ }+ end++ it "enables system horizon theme if not already enabled" do+ SiteSetting.experimental_system_themes = "foundation"++ capture_stdout { invoke_rake_task("themes:deduplicate_horizon") }+ expect(SiteSetting.experimental_system_themes_map).to eq(%w[foundation horizon])+ end++ it "does not modify system themes setting if horizon already enabled" do+ SiteSetting.experimental_system_themes = "horizon"++ capture_stdout { invoke_rake_task("themes:deduplicate_horizon") }+ expect(SiteSetting.experimental_system_themes_map).to eq(%w[horizon])+ end+ end+ endend