|
| 1 | +"""Utility functions for Slack integration.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | + |
| 6 | +from slack_sdk import WebClient |
| 7 | + |
| 8 | +GITHUB_URL_PREFIX = 'https://github.com/datacebo/download-analytics/actions/runs/' |
| 9 | +DEFAULT_SLACK_CHANNEL = 'sdv-alerts-debug' |
| 10 | + |
| 11 | + |
| 12 | +def _get_slack_client(): |
| 13 | + """Create an authenticated Slack client. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + WebClient: |
| 17 | + An authenticated Slack WebClient instance. |
| 18 | + """ |
| 19 | + token = os.getenv('SLACK_TOKEN') |
| 20 | + client = WebClient(token=token) |
| 21 | + return client |
| 22 | + |
| 23 | + |
| 24 | +def post_slack_message(channel, text): |
| 25 | + """Post a message to a Slack channel. |
| 26 | +
|
| 27 | + Args: |
| 28 | + channel (str): |
| 29 | + The name of the channel to post to. |
| 30 | + text (str): |
| 31 | + The message to send to the channel. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + SlackResponse: |
| 35 | + Response from Slack API call |
| 36 | + """ |
| 37 | + client = _get_slack_client() |
| 38 | + response = client.chat_postMessage(channel=channel, text=text) |
| 39 | + if not response['ok']: |
| 40 | + error = response.get('error', 'unknown_error') |
| 41 | + msg = f'{error} occured trying to post message to {channel}' |
| 42 | + raise RuntimeError(msg) |
| 43 | + |
| 44 | + return response |
| 45 | + |
| 46 | + |
| 47 | +def post_slack_message_in_thread(channel, text, thread_ts): |
| 48 | + """Post a message as a threaded reply in a Slack channel. |
| 49 | +
|
| 50 | + Args: |
| 51 | + channel (str): |
| 52 | + The name of the channel to post to. |
| 53 | + text (str): |
| 54 | + The message to send as a reply in the thread. |
| 55 | + thread_ts (str): |
| 56 | + The timestamp of the message that starts the thread. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + SlackResponse: |
| 60 | + Response from Slack API call. |
| 61 | + """ |
| 62 | + client = _get_slack_client() |
| 63 | + response = client.chat_postMessage(channel=channel, text=text, thread_ts=thread_ts) |
| 64 | + if not response['ok']: |
| 65 | + error = response.get('error', 'unknown_error') |
| 66 | + msg = f'{error} occurred trying to post threaded message to {channel}' |
| 67 | + raise RuntimeError(msg) |
| 68 | + |
| 69 | + return response |
| 70 | + |
| 71 | + |
| 72 | +def send_alert(args): |
| 73 | + """Send an alert message to a slack channel.""" |
| 74 | + url = GITHUB_URL_PREFIX + args.run_id |
| 75 | + message = f'Download Analytics build failed :fire: :dumpster-fire: :fire: See errors <{url}|here>' |
| 76 | + post_slack_message(args.channel, message) |
| 77 | + |
| 78 | + |
| 79 | +def get_parser(): |
| 80 | + """Get the parser.""" |
| 81 | + parser = argparse.ArgumentParser(description='Function to alert when a Github workflow fails.') |
| 82 | + parser.add_argument('-r', '--run-id', type=str, help='The id of the github run.') |
| 83 | + parser.add_argument( |
| 84 | + '-c', |
| 85 | + '--channel', |
| 86 | + type=str, |
| 87 | + help='The slack channel to post to.', |
| 88 | + default=DEFAULT_SLACK_CHANNEL, |
| 89 | + ) |
| 90 | + parser.set_defaults(action=send_alert) |
| 91 | + |
| 92 | + return parser |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + parser = get_parser() |
| 97 | + args = parser.parse_args() |
| 98 | + args.action(args) |
0 commit comments