This is an automated email from the ASF dual-hosted git repository.

elizabeth pushed a commit to branch elizabeth/fix-slack-bug
in repository https://gitbox.apache.org/repos/asf/superset.git

commit a1c3d76073f213f093bf5a2fd64e1da2f7117270
Author: Elizabeth Thompson <[email protected]>
AuthorDate: Fri Jul 26 14:17:02 2024 -0700

    add slack channels to header data logs
---
 superset/commands/report/execute.py              |  10 +
 superset/utils/core.py                           |   1 +
 tests/unit_tests/commands/report/execute_test.py | 250 +++++++++++++++++++++++
 3 files changed, 261 insertions(+)

diff --git a/superset/commands/report/execute.py 
b/superset/commands/report/execute.py
index 3ec5bdfa97..b14dfa0027 100644
--- a/superset/commands/report/execute.py
+++ b/superset/commands/report/execute.py
@@ -367,6 +367,7 @@ class BaseReportState:
         chart_id = None
         dashboard_id = None
         report_source = None
+        slack_channels = None
         if self._report_schedule.chart:
             report_source = ReportSourceFormat.CHART
             chart_id = self._report_schedule.chart_id
@@ -374,6 +375,14 @@ class BaseReportState:
             report_source = ReportSourceFormat.DASHBOARD
             dashboard_id = self._report_schedule.dashboard_id
 
+        if self._report_schedule.recipients:
+            slack_channels = [
+                recipient.recipient_config_json
+                for recipient in self._report_schedule.recipients
+                if recipient.type
+                in [ReportRecipientType.SLACK, ReportRecipientType.SLACKV2]
+            ]
+
         log_data: HeaderDataType = {
             "notification_type": self._report_schedule.type,
             "notification_source": report_source,
@@ -381,6 +390,7 @@ class BaseReportState:
             "chart_id": chart_id,
             "dashboard_id": dashboard_id,
             "owners": self._report_schedule.owners,
+            "slack_channels": slack_channels,
         }
         return log_data
 
diff --git a/superset/utils/core.py b/superset/utils/core.py
index b2f09aac2d..b0099af0ac 100644
--- a/superset/utils/core.py
+++ b/superset/utils/core.py
@@ -167,6 +167,7 @@ class HeaderDataType(TypedDict):
     notification_source: str | None
     chart_id: int | None
     dashboard_id: int | None
+    slack_channels: list[str] | None
 
 
 class DatasourceDict(TypedDict):
diff --git a/tests/unit_tests/commands/report/execute_test.py 
b/tests/unit_tests/commands/report/execute_test.py
new file mode 100644
index 0000000000..33c2695df3
--- /dev/null
+++ b/tests/unit_tests/commands/report/execute_test.py
@@ -0,0 +1,250 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from superset.commands.report.execute import BaseReportState
+from superset.reports.models import (
+    ReportRecipientType,
+    ReportSchedule,
+    ReportSourceFormat,
+)
+from superset.utils.core import HeaderDataType
+
+
+def test_log_data_with_chart(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = True
+    mock_report_schedule.chart_id = 123
+    mock_report_schedule.dashboard_id = None
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = [1, 2]
+    mock_report_schedule.recipients = []
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.CHART,
+        "notification_format": "report_format",
+        "chart_id": 123,
+        "dashboard_id": None,
+        "owners": [1, 2],
+        "slack_channels": None,
+    }
+
+    # Assertions
+    assert result == expected_result
+
+
+def test_log_data_with_dashboard(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = False
+    mock_report_schedule.chart_id = None
+    mock_report_schedule.dashboard_id = 123
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = [1, 2]
+    mock_report_schedule.recipients = []
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.DASHBOARD,
+        "notification_format": "report_format",
+        "chart_id": None,
+        "dashboard_id": 123,
+        "owners": [1, 2],
+        "slack_channels": None,
+    }
+
+    # Assertions
+    assert result == expected_result
+
+
+def test_log_data_with_email_recipients(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = False
+    mock_report_schedule.chart_id = None
+    mock_report_schedule.dashboard_id = 123
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = [1, 2]
+    mock_report_schedule.recipients = []
+    mock_report_schedule.recipients = [
+        mocker.Mock(type=ReportRecipientType.EMAIL, 
recipient_config_json="email_1"),
+        mocker.Mock(type=ReportRecipientType.EMAIL, 
recipient_config_json="email_2"),
+    ]
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.DASHBOARD,
+        "notification_format": "report_format",
+        "chart_id": None,
+        "dashboard_id": 123,
+        "owners": [1, 2],
+        "slack_channels": [],
+    }
+
+    # Assertions
+    assert result == expected_result
+
+
+def test_log_data_with_slack_recipients(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = False
+    mock_report_schedule.chart_id = None
+    mock_report_schedule.dashboard_id = 123
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = [1, 2]
+    mock_report_schedule.recipients = []
+    mock_report_schedule.recipients = [
+        mocker.Mock(type=ReportRecipientType.SLACK, 
recipient_config_json="channel_1"),
+        mocker.Mock(type=ReportRecipientType.SLACK, 
recipient_config_json="channel_2"),
+    ]
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.DASHBOARD,
+        "notification_format": "report_format",
+        "chart_id": None,
+        "dashboard_id": 123,
+        "owners": [1, 2],
+        "slack_channels": ["channel_1", "channel_2"],
+    }
+
+    # Assertions
+    assert result == expected_result
+
+
+def test_log_data_no_owners(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = False
+    mock_report_schedule.chart_id = None
+    mock_report_schedule.dashboard_id = 123
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = []
+    mock_report_schedule.recipients = [
+        mocker.Mock(type=ReportRecipientType.SLACK, 
recipient_config_json="channel_1"),
+        mocker.Mock(type=ReportRecipientType.SLACK, 
recipient_config_json="channel_2"),
+    ]
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.DASHBOARD,
+        "notification_format": "report_format",
+        "chart_id": None,
+        "dashboard_id": 123,
+        "owners": [],
+        "slack_channels": ["channel_1", "channel_2"],
+    }
+
+    # Assertions
+    assert result == expected_result
+
+
+def test_log_data_with_missing_values(mocker):
+    # Mocking the report schedule
+    mock_report_schedule = mocker.Mock(spec=ReportSchedule)
+    mock_report_schedule.chart = None
+    mock_report_schedule.chart_id = None
+    mock_report_schedule.dashboard_id = None
+    mock_report_schedule.type = "report_type"
+    mock_report_schedule.report_format = "report_format"
+    mock_report_schedule.owners = [1, 2]
+    mock_report_schedule.recipients = [
+        mocker.Mock(type=ReportRecipientType.SLACK, 
recipient_config_json="channel_1"),
+        mocker.Mock(
+            type=ReportRecipientType.SLACKV2, recipient_config_json="channel_2"
+        ),
+    ]
+
+    # Initializing the class and setting the report schedule
+    class_instance = BaseReportState(
+        mock_report_schedule, "January 1, 2021", "execution_id_example"
+    )
+    class_instance._report_schedule = mock_report_schedule
+
+    # Calling the method
+    result = class_instance._get_log_data()
+
+    # Expected result
+    expected_result: HeaderDataType = {
+        "notification_type": "report_type",
+        "notification_source": ReportSourceFormat.DASHBOARD,
+        "notification_format": "report_format",
+        "chart_id": None,
+        "dashboard_id": None,
+        "owners": [1, 2],
+        "slack_channels": ["channel_1", "channel_2"],
+    }
+
+    # Assertions
+    assert result == expected_result

Reply via email to