Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# keep-it-simple-bot

* Inform in a thread the message is too long

* Enrich jira title when ticket is mentioned
# Usage:
```
SLACK_BOT_TOKEN: slack bot token
SLACK_APP_TOKEN: slack app token
MANAGER (optional): manager name to mention in the comment, default "the manager"
MAX_MESSAGE_LEN (optional): max len of the message, default 500
JIRA_CREDS (optional): jira creds with the format of 'user:password'
```
79 changes: 76 additions & 3 deletions keep_it_simple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import re
import jira
import CONSTS

from slack_bolt import App
Expand All @@ -9,21 +11,92 @@
MANAGER = os.environ.get("MANAGER", "the manager")
MAX_MESSAGE_LEN = os.environ.get("MAX_MESSAGE_LEN", CONSTS.MAX_MESSAGE_LEN)

JIRA_SERVER = "https://issues.redhat.com"
JIRA_TICKET_TEMPLATE = "https://issues.redhat.com/browse/{issue_key}"

jira_creds = os.environ.get("JIRA_CREDS", None)
if jira_creds:
jira_user, jira_pass = jira_creds.split(":")
jira_client = jira.JIRA(JIRA_SERVER, basic_auth=(jira_user, jira_pass))
else:
jira_user = None


# Check liveness
@app.event("app_mention")
def event_test(say):
say("Hi there!")

@app.event("message")
def make_it_short(client, message):

if "text" not in message:
return

print(f"Got message {message['text']}")

channel_id = message["channel"]
if message["channel_type"] in ["group", "channel"] and "thread_ts" not in message and len(message["text"]) > CONSTS.MAX_MESSAGE_LEN:
message_len = len(message["text"])
keep_it_short(channel_id, client, message)
enrich_jira_mention(channel_id, client, message)


def keep_it_short(channel_id, client, message):
message_len = len(message["text"])
if message["channel_type"] in ["group", "channel"] and "thread_ts" not in message and message_len > CONSTS.MAX_MESSAGE_LEN:
print("Message is too long, informing user")

result = client.chat_postMessage(
channel=channel_id,
thread_ts=message["ts"],
text=CONSTS.MAX_MESSAGE_COMMENT.format(max_message_len=CONSTS.MAX_MESSAGE_LEN, message_len=message_len, manager=MANAGER)
text=CONSTS.MAX_MESSAGE_COMMENT.format(max_message_len=CONSTS.MAX_MESSAGE_LEN, message_len=message_len,
manager=MANAGER)
)



def enrich_jira_mention(channel_id, client, message):
print("Checking message for enrichment")
if not jira_user:
return

jira_links = re.findall("<https:\/\/issues\.redhat\.com\/browse\/MGMT-.*?>", message["text"])

jira_mention = re.findall("(?:^|\s)(?:MGMT|mgmt)-[1-9]{4,}", message["text"])

print(f"Message jira links: {jira_links+jira_mention}")

if not jira_links and not jira_mention:
return

new_message = message["text"]

for link in jira_links:
ticket_key = re.findall("MGMT-.*[1-9]", link)[0]
ticket_description = get_jira_info(ticket_key)
link_with_description = f"{link} \n> :jira: {ticket_description}\n"
new_message = new_message.replace(link, link_with_description)

for mention in jira_mention:
try:
ticket_key = mention.upper().strip()
ticket_description = get_jira_info(ticket_key)
link = JIRA_TICKET_TEMPLATE.format(issue_key=ticket_key)
link_with_description = f"{link} \n> :jira: {ticket_description}\n"
new_message = new_message.replace(mention, link_with_description)
except Exception as ex:
print(ex)
continue

client.chat_update(
text=new_message,
channel=channel_id,
ts=message["ts"]
)

def get_jira_info(ticket_key):
issue = jira_client.search_issues(f"key={ticket_key}")[0]
title = issue.raw['fields']['summary']
return title

if __name__ == "__main__":
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
slackclient==1.3.2
slack-bolt==1.5.0
jira==2.0.0