Skip to content

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

Remote-sync error handling improvements (#65072)

Nathan Voxlanda94c2846251b
diff --git a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
index 244c632403d..74da52d0484 100644
--- a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
+++ b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts
@@ -485,7 +485,7 @@ describe("Remote Sync", () => {
.should("exist");
cy.findByTestId("admin-layout-content")
.findByText(
- "Unable to connect to git repository with the provided settings",
+ "Failed to clone git repository: Git CloneCommand failed: URI not supported: file://invalid-path",
)
.should("exist");
});
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/settings.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/settings.clj
index a1fce553f29..bb6aac788f2 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/settings.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/settings.clj
@@ -85,17 +85,27 @@
(defn check-git-settings!
"Validates git repository settings by attempting to connect and retrieve the default branch.
- Takes a map with :remote-sync-url (required) and :remote-sync-token (optional) keys.
+ If no args are passed, it validates the current settings.
Throws ExceptionInfo if unable to connect to the repository with the provided settings."
- ([] (when (setting/get :remote-sync-enabled) (check-git-settings! {:remote-sync-url (setting/get :remote-sync-url)
- :remote-sync-token (setting/get :remote-sync-token)})))
-
- ([{:keys [remote-sync-url remote-sync-token]}]
- (let [source
- (try (git/git-source remote-sync-url "HEAD" remote-sync-token)
- (catch Exception e
- (throw (ex-info "Unable to connect to git repository with the provided settings" {:cause (.getMessage e)} e))))]
+ ([] (when (setting/get :remote-sync-enabled) (check-git-settings! {:remote-sync-url (setting/get :remote-sync-url)
+ :remote-sync-token (setting/get :remote-sync-token)
+ :remote-sync-branch (setting/get :remote-sync-branch)
+ :remote-sync-type (setting/get :remote-sync-type)})))
+
+ ([{:keys [remote-sync-url remote-sync-token remote-sync-branch remote-sync-type]}]
+ (when-not (or (not (str/index-of remote-sync-url ":"))
+ (str/starts-with? remote-sync-url "file://")
+ (and (or (str/starts-with? remote-sync-url "http://")
+ (str/starts-with? remote-sync-url "https://"))
+ (str/index-of remote-sync-url "github.com")))
+ (throw (ex-info "Invalid Repository URL format"
+ {:url remote-sync-url})))
+
+ (let [source (git/git-source remote-sync-url "HEAD" remote-sync-token)]
+ (when (and (= :production remote-sync-type) (not (str/blank? remote-sync-branch)) (not (some #{remote-sync-branch} (git/branches source))))
+ (throw (ex-info "Invalid branch name" {:url remote-sync-url :branch remote-sync-branch})))
+ (git/fetch! source)
(when-not (git/has-data? source)
(throw (ex-info "Cannot connect to an uninitialized repository" {:url remote-sync-url}))))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
index a0a08d67f16..421dc62ade3 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
@@ -20,19 +20,30 @@
(set! *warn-on-reflection* true)
+(defn- root-cause [^Throwable e]
+ (if-let [cause (ex-cause e)]
+ (recur cause)
+ e))
+
+(defn- clean-git-exception
+ [^Exception e ^GitCommand command remote?]
+ (let [root-ex (root-cause e)]
+ ;; strip off the beginning URL that is often included and ends up being duplicated later
+ (ex-info (format "Git %s failed: %s" (-> command .getClass .getSimpleName) (str/replace-first (ex-message root-ex) #"^[a-z]+://[a-zA-Z0-9\-\.]+: " ""))
+ {:remote remote?} root-ex)))
+
(defn- call-command [^GitCommand command]
- (let [analytics-labels {:operation (-> command .getClass .getName) :remote false}]
+ (let [analytics-labels {:operation (-> command .getClass .getSimpleName) :remote false}]
(analytics/inc! :metabase-remote-sync/git-operations analytics-labels)
(try
(.call command)
(catch Exception e
(analytics/inc! :metabase-remote-sync/git-operations-failed analytics-labels)
- (throw (ex-info (format "Git %s failed: %s" (:operation analytics-labels) (ex-message e))
- analytics-labels e))))))
+ (throw (clean-git-exception e command false))))))
(defn- call-remote-command [^TransportCommand command {:keys [^String token]}]
- (let [analytics-labels {:operation (-> command .getClass .getName) :remote true}
+ (let [analytics-labels {:operation (-> command .getClass .getSimpleName) :remote true}
;; GitHub convention: use "x-access-token" as username when authenticating with a personal access token
;; For Gitlab any values can be used as the user name so x-access-token works just as well
credentials-provider (when token (UsernamePasswordCredentialsProvider. "x-access-token" token))]
@@ -44,8 +55,7 @@
(.call))
(catch Exception e
(analytics/inc! :metabase-remote-sync/git-operations-failed analytics-labels)
- (throw (ex-info (format "Git %s failed: %s" (-> command .getClass .getName) (ex-message e))
- analytics-labels e))))))
+ (throw (clean-git-exception e command true))))))
(defn- qualify-branch [branch]
(if (str/starts-with? branch "refs/heads/")
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 b2984297965..e71a4f4f9ee 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
@@ -527,8 +527,8 @@
(deftest settings-handles-invalid-settings-test
(testing "PUT /api/ee/remote-sync/settings handles invalid settings"
(let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
- {:remote-sync-url "invalid-url"})]
- (is (= "Unable to connect to git repository with the provided settings" (:error response))))))
+ {:remote-sync-url "asdf://invalid-url"})]
+ (is (= "Invalid Repository URL format" (:error response))))))
(deftest settings-cannot-change-with-dirty-data
(testing "PUT /api/ee/remote-sync/settings doesn't allow loosing dirty data"