The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Share the dashboard write pipeline across tools (#85815)
Chris Truter040e07067a58
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex c3a2fe65578..c2fb8d3224b 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1734,12 +1734,15 @@metabot.toolkit{:team "Metabot"- :api #{metabase.metabot.toolkit.dashboard-op-schema metabase.metabot.toolkit.dashboard-ops}- :uses #{dashboards+ :api #{metabase.metabot.toolkit.dashboard-op-schema metabase.metabot.toolkit.dashboard-ops metabase.metabot.toolkit.dashboard-write}+ :uses #{api+ dashboardslib+ modelsparametersparameters.schema- util}}+ util}+ :model-imports #{:model/Card :model/Dashboard}}metrics{:team "UX West"diff --git a/src/metabase/dashboards/write.clj b/src/metabase/dashboards/write.cljindex d1f345ca8e4..e9c4caf5a4c 100644--- a/src/metabase/dashboards/write.clj+++ b/src/metabase/dashboards/write.clj@@ -1,5 +1,6 @@(ns metabase.dashboards.write- "Permission-checked create/update operations for Dashboards, shared by the REST API and other callers.+ "Permission-checked create/update operations for Dashboards, used by MCP's tools and the Metabot toolkit.+ The REST API keeps its own copies in `metabase.dashboards-rest.api`, so a change here may need making there too.These functions return the saved Dashboard row; response-shaping concerns such as hydration and`:last-edit-info` belong to the callers."@@ -67,21 +68,24 @@(doseq [action-id (set/difference (:actions new-references) (:actions stored))](api/read-check :model/Action action-id))))+(def NewDashboard+ "Schema for the argument to [[create-dashboard!]]: the attributes of a new dashboard."+ [:map {:closed true}+ [:name ms/NonBlankString]+ [:parameters {:optional true} [:maybe ::parameters.schema/parameters]]+ [:description {:optional true} [:maybe :string]]+ [:cache_ttl {:optional true} [:maybe ms/PositiveInt]]+ [:collection_id {:optional true} [:maybe ms/PositiveInt]]+ [:collection_position {:optional true} [:maybe ms/PositiveInt]]+ [:width {:optional true} [:maybe [:enum "fixed" "full"]]]+ [:auto_apply_filters {:optional true} [:maybe :boolean]]])+(mu/defn create-dashboard! :- :map"Create a Dashboard owned by the current user and return the saved row. `:width` and`:auto_apply_filters` fall back to the column defaults when omitted or nil.Requires create permission on `:collection_id` (nil = root). Publishes `:event/dashboard-create`."[{:keys [name description parameters cache_ttl collection_id collection_position- width auto_apply_filters]}- :- [:map {:closed true}- [:name ms/NonBlankString]- [:parameters {:optional true} [:maybe ::parameters.schema/parameters]]- [:description {:optional true} [:maybe :string]]- [:cache_ttl {:optional true} [:maybe ms/PositiveInt]]- [:collection_id {:optional true} [:maybe ms/PositiveInt]]- [:collection_position {:optional true} [:maybe ms/PositiveInt]]- [:width {:optional true} [:maybe [:enum "fixed" "full"]]]- [:auto_apply_filters {:optional true} [:maybe :boolean]]]]+ width auto_apply_filters]} :- NewDashboard];; if we're trying to save the new dashboard in a Collection make sure we have permissions to do that(api/create-check :model/Dashboard {:collection_id collection_id})(check-new-references no-references {:parameters parameters})diff --git a/src/metabase/mcp/v2/dashboard_errors.clj b/src/metabase/mcp/v2/dashboard_errors.cljindex 45d38f4b8cf..8832fd1f4c3 100644--- a/src/metabase/mcp/v2/dashboard_errors.clj+++ b/src/metabase/mcp/v2/dashboard_errors.clj@@ -21,6 +21,10 @@(message/msg ["invalid op: %s — see the tool description for each op's arguments."](common/humanize-detail explanation)))+ :invalid-payload+ (fn [{:keys [explanation]}]+ (message/msg ["This change would produce an invalid dashboard: %s"] (common/humanize-detail explanation)))+:duplicate-new-id(fn [{:keys [id]}](message/msg ["id %s is already used in this batch — give each new row its own negative id."] id))diff --git a/src/metabase/mcp/v2/tools/dashboard.clj b/src/metabase/mcp/v2/tools/dashboard.cljindex 1b0b651261f..58ab2402d73 100644--- a/src/metabase/mcp/v2/tools/dashboard.clj+++ b/src/metabase/mcp/v2/tools/dashboard.clj@@ -9,17 +9,12 @@on the real save. Either way a failed call leaves nothing behind.A whole dashboard's JSON cannot survive a round trip through model context, so callers send- *ops*. This namespace reads current state, hands it to the pure compiler in- [[metabase.metabot.toolkit.dashboard-ops]] through MCP's error wording in- [[metabase.mcp.v2.dashboard-errors]], and passes the compiled payload to the same domain fns the- REST endpoints use ([[metabase.dashboards.write/create-dashboard!]] and- [[metabase.dashboards.write/update-dashboard!]]) — so write permission enforcement,- transactionality, and event publishing are inherited, never reimplemented."+ *ops*. This namespace reads current state and hands it, with the ops, to the shared pipeline in+ [[metabase.metabot.toolkit.dashboard-write]]. That compiles the ops and saves through+ [[metabase.dashboards.write]], so write permission enforcement, transactionality, and event+ publishing are inherited, never reimplemented. This namespace words the pipeline's errors through+ [[metabase.mcp.v2.dashboard-errors]] and projects the result for the model."(:require- [malli.error :as me]- [metabase.dashboards.write :as dashboards.write]- [metabase.lib.core :as lib]- [metabase.mcp.db :as mcp.db][metabase.mcp.v2.common :as common][metabase.mcp.v2.dashboard-errors :as dashboard-errors][metabase.mcp.v2.message :as message]@@ -30,10 +25,7 @@[metabase.mcp.v2.write :as v2.write][metabase.metabot.scope :as metabot.scope][metabase.metabot.toolkit.dashboard-op-schema :as dashboard-op-schema]- [metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]- [metabase.models.interface :as mi]- [metabase.util :as u]- [metabase.util.malli.registry :as mr]+ [metabase.metabot.toolkit.dashboard-write :as dashboard-write][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -46,67 +38,16 @@(-> (v2.resolve/resolve-and-read :model/Dashboard id-or-eid)(t2/hydrate [:dashcards :series :card] :tabs)))-(defn- fetch-cards- "The `{card-id card}` map for `ids`, behind read checks.- It omits a card the caller cannot read, so an op naming one fails the compiler's `:unknown-card` check.- That is the same error as for a card that does not exist."- [ids]- (let [ids (into #{} (filter int?) ids)]- (when (seq ids)- (into {} (for [card (mcp.db/select-by-ids :model/Card ids)- :when (mi/can-read? card)]- [(:id card) card])))))-;;; ------------------------------------------------ Response -------------------------------------------------------(defn- saved-row- "The projection row for the dashboard `id`, read back through the same hydration `get_content`- uses so both tools return byte-identical shapes."- [id]- (-> (mcp.db/select-one-by-id :model/Dashboard id)- (t2/hydrate [:dashcards :series :card] :tabs)- redaction/redact-dashboard- projections/dashboard-row))--(defn- attach-card-metadata- "Give a compiled dashcard the `:card` and `:series` card rows the projection reads. Compiled- rows carry ids only — a new one has never been hydrated, and hydration would fail on its- negative id — so the cards already fetched for the ops stand in."- [cards dc]- (cond-> dc- (and (:card_id dc) (nil? (:card dc)))- (assoc :card (get cards (:card_id dc)))-- (seq (:series dc))- (update :series (partial mapv (fn [s] (or (get cards (:id s)) s))))))--(defn- dry-run-row- "The projection row the ops would produce, built without a save. Same builder as [[saved-row]],- so a dry run and a real response have the same shape."- [dash attrs payload cards]- (-> (merge dash attrs payload)- (update :dashcards (partial mapv (partial attach-card-metadata cards)))+(defn- dashboard-row+ "The projection row for `dash`, a dashboard from [[dashboard-write/write!]], saved or not.+ It goes through the same hydration and redaction `get_content` uses, so both tools return the same shape."+ [dash]+ (-> dashredaction/redact-dashboardprojections/dashboard-row))-(defn- normalize-parameters- "Coerce the compiled `:parameters` from their JSON shape — string `type`, string enums — to the- internal keyword shape the REST endpoint's body decoding produces, so the same payload- validates and saves. A value too malformed to normalize passes through untouched and is- reported by [[validate-payload!]] instead."- [payload]- (cond-> payload- (seq (:parameters payload))- (update :parameters #(try (lib/normalize :metabase.parameters.schema/parameters %) (catch Exception _ %)))))--(defn- validate-payload!- "Reject a payload the real save would reject, so a dry run is worth trusting."- [payload]- (when-let [explanation (mr/explain dashboards.write/DashUpdates payload)]- (common/throw-teaching-error- (message/msg ["The requested ops produce an invalid dashboard: %s"]- (common/humanize-detail (me/humanize explanation))))))-;;; ------------------------------------------------- Schema -------------------------------------------------------(def ^:private dashboard-write-args-schema@@ -170,27 +111,6 @@(assoc (dashboard-attrs body):collection_id (v2.resolve/resolve-collection-id-or-personal (:collection_id body))))-(defn- apply-ops!- "Compile `ops` against `dash` and save, returning the projection row. With `validate-only?` the- would-be row is built from the compiled payload instead and nothing is written."- [dash ops attrs validate-only?]- (let [{:keys [payload cards]}- (dashboard-errors/do-with-teaching-errors- #(let [ops (dashboard-ops/prepare-ops ops)- cards (fetch-cards (dashboard-ops/referenced-card-ids dash ops))]- {:payload (normalize-parameters (dashboard-ops/compile-ops dash ops cards))- :cards cards}))]- (validate-payload! (merge attrs payload))- (if validate-only?- (dry-run-row dash attrs payload cards)- (saved-row (:id (dashboards.write/update-dashboard! (u/the-id dash) (merge attrs payload)))))))--(defn- blank-dashboard- "The empty dashboard a `validate_only` create compiles against, so a dry run writes nothing at- all — not even the dashboard row."- [attrs]- (merge {:id nil :dashcards [] :tabs [] :parameters []} attrs))-(registry/deftool dashboard-write"Create or update a dashboard and edit its layout with ordered ops. Dashboard changes and ops are savedatomically, create included. Rejected ops leave the dashboard untouched (or, for create, create nothing).@@ -222,44 +142,24 @@(v2.write/readback token-scopes [metabot.scope/agent-content-read](projections/project:dashboard :concise- (case (first dispatched)- :create- (let [[_ body] dispatched- attrs (create-attrs body)- ops (:ops body)- validate-only? (boolean (:validate_only body))]- (when (contains? body :archived)- (common/throw-teaching-error- (message/msg [(str "\"archived\" applies to method \"update\" "- "only — remove it from this create call.")])))- (cond- validate-only? (apply-ops! (blank-dashboard attrs) (or ops []) attrs true)- (seq ops) (do- ;; Compile before creating anything, so a bad op can't leave an- ;; orphaned empty dashboard behind for a retry to duplicate. A new- ;; dashboard is empty by definition, so compiling against a blank- ;; checks everything the post-create compile would.- (apply-ops! (blank-dashboard attrs) ops attrs true)- ;; ...and the two writes share a transaction, because the compile- ;; above only proves the OPS are good. Anything else that throws- ;; after the row exists — a schema check, a permission check, a- ;; failing save — would otherwise strand an empty dashboard,- ;; and every retry would add another (GHY-4501).- (t2/with-transaction [_conn]- (apply-ops! (t2/hydrate (dashboards.write/create-dashboard! attrs)- [:dashcards :series :card] :tabs)- ops {} false)))- :else (saved-row (:id (dashboards.write/create-dashboard! attrs)))))-- :update- (let [[_ id body] dispatched- dash (fetch-dashboard id)- attrs (dashboard-attrs body)- ops (:ops body)- validate-only? (boolean (:validate_only body))]- (cond- (seq ops) (apply-ops! dash ops attrs validate-only?)- validate-only? (do (validate-payload! attrs)- (dry-run-row dash attrs nil nil))- :else (saved-row (:id (dashboards.write/update-dashboard! (:id dash) attrs)))))))+ (dashboard-errors/do-with-teaching-errors+ #(dashboard-row+ (case (first dispatched)+ :create+ (let [[_ body] dispatched+ attrs (create-attrs body)]+ (when (contains? body :archived)+ (common/throw-teaching-error+ (message/msg [(str "\"archived\" applies to method \"update\" "+ "only — remove it from this create call.")])))+ (dashboard-write/write! {:attrs attrs+ :ops (:ops body)+ :validate-only? (boolean (:validate_only body))}))++ :update+ (let [[_ id body] dispatched]+ (dashboard-write/write! {:dashboard (fetch-dashboard id)+ :attrs (dashboard-attrs body)+ :ops (:ops body)+ :validate-only? (boolean (:validate_only body))}))))))nil))))diff --git a/src/metabase/metabot/toolkit/dashboard_ops.clj b/src/metabase/metabot/toolkit/dashboard_ops.cljindex 5fa7f96f14f..bfc1be2307a 100644--- a/src/metabase/metabot/toolkit/dashboard_ops.clj+++ b/src/metabase/metabot/toolkit/dashboard_ops.clj@@ -33,12 +33,15 @@;;; ------------------------------------------------- Errors -------------------------------------------------------(def errors- "Every error code [[compile-ops]] throws, mapped to the schema of the values that explain it.+ "Every error code the dashboard op tools throw, mapped to the schema of the values that explain it.+ That is [[compile-ops]], and the save checks in [[metabase.metabot.toolkit.dashboard-write]].Values from an op have the types [[dashboard-op-schema/op-schema]] gives them, or `:any` where it leaves them open.Values read from the dashboard have their types.Errors thrown while checking or applying an op also carry `:op-index` (0-based) and `:op-name` (as sent, or nil).- The `:cards-without-tab` check runs after the last op, so that error carries neither."+ The `:cards-without-tab` check runs after the last op, and `:invalid-payload` checks the whole save, sent ops or not.+ So those two errors carry neither."{:invalid-op [:map {:closed true} [:explanation :any]]+ :invalid-payload [:map {:closed true} [:explanation :any]]:duplicate-new-id [:map {:closed true} [:id :int]]:unknown-dashcard [:map {:closed true} [:dashcard-id :int]]:unknown-card [:map {:closed true} [:card-id :any]]@@ -68,7 +71,7 @@:unresolved-target [:map {:closed true} [:target :any] [:card-id :int] [:parameter-id :string]]:missing-wire-target [:map {:closed true}]})-(defn- fail!+(defn fail!"Throw the error `code` from [[errors]], with `data` holding the values that explain it.An unknown code, or data that breaks the code's schema, throws a plain `ex-info` instead.That is a bug in the caller, and no tool words it as a teaching error."diff --git a/src/metabase/metabot/toolkit/dashboard_write.clj b/src/metabase/metabot/toolkit/dashboard_write.cljnew file mode 100644index 00000000000..ac2372b451d--- /dev/null+++ b/src/metabase/metabot/toolkit/dashboard_write.clj@@ -0,0 +1,169 @@+(ns metabase.metabot.toolkit.dashboard-write+ "Compile dashboard ops and save them, for every tool that edits dashboards with ops.+ The ops are compiled by [[metabase.metabot.toolkit.dashboard-ops]].+ The saves go through [[dashboards.write/create-dashboard!]] and [[dashboards.write/update-dashboard!]],+ so write permission checks, transactions and events come with them.+ The REST endpoints have their own copies of those two functions, so the two paths can drift.+ Errors are codes from [[metabase.metabot.toolkit.dashboard-ops/errors]], and each tool words them its own way.+ Reading the dashboard, and projecting the result for the model, are left to the tool."+ (:require+ [malli.error :as me]+ [malli.util :as mut]+ [metabase.api.common :as api]+ [metabase.dashboards.write :as dashboards.write]+ [metabase.lib.core :as lib]+ [metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]+ [metabase.metabot.toolkit.db :as toolkit.db]+ [metabase.models.interface :as mi]+ [metabase.util :as u]+ [metabase.util.malli.registry :as mr]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- fetch-cards+ "The `{card-id card}` map for `ids`, behind read checks.+ It omits a card the user cannot read, so an op naming one fails the compiler's `:unknown-card` check.+ That is the same error as for a card that does not exist."+ [ids]+ (let [ids (into #{} (filter int?) ids)]+ (when (seq ids)+ (into {} (for [card (toolkit.db/cards ids)+ :when (mi/can-read? card)]+ [(:id card) card])))))++(defn- normalize-parameters+ "Coerce the compiled `:parameters` in `m` from their JSON shape to the internal keyword shape.+ That is the shape the REST endpoint's body decoding produces, so the same payload validates and saves.+ A value too malformed to normalize passes through untouched, and [[validate-write!]] reports it."+ [m]+ (cond-> m+ (seq (:parameters m))+ (update :parameters #(try (lib/normalize :metabase.parameters.schema/parameters %) (catch Exception _ %)))))++(defn- check-schema!+ "Throws `:invalid-payload` when `value` breaks `schema`."+ [schema value]+ (when-let [explanation (mr/explain schema value)]+ (dashboard-ops/fail! :invalid-payload {:explanation (me/humanize explanation)})))++(def ^:private layout-keys+ "The dashboard keys that ops edit, which `attrs` must not carry."+ #{:dashcards :parameters :tabs})++(def ^:private UpdateAttrs+ "The attributes an update takes: [[dashboards.write/DashUpdates]] without the keys ops edit, and closed."+ (as-> (mut/closed-schema (mr/resolve-schema dashboards.write/DashUpdates)) s+ (reduce mut/dissoc s layout-keys)))++(defn- validate-write!+ "Throws `:invalid-payload` when the real save would reject `attrs` or the compiled `payload`.+ A create checks `attrs` against [[dashboards.write/NewDashboard]], and saves any `payload` as an update.+ An update checks `attrs` against [[UpdateAttrs]], then checks them and the payload together, as it saves them.+ Dry runs and saves both call it, so a clean dry run is worth trusting."+ [creating? attrs payload]+ (if creating?+ (do (check-schema! dashboards.write/NewDashboard attrs)+ (when payload+ (check-schema! dashboards.write/DashUpdates payload)))+ (do (check-schema! UpdateAttrs attrs)+ (check-schema! dashboards.write/DashUpdates (merge attrs payload)))))++(defn- check-permission!+ "Throws a 403 unless the user may update `dashboard`, or create a dashboard in the collection `attrs` names.+ [[write!]] runs it once the ops' shape is checked, before anything is compiled, on a save as on a dry run.+ So both refuse a user who cannot write before saying anything about the payload."+ [dashboard attrs]+ (if dashboard+ (api/write-check dashboard)+ (api/create-check :model/Dashboard {:collection_id (:collection_id attrs)})))++(defn- blank-dashboard+ "The empty dashboard with `attrs` that a create compiles its ops against, before anything is written."+ [attrs]+ (merge {:id nil :dashcards [] :tabs [] :parameters []} attrs))++(defn- compile-write+ "Compiles `ops`, prepared, against `dash` without writing, and returns `{:payload :cards}`.+ The payload is what saving the ops would write alongside the attributes.+ The cards map holds the row of every card the payload references that the user can read, keyed by id."+ [dash ops]+ (let [cards (fetch-cards (dashboard-ops/referenced-card-ids dash ops))]+ {:payload (normalize-parameters (dashboard-ops/compile-ops dash ops cards))+ :cards cards}))++(defn- attach-card-metadata+ "Give a compiled dashcard the `:card` and `:series` card rows a saved one would be hydrated with.+ Compiled rows carry ids only, and a new one cannot be hydrated by its negative id, so the cards already fetched+ for the ops stand in."+ [cards dc]+ (cond-> dc+ (and (:card_id dc) (nil? (:card dc)))+ (assoc :card (get cards (:card_id dc)))++ (seq (:series dc))+ (update :series (partial mapv (fn [s] (or (get cards (:id s)) s))))))++(defn- dry-run-dashboard+ "The dashboard saving `attrs` and the compiled `payload` to `dash` would produce, shaped like a saved one."+ [dash attrs payload cards]+ ;; The payload's series carry ids only, so the rows the dashboard was hydrated with stand in for the stored ones.+ (let [cards (merge (into {} (comp (mapcat :series) (map (juxt :id identity))) (:dashcards dash)) cards)]+ (-> (merge dash attrs payload)+ (update :dashcards (partial mapv (partial attach-card-metadata cards))))))++(defn- hydrated+ "`dash`, a saved row, hydrated with `[:dashcards :series :card] :tabs`."+ [dash]+ (t2/hydrate dash [:dashcards :series :card] :tabs))++(defn- create-dashboard!+ "Creates a dashboard from `attrs`, saves the compiled `payload` to it when there is one, and returns it hydrated.+ Nothing is written if the save or the read back fails."+ [attrs payload]+ ;; One transaction, because anything that throws after the row exists (a permission check, a failing save) would+ ;; otherwise strand an empty dashboard, and every retry would add another (GHY-4501). The read back runs inside it+ ;; too, so a failure there doesn't report an error for a dashboard that was in fact created.+ (t2/with-transaction [_conn]+ (let [dash (dashboards.write/create-dashboard! attrs)]+ (hydrated (if payload+ (dashboards.write/update-dashboard! (:id dash) payload)+ dash)))))++(defn write!+ "Creates or updates a dashboard with the attributes `attrs` and the ops `ops`, and returns it.+ It returns a dashboard hydrated with `[:dashcards :series :card] :tabs` whether or not it saved.+ With a `dashboard`, hydrated the same way and already read-checked by the caller, it updates that one.+ Without one it creates a dashboard.+ With `validate-only?` it writes nothing, and returns the dashboard the save would produce.+ It checks write permission on the dashboard, or create permission on the collection, first after the ops' shape.+ A dry run skips the save's other permission checks, such as moving to a collection and per-field mapping checks.+ Ops edit the layout and parameters, so `attrs` must not carry `:dashcards`, `:tabs` or `:parameters`.+ A nil `ops` means none were sent; anything else must be a list of ops.+ Throws an error from [[dashboard-ops/errors]] when an op is invalid or the save would be rejected."+ [{:keys [dashboard validate-only?] :as opts}]+ ;; Prepared before any branch reads them, so a malformed `ops` fails as :invalid-op rather than crashing a `seq`+ ;; or passing as no ops.+ (let [ops (dashboard-ops/prepare-ops (:ops opts))+ attrs (:attrs opts)+ _ (check-permission! dashboard attrs)+ ;; Sent as attributes, `:dashcards` would replace the layout behind the ops' back, and `:tabs` would be+ ;; dropped silently. Compiled against the parameters in attrs, a source card in them would count as already+ ;; stored, and a dry run would skip the read check the save then fails.+ _ (when-let [ks (seq (filter (partial contains? attrs) layout-keys))]+ (dashboard-ops/fail! :invalid-payload+ {:explanation (zipmap ks (repeat ["should be edited with ops"]))}))+ dash (or dashboard (blank-dashboard attrs))+ ;; Without ops nothing is compiled, so an attribute-only save doesn't rewrite the layout.+ {:keys [payload cards]} (when (seq ops)+ (compile-write dash ops))]+ (validate-write! (nil? dashboard) attrs payload)+ (cond+ validate-only?+ (dry-run-dashboard dash attrs payload cards)++ (nil? dashboard)+ (create-dashboard! attrs payload)++ :else+ (hydrated (dashboards.write/update-dashboard! (u/the-id dashboard) (merge attrs payload))))))diff --git a/src/metabase/metabot/toolkit/db.clj b/src/metabase/metabot/toolkit/db.cljnew file mode 100644index 00000000000..08f27001cde--- /dev/null+++ b/src/metabase/metabot/toolkit/db.clj@@ -0,0 +1,10 @@+(ns metabase.metabot.toolkit.db+ "Application database queries for the Metabot toolkit."+ (:require+ [metabase.util.malli :as mu]+ [toucan2.core :as t2]))++(mu/defn cards :- [:sequential :map]+ "The cards whose ids are in `ids`."+ [ids :- [:set {:min 1} :int]]+ (t2/select :model/Card :id [:in [:auto/param ids]]))diff --git a/test/metabase/mcp/v2/tools/dashboard_test.clj b/test/metabase/mcp/v2/tools/dashboard_test.cljindex 0d013e2d02d..3a9f5523847 100644--- a/test/metabase/mcp/v2/tools/dashboard_test.clj+++ b/test/metabase/mcp/v2/tools/dashboard_test.clj@@ -9,12 +9,14 @@[clojure.test :refer :all][metabase.collections.models.collection :as collection][metabase.dashboards.write :as dashboards.write]+ [metabase.mcp.v2.dashboard-errors :as dashboard-errors][metabase.mcp.v2.message :as message][metabase.mcp.v2.registry :as registry][metabase.mcp.v2.test-util :as v2.tu];; Registers the tool the assertions below drive.[metabase.mcp.v2.tools.dashboard :as tools.dashboard][metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]+ [metabase.metabot.toolkit.dashboard-write :as dashboard-write][metabase.permissions.core :as perms][metabase.permissions.models.permissions-group :as perms-group][metabase.test :as mt]@@ -451,14 +453,19 @@(tool-error (call-tool! :crowberto v2.tu/all-scopes "dashboard_write"(wire {:method "create" :name "Sales" :archived true})))))))-;; not ^:parallel: the `!` in validate-payload! trips the kondo deftest lint+;; not ^:parallel: the `!` in write! trips the kondo deftest lint(deftest invalid-payload-error-text-test- (testing "GHY-4544: an invalid compiled payload is explained as humanized, quoted schema detail"- (let [text (try (#'tools.dashboard/validate-payload! {:parameters "nope"})- nil- (catch clojure.lang.ExceptionInfo e (ex-message e)))]- (is (str/starts-with? text "The requested ops produce an invalid dashboard: \"parameters\": "))- (is (not (str/includes? text "\n"))))))+ (testing "GHY-4544: a payload the save would reject is explained as humanized, quoted schema detail"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {}]+ (let [text (try (dashboard-errors/do-with-teaching-errors+ #(dashboard-write/write! {:dashboard (t2/hydrate dash [:dashcards :series :card] :tabs)+ :attrs {:cache_ttl 0}+ :validate-only? true}))+ nil+ (catch clojure.lang.ExceptionInfo e (ex-message e)))]+ (is (str/starts-with? text "This change would produce an invalid dashboard: \"cache_ttl\": "))+ (is (not (str/includes? text "\n"))))))))(deftest unreadable-card-is-refused-for-a-non-admin-test(testing "a card that EXISTS but the caller cannot read is refused the same way a nonexistent one is, beforediff --git a/test/metabase/metabot/toolkit/dashboard_write_test.clj b/test/metabase/metabot/toolkit/dashboard_write_test.cljnew file mode 100644index 00000000000..f5509ec5567--- /dev/null+++ b/test/metabase/metabot/toolkit/dashboard_write_test.clj@@ -0,0 +1,218 @@+(ns metabase.metabot.toolkit.dashboard-write-test+ "The shared dashboard write pipeline, called the way a Metabot tool will call it.+ The dashboard is read without MCP's id resolution, and errors are checked as data, never as MCP's wording.+ MCP's `dashboard_write` tests cover the same paths through that tool."+ (:require+ [clojure.test :refer :all]+ [metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]+ [metabase.metabot.toolkit.dashboard-write :as dashboard-write]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- hydrated+ [dashboard-id]+ (t2/hydrate (t2/select-one :model/Dashboard :id dashboard-id) [:dashcards :series :card] :tabs))++(defn- error-data+ [thunk]+ (try (thunk) nil (catch clojure.lang.ExceptionInfo e (dashboard-ops/error-data e))))++(deftest update-test+ (testing "write! saves the ops and the attributes in one update, and returns the saved dashboard hydrated"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {:name "Orders"}+ :model/Dashboard dash {:name "Sales"}]+ (is (=? {:name "Revenue"+ :dashcards [{:card_id (:id card), :card {:name "Orders"}}]}+ (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {:name "Revenue"}+ :ops [{:op "add_card" :id -1 :card_id (:id card)}]})))+ (is (=? {:name "Revenue", :dashcards [{:card_id (:id card)}]}+ (hydrated (:id dash))))))))++(deftest dry-run-test+ (testing "with validate-only? write! returns what the save would produce, shaped the same, and writes nothing"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {:name "Orders"}+ :model/Dashboard dash {:name "Sales"}]+ (is (=? {:name "Revenue"+ :dashcards [{:id -1, :card_id (:id card), :card {:name "Orders"}}]}+ (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {:name "Revenue"}+ :ops [{:op "add_card" :id -1 :card_id (:id card)}]+ :validate-only? true})))+ (is (=? {:name "Sales", :dashcards empty?}+ (hydrated (:id dash))))))))++(deftest create-test+ (testing "without a dashboard write! creates one with its ops applied"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {}]+ (let [dash (dashboard-write/write! {:attrs {:name "Created by the toolkit"}+ :ops [{:op "add_card" :id -1 :card_id (:id card)}]})]+ (try+ (is (=? {:name "Created by the toolkit", :dashcards [{:card_id (:id card)}]}+ dash))+ (finally+ (t2/delete! :model/Dashboard :id (:id dash))))))))+ (testing "an invalid op creates nothing"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [dash-name "Never created by the toolkit"]+ (is (=? {::dashboard-ops/error :invalid-op, :op-index 0}+ (error-data #(dashboard-write/write! {:attrs {:name dash-name} :ops [{:op "frobnicate"}]}))))+ (is (not (t2/exists? :model/Dashboard :name dash-name)))))))++(deftest create-read-back-failure-test+ (testing "a create whose read back fails leaves nothing behind, so the error it reports is true"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {}]+ (doseq [ops [nil [{:op "add_card" :id -1 :card_id (:id card)}]]]+ (testing (if ops "with ops" "without ops")+ (let [dash-name "Created then read back by the toolkit"]+ (mt/with-dynamic-fn-redefs [dashboard-write/hydrated+ (fn [_] (throw (ex-info "read back failed" {})))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"read back failed"+ (dashboard-write/write! {:attrs {:name dash-name}, :ops ops}))))+ (is (not (t2/exists? :model/Dashboard :name dash-name))))))))))++(deftest malformed-ops-test+ (testing "ops that are not a list fail as :invalid-op on every path, before anything reads them"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (doseq [ops [5 {}]+ validate-only? [true false]]+ (testing (pr-str ops validate-only?)+ (is (=? {::dashboard-ops/error :invalid-op}+ (error-data #(dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {:name "Revenue"}+ :ops ops+ :validate-only? validate-only?}))))))+ (is (= "Sales" (:name (hydrated (:id dash)))))))))++(deftest invalid-payload-test+ (testing "ops that compile to a payload the save would reject fail with :invalid-payload, before any write"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:parameters some?}}+ (error-data #(dashboard-write/write!+ {:dashboard (hydrated (:id dash))+ :attrs {}+ :ops [{:op "add_parameter" :parameter_id "p" :name "P" :type "not-a-type"}]}))))+ (is (empty? (:parameters (hydrated (:id dash)))))))))++(deftest unreadable-card-test+ (testing "a card the user cannot read is refused as unknown, before any write"+ (mt/with-temp [:model/Collection collection {}+ :model/Card card {:collection_id (:id collection)}+ :model/Dashboard dash {:name "Sales"}]+ (mt/with-non-admin-groups-no-collection-perms collection+ (mt/with-current-user (mt/user->id :rasta)+ (is (=? {::dashboard-ops/error :unknown-card, :card-id (:id card)}+ (error-data #(dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {}+ :ops [{:op "add_card" :id -1 :card_id (:id card)}]+ :validate-only? true})))))))))++(deftest save-without-ops-checks-what-a-dry-run-checks-test+ (testing "a save without ops refuses the attributes its dry run refuses, before any write"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (doseq [validate-only? [true false]]+ (testing (if validate-only? "dry run" "save")+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:cache_ttl some?}}+ (error-data #(dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {:cache_ttl 0}+ :validate-only? validate-only?}))))))+ (is (nil? (:cache_ttl (hydrated (:id dash))))))))+ (testing "a create is held to what creating a dashboard requires, with or without ops"+ (mt/with-current-user (mt/user->id :crowberto)+ (doseq [ops [nil [{:op "add_text" :id -1 :markdown "hi"}]]+ validate-only? [true false]]+ (testing (pr-str ops validate-only?)+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:name some?}}+ (error-data #(dashboard-write/write! {:attrs {:description "No name"}+ :ops ops+ :validate-only? validate-only?})))))+ (is (not (t2/exists? :model/Dashboard :description "No name")))))))++(deftest save-without-ops-keeps-the-layout-test+ (testing "a save without ops changes the attributes and leaves the dashcards as they are"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {}+ :model/Dashboard dash {:name "Sales"}+ :model/DashboardCard dc {:dashboard_id (:id dash), :card_id (:id card)}]+ (is (=? {:name "Revenue", :dashcards [{:id (:id dc)}]}+ (dashboard-write/write! {:dashboard (hydrated (:id dash)), :attrs {:name "Revenue"}})))))))++(deftest permission-is-checked-first-test+ (testing "a user who may not write is refused before anything else is checked, on a dry run and a save alike"+ (mt/with-temp [:model/Collection collection {}+ :model/Dashboard dash {:name "Sales", :collection_id (:id collection)}]+ (mt/with-non-admin-groups-no-collection-perms collection+ (mt/with-current-user (mt/user->id :rasta)+ (doseq [validate-only? [true false]]+ (testing (if validate-only? "dry run" "save")+ (testing "update"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."+ (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {:cache_ttl 0}+ :validate-only? validate-only?}))))+ (testing "create"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."+ (dashboard-write/write! {:attrs {:name "New"+ :collection_id (:id collection)+ :cache_ttl 0}+ :validate-only? validate-only?}))))))+ (is (=? {:name "Sales"} (hydrated (:id dash))))+ (is (not (t2/exists? :model/Dashboard :name "New"))))))))++(deftest layout-keys-in-attrs-are-refused-test+ (testing "ops edit the layout and parameters, so attributes carrying them are refused on every path"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {}+ :model/Dashboard dash {:name "Sales"}+ :model/DashboardCard dc {:dashboard_id (:id dash), :card_id (:id card)}]+ (doseq [k [:dashcards :parameters :tabs]+ dashboard [nil (hydrated (:id dash))]+ validate-only? [true false]]+ (testing (pr-str k (some? dashboard) validate-only?)+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {k some?}}+ (error-data #(dashboard-write/write! {:dashboard dashboard+ :attrs {:name "With layout", k []}+ :validate-only? validate-only?}))))))+ (is (=? {:name "Sales", :dashcards [{:id (:id dc)}]} (hydrated (:id dash))))+ (is (not (t2/exists? :model/Dashboard :name "With layout"))))))+ (testing "an update refuses an attribute it doesn't know"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:nmae some?}}+ (error-data #(dashboard-write/write! {:dashboard (hydrated (:id dash)), :attrs {:nmae "Typo"}}))))))))++(deftest ops-shape-is-checked-before-permission-test+ (testing "a malformed op fails as :invalid-op even for a user who may not write, since it needs no dashboard"+ (mt/with-temp [:model/Collection collection {}+ :model/Dashboard dash {:name "Sales", :collection_id (:id collection)}]+ (mt/with-non-admin-groups-no-collection-perms collection+ (mt/with-current-user (mt/user->id :rasta)+ (is (=? {::dashboard-ops/error :invalid-op}+ (error-data #(dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {}+ :ops [{:op "frobnicate"}]})))))))))++(deftest dry-run-keeps-stored-series-rows-test+ (testing "a dry run returns a dashcard's stored series as card rows, as the save reads them back"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card card {:name "Orders"}+ :model/Card overlay {:name "Overlay"}+ :model/Dashboard dash {:name "Sales"}+ :model/DashboardCard dc {:dashboard_id (:id dash), :card_id (:id card)}+ :model/DashboardCardSeries _ {:dashboardcard_id (:id dc), :card_id (:id overlay)}]+ (is (=? {:dashcards [{:id (:id dc), :series [{:id (:id overlay), :name "Overlay"}]}]}+ (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :attrs {}+ :ops [{:op "resize"+ :dashcard_id (:id dc)+ :size {:size_x 6 :size_y 4}}]+ :validate-only? true})))))))