Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Don't automatically pull when in read-write mode (#68500)

John Swanson38f42a9386a4
diff --git a/e2e/support/helpers/e2e-remote-sync-helpers.ts b/e2e/support/helpers/e2e-remote-sync-helpers.ts
index 545f0ddf5d5..138e08bf0ff 100644
--- a/e2e/support/helpers/e2e-remote-sync-helpers.ts
+++ b/e2e/support/helpers/e2e-remote-sync-helpers.ts
@@ -54,6 +54,23 @@ export function configureGit(
});
}
+// Setup remote sync via the API and wait for/trigger the initial import
+export function configureGitAndPullChanges(
+ syncType: "read-write" | "read-only",
+ syncUrl = LOCAL_GIT_PATH + "/.git",
+) {
+ configureGit(syncType, syncUrl);
+
+ if (syncType === "read-only") {
+ // Read-only mode automatically triggers an import, just wait for it
+ pollForTask({ taskName: "import" });
+ } else {
+ // Read-write mode needs manual import trigger
+ cy.request("POST", "/api/ee/remote-sync/import", {});
+ pollForTask({ taskName: "import" });
+ }
+}
+
// Prepare the local git repo and initializing with an empty commit
export function setupGitSync() {
cy.exec("rm -rf " + LOCAL_GIT_PATH);
@@ -222,3 +239,47 @@ export const waitForTask = (
}
});
};
+
+// Poll for task completion by actively querying the endpoint
+// Use this when the app isn't loaded yet (e.g., in setup helpers before cy.visit)
+export const pollForTask = (
+ { taskName }: { taskName: "import" | "export" },
+ retries = 0,
+) => {
+ if (retries > 30) {
+ throw Error(`Too many retries waiting for ${taskName}`);
+ }
+
+ return cy
+ .request("GET", "/api/ee/remote-sync/current-task")
+ .then((response) => {
+ const { body } = response;
+
+ // No task exists yet, keep waiting
+ if (!body) {
+ cy.wait(500);
+ return pollForTask({ taskName }, retries + 1);
+ }
+
+ // Wrong task type, keep waiting
+ if (body.sync_task_type !== taskName) {
+ cy.wait(500);
+ return pollForTask({ taskName }, retries + 1);
+ }
+
+ // Task hasn't completed successfully yet
+ if (body.status !== "successful") {
+ // Check if it errored
+ if (body.status === "errored") {
+ throw Error(
+ `Task ${taskName} failed: ${body.error_message || "Unknown error"}`,
+ );
+ }
+ cy.wait(500);
+ return pollForTask({ taskName }, retries + 1);
+ }
+
+ // Success!
+ return cy.wrap(body);
+ });
+};
diff --git a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
index 76368c1fd9e..29058af25b2 100644
--- a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
+++ b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
@@ -26,7 +26,7 @@ describe("Remote Sync", () => {
describe("read-write Mode", () => {
it("can push and pull changes", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
H.wrapSyncedCollection();
const UPDATED_REMOTE_QUESTION_NAME = "Updated Question Name";
@@ -92,7 +92,7 @@ describe("Remote Sync", () => {
});
it("should not allow you to move content to the Synced Collection that references non Synced Collection items", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
H.wrapSyncedCollection();
cy.intercept("PUT", `/api/dashboard/${ORDERS_DASHBOARD_ID}`).as(
"updateDashboard",
@@ -135,7 +135,7 @@ describe("Remote Sync", () => {
const NEW_BRANCH = `new-branch-${Date.now()}`;
H.copySyncedCollectionFixture();
H.commitToRepo();
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
H.wrapSyncedCollection();
cy.visit("/collection/root");
@@ -236,7 +236,7 @@ describe("Remote Sync", () => {
};
it("should allow you to create new branches and switch between them", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
H.wrapSyncedCollection();
const NEW_BRANCH_1 = `new-branch-${Date.now()}`;
@@ -282,7 +282,7 @@ describe("Remote Sync", () => {
});
it("should show a popup when trying to switch branches with unsynced changes", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
const NEW_BRANCH = `new-branch-${Date.now()}`;
@@ -333,7 +333,7 @@ describe("Remote Sync", () => {
beforeEach(() => {
H.copySyncedCollectionFixture();
H.commitToRepo();
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
H.wrapSyncedCollection();
cy.visit("/collection/root");
@@ -439,19 +439,11 @@ describe("Remote Sync", () => {
.findByText("Success")
.should("exist");
- H.waitForTask({ taskName: "import" });
H.modal().should("not.exist");
cy.findByTestId("exit-admin").click();
- // Branch picker is now in the app bar
+ // Branch picker should appear in the app bar (doesn't require import)
H.getGitSyncControls().should("contain.text", "main");
-
- // Synced collection appears in regular collections list (no separate heading)
- H.navigationSidebar().within(() => {
- cy.findByRole("treeitem", { name: /Synced Collection/i }).should(
- "exist",
- );
- });
});
it("can set up read-only mode", () => {
@@ -540,7 +532,7 @@ describe("Remote Sync", () => {
it("can deactivate remote sync", () => {
H.copySyncedCollectionFixture();
H.commitToRepo();
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
cy.visit("/admin/settings/remote-sync");
@@ -642,7 +634,7 @@ describe("Remote Sync", () => {
describe("admin settings", () => {
it("should show shared tenant collections section when tenants are enabled and remote sync is configured", () => {
// First set up remote sync
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
// Create some tenant collections
H.createSharedTenantCollection("Tenant A Shared");
@@ -670,7 +662,7 @@ describe("Remote Sync", () => {
// Disable tenants
cy.request("PUT", "/api/setting/use-tenants", { value: false });
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
cy.visit("/admin/settings/remote-sync");
// Shared collections sub-section should NOT be visible
@@ -689,7 +681,7 @@ describe("Remote Sync", () => {
});
it("should show empty state when no shared tenant collections exist", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
cy.visit("/admin/settings/remote-sync");
cy.findByTestId("admin-layout-content").within(() => {
@@ -699,7 +691,7 @@ describe("Remote Sync", () => {
});
it("can toggle sync for a shared tenant collection", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
// Create a tenant collection
H.createSharedTenantCollection("Tenant Collection To Sync");
@@ -739,7 +731,7 @@ describe("Remote Sync", () => {
it("should reset collection toggles when switching from read-write to read-nly", () => {
H.copySyncedCollectionFixture();
H.commitToRepo();
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
// Create a tenant collection
H.createSharedTenantCollection("Mode Switch Test Collection");
@@ -770,7 +762,7 @@ describe("Remote Sync", () => {
describe("syncing tenant collections", () => {
it("can push changes from a synced tenant collection", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
// Create a tenant collection
H.createSharedTenantCollection("Syncable Tenant Collection").then(
@@ -818,7 +810,7 @@ describe("Remote Sync", () => {
});
it("shows sync status badge on synced tenant collections in sidebar", () => {
- H.configureGit("read-write");
+ H.configureGitAndPullChanges("read-write");
// Create a tenant collection
H.createSharedTenantCollection("Badge Test Collection").then(
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
index cad78cc10fc..fe4d3bac4ea 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
@@ -164,11 +164,10 @@
(events/publish-event! :event/remote-sync-settings-update
{:details {:remote-sync-type remote-sync-type}
:user-id api/*current-user-id*})
- (let [task-id (impl/finish-remote-config!)]
- (if task-id
- {:success true
- :task_id task-id}
- {:success true})))
+ (if-let [task-id (impl/finish-remote-config!)]
+ {:success true
+ :task_id task-id}
+ {:success true}))
(api.macros/defendpoint :get "/branches" :- remote-sync.schema/BranchesResponse
"Get list of branches from the configured source.
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index 1bd6a6b3299..1a31ed83281 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -409,7 +409,7 @@
(do
(when (str/blank? (setting/get :remote-sync-branch))
(setting/set! :remote-sync-branch (source.p/default-branch (source/source-from-settings))))
- (when (or (nil? (collection/remote-synced-collection)) (= :read-only (settings/remote-sync-type)))
+ (when (= :read-only (settings/remote-sync-type))
(:id (async-import! (settings/remote-sync-branch) true {}))))
(u/prog1 nil
(collection/clear-remote-synced-collection!))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
index 97cbd1787c9..fd0d7123377 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
@@ -508,12 +508,10 @@
source/source-from-settings (constantly mock-main)]
(mt/with-temporary-setting-values [remote-sync-url nil
remote-sync-type :read-write]
- (let [{:as resp :keys [task_id]} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
- {:remote-sync-url "https://github.com/test/repo.git"
- :remote-sync-branch "main"})
- task (wait-for-task-completion task_id)]
- (is (=? {:success true} resp))
- (is (remote-sync.task/successful? task))))))))
+ (let [resp (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
+ {:remote-sync-url "https://github.com/test/repo.git"
+ :remote-sync-branch "main"})]
+ (is (= {:success true} resp))))))))
(deftest settings-update-triggers-import-in-read-only-test
(testing "PUT /api/ee/remote-sync/settings triggers import when type is read-only"
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
index fb168ba1dcd..2ae1d5e5113 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
@@ -473,34 +473,7 @@
impl/async-import! (fn [& _args] (reset! import-started? true) 123)]
(impl/finish-remote-config!)
(is (= "main" (setting/get :remote-sync-branch))
- "Should set branch to default branch")
- (is @import-started?
- "Should start import when no collection exists")))))))
-
-(deftest finish-remote-config!-starts-import-when-no-collection-exists-test
- (testing "finish-remote-config! starts import when no remote-synced collection exists"
- (mt/with-model-cleanup [:model/RemoteSyncTask]
- (let [mock-source (test-helpers/create-mock-source)
- import-called? (atom false)
- import-args (atom nil)]
- (mt/with-temporary-setting-values [remote-sync-enabled true
- remote-sync-url "https://github.com/test/repo.git"
- remote-sync-branch "main"
- remote-sync-type :read-write]
- (with-redefs [source/source-from-settings (constantly mock-source)
- impl/async-import! (fn [branch force? args]
- (reset! import-called? true)
- (reset! import-args {:branch branch :force? force? :args args})
- {:id 123})
- collection/remote-synced-collection (constantly nil)]
- (let [task-id (impl/finish-remote-config!)]
- (is (= 123 task-id)
- "Should return task ID from async-import!")
- (is @import-called?
- "Should call async-import!")
- (is (= {:branch "main" :force? true :args {}}
- @import-args)
- "Should call async-import! with correct arguments"))))))))
+ "Should set branch to default branch")))))))
(deftest finish-remote-config!-starts-import-in-read-only-mode-test
(testing "finish-remote-config! starts import in read-only mode even when collection exists"