diff --git a/README.md b/README.md index 5262df9..33affe0 100644 --- a/README.md +++ b/README.md @@ -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' ``` diff --git a/keep_it_simple.py b/keep_it_simple.py index d5f913f..7b9c83e 100644 --- a/keep_it_simple.py +++ b/keep_it_simple.py @@ -1,4 +1,6 @@ import os +import re +import jira import CONSTS from slack_bolt import App @@ -9,6 +11,17 @@ 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): @@ -16,14 +29,74 @@ def event_test(say): @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("", 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() diff --git a/requirements.txt b/requirements.txt index 032daf6..4f09bc2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ slackclient==1.3.2 slack-bolt==1.5.0 +jira==2.0.0 \ No newline at end of file