Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add threaded message on deploy completion #6467

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/commcare_cloud/commands/deploy/slack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from enum import Enum
from datetime import datetime

import random
import requests
import re
from clint.textui import puts
from requests import RequestException

Expand All @@ -13,6 +16,10 @@ class Emoji(Enum):
success_reaction = 'white_check_mark'
failure_reaction = 'x'

slow_reaction = random.choice(['snail', 'turtle', 'tortoise_wag'])
medium_reaction = random.choice(['meh', 'meh_blue', 'cat-roomba'])
fast_reaction = random.choice(['racing_car', 'zap', 'dash', 'rocket', 'cat-roomba-exceptionally-fast'])
Comment on lines +19 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


@property
def code(self):
return f":{self.value}:"
Expand Down Expand Up @@ -78,6 +85,23 @@ def send_deploy_end_message(self, context, is_success):
reaction_emoji = Emoji.success_reaction if is_success else Emoji.failure_reaction
self._post_reaction(thread_ts, reaction_emoji)

end = datetime.utcnow()
duration = end - context.start_time

if is_success:
if duration.seconds < 60 * 15:
speed_emoji = Emoji.fast_reaction
elif duration.seconds > 60 * 30:
speed_emoji = Emoji.slow_reaction
else:
speed_emoji = Emoji.medium_reaction
self._post_reaction(thread_ts, speed_emoji)

status = "completed" if is_success else "failed"
duration = re.sub(r'\.\d+', '', str(duration))
message = f"Deploy {status} in {duration}"
self._post_message(message, self._get_text_blocks(message), thread_ts)

def _post_message(self, notification_text, blocks, thread_ts=None):
data = {
"channel": self.channel,
Expand All @@ -91,14 +115,7 @@ def _post_message(self, notification_text, blocks, thread_ts=None):

def _get_message_blocks(self, message, context):
env_name = self.environment.meta_config.deploy_env
return [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message,
}
},
return self._get_text_blocks(message) + [
{
"type": "context",
"elements": [
Expand All @@ -118,6 +135,15 @@ def _get_message_blocks(self, message, context):
}
]

def _get_text_blocks(self, message):
return [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message,
}
}]

def _post_reaction(self, thread_ts, emoji):
data = {
"channel": self.channel,
Expand Down
Loading