Skip to content

Commit

Permalink
update slack pings to go to new channels
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Jan 14, 2022
1 parent 9958419 commit 724ee1f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
25 changes: 19 additions & 6 deletions api/tests/submissions/test_submission_done.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytz
from django.test import Client
from django.urls import reverse
from pytest_django.fixtures import SettingsWrapper
from rest_framework import status

from api.views.slack_helpers import client as slack_client
Expand Down Expand Up @@ -156,6 +157,7 @@ def test_done_already_completed(self, client: Client) -> None:
def test_done_random_checks(
self,
client: Client,
settings: SettingsWrapper,
probability: float,
gamma: int,
message: bool,
Expand Down Expand Up @@ -192,7 +194,10 @@ def test_done_random_checks(
assert result.status_code == status.HTTP_201_CREATED
if message:
assert (
call(channel="#transcription_check", text=slack_message)
call(
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL,
text=slack_message,
)
in slack_client.chat_postMessage.call_args_list
)
else:
Expand Down Expand Up @@ -225,7 +230,9 @@ def test_is_returning_transcriber(

assert _is_returning_transcriber(user) == expected

def test_send_transcription_to_slack(self, client: Client) -> None:
def test_send_transcription_to_slack(
self, client: Client, settings: SettingsWrapper
) -> None:
"""Verify that a new user gets a different welcome message."""
# Mock both the gamma property and the random.random function.
with patch(
Expand Down Expand Up @@ -258,7 +265,10 @@ def test_send_transcription_to_slack(self, client: Client) -> None:

assert result.status_code == status.HTTP_201_CREATED
assert (
call(channel="#transcription_check", text=first_slack_message)
call(
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL,
text=first_slack_message,
)
== slack_client.chat_postMessage.call_args_list[0]
)
submission.refresh_from_db()
Expand All @@ -275,7 +285,10 @@ def test_send_transcription_to_slack(self, client: Client) -> None:
)
assert result.status_code == status.HTTP_201_CREATED
assert (
call(channel="#transcription_check", text=second_slack_message)
call(
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL,
text=second_slack_message,
)
== slack_client.chat_postMessage.call_args_list[-1]
)

Expand Down Expand Up @@ -324,7 +337,7 @@ def test_removed_transcription_changes(self, client: Client) -> None:
assert result.status_code == status.HTTP_201_CREATED
assert len(slack_client.chat_postMessage.call_args_list) == 0

def test_check_for_rank_up(self, client: Client) -> None:
def test_check_for_rank_up(self, client: Client, settings: SettingsWrapper) -> None:
"""Verify that a slack message fires when a volunteer ranks up."""
client, headers, user = setup_user_client(client)
for iteration in range(24):
Expand All @@ -351,7 +364,7 @@ def test_check_for_rank_up(self, client: Client) -> None:
f" {submission.tor_url}"
)
assert (
call(channel="#new_volunteers_meta", text=slack_message)
call(channel=settings.SLACK_RANK_UP_CHANNEL, text=slack_message)
== slack_client.chat_postMessage.call_args_list[0]
)

Expand Down
39 changes: 37 additions & 2 deletions api/views/slack_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,44 @@

logger = logging.getLogger("api.views.slack_helpers")


class DictWithNoKeyError(dict):
def __getitem__(self, item: Any) -> Any:
# make it so that mydict['unknown_value'] returns None instead of KeyError.
result = self.get(item, None)
if not result:
logging.warning(f"Requested nonexistent key {item} - is Slack initialized?")
return result


rooms_list = DictWithNoKeyError()

if settings.ENABLE_SLACK is True:
try:
client = slack.WebClient(token=os.environ["SLACK_API_KEY"]) # pragma: no cover
# Define the list of rooms (useful to retrieve the ID of the rooms, knowing their
# name)
rooms = client.conversations_list()
for room in rooms["channels"]:
rooms_list[room["id"]] = room["name"]
rooms_list[room["name"]] = room["id"]

# Now that we have an understanding of what channels are available and what the
# actual IDs are, we can't send a message to a named channel anymore (e.g.
# "dev_test") -- it's gotta go to the internal Slack channel ID. So now we'll
# redefine DEFAULT_CHANNEL to be the internal slack ID version.
# Can't make this an easy loop because lazysettings doesn't want to play along.
settings.SLACK_GITHUB_SPONSORS_CHANNEL = rooms_list[
settings.SLACK_GITHUB_SPONSORS_CHANNEL
]
settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL = rooms_list[
settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL
]
settings.SLACK_REPORTED_POST_CHANNEL = rooms_list[
settings.SLACK_REPORTED_POST_CHANNEL
]
settings.SLACK_RANK_UP_CHANNEL = rooms_list[settings.SLACK_RANK_UP_CHANNEL]

except KeyError:
raise ConfigurationError(
"ENABLE_SLACK is set to True, but no API key was found. Set the"
Expand Down Expand Up @@ -121,7 +156,7 @@ def send_github_sponsors_message(data: Dict, action: str) -> None:
msg = i18n["slack"]["github_sponsor_update"].format(
emote, action, username, sponsorlevel
)
client.chat_postMessage(channel="org_running", text=msg)
client.chat_postMessage(channel=settings.SLACK_GITHUB_SPONSORS_CHANNEL, text=msg)


def process_submission_update(data: dict) -> None:
Expand Down Expand Up @@ -494,7 +529,7 @@ def _send_transcription_to_slack(

try:
slack_client.chat_postMessage(
channel="#transcription_check", text=msg,
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL, text=msg,
)
except: # noqa
logger.warning(f"Cannot post message to slack. Msg: {msg}")
2 changes: 1 addition & 1 deletion api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _check_for_rank_up(user: BlossomUser, submission: Submission = None) -> None
f" {submission.tor_url}"
)
try:
slack.chat_postMessage(channel="#new_volunteers_meta", text=msg)
slack.chat_postMessage(channel=settings.SLACK_RANK_UP_CHANNEL, text=msg)
except: # noqa
logger.warning(f"Cannot post message to slack. Msg: {msg}")
pass
Expand Down
2 changes: 1 addition & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def ask_about_removing_post(request: HttpRequest, submission: Submission) -> Non
submission.id
)
log.info(f"Sending message to Slack to ask about removing {submission.id}")
client.chat_postMessage(channel="reported_posts", blocks=blocks)
client.chat_postMessage(channel=settings.SLACK_REPORTED_POST_CHANNEL, blocks=blocks)


@login_required
Expand Down
11 changes: 11 additions & 0 deletions blossom/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@
"social_core.pipeline.social_auth.load_extra_data",
"social_core.pipeline.user.user_details",
)
SLACK_DEFAULT_CHANNEL = os.environ.get("SLACK_DEFAULT_CHANNEL", "dev_alerts")
SLACK_GITHUB_SPONSORS_CHANNEL = os.environ.get(
"SLACK_GITHUB_SPONSORS_CHANNEL", "admin_general"
)
SLACK_TRANSCRIPTION_CHECK_CHANNEL = os.environ.get(
"SLACK_TRANSCRIPTION_CHECK_CHANNEL", "qa_transcription_check_pings"
)
SLACK_REPORTED_POST_CHANNEL = os.environ.get(
"SLACK_REPORTED_POST_CHANNEL", "qa_reported_posts"
)
SLACK_RANK_UP_CHANNEL = os.environ.get("SLACK_RANK_UP_CHANNEL", "general")

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

Expand Down
2 changes: 1 addition & 1 deletion utils/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _worker() -> None:
message = f"Background worker exception: ```{details}```"
if settings.ENABLE_SLACK:
client.chat_postMessage(
channel="botstuffs", text=message,
channel=settings.SLACK_DEFAULT_CHANNEL, text=message,
)
log.error(message)
finally:
Expand Down

0 comments on commit 724ee1f

Please sign in to comment.