Skip to content

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

FIX: Theme source updates are not atomic on import or migration failure (#39969)

discourse-patch-triage[bot]e0b78cf08b7f
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index 1aabb1fb5d1..256e65dee34 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -397,28 +397,28 @@ class Admin::ThemesController < Admin::AdminController
theme = Theme.include_relations.find(theme_id)
remote_theme = theme.remote_theme
- original_url = remote_theme.remote_url
- original_branch = remote_theme.branch
- original_private_key = remote_theme.private_key
- remote_theme.remote_url = remote_url
- remote_theme.branch = params[:branch].presence
- remote_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.save!
+ remote_theme.transaction do
+ remote_theme.remote_url = remote_url
+ remote_theme.branch = params[:branch].presence
+ remote_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
+ remote_theme.update_from_remote(already_in_transaction: true)
+
+ if remote_theme.last_error_text.present?
+ raise RemoteTheme::ImportError.new(remote_theme.last_error_text)
+ end
+ end
log_theme_change(nil, theme.reload)
render json: serialize_data(theme, ThemeSerializer), status: :ok
- rescue RemoteTheme::ImportError, ActiveRecord::RecordInvalid => e
- remote_theme.update!(
- remote_url: original_url,
- branch: original_branch,
- private_key: original_private_key,
- )
+ rescue RemoteTheme::ImportError,
+ ActiveRecord::RecordInvalid,
+ Theme::SettingsMigrationError => e
+ remote_theme.reload
render_json_error e.message
end
end
diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rb
index 3a125f86ae4..2e50f2ad3f2 100644
--- a/spec/requests/admin/themes_controller_spec.rb
+++ b/spec/requests/admin/themes_controller_spec.rb
@@ -1788,6 +1788,107 @@ RSpec.describe Admin::ThemesController do
)
end
+ it "reverts changes when the new source cannot be fetched" do
+ theme.remote_theme =
+ RemoteTheme.create!(
+ remote_url: repo_url,
+ branch: "main",
+ private_key: "old_key",
+ local_version: "abc",
+ remote_version: "def",
+ commits_behind: 2,
+ )
+ theme.save!
+ Discourse.redis.setex("ssh_key_new_public_key", 1.hour, "new_key")
+ RemoteTheme.any_instance.expects(:reload).once
+
+ put "/admin/themes/#{theme.id}/source.json",
+ params: {
+ remote_url: "https://github.com/discourse/missing-theme.git",
+ branch: "develop",
+ public_key: "new_public_key",
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(I18n.t("themes.import_error.git"))
+
+ theme.reload
+ expect(theme.remote_theme.remote_url).to eq(repo_url)
+ expect(theme.remote_theme.branch).to eq("main")
+ expect(theme.remote_theme.private_key).to eq("old_key")
+ expect(theme.remote_theme.local_version).to eq("abc")
+ expect(theme.remote_theme.remote_version).to eq("def")
+ expect(theme.remote_theme.commits_behind).to eq(2)
+ expect(theme.remote_theme.last_error_text).to eq(nil)
+ end
+
+ it "reverts changes when a settings migration fails" do
+ initial_repo =
+ setup_git_repo(
+ "about.json" => {
+ name: "migration-theme",
+ about_url: "https://original.example.com/about",
+ }.to_json,
+ "settings.yml" => <<~YAML,
+ some_setting:
+ type: string
+ default: default value
+ YAML
+ )
+ initial_repo_url =
+ MockGitImporter.register("https://example.com/migration-source-theme.git", initial_repo)
+ migration_theme = RemoteTheme.import_theme(initial_repo_url)
+ migration_theme.remote_theme.update!(branch: "main", private_key: "old_key")
+
+ original_remote_version = migration_theme.remote_theme.remote_version
+ original_local_version = migration_theme.remote_theme.local_version
+
+ replacement_repo =
+ setup_git_repo(
+ "about.json" => {
+ name: "migration-theme",
+ about_url: "https://updated.example.com/about",
+ }.to_json,
+ "settings.yml" => <<~YAML,
+ some_setting:
+ type: string
+ default: default value
+ YAML
+ "migrations/settings/0001-bad-migration.js" => <<~JS,
+ export default function migrate(settings) {
+ return null;
+ }
+ JS
+ )
+ replacement_repo_url =
+ MockGitImporter.register(
+ "https://example.com/migration-replacement-theme.git",
+ replacement_repo,
+ )
+ Discourse.redis.setex("ssh_key_migration_public_key", 1.hour, "new_key")
+
+ put "/admin/themes/#{migration_theme.id}/source.json",
+ params: {
+ remote_url: replacement_repo_url,
+ branch: "develop",
+ public_key: "migration_public_key",
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(
+ I18n.t("themes.import_error.migrations.no_returned_value", name: "0001-bad-migration"),
+ )
+
+ migration_theme.reload
+ expect(migration_theme.remote_theme.remote_url).to eq(initial_repo_url)
+ expect(migration_theme.remote_theme.branch).to eq("main")
+ expect(migration_theme.remote_theme.private_key).to eq("old_key")
+ expect(migration_theme.remote_theme.about_url).to eq("https://original.example.com/about")
+ expect(migration_theme.remote_theme.remote_version).to eq(original_remote_version)
+ expect(migration_theme.remote_theme.local_version).to eq(original_local_version)
+ expect(migration_theme.remote_theme.commits_behind).to eq(0)
+ end
+
it "reverts changes on import failure" do
theme.remote_theme =
RemoteTheme.create!(remote_url: repo_url, branch: "main", private_key: "old_key")