Skip to content

Commit 2c7d8dc

Browse files
committed
Addressing PR feedback
1 parent 802c0aa commit 2c7d8dc

File tree

9 files changed

+52
-50
lines changed

9 files changed

+52
-50
lines changed

superset/commands/report/execute.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
)
7373
from superset.tasks.utils import get_executor
7474
from superset.utils import json
75-
from superset.utils.core import get_recipients_list, HeaderDataType, override_user
75+
from superset.utils.core import HeaderDataType, override_user, recipients_string_to_list
7676
from superset.utils.csv import get_chart_csv_data, get_chart_dataframe
7777
from superset.utils.decorators import logs_context, transaction
7878
from superset.utils.pdf import build_pdf_from_screenshots
@@ -137,18 +137,19 @@ def update_report_schedule_slack_v2(self) -> None:
137137
if recipient.type == ReportRecipientType.SLACK:
138138
recipient.type = ReportRecipientType.SLACKV2
139139
slack_recipients = json.loads(recipient.recipient_config_json)
140+
# V1 method allowed to use leading `#` in the channel name
141+
channel_names = (slack_recipients["target"] or "").replace("#", "")
140142
# we need to ensure that existing reports can also fetch
141143
# ids from private channels
142-
channels_list = get_recipients_list(slack_recipients["target"])
143-
channels_list = [channel.lstrip("#") for channel in channels_list]
144144
channels = get_channels_with_search(
145-
search_string=channels_list,
145+
search_string=channel_names,
146146
types=[
147147
SlackChannelTypes.PRIVATE,
148148
SlackChannelTypes.PUBLIC,
149149
],
150150
exact_match=True,
151151
)
152+
channels_list = recipients_string_to_list(channel_names)
152153
if len(channels_list) != len(channels):
153154
missing_channels = set(channels_list) - {
154155
channel["name"] for channel in channels
@@ -158,7 +159,7 @@ def update_report_schedule_slack_v2(self) -> None:
158159
f"{', '.join(missing_channels)}"
159160
)
160161
raise UpdateFailedError(msg)
161-
channel_ids = ", ".join(channel["id"] for channel in channels)
162+
channel_ids = ",".join(channel["id"] for channel in channels)
162163
recipient.recipient_config_json = json.dumps(
163164
{
164165
"target": channel_ids,
@@ -168,7 +169,7 @@ def update_report_schedule_slack_v2(self) -> None:
168169
# Revert to v1 to preserve configuration (requires manual fix)
169170
recipient.type = ReportRecipientType.SLACK
170171
msg = f"Failed to update slack recipients to v2: {str(ex)}"
171-
logger.error(msg, exc_info=True)
172+
logger.exception(msg)
172173
raise UpdateFailedError(msg) from ex
173174

174175
def create_log(self, error_message: Optional[str] = None) -> None:
@@ -568,33 +569,32 @@ def _send(
568569
for recipient in recipients:
569570
notification = create_notification(recipient, notification_content)
570571
try:
571-
if app.config["ALERT_REPORTS_NOTIFICATION_DRY_RUN"]:
572+
try:
573+
if app.config["ALERT_REPORTS_NOTIFICATION_DRY_RUN"]:
574+
logger.info(
575+
"Would send notification for alert %s, to %s. "
576+
"ALERT_REPORTS_NOTIFICATION_DRY_RUN is enabled, "
577+
"set it to False to send notifications.",
578+
self._report_schedule.name,
579+
recipient.recipient_config_json,
580+
)
581+
else:
582+
notification.send()
583+
except SlackV1NotificationError as ex:
584+
# The slack notification should be sent with the v2 api
572585
logger.info(
573-
"Would send notification for alert %s, to %s. "
574-
"ALERT_REPORTS_NOTIFICATION_DRY_RUN is enabled, "
575-
"set it to False to send notifications.",
576-
self._report_schedule.name,
577-
recipient.recipient_config_json,
586+
"Attempting to upgrade the report to Slackv2: %s", str(ex)
578587
)
579-
else:
580-
notification.send()
581-
except SlackV1NotificationError as ex:
582-
# The slack notification should be sent with the v2 api
583-
logger.info("Attempting to upgrade the report to Slackv2: %s", str(ex))
584-
try:
585588
self.update_report_schedule_slack_v2()
586589
recipient.type = ReportRecipientType.SLACKV2
587590
notification = create_notification(recipient, notification_content)
588591
notification.send()
589-
except (UpdateFailedError, NotificationParamException) as mig_err:
590-
notification_errors.append(
591-
SupersetError(
592-
message=mig_err.message,
593-
error_type=SupersetErrorType.REPORT_NOTIFICATION_ERROR,
594-
level=ErrorLevel.ERROR,
595-
)
596-
)
597-
except (NotificationError, SupersetException) as ex:
592+
except (
593+
UpdateFailedError,
594+
NotificationParamException,
595+
NotificationError,
596+
SupersetException,
597+
) as ex:
598598
# collect errors but keep processing them
599599
notification_errors.append(
600600
SupersetError(

superset/reports/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def slack_channels(self, **kwargs: Any) -> Response:
574574
"""
575575
try:
576576
params = kwargs.get("rison", {})
577-
search_string = [params.get("search_string", "").strip()]
577+
search_string = params.get("search_string")
578578
types = params.get("types", [])
579579
exact_match = params.get("exact_match", False)
580580
channels = get_channels_with_search(

superset/reports/notifications/slack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444
from superset.reports.notifications.slack_mixin import SlackMixin
4545
from superset.utils import json
46-
from superset.utils.core import get_recipients_list
46+
from superset.utils.core import recipients_string_to_list
4747
from superset.utils.decorators import statsd_gauge
4848
from superset.utils.slack import (
4949
get_slack_client,
@@ -70,7 +70,7 @@ def _get_channel(self) -> str:
7070
"""
7171
recipient_str = json.loads(self._recipient.recipient_config_json)["target"]
7272

73-
return ",".join(get_recipients_list(recipient_str))
73+
return ",".join(recipients_string_to_list(recipient_str))
7474

7575
def _get_inline_files(
7676
self,

superset/reports/notifications/slackv2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
)
4343
from superset.reports.notifications.slack_mixin import SlackMixin
4444
from superset.utils import json
45-
from superset.utils.core import get_recipients_list
45+
from superset.utils.core import recipients_string_to_list
4646
from superset.utils.decorators import statsd_gauge
4747
from superset.utils.slack import get_slack_client
4848

@@ -64,7 +64,7 @@ def _get_channels(self) -> List[str]:
6464
""" # noqa: E501
6565
recipient_str = json.loads(self._recipient.recipient_config_json)["target"]
6666

67-
return get_recipients_list(recipient_str)
67+
return recipients_string_to_list(recipient_str)
6868

6969
def _get_inline_files(
7070
self,

superset/utils/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def send_email_smtp( # pylint: disable=invalid-name,too-many-arguments,too-many
700700
'[email protected]', 'foo', '<b>Foo</b> bar',['/dev/null'], dryrun=True)
701701
"""
702702
smtp_mail_from = config["SMTP_MAIL_FROM"]
703-
smtp_mail_to = get_recipients_list(to)
703+
smtp_mail_to = recipients_string_to_list(to)
704704

705705
msg = MIMEMultipart(mime_subtype)
706706
msg["Subject"] = subject
@@ -711,14 +711,14 @@ def send_email_smtp( # pylint: disable=invalid-name,too-many-arguments,too-many
711711

712712
recipients = smtp_mail_to
713713
if cc:
714-
smtp_mail_cc = get_recipients_list(cc)
714+
smtp_mail_cc = recipients_string_to_list(cc)
715715
msg["Cc"] = ", ".join(smtp_mail_cc)
716716
recipients = recipients + smtp_mail_cc
717717

718718
smtp_mail_bcc = []
719719
if bcc:
720720
# don't add bcc in header
721-
smtp_mail_bcc = get_recipients_list(bcc)
721+
smtp_mail_bcc = recipients_string_to_list(bcc)
722722
recipients = recipients + smtp_mail_bcc
723723

724724
msg["Date"] = formatdate(localtime=True)
@@ -811,7 +811,7 @@ def send_mime_email(
811811
smtp.quit()
812812

813813

814-
def get_recipients_list(address_string: str | None) -> list[str]:
814+
def recipients_string_to_list(address_string: str | None) -> list[str]:
815815
"""
816816
Returns the list of target recipients for alerts and reports.
817817

superset/utils/slack.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from superset.exceptions import SupersetException
2828
from superset.reports.schemas import SlackChannelSchema
2929
from superset.utils.backports import StrEnum
30+
from superset.utils.core import recipients_string_to_list
3031

3132
logger = logging.getLogger(__name__)
3233

@@ -48,7 +49,7 @@ def get_slack_client() -> WebClient:
4849

4950

5051
def get_channels_with_search(
51-
search_string: list[str] | None = None,
52+
search_string: str = "",
5253
limit: int = 999,
5354
types: Optional[list[SlackChannelTypes]] = None,
5455
exact_match: bool = False,
@@ -80,6 +81,7 @@ def get_channels_with_search(
8081

8182
# The search string can be multiple channels separated by commas
8283
if search_string:
84+
search_array = recipients_string_to_list(search_string)
8385
channels = [
8486
channel
8587
for channel in channels
@@ -93,7 +95,7 @@ def get_channels_with_search(
9395
or search.lower() in channel["id"].lower()
9496
)
9597
)
96-
for search in search_string
98+
for search in search_array
9799
)
98100
]
99101
return channels

tests/integration_tests/utils_tests.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
GenericDataType,
5353
get_form_data_token,
5454
as_list,
55-
get_recipients_list,
55+
recipients_string_to_list,
5656
get_stacktrace,
5757
merge_extra_filters,
5858
merge_extra_form_data,
@@ -809,12 +809,12 @@ def test_ssl_certificate_file_creation(self):
809809
assert expected_filename in path
810810
assert os.path.exists(path)
811811

812-
def test_get_recipients_list(self):
813-
assert get_recipients_list("a@a") == ["a@a"]
814-
assert get_recipients_list(" a@a ") == ["a@a"]
815-
assert get_recipients_list("a@a\n") == ["a@a"]
816-
assert get_recipients_list(",a@a;") == ["a@a"]
817-
assert get_recipients_list(",a@a; b@b c@c a-c@c; d@d, f@f") == [
812+
def test_recipients_string_to_list(self):
813+
assert recipients_string_to_list("a@a") == ["a@a"]
814+
assert recipients_string_to_list(" a@a ") == ["a@a"]
815+
assert recipients_string_to_list("a@a\n") == ["a@a"]
816+
assert recipients_string_to_list(",a@a;") == ["a@a"]
817+
assert recipients_string_to_list(",a@a; b@b c@c a-c@c; d@d, f@f") == [
818818
"a@a",
819819
"b@b",
820820
"c@c",

tests/unit_tests/commands/report/execute_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def test_update_recipient_to_slack_v2(mocker: MockerFixture):
521521

522522
assert (
523523
mock_cmmd._report_schedule.recipients[0].recipient_config_json
524-
== '{"target": "abc124f, blah_!channel_2"}'
524+
== '{"target": "abc124f,blah_!channel_2"}'
525525
)
526526
assert mock_cmmd._report_schedule.recipients[0].type == ReportRecipientType.SLACKV2
527527

tests/unit_tests/utils/slack_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_handle_empty_search_string(self, mocker):
6060
mock_client.conversations_list.return_value = mock_response_instance
6161
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
6262

63-
result = get_channels_with_search(search_string=[])
63+
result = get_channels_with_search(search_string="")
6464
assert result == [{"name": "general", "id": "C12345"}]
6565

6666
def test_handle_exact_match_search_string_single_channel(self, mocker):
@@ -81,7 +81,7 @@ def test_handle_exact_match_search_string_single_channel(self, mocker):
8181
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
8282

8383
# Call the function with a search string that matches a single channel
84-
result = get_channels_with_search(search_string=["general"], exact_match=True)
84+
result = get_channels_with_search(search_string="general", exact_match=True)
8585

8686
# Assert that the result is a list with a single channel dictionary
8787
assert result == [{"name": "general", "id": "C12345"}]
@@ -102,7 +102,7 @@ def test_handle_exact_match_search_string_multiple_channels(self, mocker):
102102
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
103103

104104
result = get_channels_with_search(
105-
search_string=["general", "random"], exact_match=True
105+
search_string="general,random", exact_match=True
106106
)
107107
assert result == [
108108
{"name": "general", "id": "C12345"},
@@ -124,7 +124,7 @@ def test_handle_loose_match_search_string_multiple_channels(self, mocker):
124124
mock_client.conversations_list.return_value = mock_response_instance
125125
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
126126

127-
result = get_channels_with_search(search_string=["general", "random"])
127+
result = get_channels_with_search(search_string="general,random")
128128
assert result == [
129129
{"name": "general", "id": "C12345"},
130130
{"name": "general2", "id": "C13454"},

0 commit comments

Comments
 (0)