-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjira-unfurl-bot.py
67 lines (55 loc) · 1.72 KB
/
jira-unfurl-bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import jira
import consts
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
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):
say("I'm alive")
@app.event("link_shared")
def got_link(client, payload):
for link in payload["links"]:
url = link["url"]
issue_id = url.split("/")[-1]
issue = jira_client.issue(issue_id)
print(issue.fields.components[0])
if issue.fields.project.name != consts.PROJECT_NAME and issue.fields.components[0].name not in consts.COMPONENTS:
continue
_payload = get_payload(url, issue)
channel_id = payload["channel"]
client.chat_unfurl(
channel=channel_id,
ts=payload["message_ts"],
unfurls=_payload,
)
def get_payload(url, issue):
key = issue.key
status = issue.fields.status.name
summary = issue.fields.summary
unfurl_text = f":jira: *{key}* [*{status}*] : {summary}"
payload = {
url: {
"color": "#025BA6",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": unfurl_text,
}
},
]
}
}
return payload
if __name__ == "__main__":
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start()