Skip to content

A platform for community discussion. Free, open, simple.

FIX: Install child components when finishing a theme placeholder

Régis Hanol6a0f7dfb5b21
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index dc1b8cc60da..4837bd0d705 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -211,10 +211,11 @@ class Admin::ThemesController < Admin::AdminController
@theme.remote_theme.update_remote_version if params[:theme][:remote_check]
if params[:theme][:remote_update]
- @theme.remote_theme.update_from_remote(
- raise_if_theme_save_fails: false,
- raise_on_import_error: true,
- )
+ skipped_components =
+ @theme
+ .remote_theme
+ .update_from_remote(raise_if_theme_save_fails: false, raise_on_import_error: true)
+ .skipped_child_components
else
@theme.save
end
@@ -231,7 +232,7 @@ class Admin::ThemesController < Admin::AdminController
log_theme_component_disabled if disables_component
log_theme_component_enabled if enables_component
- format.json { render json: serialize_data(@theme, ThemeSerializer), status: :ok }
+ format.json { render json: theme_json(@theme, skipped_components:), status: :ok }
else
format.json do
error = @theme.errors.full_messages.join(", ").presence
@@ -242,9 +243,7 @@ class Admin::ThemesController < Admin::AdminController
end
end
end
- rescue RemoteTheme::ImportError => e
- render_json_error e.message
- rescue Theme::SettingsMigrationError => e
+ rescue RemoteTheme::ImportError, Theme::SettingsMigrationError => e
render_json_error e.message
end
@@ -409,7 +408,8 @@ class Admin::ThemesController < Admin::AdminController
end
log_theme_change(nil, theme.reload)
- render json: serialize_data(theme, ThemeSerializer), status: :ok
+ render json: theme_json(theme, skipped_components: remote_theme.skipped_child_components),
+ status: :ok
rescue RemoteTheme::ImportError, ActiveRecord::RecordInvalid, Theme::SettingsMigrationError => e
remote_theme.reload
render_json_error e.message
@@ -418,6 +418,12 @@ class Admin::ThemesController < Admin::AdminController
private
+ def theme_json(theme, skipped_components:)
+ json = serialize_data(theme, ThemeSerializer)
+ json[:skipped_components] = skipped_components if skipped_components.present?
+ json
+ end
+
def create_remote_theme_placeholder(remote, branch:, private_key:)
Theme.transaction do
remote_theme =
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 4fdb512fc4b..321ed3ebe8c 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -211,6 +211,10 @@ class RemoteTheme < ActiveRecord::Base
remote_url.present? && local_version.blank?
end
+ def skipped_child_components
+ @skipped_child_components || []
+ end
+
def update_remote_version
return unless is_git?
importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key, branch: branch)
@@ -296,11 +300,15 @@ class RemoteTheme < ActiveRecord::Base
raise ImportError, err.message
end
- if placeholder? || refresh_name_and_type
+ # Captured before saving, which sets local_version and reloads the theme
+ was_placeholder = placeholder?
+ new_theme = theme.new_record?
+
+ if was_placeholder || refresh_name_and_type
theme.name = theme_info["name"]
theme.component = [true, "true"].include?(theme_info["component"])
end
- theme.child_components = theme_info["components"].presence || [] if placeholder?
+ theme.child_components = theme_info["components"].presence || [] if was_placeholder
METADATA_PROPERTIES.each { |property| public_send(:"#{property}=", theme_info[property.to_s]) }
@@ -413,6 +421,14 @@ class RemoteTheme < ActiveRecord::Base
transaction(&transaction_block)
end
+ # A new theme installed them when it was created. A child may need repository access of its own,
+ # so failing to install one must not block finishing, nor hold the caller's transaction open.
+ if was_placeholder && !new_theme && theme.errors.empty?
+ DB.after_commit do
+ @skipped_child_components = theme.update_child_components(skip_failures: true)
+ end
+ end
+
theme.theme_modifier_set.save! if theme.theme_modifier_set.refresh_theme_setting_modifiers
self
diff --git a/app/models/theme.rb b/app/models/theme.rb
index 51fa7665506..eb7446f0aa1 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -202,17 +202,34 @@ class Theme < ActiveRecord::Base
Rails.env.development?
end
- def update_child_components
- return if component? || child_components.blank? || skip_child_components_update
+ def update_child_components(skip_failures: false)
+ return [] if component? || child_components.blank? || skip_child_components_update
+ skipped = []
+ linked = false
linked_urls = child_themes.joins(:remote_theme).pluck("remote_themes.remote_url").to_set
+
child_components.each do |url|
url = ThemeStore::GitImporter.new(url.strip).url
next if linked_urls.include?(url)
+
theme = RemoteTheme.find_by(remote_url: url)&.theme
+ imported = theme.nil?
theme ||= RemoteTheme.import_theme(url, user)
child_themes << theme
+ linked = true
+ rescue StandardError => e
+ theme.destroy if imported && theme&.persisted?
+ raise if !skip_failures
+ skipped << { url:, error: e.message }
+ end
+
+ if linked
+ Theme.clear_cache!
+ notify_theme_change
end
+
+ skipped
end
def load_all_extra_js
diff --git a/app/services/themes_install_task.rb b/app/services/themes_install_task.rb
index 20c42ed1aed..2f1ccaf629c 100644
--- a/app/services/themes_install_task.rb
+++ b/app/services/themes_install_task.rb
@@ -14,6 +14,9 @@ class ThemesInstallTask
counts[:skipped] += 1
elsif installer.update
log << "#{name}: is already installed. Updating from remote."
+ installer.skipped_components.each do |component|
+ log << "#{name}: couldn't install component #{component[:url]}: #{component[:error]}"
+ end
counts[:updated] += 1
else
log << "#{name}: is already installed, but there was an error updating from remote."
@@ -73,6 +76,10 @@ class ThemesInstallTask
add_component_to_all_themes
end
+ def skipped_components
+ @remote_theme.skipped_child_components
+ end
+
def update
@remote_theme.update_from_remote
add_component_to_all_themes
diff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rb
index d2d575393a2..847deb37e3e 100644
--- a/spec/models/remote_theme_spec.rb
+++ b/spec/models/remote_theme_spec.rb
@@ -815,6 +815,81 @@ RSpec.describe RemoteTheme do
expect(parent_theme.reload.child_themes).to be_empty
end
+ it "installs the child components listed in about.json" do
+ linked_url = "https://github.com/discourse/linked.git"
+ linked =
+ Fabricate(
+ :theme,
+ component: true,
+ remote_theme: Fabricate(:remote_theme, remote_url: linked_url),
+ )
+ new_repo = setup_git_repo("about.json" => { name: "New", component: true }.to_json)
+ new_url = MockGitImporter.register("https://github.com/discourse/new.git", new_repo)
+ theme.update!(child_theme_ids: [linked.id])
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [linked_url, new_url] }.to_json,
+ )
+
+ remote_theme.update_from_remote
+
+ expect(theme.reload.child_themes.map(&:name)).to contain_exactly(linked.name, "New")
+ ensure
+ FileUtils.rm_rf(new_repo)
+ end
+
+ it "applies the already installed child components to the site" do
+ linked_url = "https://github.com/discourse/linked.git"
+ linked =
+ Fabricate(
+ :theme,
+ component: true,
+ remote_theme: Fabricate(:remote_theme, remote_url: linked_url),
+ )
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [linked_url] }.to_json,
+ )
+
+ remote_theme.update_from_remote
+
+ expect(Theme.transform_ids(theme.id)).to contain_exactly(theme.id, linked.id)
+ end
+
+ it "finishes the installation without the child components that can't be installed" do
+ child_url = "https://github.com/discourse/private-child.git"
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [child_url] }.to_json,
+ )
+
+ remote_theme.update_from_remote
+
+ expect(remote_theme.skipped_child_components).to contain_exactly(
+ url: child_url,
+ error: I18n.t("themes.import_error.git"),
+ )
+ expect(remote_theme.reload).not_to be_placeholder
+ expect(theme.reload.child_themes).to be_empty
+ end
+
+ it "finishes the installation when a child component fails unexpectedly" do
+ child_url = "https://github.com/discourse/child.git"
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [child_url] }.to_json,
+ )
+ RemoteTheme.stubs(:import_theme).raises(Theme::SettingsMigrationError, "boom")
+
+ remote_theme.update_from_remote
+
+ expect(remote_theme.skipped_child_components).to contain_exactly(
+ url: child_url,
+ error: "boom",
+ )
+ expect(remote_theme.reload).not_to be_placeholder
+ end
+
it "keeps everything when the theme can't become a component" do
child_component = Fabricate(:theme, component: true)
theme.update!(child_theme_ids: [child_component.id])
@@ -930,6 +1005,15 @@ RSpec.describe RemoteTheme do
)
end
+ it "applies the already installed components it links to the site" do
+ new_component = RemoteTheme.import_theme(new_url)
+ Theme.transform_ids(theme.id)
+
+ upload(update_components: "add")
+
+ expect(Theme.transform_ids(theme.id)).to include(new_component.id)
+ end
+
it "unlinks the components that are no longer listed from this theme only when syncing" do
other_theme = Fabricate(:theme, child_theme_ids: [unlisted_component.id])
diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rb
index 2e817f0cd96..872c0af5933 100644
--- a/spec/requests/admin/themes_controller_spec.rb
+++ b/spec/requests/admin/themes_controller_spec.rb
@@ -1181,6 +1181,28 @@ RSpec.describe Admin::ThemesController do
expect(response.parsed_body["errors"].first).to eq("error message")
end
+ it "finishes a placeholder without a child component that is a theme" do
+ child_repo = setup_git_repo("about.json" => { name: "Not a component" }.to_json)
+ child_url = MockGitImporter.register("https://github.com/discourse/child.git", child_repo)
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [child_url] }.to_json,
+ )
+ theme.update!(remote_theme: RemoteTheme.create!(remote_url: repo_url))
+
+ put "/admin/themes/#{theme.id}.json", params: { theme: { remote_update: true } }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["skipped_components"]).to contain_exactly(
+ include("url" => child_url),
+ )
+ expect(theme.remote_theme.reload).not_to be_placeholder
+ expect(theme.reload.child_themes).to be_empty
+ expect(RemoteTheme.where(remote_url: child_url)).not_to exist
+ ensure
+ FileUtils.rm_rf(child_repo)
+ end
+
it "returns the right error message" do
theme.update!(component: true)
@@ -1802,6 +1824,23 @@ RSpec.describe Admin::ThemesController do
expect(theme.remote_theme.local_version).to be_present
end
+ it "returns the components it couldn't install when finishing a placeholder" do
+ missing_url = "https://github.com/discourse/missing.git"
+ add_to_git_repo(
+ other_repo,
+ "about.json" => { name: "other-theme", components: [missing_url] }.to_json,
+ )
+ theme.update!(remote_theme: RemoteTheme.create!(remote_url: repo_url))
+
+ put "/admin/themes/#{theme.id}/source.json", params: { remote_url: other_repo_url }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["skipped_components"]).to contain_exactly(
+ "url" => missing_url,
+ "error" => I18n.t("themes.import_error.git"),
+ )
+ end
+
it "refuses to turn the default theme into a component" do
add_to_git_repo(other_repo, "about.json" => { name: "comp", component: true }.to_json)
theme.update!(remote_theme: RemoteTheme.create!(remote_url: repo_url, local_version: "abc"))
diff --git a/spec/services/themes_spec.rb b/spec/services/themes_spec.rb
index eea87de1984..98ce048e01f 100644
--- a/spec/services/themes_spec.rb
+++ b/spec/services/themes_spec.rb
@@ -194,6 +194,33 @@ RSpec.describe ThemesInstallTask do
after { FileUtils.rm_rf(repo) }
+ it "finishes the installation with its child components" do
+ child_repo = setup_git_repo("about.json" => { name: "Child", component: true }.to_json)
+ MockGitImporter.register(child_url, child_repo)
+
+ _log, counts = ThemesInstallTask.install({ private_theme: url })
+
+ expect(counts).to include(updated: 1, errors: 0)
+ expect(placeholder.reload.child_themes.map(&:name)).to contain_exactly("Child")
+ ensure
+ FileUtils.rm_rf(child_repo)
+ end
+
+ it "logs the child components it couldn't install" do
+ private_child_url = "https://github.com/discourse/private-child.git"
+ add_to_git_repo(
+ repo,
+ "about.json" => { name: "Private theme", components: [private_child_url] }.to_json,
+ )
+
+ log, counts = ThemesInstallTask.install({ private_theme: url })
+
+ expect(counts).to include(updated: 1, errors: 0)
+ expect(log).to include(
+ "private_theme: couldn't install component #{private_child_url}: #{I18n.t("themes.import_error.git")}",
+ )
+ end
+
it "counts a theme that can't be saved as an error and carries on" do
add_to_git_repo(
repo,