-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (138 loc) · 5.94 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, request, jsonify
from requests import post as post_request
from urllib.parse import quote_plus
from json import dumps as json_dumps
from config import *
app = Flask(__name__)
def post_to_mattermost_(text, channel=CHANNEL, username=USER_NAME, icon=USER_ICON):
payload = """{}"channel": "{}", "text": "{}", "username": "{}", "icon_url":"{}"{}""".\
format("payload={", channel, quote_plus(text), username, quote_plus(icon), "}")
# payload = "payload={"+payload+"}"
print(payload)
headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache"
}
response = curl_request("POST", MM_URL+"hooks/"+HOOK_ID, data=payload, headers=headers)
result = response.text
print("---hook response---")
print(result)
return result
def post_to_mattermost(attachment, channel=CHANNEL, username=USER_NAME, icon=USER_ICON):
# This function submits the new hook to mattermost
data = {
'attachments': [attachment],
'username': USER_NAME,
'icon_url': icon,
'channel': channel
}
# Post the webhook
response = post_request(
MM_URL+"hooks/"+HOOK_ID,
data=json_dumps(data).encode('utf-8'),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
err = 'Request to mattermost returned an error %s, the response is:\n%s'
raise ValueError(err % (response.status_code, response.text))
print("---hook response---")
print(response.text)
return "Posted successfully."
@app.route('/', methods=['GET'])
def hello_world():
return "Welcome to W3IM"
@app.route('/hooks/<token>', methods=['POST', 'GET'])
def mattermost_jira(token):
if token == HOOK_ID:
data = request.get_json(force=True)
# print("-------------START------------------")
print(data)
# print("-------------END------------------")
if data and data.get("issue_event_type_name", None) and data.get("issue", None):
issue_type = data.get("issue_event_type_name").replace("_", " ").replace("issue", "").title().strip()
issue_details = data.get("issue", None)
user_details = data.get("user", None)
key = issue_details.get('key', "N/A")
user_name = user_details.get('displayName', "N/A")
if issue_type == "Generic":
issue_type = "Changed on "
elif issue_type == "Work Started":
issue_type = "Started Work on"
elif issue_type == "Worklog Deleted":
issue_type = "Deleted Worklog"
elif issue_type == "Worklog Updated":
issue_type = "Updated Worklog"
elif issue_type == "Work Stopped":
issue_type = "Stopped Work on"
elif issue_type == "Work Logged":
issue_type = "Logged work on"
elif issue_type == "Comment Edited":
issue_type = "Edited comment on"
elif issue_type == "Commented":
issue_type = "Commented on"
if user_details:
user_photo = user_details['avatarUrls']['48x48']
issue_fields = issue_details.get('fields', None)
if issue_fields:
title = issue_fields.get("summary", "N/A")
description = issue_fields.get("description", "N/A")
creator = issue_fields.get('creator', None)
if creator:
creator = creator.get("displayName", "N/A")
else:
creator = "N/A"
project_data = issue_fields.get("project", None)
project = "N/A"
project_url = "#"
project_key = "N/A"
if project_data:
project = project_data.get("name", "N/A")
project_url = project_data['avatarUrls']['48x48']
project_key = project_data.get("key", "N/A")
assignee = issue_fields.get('assignee', None)
if assignee:
assignee = assignee.get("displayName", "N/A")
else:
assignee = "N/A"
# assignee = issue_fields.get('assignee', {}).get("displayName", "N/A")
status = issue_fields.get('status', {}).get("name", "N/A")
post_data = dict()
post_data['author_name'] = "Project: "+project
post_data['author_icon'] = user_photo
post_data['author_link'] = PROJECT_URL+project_key+"/summary"
post_data['title'] = "{} - {}".format(key, title)
post_data['text'] = "**Description:**\n"+description
post_data['title_link'] = ISSUE_URL+key
post_data['fields'] = [
{
"short": True,
"title": "Action Type",
"value": issue_type
},
{
"short": True,
"title": "Creator",
"value": user_name
},
{
"short": True,
"title": "Assignee",
"value": assignee
},
{
"short": True,
"title": "Status",
"value": status
},
]
return post_to_mattermost(post_data)
else:
return "No Issue data!"
else:
print("Wrong hook.")
return token
@app.route('/oauth', methods=['POST', 'GET'])
def oauth():
return "Thanks for using my app"
if __name__ == "__main__":
app.run(debug=DEBUG)