From 1ec1b61a451df14f59803dff89d3095e7974547e Mon Sep 17 00:00:00 2001
From: bryan <bryan.maass@gmail.com>
Date: Thu, 19 Oct 2023 15:48:31 -0600
Subject: [PATCH] PoC for explicitization of dynamic vars (#34030)

* PoC for explicitization of dynamic vars

* deref the current-user delay

* missed one

* fix flaky log test

* create-card! takes a card as first arg
---
 src/metabase/api/card.clj        | 15 ++++++++-------
 src/metabase/api/dashboard.clj   |  1 +
 test/metabase/util/log_test.cljc |  8 ++++++--
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/metabase/api/card.clj b/src/metabase/api/card.clj
index 5c884787af8..77994e3300f 100644
--- a/src/metabase/api/card.clj
+++ b/src/metabase/api/card.clj
@@ -516,14 +516,14 @@ saved later when it is ready."
   transaction and work in the `:card-create` event cannot proceed because the cards would not be visible outside of
   the transaction yet. If you pass true here it is important to call the event after the cards are successfully
   created."
-  ([card] (create-card! card false))
-  ([{:keys [dataset_query result_metadata dataset parameters parameter_mappings], :as card-data} delay-event?]
+  ([card creator] (create-card! card creator false))
+  ([{:keys [dataset_query result_metadata dataset parameters parameter_mappings], :as card-data} creator delay-event?]
    ;; `zipmap` instead of `select-keys` because we want to get `nil` values for keys that aren't present. Required by
    ;; `api/maybe-reconcile-collection-position!`
    (let [data-keys            [:dataset_query :description :display :name :visualization_settings
                                :parameters :parameter_mappings :collection_id :collection_position :cache_ttl]
          card-data            (assoc (zipmap data-keys (map card-data data-keys))
-                                     :creator_id api/*current-user-id*
+                                     :creator_id (:id creator)
                                      :dataset (boolean (:dataset card-data))
                                      :parameters (or parameters [])
                                      :parameter_mappings (or parameter_mappings []))
@@ -553,7 +553,7 @@ saved later when it is ready."
                            :average_query_time
                            :last_query_start
                            :collection [:moderation_reviews :moderator_details])
-                  (assoc :last-edit-info (last-edit/edit-information-for-user @api/*current-user*)))
+                  (assoc :last-edit-info (last-edit/edit-information-for-user creator)))
        (when timed-out?
          (schedule-metadata-saving result-metadata-chan <>))))))
 
@@ -577,7 +577,7 @@ saved later when it is ready."
   (check-data-permissions-for-query dataset_query)
   ;; check that we have permissions for the collection we're trying to save this card to, if applicable
   (collection/check-write-perms-for-collection collection_id)
-  (create-card! body))
+  (create-card! body @api/*current-user*))
 
 (api/defendpoint POST "/:id/copy"
   "Copy a `Card`, with the new name 'Copy of _name_'"
@@ -586,7 +586,7 @@ saved later when it is ready."
   (let [orig-card (api/read-check Card id)
         new-name  (str (trs "Copy of ") (:name orig-card))
         new-card  (assoc orig-card :name new-name)]
-    (create-card! new-card)))
+    (create-card! new-card @api/*current-user*)))
 
 
 ;;; ------------------------------------------------- Updating Cards -------------------------------------------------
@@ -1232,7 +1232,8 @@ saved later when it is ready."
                                                        :type     :query}
                               :display                :table
                               :name                   (humanization/name->human-readable-name filename-prefix)
-                              :visualization_settings {}})
+                              :visualization_settings {}}
+                             @api/*current-user*)
           upload-seconds    (/ (- (System/currentTimeMillis) start-time)
                                1000.0)]
       (snowplow/track-event! ::snowplow/csv-upload-successful
diff --git a/src/metabase/api/dashboard.clj b/src/metabase/api/dashboard.clj
index 1f9ad41fc6d..5b40a85ac2a 100644
--- a/src/metabase/api/dashboard.clj
+++ b/src/metabase/api/dashboard.clj
@@ -273,6 +273,7 @@
                            (cond-> (assoc card :collection_id dest-coll-id)
                              same-collection?
                              (update :name #(str % " - " (tru "Duplicate"))))
+                           @api/*current-user*
                            ;; creating cards from a transaction. wait until tx complete to signal event
                            true))))
             {:copied {}
diff --git a/test/metabase/util/log_test.cljc b/test/metabase/util/log_test.cljc
index 4c881b05410..848e4770953 100644
--- a/test/metabase/util/log_test.cljc
+++ b/test/metabase/util/log_test.cljc
@@ -19,7 +19,8 @@
            (log/info :keyword 78)))))
 
 (deftest logp-levels-test
-  (let [spam (fn []
+  (let [important-message #{"fatal" "error" "warn" "info" "debug" "trace"}
+        spam (fn []
                (log/fatal "fatal")
                (log/error "error")
                (log/warn  "warn")
@@ -32,7 +33,10 @@
               [:info  nil "info"]
               [:debug nil "debug"]
               [:trace nil "trace"]]]
-    (are [prefix level] (= (take prefix logs) (tlog/with-log-messages-for-level level (spam)))
+    (are [prefix level] (= (->> logs
+                                (filter (fn [[_ _ msg]] (contains? important-message msg)))
+                                (take prefix))
+                           (tlog/with-log-messages-for-level level (spam)))
          ;0 :off - this doesn't work in CLJ and perhaps should?
          1 :fatal
          2 :error
-- 
GitLab