Skip to content

Commit

Permalink
pr/notifier: Use async lib and escape messages (envoyproxy#30154)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Oct 12, 2023
1 parent 24efd20 commit 6f956e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
1 change: 0 additions & 1 deletion tools/base/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pygithub
pyreadline
pyyaml
setuptools
slackclient
slack_sdk
sphinx>=7
sphinxcontrib.googleanalytics
Expand Down
5 changes: 0 additions & 5 deletions tools/base/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ aiohttp==3.8.6 \
# envoy-github-abstract
# envoy-github-release
# google-auth
# slackclient
aiosignal==1.3.1 \
--hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \
--hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17
Expand Down Expand Up @@ -1204,10 +1203,6 @@ slack-sdk==3.23.0 \
--hash=sha256:2a8513505cced20ceee22b5b49c11d9545caa6234b56bf0ad47133ea5b357d10 \
--hash=sha256:9d6ebc4ff74e7983e1b27dbdb0f2bb6fc3c2a2451694686eaa2be23bbb085a73
# via -r requirements.in
slackclient==2.9.4 \
--hash=sha256:a8cab9146795e23d66a03473b80dd23df8c500829dfa9d06b3e3d5aec0a2b293 \
--hash=sha256:ab79fefb5412d0595bc01d2f195a787597f2a617b6766562932ab9ffbe5cb173
# via -r requirements.in
smmap==5.0.0 \
--hash=sha256:2aba19d6a040e78d8b09de5c57e96207b09ed71d8e55ce0959eeee6c8e190d94 \
--hash=sha256:c840e62059cd3be204b0c9c9f74be2c09d5648eddd4580d9314c3ecde0b30936
Expand Down
16 changes: 8 additions & 8 deletions tools/repo/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# NOTE: Slack IDs can be found in the user's full profile from within Slack.

import datetime
import html
import json
import os
import sys
Expand All @@ -16,7 +17,7 @@

import aiohttp

from slack_sdk import WebClient
from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.errors import SlackApiError

from aio.api import github as github
Expand Down Expand Up @@ -114,7 +115,7 @@ def should_report(self):

@cached_property
def slack_client(self):
return WebClient(token=self.slack_bot_token)
return AsyncWebClient(token=self.slack_bot_token)

@cached_property
def slack_bot_token(self):
Expand Down Expand Up @@ -278,27 +279,26 @@ async def report(self):
print(json.dumps(report))

async def send_message(self, channel, text):
# TODO(phlax): this is blocking, switch to async slack client
text = html.escape(text)
if self.dry_run:
self.log.notice(f"Slack message ({channel}):\n{text}")
return
self.slack_client.chat_postMessage(channel=channel, text=text)
await self.slack_client.chat_postMessage(channel=channel, text=text)

async def _post_to_assignees(self, assignees, messages):
# TODO(phlax): this is blocking, switch to async slack client
for name, text in messages.items():
# Only send texts if we have the slack UID
if not (uid := assignees.get(name)):
continue
message = "\n".join(text)
message = html.escape("\n".join(text))
if self.dry_run:
self.log.notice(f"Slack message ({name}):\n{message}")
continue
# Ship texts off to slack.
try:
response = self.slack_client.conversations_open(users=uid, text="hello")
response = await self.slack_client.conversations_open(users=uid, text="hello")
channel_id = response["channel"]["id"]
self.slack_client.chat_postMessage(channel=channel_id, text=message)
await self.slack_client.chat_postMessage(channel=channel_id, text=message)
except SlackApiError as e:
print(f"Unexpected error {e.response['error']}")

Expand Down

0 comments on commit 6f956e8

Please sign in to comment.