Skip to content

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

Clean up Metabot's notebook fallback (#85692)

Chris Trutera09f9b4310d2
diff --git a/src/metabase/metabot/tools/sql.clj b/src/metabase/metabot/tools/sql.clj
index fc6f9d67e78..34bdef3b445 100644
--- a/src/metabase/metabot/tools/sql.clj
+++ b/src/metabase/metabot/tools/sql.clj
@@ -61,16 +61,18 @@
:mode "rewrite"
:value sql}))
+(def ^:private notebook-tool-name "construct_notebook_query")
+
(tools/defrecoverable no-native-permission-with-notebook-fallback!
"The user may not write SQL against the target database, and the notebook builder is offered this turn."
{:payload [:map {:closed true}]}
[_]
{:message "You do not have permission to write SQL against this database."
- :recovery [{:uses #{"construct_notebook_query"}
+ :recovery [{:uses #{notebook-tool-name}
:text (str "Build the question with `construct_notebook_query` instead. Don't write SQL against"
" this database again.")}]})
-(defn- with-notebook-fallback
+(defn- do-with-notebook-fallback
"Call `thunk`, rethrowing its native-query refusal as [[no-native-permission-with-notebook-fallback!]]
when the notebook builder is offered this turn.
Without the notebook builder the refusal still ends the turn."
@@ -81,7 +83,7 @@
(thunk)
(catch clojure.lang.ExceptionInfo e
(if (and (= ::sql.common/no-native-permission (get-in (ex-data e) [tools.error/error-key :code]))
- (contains? (:tool-names ctx) "construct_notebook_query"))
+ (contains? (:tool-names ctx) notebook-tool-name))
(tools.error/throw-recoverable! ::no-native-permission-with-notebook-fallback {} {:cause e})
(throw e)))))
@@ -118,8 +120,8 @@
(tools/unrecoverable! ::missing-editor
{:user-message (tru "No active SQL editor was found. Open a SQL editor and try again.")})))
action-result (sql.common/validated-sql-action!
- (with-notebook-fallback
- ctx #(create-sql-query-tools/create-sql-query {:database-id database-id :sql sql-query})))
+ (do-with-notebook-fallback
+ ctx #(create-sql-query-tools/create-sql-query {:database-id database-id :sql sql-query})))
{:keys [query-id query-content]} action-result
structured (assoc action-result :result-type :query)
instr (if code-edit?
@@ -175,12 +177,12 @@
:metabot/capabilities #{:permission-write-sql-queries}
:args edit-sql-schema})
(handle [_ {query-id :query_id :keys [edits checklist title]} ctx]
- (sql-edit-result (with-notebook-fallback
- ctx #(edit-sql-query-tools/edit-sql-query
- {:query-id query-id
- :edits edits
- :checklist checklist
- :queries-state (shared/current-queries-state (:memory-atom ctx))}))
+ (sql-edit-result (do-with-notebook-fallback
+ ctx #(edit-sql-query-tools/edit-sql-query
+ {:query-id query-id
+ :edits edits
+ :checklist checklist
+ :queries-state (shared/current-queries-state (:memory-atom ctx))}))
title instructions/edit-sql-query-instructions-for ctx)))
(def edit-sql-query-tool
@@ -208,12 +210,12 @@
:metabot/capabilities #{:permission-write-sql-queries}
:args replace-sql-schema})
(handle [_ {query-id :query_id new-query :new_query :keys [checklist title]} ctx]
- (sql-edit-result (with-notebook-fallback
- ctx #(replace-sql-query-tools/replace-sql-query
- {:query-id query-id
- :sql new-query
- :checklist checklist
- :queries-state (shared/current-queries-state (:memory-atom ctx))}))
+ (sql-edit-result (do-with-notebook-fallback
+ ctx #(replace-sql-query-tools/replace-sql-query
+ {:query-id query-id
+ :sql new-query
+ :checklist checklist
+ :queries-state (shared/current-queries-state (:memory-atom ctx))}))
title instructions/replace-sql-query-instructions-for ctx)))
(def replace-sql-query-tool
diff --git a/test/metabase/metabot/tools/sql_test.clj b/test/metabase/metabot/tools/sql_test.clj
index 4b778b3837d..8c3fce28834 100644
--- a/test/metabase/metabot/tools/sql_test.clj
+++ b/test/metabase/metabot/tools/sql_test.clj
@@ -221,6 +221,16 @@
(is (= ::sql.edit/edit-not-found (get-in result [:error :code])))
(is (str/includes? (:output result) "SQL for this edit: \"SELECT 'owl'\\n-- \\u001b[4mnotes\"")))))
+(def ^:private native-refusal
+ {:class :unrecoverable
+ :code ::sql.common/no-native-permission
+ :user-message (str "You do not have permission to write SQL queries against this database."
+ " Native query permissions are required.")})
+
+(def ^:private notebook-fallback
+ {:class :recoverable
+ :code ::sql/no-native-permission-with-notebook-fallback})
+
(deftest sql-edit-permission-error-test
(mt/with-temp [:model/Database {db-id :id :as db} {:engine :h2}]
(mt/with-no-data-perms-for-all-users!
@@ -235,16 +245,11 @@
:new_query "SELECT 2"})]]
(testing (str tool-name " without the notebook builder")
(let [result (invoke :sql)]
- (is (= {:class :unrecoverable
- :code ::sql.common/no-native-permission
- :user-message "You do not have permission to write SQL queries against this database. Native query permissions are required."}
- (:error result)))
+ (is (= native-refusal (:error result)))
(is (str/includes? (:output result) "Don't retry"))))
(testing (str tool-name " with the notebook builder")
(let [result (invoke :internal)]
- (is (= {:class :recoverable
- :code ::sql/no-native-permission-with-notebook-fallback}
- (:error result)))
+ (is (= notebook-fallback (:error result)))
(is (str/includes? (:output result) "Build the question with `construct_notebook_query`")))))))))))
(deftest missing-sql-editor-test
@@ -283,20 +288,16 @@
(mt/with-temp [:model/Database {db-id :id} {:engine :h2}]
(mt/with-no-data-perms-for-all-users!
(perms/set-database-permission! (perms-group/all-users) db-id :perms/view-data :unrestricted)
- (doseq [[permission profile expected-error]
+ (doseq [:let [access-denied {:class :unrecoverable
+ :code ::sql.create/database-access-denied
+ :user-message "You do not have access to this database."}]
+ [permission profile expected-error]
[[:query-builder-and-native :internal nil]
- [:query-builder-and-native :sql nil]
- [:query-builder :internal {:class :recoverable
- :code ::sql/no-native-permission-with-notebook-fallback}]
- [:query-builder :sql {:class :unrecoverable
- :code ::sql.common/no-native-permission
- :user-message "You do not have permission to write SQL queries against this database. Native query permissions are required."}]
- [:no :internal {:class :unrecoverable
- :code ::sql.create/database-access-denied
- :user-message "You do not have access to this database."}]
- [:no :sql {:class :unrecoverable
- :code ::sql.create/database-access-denied
- :user-message "You do not have access to this database."}]]]
+ [:query-builder-and-native :sql nil]
+ [:query-builder :internal notebook-fallback]
+ [:query-builder :sql native-refusal]
+ [:no :internal access-denied]
+ [:no :sql access-denied]]]
(perms/set-database-permission! (perms-group/all-users) db-id :perms/create-queries permission)
(mt/with-current-user (mt/user->id :rasta)
(testing (str permission " " profile)