A platform for community discussion. Free, open, simple.
DEV: Improve handling of private theme installs (#41845)
Penar Musaraj545168826bcc
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex b8aae90450d..cda20661eef 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -73,22 +73,21 @@ class Admin::ThemesController < Admin::AdminControllerreturn render_json_error I18n.t("themes.import_error.ssh_key_gone")end+ if params[:placeholder]+ if private_key.blank?+ return render_json_error I18n.t("themes.import_error.git_authentication")+ end++ @theme = create_remote_theme_placeholder(remote, branch:, private_key:)+ return render json: serialize_data(@theme, ThemeSerializer), status: :created+ end+@theme =RemoteTheme.import_theme(remote, theme_user, private_key: private_key, branch: branch)render json: serialize_data(@theme, ThemeSerializer), status: :createdrescue RemoteTheme::ImportError => eif params[:force]- theme_name = params[:remote].gsub(/.git\z/, "").split("/").last-- remote_theme = RemoteTheme.new- remote_theme.private_key = private_key- remote_theme.branch = params[:branch] ? params[:branch] : nil- remote_theme.remote_url = params[:remote]- remote_theme.save!-- @theme = Theme.new(user_id: theme_user&.id || -1, name: theme_name)- @theme.remote_theme = remote_theme- @theme.save!+ @theme = create_remote_theme_placeholder(remote, branch:, private_key:)render json: serialize_data(@theme, ThemeSerializer), status: :createdelse@@ -129,6 +128,21 @@ class Admin::ThemesController < Admin::AdminControllerrender_json_error err.messageend+ def create_remote_theme_placeholder(remote, branch:, private_key:)+ Theme.transaction do+ remote_theme =+ RemoteTheme.create!(remote_url: remote, branch: branch, private_key: private_key)++ Theme.create!(+ user_id: theme_user&.id || -1,+ name: remote.gsub(/\.git\z/, "").split("/").last,+ remote_theme: remote_theme,+ )+ end+ end++ private :create_remote_theme_placeholder+def index@themes = Theme.strict_loading.include_relations.order(:name)@@ -213,7 +227,10 @@ 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)+ @theme.remote_theme.update_from_remote(+ raise_if_theme_save_fails: false,+ raise_on_import_error: true,+ )else@theme.saveenddiff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rbindex b86002fdacd..1061b7a695c 100644--- a/app/models/remote_theme.rb+++ b/app/models/remote_theme.rb@@ -249,7 +249,8 @@ class RemoteTheme < ActiveRecord::Basealready_in_transaction: false,run_migrations: true,allow_out_of_sequence_migration: false,- before_save: nil+ before_save: nil,+ raise_on_import_error: false)cleanup = false@@ -261,6 +262,7 @@ class RemoteTheme < ActiveRecord::Baserescue RemoteTheme::ImportError => errself.last_error_text = err.messagesave!+ raise if raise_on_import_errorreturn selfelseself.last_error_text = nildiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 89c6288002b..8bd64759712 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -8455,10 +8455,13 @@ en:import_file_tip: ".tar.gz or .zip file containing theme"is_private: "Theme is in a private git repository"finish_install: "Finish Theme Installation"+ placeholder_description: "This is a private theme that requires a deploy key to be installed in the repository's configuration. Once that is done, click the button below to finish installation."last_attempt: "Installation process did not finish, last attempted:"remote_branch: "Branch name (optional)"remote_branch_placeholder: "Choose a specific branch or keep empty"- public_key: "Grant the following public key access to the repo:"+ public_key: "Grant the following public key read-only access to the repo:"+ public_key_github: "Grant the following public key read-only access to the repo. See %{link} for instructions."+ deploy_key_instructions: "GitHub deploy key instructions"install: "Install"installed: "Installed"screenshot: "%{name} screenshot"@@ -8470,7 +8473,7 @@ en:installing_message_long_time: "Setting up your new theme is taking a little longer than expected, please hang tight…"duplicate_remote_theme: "The theme component “%{name}” is already installed, are you sure you want to install another copy?"force_install: "The theme cannot be installed because the Git repository is inaccessible. Are you sure you want to continue installing it?"- create_placeholder: "Create Placeholder"+ create_placeholder: "Create placeholder"about_theme: "About"license: "License"version: "Version:"diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex b4024220637..98af2dae503 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -102,6 +102,10 @@ en:asset_too_big: "Asset %{filename} is bigger than the %{limit} limit"theme_too_big: "Theme is bigger than the %{limit} limit"git: "Error cloning git repository, access is denied or repository is not found"+ git_authentication: "The git repository rejected the deploy key. Check that the public key has access to the repository and try again. (If you no longer have the deploy key, delete this theme and install again.)"+ git_timeout: "The connection to the git repository timed out. Please try again later."+ git_repository_not_found: "The git repository could not be found. Check the repository URL."+ git_branch_not_found: "The git branch could not be found. Check the branch name."git_ref_not_found: "Unable to checkout git reference: %{ref}"git_unsupported_scheme: "Unable to clone git repo: scheme unsupported"unpack_failed: "Failed to unpack file"diff --git a/frontend/discourse/admin/components/admin-config-areas/themes.gjs b/frontend/discourse/admin/components/admin-config-areas/themes.gjsindex 2a6f6ee9aef..c1034a91dbb 100644--- a/frontend/discourse/admin/components/admin-config-areas/themes.gjs+++ b/frontend/discourse/admin/components/admin-config-areas/themes.gjs@@ -9,6 +9,7 @@ import { THEMES } from "discourse/admin/models/theme";import PluginOutlet from "discourse/components/plugin-outlet";import lazyHash from "discourse/helpers/lazy-hash";import getURL from "discourse/lib/get-url";+import DiscourseURL from "discourse/lib/url";import DPageSubheader from "discourse/ui-kit/d-page-subheader";import { i18n } from "discourse-i18n";@@ -69,11 +70,7 @@ export default class AdminConfigAreasThemes extends Component {duration: "short",});- this.router.transitionTo(- "adminCustomizeThemes.show.index",- "themes",- theme.id- );+ DiscourseURL.routeTo(`/admin/customize/themes/${theme.id}`);}@actiondiff --git a/frontend/discourse/admin/components/modal/install-theme.gjs b/frontend/discourse/admin/components/modal/install-theme.gjsindex 74f5f24e8ad..b7a0c1b20ec 100644--- a/frontend/discourse/admin/components/modal/install-theme.gjs+++ b/frontend/discourse/admin/components/modal/install-theme.gjs@@ -18,6 +18,7 @@ import ComboBox from "discourse/select-kit/components/combo-box";import DButton from "discourse/ui-kit/d-button";import DConditionalLoadingSection from "discourse/ui-kit/d-conditional-loading-section";import DCopyButton from "discourse/ui-kit/d-copy-button";+import DInterpolatedTranslation from "discourse/ui-kit/d-interpolated-translation";import DModal from "discourse/ui-kit/d-modal";import dIcon from "discourse/ui-kit/helpers/d-icon";import { i18n } from "discourse-i18n";@@ -57,6 +58,10 @@ export default class InstallThemeModal extends Component {return this.uploadUrl?.match?.(/^ssh:\/\/.+@.+$|.+@.+:.+$/);}+ get showGithubDeployKeyHelp() {+ return /^(?:ssh:\/\/)?git@github\.com[:/]/.test(this.uploadUrl?.trim());+ }+get submitLabel() {if (this.themeCannotBeInstalled) {return "admin.customize.theme.create_placeholder";@@ -65,6 +70,10 @@ export default class InstallThemeModal extends Component {return `admin.customize.theme.${this.create ? "create" : "install"}`;}+ get showStageButton() {+ return this.remote && this.showPublicKey && !this.themeCannotBeInstalled;+ }+get component() {return this.selectedType === COMPONENTS;}@@ -97,6 +106,7 @@ export default class InstallThemeModal extends Component {return (this.loading ||(this.remote && !this.uploadUrl) ||+ (this.remote && this.showPublicKey && !this.publicKey) ||(this.local && !this.localFile) ||(this.create && this.nameTooShort));@@ -189,6 +199,15 @@ export default class InstallThemeModal extends Component {@actionasync installTheme() {+ return this.#installTheme();+ }++ @action+ async stageTheme() {+ return this.#installTheme({ placeholder: true });+ }++ async #installTheme({ placeholder = false } = {}) {if (this.create) {return this.#createTheme();}@@ -222,6 +241,10 @@ export default class InstallThemeModal extends Component {branch: this.branch,public_key: this.publicKey,};++ if (placeholder) {+ options.data.placeholder = true;+ }}// User knows that theme cannot be installed, but they want to continue@@ -244,7 +267,7 @@ export default class InstallThemeModal extends Component {this.args.model.addTheme(theme);this.args.closeModal();} catch (err) {- if (!this.publicKey || this.themeCannotBeInstalled) {+ if (placeholder || !this.publicKey || this.themeCannotBeInstalled) {return popupAjaxError(err);}this.themeCannotBeInstalled = i18n("admin.customize.theme.force_install");@@ -432,7 +455,24 @@ export default class InstallThemeModal extends Component {{{#if this.showPublicKey}}<div class="public-key"><div class="label">- {{i18n "admin.customize.theme.public_key"}}+ {{#if this.showGithubDeployKeyHelp}}+ <DInterpolatedTranslation+ @key="admin.customize.theme.public_key_github"+ as |Placeholder|+ >+ <Placeholder @name="link">+ <a+ href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys#set-up-deploy-keys"+ target="_blank"+ rel="noopener noreferrer"+ >{{i18n+ "admin.customize.theme.deploy_key_instructions"+ }}</a>+ </Placeholder>+ </DInterpolatedTranslation>+ {{else}}+ {{i18n "admin.customize.theme.public_key"}}+ {{/if}}</div><div class="public-key-text-wrapper"><textarea@@ -507,9 +547,17 @@ export default class InstallThemeModal extends Component {<DButton@action={{this.installTheme}}@disabled={{this.installDisabled}}- class={{if this.themeCannotBeInstalled "btn-danger" "btn-primary"}}+ class={{if this.themeCannotBeInstalled "btn-default" "btn-primary"}}@label={{this.submitLabel}}/>+ {{#if this.showStageButton}}+ <DButton+ @action={{this.stageTheme}}+ @disabled={{this.installDisabled}}+ class="btn-default create-placeholder"+ @label="admin.customize.theme.create_placeholder"+ />+ {{/if}}<DButtonclass="btn-flat d-modal-cancel"@action={{@closeModal}}diff --git a/frontend/discourse/admin/templates/admin-customize-themes/show.gjs b/frontend/discourse/admin/templates/admin-customize-themes/show.gjsindex 48c1c47fb4d..fcc5b34084f 100644--- a/frontend/discourse/admin/templates/admin-customize-themes/show.gjs+++ b/frontend/discourse/admin/templates/admin-customize-themes/show.gjs@@ -93,6 +93,10 @@ export default <template></div>{{/if}}+ <p class="description">+ {{i18n "admin.customize.theme.placeholder_description"}}+ </p>+{{#if @controller.showRemoteError}}<div class="error-message">{{dIcon "triangle-exclamation"}}diff --git a/frontend/discourse/tests/acceptance/themes-test.js b/frontend/discourse/tests/acceptance/themes-test.jsindex 551ea89211d..f2df15ebff4 100644--- a/frontend/discourse/tests/acceptance/themes-test.js+++ b/frontend/discourse/tests/acceptance/themes-test.js@@ -183,7 +183,7 @@ acceptance("Theme", function (needs) {.dom(".admin-install-theme-modal .d-modal__footer .install-theme-warning").exists("Git warning is displayed");- await click(".admin-install-theme-modal .d-modal__footer .btn-danger");+ await click(".admin-install-theme-modal .d-modal__footer .btn-default");assert.dom(".admin-install-theme-modal").doesNotExist("modal is closed");});diff --git a/lib/theme_store/git_importer.rb b/lib/theme_store/git_importer.rbindex 0be02aee170..7e1f9d754c7 100644--- a/lib/theme_store/git_importer.rb+++ b/lib/theme_store/git_importer.rb@@ -82,6 +82,24 @@ class ThemeStore::GitImporter < ThemeStore::BaseImporterraise RemoteTheme::ImportError.new(I18n.t("themes.import_error.git"))end+ def raise_command_import_error!(error)+ message = error.stderr.to_s+ translation_key =+ if error.status && (error.status.exitstatus == 124 || !error.status.exited?)+ "themes.import_error.git_timeout"+ elsif message.match?(/Permission denied|publickey|Authentication failed|access denied/i)+ "themes.import_error.git_authentication"+ elsif message.match?(/Remote branch .* not found/i)+ "themes.import_error.git_branch_not_found"+ elsif message.match?(/Repository not found|does not appear to be a git repository/i)+ "themes.import_error.git_repository_not_found"+ else+ "themes.import_error.git"+ end++ raise RemoteTheme::ImportError.new(I18n.t(translation_key))+ end+def clone!begin@uri = URI.parse(@url)@@ -133,8 +151,8 @@ class ThemeStore::GitImporter < ThemeStore::BaseImporterbeginDiscourse::Utils.execute_command(env, *args, timeout: COMMAND_TIMEOUT_SECONDS)- rescue RuntimeError- raise_import_error!+ rescue Discourse::Utils::CommandError => error+ raise_command_import_error!(error)endend@@ -151,8 +169,8 @@ class ThemeStore::GitImporter < ThemeStore::BaseImporterbeginDiscourse::Utils.execute_command(env, *args, timeout: COMMAND_TIMEOUT_SECONDS)- rescue RuntimeError- raise_import_error!+ rescue Discourse::Utils::CommandError => error+ raise_command_import_error!(error)endendenddiff --git a/spec/lib/theme_store/git_importer_spec.rb b/spec/lib/theme_store/git_importer_spec.rbindex 0a89a6105c7..5c19f0ce816 100644--- a/spec/lib/theme_store/git_importer_spec.rb+++ b/spec/lib/theme_store/git_importer_spec.rb@@ -26,6 +26,40 @@ RSpec.describe ThemeStore::GitImporter do@ssh_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_ssh_#{hex}"end+ it "reports safe reasons for Git command failures" do+ failures = {+ "Permission denied (publickey)." => "themes.import_error.git_authentication",+ "fatal: Remote branch missing not found in upstream origin" =>+ "themes.import_error.git_branch_not_found",+ "ERROR: Repository not found." => "themes.import_error.git_repository_not_found",+ }++ failures.each do |stderr, translation_key|+ status = stub(exited?: true, exitstatus: 128)+ error = Discourse::Utils::CommandError.new("Git failed", stderr: stderr, status: status)+ Discourse::Utils.stubs(:execute_command).raises(error)++ importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key")++ expect { importer.import! }.to raise_error(+ RemoteTheme::ImportError,+ I18n.t(translation_key),+ )+ end++ timeout_status = stub(exited?: true, exitstatus: 124)+ timeout_error =+ Discourse::Utils::CommandError.new("Git timed out", stderr: "", status: timeout_status)+ Discourse::Utils.stubs(:execute_command).raises(timeout_error)++ importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key")++ expect { importer.import! }.to raise_error(+ RemoteTheme::ImportError,+ I18n.t("themes.import_error.git_timeout"),+ )+ end+it "imports ssh urls" doDiscourse::Utils.expects(:execute_command).with({diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rbindex 6b42c077330..e543743cbaf 100644--- a/spec/requests/admin/themes_controller_spec.rb+++ b/spec/requests/admin/themes_controller_spec.rb@@ -293,6 +293,44 @@ RSpec.describe Admin::ThemesController doexpect(response.parsed_body["theme"]["name"]).to eq("discourse-inexistent-theme")end+ it "creates a private theme placeholder without cloning the repository" do+ Discourse.redis.setex("ssh_key_public_key", 1.hour, "private_key")++ expect do+ post "/admin/themes/import.json",+ params: {+ remote: "[email protected]:discourse/private-theme.git",+ branch: "main",+ public_key: "public_key",+ placeholder: true,+ }+ end.to change { Theme.count }.by(1).and change { RemoteTheme.count }.by(1)++ expect(response.status).to eq(201)+ remote_theme = Theme.last.remote_theme+ expect(remote_theme.attributes.slice("remote_url", "branch", "private_key")).to eq(+ "remote_url" => "[email protected]:discourse/private-theme.git",+ "branch" => "main",+ "private_key" => "private_key",+ )+ end++ it "rolls back the remote theme when placeholder creation fails" do+ Discourse.redis.setex("ssh_key_public_key", 1.hour, "private_key")+ Theme.stubs(:create!).raises(ActiveRecord::RecordInvalid.new(Theme.new))++ expect do+ post "/admin/themes/import.json",+ params: {+ remote: "[email protected]:discourse/private-theme.git",+ public_key: "public_key",+ placeholder: true,+ }+ end.not_to change { RemoteTheme.count }++ expect(response.status).to eq(422)+ end+it "fails to import with an error if uploads are not allowed" doSiteSetting.theme_authorized_extensions = "nothing"diff --git a/spec/system/page_objects/modals/install_theme.rb b/spec/system/page_objects/modals/install_theme.rbindex 0f142030964..6e407572b0d 100644--- a/spec/system/page_objects/modals/install_theme.rb+++ b/spec/system/page_objects/modals/install_theme.rb@@ -24,6 +24,28 @@ module PageObjectsclick_button(I18n.t("admin_js.admin.customize.theme.create"))endend++ def choose_remote_repository(url)+ within(MODAL_CLASS) do+ find(".install-theme-item__remote").click+ find(".install-theme-content .repo input").fill_in(with: url)+ end+ self+ end++ def stage_private_theme+ within(MODAL_CLASS) { find(".create-placeholder").click }+ end++ def has_private_theme_actions_disabled?+ has_button?(I18n.t("admin_js.admin.customize.theme.install"), disabled: true) &&+ has_button?(I18n.t("admin_js.admin.customize.theme.create_placeholder"), disabled: true)+ end++ def has_private_theme_actions_enabled?+ has_button?(I18n.t("admin_js.admin.customize.theme.install"), disabled: false) &&+ has_button?(I18n.t("admin_js.admin.customize.theme.create_placeholder"), disabled: false)+ endendendenddiff --git a/spec/system/page_objects/pages/admin_customize_themes.rb b/spec/system/page_objects/pages/admin_customize_themes.rbindex de8f725407a..d8cefa61cad 100644--- a/spec/system/page_objects/pages/admin_customize_themes.rb+++ b/spec/system/page_objects/pages/admin_customize_themes.rb@@ -10,6 +10,14 @@ module PageObjectsselfend+ def finish_install+ find(".finish-install").click+ end++ def has_incomplete_installation?+ has_button?(I18n.t("admin_js.admin.customize.theme.finish_install"))+ end+def visit_editor(theme, target: "common", field: "scss")page.visit("/admin/customize/themes/#{theme.id}/#{target}/#{field}/edit")selfdiff --git a/spec/system/page_objects/pages/admin_customize_themes_config_area.rb b/spec/system/page_objects/pages/admin_customize_themes_config_area.rbindex 9e4dbca7ebd..4ea999703ed 100644--- a/spec/system/page_objects/pages/admin_customize_themes_config_area.rb+++ b/spec/system/page_objects/pages/admin_customize_themes_config_area.rb@@ -66,6 +66,10 @@ module PageObjectshas_css?(".themes-cards-container > .admin-config-area-card", count: count)end+ def has_theme_named?(name)+ has_css?(".theme-card.#{name.parameterize}")+ end+def has_install_more_themes_card?has_css?(".theme-card.--install-more",@@ -93,6 +97,10 @@ module PageObjectsfind_theme_card(theme).find(".edit").clickend+ def click_edit_by_name(name)+ find(".theme-card.#{name.parameterize}").find(".edit").click+ end+def click_install_buttonPageObjects::Components::AdminCustomizeThemeInstallButton.new.clickenddiff --git a/spec/system/stage_private_theme_spec.rb b/spec/system/stage_private_theme_spec.rbnew file mode 100644index 00000000000..88ac1513102--- /dev/null+++ b/spec/system/stage_private_theme_spec.rb@@ -0,0 +1,61 @@+# frozen_string_literal: true++RSpec.describe "Stage a private theme" do+ fab!(:admin)++ let(:themes_page) { PageObjects::Pages::AdminCustomizeThemesConfigArea.new }+ let(:theme_page) { PageObjects::Pages::AdminCustomizeThemes.new }+ let(:dialog) { PageObjects::Components::Dialog.new }+ let(:repository_url) { "[email protected]:discourse/private-theme.git" }++ before { sign_in(admin) }++ it "lets an admin stage a private theme before repository access is granted" do+ themes_page.visit+ install_modal = themes_page.click_install_button+ install_modal.choose_remote_repository(repository_url)++ expect(install_modal).to have_private_theme_actions_enabled++ install_modal.stage_private_theme++ expect(install_modal).to be_closed+ expect(themes_page).to have_theme_named("private-theme")++ themes_page.click_edit_by_name("private-theme")+ expect(theme_page).to have_incomplete_installation+ end++ it "keeps installation unavailable when deploy key generation fails" do+ page.driver.with_playwright_page do |playwright_page|+ pattern = %r{/admin/themes/generate_key_pair}+ playwright_page.route(+ pattern,+ ->(route, _request) { route.fulfill(status: 422, body: '{"errors":["failed"]}') },+ )++ themes_page.visit+ install_modal = themes_page.click_install_button+ install_modal.choose_remote_repository(repository_url)+ expect(install_modal).to have_private_theme_actions_disabled+ ensure+ playwright_page.unroute(pattern)+ end+ end++ it "shows a categorized error when finishing the installation fails" do+ private_theme = Fabricate(:theme, name: "private-theme")+ private_theme.remote_theme =+ RemoteTheme.create!(remote_url: repository_url, private_key: "private key")+ private_theme.save!++ allow_any_instance_of(ThemeStore::GitImporter).to receive(:import!).and_raise(+ RemoteTheme::ImportError.new(I18n.t("themes.import_error.git_authentication")),+ )++ theme_page.visit(private_theme)+ theme_page.finish_install++ expect(dialog).to have_content(I18n.t("themes.import_error.git_authentication"))+ end+end