From 1da52b6d29fb5f4e9f529853003fc60c7fa37b5f Mon Sep 17 00:00:00 2001 From: osher cohen Date: Tue, 4 May 2021 22:19:39 +0300 Subject: [PATCH 1/3] Enrich jira when mentiond in ticket --- README.md | 3 ++- keep_it_simple.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- requirements.txt | 1 + 3 files changed, 47 insertions(+), 3 deletions(-) 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..e8d3d6f 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,15 @@ 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_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): @@ -17,13 +28,44 @@ def event_test(say): @app.event("message") def make_it_short(client, message): 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: + keep_it_short(channel_id, client, message) + enrich_jira_mention(channel_id, client, message) + + +def keep_it_short(channel_id, client, message): + 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"]) 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): + if not jira_user: + return + jira_links = re.findall("", message["text"]) + if not jira_links: + return + new_message = message["text"] + for link in jira_links: + ticket_description = get_jira_info(link) + link_with_description = f"{link} \n> :jira: {ticket_description}\n" + new_message = new_message.replace(link, link_with_description) + client.chat_update( + text=new_message, + channel=channel_id, + ts=message["ts"] + ) + +def get_jira_info(ticket): + ticket_key = re.findall("MGMT-.*[1-9]",ticket)[0] + 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 From ab442e5323c335a97fa4f351af22d1ae762ceda6 Mon Sep 17 00:00:00 2001 From: osher cohen Date: Tue, 4 May 2021 23:40:12 +0300 Subject: [PATCH 2/3] Add option to only mention a ticket --- keep_it_simple.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/keep_it_simple.py b/keep_it_simple.py index e8d3d6f..0e63047 100644 --- a/keep_it_simple.py +++ b/keep_it_simple.py @@ -12,6 +12,8 @@ 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(":") @@ -33,6 +35,8 @@ def make_it_short(client, message): def keep_it_short(channel_id, client, message): + if "text" not in message: + return 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"]) @@ -44,25 +48,45 @@ def keep_it_short(channel_id, client, message): ) + def enrich_jira_mention(channel_id, client, message): if not jira_user: return + if "text" not in message: + return + jira_links = re.findall("", message["text"]) - if not jira_links: + + jira_mention = re.findall("(?:^|\s)(?:MGMT|mgmt)-[1-9]{4,}", message["text"]) + + if not jira_links and not jira_mention: return new_message = message["text"] + for link in jira_links: - ticket_description = get_jira_info(link) + 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): - ticket_key = re.findall("MGMT-.*[1-9]",ticket)[0] +def get_jira_info(ticket_key): issue = jira_client.search_issues(f"key={ticket_key}")[0] title = issue.raw['fields']['summary'] return title From c523e33bf37909ba2f9919a0461d9b84b76b4f48 Mon Sep 17 00:00:00 2001 From: osher cohen Date: Wed, 5 May 2021 12:51:14 +0300 Subject: [PATCH 3/3] Small updates --- keep_it_simple.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/keep_it_simple.py b/keep_it_simple.py index 0e63047..7b9c83e 100644 --- a/keep_it_simple.py +++ b/keep_it_simple.py @@ -29,17 +29,22 @@ 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"] keep_it_short(channel_id, client, message) enrich_jira_mention(channel_id, client, message) def keep_it_short(channel_id, client, message): - if "text" not in message: - return - 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"]) + 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"], @@ -50,17 +55,19 @@ def keep_it_short(channel_id, client, message): def enrich_jira_mention(channel_id, client, message): + print("Checking message for enrichment") if not jira_user: return - if "text" not in message: - 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: