A platform for community discussion. Free, open, simple.
FIX: Use the type from `about.json` when finishing a theme placeholder
Régis Hanolbd024d7c1b74
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex 0a0a5929716..ed69c4a24f5 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -400,11 +400,8 @@ class Admin::ThemesController < Admin::AdminControllerremote_theme.remote_url = remote_urlremote_theme.branch = params[:branch].presenceremote_theme.private_key = private_key if private_key.present?- remote_theme.local_version = nil- remote_theme.remote_version = nil- remote_theme.commits_behind = nil- remote_theme.update_from_remote(already_in_transaction: true)+ remote_theme.update_from_remote(already_in_transaction: true, refresh_name_and_type: true)if remote_theme.last_error_text.present?raise RemoteTheme::ImportError.new(remote_theme.last_error_text)diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rbindex 573cac0f117..4fdb512fc4b 100644--- a/app/models/remote_theme.rb+++ b/app/models/remote_theme.rb@@ -173,7 +173,6 @@ class RemoteTheme < ActiveRecord::Basecomponent = [true, "true"].include?(theme_info["component"])theme = Theme.new(user_id: user&.id || -1, name: theme_info["name"], component: component)- theme.child_components = theme_info["components"].presence || []remote_theme = newtheme.remote_theme = remote_theme@@ -208,6 +207,10 @@ class RemoteTheme < ActiveRecord::Basecommits_behind > 0 || remote_version != local_versionend+ def placeholder?+ remote_url.present? && local_version.blank?+ end+def update_remote_versionreturn unless is_git?importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key, branch: branch)@@ -238,7 +241,8 @@ class RemoteTheme < ActiveRecord::Baserun_migrations: true,allow_out_of_sequence_migration: false,before_save: nil,- raise_on_import_error: false+ raise_on_import_error: false,+ refresh_name_and_type: false)cleanup = false@@ -292,12 +296,11 @@ class RemoteTheme < ActiveRecord::Baseraise ImportError, err.messageend- # Update all theme attributes if this is just a placeholder- if remote_url.present? && !local_version && !commits_behind+ if placeholder? || refresh_name_and_typetheme.name = theme_info["name"]theme.component = [true, "true"].include?(theme_info["component"])- theme.child_components = theme_info["components"].presence || []end+ theme.child_components = theme_info["components"].presence || [] if placeholder?METADATA_PROPERTIES.each { |property| public_send(:"#{property}=", theme_info[property.to_s]) }diff --git a/app/models/theme.rb b/app/models/theme.rbindex 2cf00c6e66a..51fa7665506 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -93,8 +93,11 @@ class Theme < ActiveRecord::Basevalidate :component_validationsvalidate :validate_theme_fields+ before_validation :reset_attributes_for_type,+ if: -> { persisted? && will_save_change_to_component? }after_create :update_child_componentsbefore_update :check_editable_attributes, if: :system?+ after_update :unlink_relatives_for_type, if: :saved_change_to_component?before_destroy :raise_invalid_parameters, if: :system?scope :user_selectable, -> { where("user_selectable OR id = ?", SiteSetting.default_theme_id) }@@ -435,13 +438,8 @@ class Theme < ActiveRecord::Basereturn if componentTheme.transaction do- self.component = true-- self.color_scheme_id = nil- self.user_selectable = falseTheme.clear_default! if default?-- ChildTheme.where("parent_theme_id = ?", id).destroy_all+ self.component = truesave!endend@@ -449,12 +447,8 @@ class Theme < ActiveRecord::Basedef switch_to_theme!return unless component- Theme.transaction do- self.enabled = true- self.component = false- ChildTheme.where("child_theme_id = ?", id).destroy_all- save!- end+ self.component = false+ save!enddef self.find_default@@ -1179,6 +1173,20 @@ class Theme < ActiveRecord::Basedef raise_invalid_parametersraise Discourse::InvalidParametersend++ def reset_attributes_for_type+ if component+ self.color_scheme_id = nil+ self.user_selectable = false+ else+ self.enabled = true+ end+ end++ def unlink_relatives_for_type+ column = component ? :parent_theme_id : :child_theme_id+ ChildTheme.where(column => id).delete_all+ endend# == Schema Informationdiff --git a/app/serializers/remote_theme_serializer.rb b/app/serializers/remote_theme_serializer.rbindex eaf9cd6d549..8c9ca9c287e 100644--- a/app/serializers/remote_theme_serializer.rb+++ b/app/serializers/remote_theme_serializer.rb@@ -14,6 +14,7 @@ class RemoteThemeSerializer < ApplicationSerializer:github_diff_link,:last_error_text,:is_git?,+ :placeholder?,:license_url,:about_url,:authors,diff --git a/app/services/themes_install_task.rb b/app/services/themes_install_task.rbindex b721b784919..20c42ed1aed 100644--- a/app/services/themes_install_task.rb+++ b/app/services/themes_install_task.rb@@ -20,15 +20,15 @@ class ThemesInstallTaskcounts[:errors] += 1endelse- begin- installer.install- log << "#{name}: installed from #{installer.url}"- counts[:installed] += 1- rescue RemoteTheme::ImportError, Discourse::InvalidParameters => err- log << "#{name}: #{err.message}"- counts[:errors] += 1- end+ installer.install+ log << "#{name}: installed from #{installer.url}"+ counts[:installed] += 1end+ rescue RemoteTheme::ImportError,+ ActiveRecord::RecordInvalid,+ Discourse::InvalidParameters => err+ log << "#{name}: #{err.message}"+ counts[:errors] += 1end[log, counts]@@ -74,7 +74,7 @@ class ThemesInstallTaskenddef update- @remote_theme.update_from_remote(raise_if_theme_save_fails: false)+ @remote_theme.update_from_remoteadd_component_to_all_themes@remote_theme.last_error_text.nil?enddiff --git a/frontend/discourse/admin/controllers/admin-customize-themes/show.js b/frontend/discourse/admin/controllers/admin-customize-themes/show.jsindex c1cb34d5b51..92a15b27ee5 100644--- a/frontend/discourse/admin/controllers/admin-customize-themes/show.js+++ b/frontend/discourse/admin/controllers/admin-customize-themes/show.js@@ -45,17 +45,9 @@ export default class AdminCustomizeThemesShowController extends Controller {return this.model?.remoteError && !this.updatingRemote;}- @computed(- "model.remote_theme.remote_url",- "model.remote_theme.local_version",- "model.remote_theme.commits_behind"- )+ @computed("model.remote_theme.placeholder")get finishInstall() {- return (- this.model?.remote_theme?.remote_url &&- !this.model?.remote_theme?.local_version &&- !this.model?.remote_theme?.commits_behind- );+ return this.model?.remote_theme?.placeholder;}@actiondiff --git a/frontend/discourse/tests/acceptance/themes-test.js b/frontend/discourse/tests/acceptance/themes-test.jsindex f2df15ebff4..3540984640e 100644--- a/frontend/discourse/tests/acceptance/themes-test.js+++ b/frontend/discourse/tests/acceptance/themes-test.js@@ -32,6 +32,7 @@ acceptance("Theme", function (needs) {updated_at: "2022-01-01T12:00:00.000Z",last_error_text: null,is_git: true,+ placeholder: true,license_url: null,about_url: null,authors: null,@@ -99,6 +100,7 @@ acceptance("Theme", function (needs) {updated_at: "2022-01-01T12:00:00.000Z",last_error_text: null,is_git: true,+ placeholder: true,license_url: null,about_url: null,authors: null,@@ -150,6 +152,7 @@ acceptance("Theme", function (needs) {updated_at: "2022-01-01T12:00:30.000Z",last_error_text: null,is_git: true,+ placeholder: false,license_url: "URL",about_url: "URL",authors: null,diff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rbindex 82b638b06b8..d2d575393a2 100644--- a/spec/models/remote_theme_spec.rb+++ b/spec/models/remote_theme_spec.rb@@ -760,6 +760,76 @@ RSpec.describe RemoteTheme doendend+ describe "#update_from_remote" do+ context "when the theme is a placeholder" do+ let(:repo_url) { "[email protected]:discourse/private-component.git" }+ let(:repo) do+ setup_git_repo(+ "about.json" => { name: "Private component", component: true }.to_json,+ "common/header.html" => "HI",+ )+ end+ let(:theme) do+ Fabricate(+ :theme,+ name: "private-component",+ remote_theme: RemoteTheme.create!(remote_url: repo_url, private_key: "private key"),+ )+ end+ let(:remote_theme) { theme.remote_theme }++ around(:each) { |example| MockGitImporter.with_mock { example.run } }++ before { MockGitImporter.register(repo_url, repo) }+ after { FileUtils.rm_rf(repo) }++ it "uses the type from about.json even after the remote version has been checked" do+ remote_theme.update_remote_version+ expect(remote_theme.commits_behind).to eq(0)++ remote_theme.update_from_remote++ expect(remote_theme).not_to be_placeholder+ expect(theme.reload).to have_attributes(name: "Private component", component: true)+ end++ it "clears what a component can't have when switching to a component" do+ child_component = Fabricate(:theme, component: true)+ theme.update!(user_selectable: true, child_theme_ids: [child_component.id])++ remote_theme.update_from_remote++ expect(theme.reload).to have_attributes(component: true, user_selectable: false)+ expect(theme.child_themes).to be_empty+ end++ it "clears what a theme can't have when switching to a theme" do+ add_to_git_repo(repo, "about.json" => { name: "Private theme" }.to_json)+ parent_theme = Fabricate(:theme)+ theme.update!(component: true)+ parent_theme.update!(child_theme_ids: [theme.id])++ remote_theme.update_from_remote++ expect(theme.reload.component).to eq(false)+ expect(parent_theme.reload.child_themes).to be_empty+ 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])+ theme.set_default!++ remote_theme.update_from_remote(raise_if_theme_save_fails: false)++ expect(theme.reload.component).to eq(false)+ expect(theme).to be_default+ expect(theme.child_themes).to contain_exactly(child_component)+ expect(remote_theme.reload).to be_placeholder+ end+ end+ end+describe ".joined_remotes" doit "finds records that are associated with themes" dogithub_repodiff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rbindex b4be33d9b39..f30d2389cc8 100644--- a/spec/requests/admin/themes_controller_spec.rb+++ b/spec/requests/admin/themes_controller_spec.rb@@ -1757,6 +1757,35 @@ RSpec.describe Admin::ThemesController doexpect(theme.remote_theme.branch).to eq("develop")end+ it "adopts the name and type of the new source without installing its components" do+ add_to_git_repo(+ other_repo,+ "about.json" => {+ name: "comp",+ component: true,+ components: ["https://github.com/discourse/missing.git"],+ }.to_json,+ )+ theme.update!(remote_theme: RemoteTheme.create!(remote_url: repo_url, local_version: "abc"))++ put "/admin/themes/#{theme.id}/source.json", params: { remote_url: other_repo_url }++ expect(response.status).to eq(200)+ expect(theme.reload).to have_attributes(name: "comp", component: true)+ expect(theme.remote_theme.local_version).to be_present+ 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"))+ theme.set_default!++ put "/admin/themes/#{theme.id}/source.json", params: { remote_url: other_repo_url }++ expect(response.status).to eq(422)+ expect(theme.reload.component).to eq(false)+ end+it "returns error for non-git theme" doput "/admin/themes/#{theme.id}/source.json", params: { remote_url: other_repo_url }diff --git a/spec/services/themes_spec.rb b/spec/services/themes_spec.rbindex abeac130002..eea87de1984 100644--- a/spec/services/themes_spec.rb+++ b/spec/services/themes_spec.rb@@ -174,6 +174,45 @@ RSpec.describe ThemesInstallTask doendend+ describe ".install" do+ context "when the theme has been staged as a placeholder" do+ let(:url) { "https://github.com/discourse/private-theme.git" }+ let(:child_url) { "https://github.com/discourse/child.git" }+ let(:repo) do+ setup_git_repo("about.json" => { name: "Private theme", components: [child_url] }.to_json)+ end+ let!(:placeholder) do+ Fabricate(+ :theme,+ remote_theme: RemoteTheme.create!(remote_url: url, private_key: "private key"),+ )+ end++ around(:each) { |example| MockGitImporter.with_mock { example.run } }++ before { MockGitImporter.register(url, repo) }++ after { FileUtils.rm_rf(repo) }++ it "counts a theme that can't be saved as an error and carries on" do+ add_to_git_repo(+ repo,+ "about.json" => { name: "Private component", component: true }.to_json,+ )+ placeholder.set_default!+ other_repo = setup_git_repo("about.json" => { name: "Other theme" }.to_json)+ other_url = MockGitImporter.register("https://github.com/discourse/other.git", other_repo)++ _log, counts = ThemesInstallTask.install({ private_theme: url, other_theme: other_url })++ expect(counts).to include(installed: 1, updated: 0, errors: 1)+ expect(placeholder.remote_theme.reload).to be_placeholder+ ensure+ FileUtils.rm_rf(other_repo)+ end+ end+ end+describe "#theme_exists?" doit "can use https or ssh and find the same repo" doremote_theme =