Skip to content

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

Keep the schedule day in Metabot's Slack alerts and subscriptions (#83043)

Andrei Simionescu120f637fe84b
diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.clj
index 8d2f96f0e26..be4e2d07f47 100644
--- a/src/metabase/metabot/tools/util.clj
+++ b/src/metabase/metabot/tools/util.clj
@@ -133,17 +133,22 @@
(defn schedule->schedule-map
"Convert a tool schedule map to the schedule-map format used by cron and pulse channels.
+ Keys can be snake_case, as the tool schemas define them, or kebab-case.
E.g. {:frequency :daily :hour 9} => {:schedule_type \"daily\" :schedule_hour 9 ...}"
- [{:keys [frequency hour day-of-week day-of-month]}]
- {:schedule_type (name frequency)
- :schedule_hour hour
- :schedule_day (or (some-> day-of-week name (subs 0 3) u/lower-case-en)
- (some->> day-of-month
- name
- u/lower-case-en
- (re-find #"^(?:first|last)-(mon|tue|wed|thu|fri|sat|sun)")
- second))
- :schedule_frame (some->> day-of-month name (re-find #"^(?:first|mid|last)"))})
+ [schedule]
+ (let [{:keys [frequency hour day-of-week day-of-month]} (u/normalize-map schedule)]
+ {:schedule_type (name frequency)
+ :schedule_hour hour
+ :schedule_day (or (some->> day-of-week
+ name
+ u/lower-case-en
+ (re-find #"^(?:mon|tue|wed|thu|fri|sat|sun)"))
+ (some->> day-of-month
+ name
+ u/lower-case-en
+ (re-find #"^(?:first|last)-(mon|tue|wed|thu|fri|sat|sun)")
+ second))
+ :schedule_frame (some->> day-of-month name (re-find #"^(?:first|mid|last)"))}))
(defn get-database
"Get the `fields` of the database with ID `id`."
diff --git a/test/metabase/metabot/tools/util_test.clj b/test/metabase/metabot/tools/util_test.clj
index 91498f3bacd..02a2511bc87 100644
--- a/test/metabase/metabot/tools/util_test.clj
+++ b/test/metabase/metabot/tools/util_test.clj
@@ -84,6 +84,15 @@
:hour 12
:day-of-month :mid})))))
+(deftest ^:parallel schedule->schedule-map-drops-unknown-days-test
+ (is (= [nil nil]
+ (for [day ["" "mo"]]
+ (:schedule_day
+ (metabot.tools.util/schedule->schedule-map
+ {:frequency :daily
+ :hour 9
+ :day-of-week day}))))))
+
(deftest metabot-scope-query-test
(testing "metabot-scope-query with collection hierarchy"
(mt/dataset test-data
diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.clj
index ce83a6f0005..4309fe2f3a9 100644
--- a/test/metabase/metabot/tools_test.clj
+++ b/test/metabase/metabot/tools_test.clj
@@ -2,6 +2,7 @@
(:require
[clojure.test :refer :all]
[metabase.api-scope.core :as api-scope]
+ [metabase.channel.settings :as channel.settings]
[metabase.entity-retrieval.core :as entity-retrieval]
[metabase.metabot.agent.profiles :as profiles]
[metabase.metabot.scope :as scope]
@@ -9,7 +10,9 @@
[metabase.metabot.tools.charts.create :as create-chart-tools]
[metabase.metabot.tools.construct :as construct]
[metabase.metabot.tools.shared :as shared]
- [metabase.test :as mt]))
+ [metabase.notification.models :as models.notification]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
(deftest all-tools-test
(testing "profile tools are vars with required metadata"
@@ -278,3 +281,27 @@
[_:=> [_:cat params] _out] schema]
(is (not-any? #(= :queries_state (first %)) (rest params)))
(is (not-any? #(= :charts_state (first %)) (rest params))))))
+
+(deftest slackbot-schedules-keep-their-day-test
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames
+ (constantly {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})]
+ (mt/with-model-cleanup [:model/Notification :model/Pulse]
+ (mt/with-temp [:model/Card {card-id :id} {}
+ :model/Dashboard {dash-id :id} {}]
+ (binding [shared/*memory-atom* (atom {:context {:slack_channel_id "C123"}})]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [schedule [{:frequency "weekly" :day_of_week "monday" :hour 9}
+ {:frequency "monthly" :day_of_month "first-monday" :hour 9}]]
+ (agent-tools/create-alert-tool {:card_id card-id :send_condition "has_result" :schedule schedule})
+ (agent-tools/slackbot-create-dashboard-subscription-tool {:dashboard_id dash-id :schedule schedule}))))
+ (testing "alerts"
+ (is (= #{"0 0 9 ? * 2 *" "0 0 9 ? * 2#1 *"}
+ (->> (models.notification/notifications-for-card card-id)
+ (mapcat :subscriptions)
+ (into #{} (map :cron_schedule))))))
+ (testing "dashboard subscriptions"
+ (is (= #{[:weekly "mon" nil] [:monthly "mon" :first]}
+ (->> (t2/hydrate (t2/select :model/Pulse :dashboard_id dash-id) :channels)
+ (mapcat :channels)
+ (into #{} (map (juxt :schedule_type :schedule_day :schedule_frame)))))))))))