-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhook.py
230 lines (177 loc) · 9.87 KB
/
webhook.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# Written by ahmedsajid
# Version 1.1
#
# This script is triggered as webhook when an issue is approved
# It grabs email address, sends the user email invitiation, adds the user to the appropriate groups, Comments on the issue and closes the issue
import logging
import os, json
import re
import sys
import requests
from jira import JIRA
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import pycurl, requests
from requests.auth import HTTPBasicAuth
# Change the following specific fo your environment
# Port for webhook to listen on
port = 8888
# URL to JSD
jira_url='https://your.jira.com'
# JIRA API User credentials
jira_user='adminuser'
jira_pass='password'
# Specific JIRA Project for searching organizations
project_key = 'PROJECTNAME'
# User with domain.com to be added to list of groups specified
groups = {'domain.com' : ['group1','group2']}
header = {
'Content-Type': 'application/json'
}
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# create a file handler
handler = logging.FileHandler('/opt/webhook/webhook.log')
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
# Try connecting to JIRA, fail on unable to connect
try:
jac = JIRA(jira_url,basic_auth=(jira_user, jira_pass))
del jac
except Exception:
pass
class WebhookHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = self.headers['content-length']
data = self.rfile.read(int(length))
# Replace any u' with "
data = data.replace("u\'", '"')
# Replace any ' with "
data = data.replace("\'",'"')
sys.stdout.write(data)
# Coverting String into JSON
json_data = json.loads(data)
logger.info(json_data)
# Authenticate with JIRA
jac = JIRA(jira_url,basic_auth=(jira_user, jira_pass))
if "webhookEvent" in json_data:
logger.info("webhookEvent")
if json_data["webhookEvent"] == "user_created":
email = json_data['user']['emailAddress'].lower()
email_json = json.dumps({ "emails" : [ "" + email + "" ]})
# extract domain part from email address
domain = email.split('@')[1].lower()
# extract org part from email address
org = email.split('@')[1].split('.')[0].lower()
# If user's domain is in working group dictionary
if domain in working_groups:
jira_org_invite_url = jira_url + '/rest/servicedesk/1/pages/people/customers/pagination/' + project_key + '/invite/organisation'
payload = {'query':org}
# Search for Organization
jira_search_url = jira_url + "/rest/servicedesk/1/pages/people/customers/pagination/" + project_key + "/search"
r = requests.get(jira_search_url, auth=(jira_user, jira_pass), params=payload, headers=header)
logger.info("Searching for Organization %s", org)
# If the search returned 200
if r.status_code == 200:
response = json.loads(r.content)
num_of_results = response['total']
search_results = response['results']
# If search yielded results
if num_of_results > 0:
for row in search_results:
if row['type'] == "Organisation" and row['displayName'].lower() == org:
org_id = row['id']
payload = {'id': org_id}
logger.info("Inviting %s", email)
logger.info("Org_id %s",org_id)
# Invite user and add user to Org
r = requests.put(jira_org_invite_url, auth=(jira_user, jira_pass), data=email_json, params=payload, headers=header)
logger.info("Invite returned %s", r.status_code)
logger.info("Invite returned %s", r.content)
# Add user to groups
for group in working_groups[domain]:
jac.add_user_to_group(email,group)
logger.info("Adding user to group %s",group)
elif "transition" in json_data:
logger.info("transition")
# Grab issue key
issue_type = json_data['issue']['fields']['issuetype']['name']
# Grab project key
project_key = json_data['issue']['fields']['project']['key']
# Get Issue Key
issue_key = json_data['issue']['key']
# If issue type is Access
if issue_type == "Access":
# Sanity check: If the issue was approved
if json_data['transition']['transitionName'] == "Approved":
# Grab Custom email field
email = json_data['issue']['fields']['customfield_10304'].strip().lower()
match = re.match("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",email)
# If email address matches regex
if not match == None:
# Assign Issue to Admin user so we get email
jac.assign_issue(issue_key, jira_user)
# Email in json format
email_json = json.dumps({ "emails" : [ "" + email + "" ]})
# Check if user exists
if jac.search_users(email):
# Add comment to issue
jac.add_comment(issue_key,"User already exists.")
else:
# Get Organization part from email
org = email.split('@')[1].split('.')[0].lower()
# build payload with query searching for organization
payload = {'query': org}
# search under specific project
jira_search_url = jira_url + "/rest/servicedesk/1/pages/people/customers/pagination/" + project_key + "/search"
# run search
r = requests.get(jira_search_url, auth=(jira_user, jira_pass), params=payload, headers=header)
# If the search returned 200, try getting organization id
if r.status_code == 200:
response = json.loads(r.content)
num_of_results = response['total']
search_results = response['results']
# If search yielded results
if num_of_results > 0:
# Loop through list of results
for row in search_results:
# if result type if organization
if row['type'] == "Organisation" and row['displayName'].lower() == org:
org_id = row['id']
payload = {'id': org_id}
jira_org_invite_url = jira_url + '/rest/servicedesk/1/pages/people/customers/pagination/' + project_key + '/invite/organisation'
# Invite user and add user to Org
r = requests.put(jira_org_invite_url, auth=(jira_user, jira_pass), data=email_json, params=payload, headers=header)
else:
jira_invite_url = jira_url + '/rest/servicedesk/1/pages/people/customers/pagination/' + project_key + '/invite'
# Send invite to user using API
r = requests.post(jira_invite_url, auth=(jira_user, jira_pass), data=email_json,headers=header)
# If invite was sent OK
if r.status_code == 200:
# extract domain part from email address
domain = email.split('@')[1].lower()
# If user's domain is in working group dictionary
if domain in working_groups:
# Add user to groups
for group in working_groups[domain]:
jac.add_user_to_group(email,group)
logger.info("Adding user to group %s",group)
# Close issue
jac.transition_issue(issue_key,"Resolve this issue",fields={"resolution":{"name":"Done"}},comment="Invitation sent to user. Resolving this issue.")
else:
# Add comment on issue
jac.add_comment(issue_key,"The email address is invalid")
else:
# Add comment on issue visibility to Administrators
jac.add_comment(issue_key,'Nothing to do', visibility={'type': 'role', 'value': 'Administrators'})
else:
# Add comment on issue visibility to Administrators
jac.add_comment(issue_key,'Issue type is not Access', visibility={'type': 'role', 'value': 'Administrators'})
self.send_response(200)
server = HTTPServer(('', port), WebhookHandler)
logger.info("serving at port=%s", port)
server.serve_forever()