-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompleteTaskJsonWebhook.py
executable file
·75 lines (55 loc) · 2.55 KB
/
CompleteTaskJsonWebhook.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
import sys, string
from com.xebialabs.xlrelease.plugin.webhook import JsonPathResult
from com.xebialabs.xlrelease.plugin.webhook import XmlPathResult
from urlparse import urlparse
def isJson():
return 'jsonPathExpression' in globals() or 'jsonPathExpression2' in globals() or 'jsonPathExpression3' in globals()
def isXml():
return 'xPathExpression' in globals() or 'xPathExpression2' in globals() or 'xPathExpression3' in globals()
def process(response, expression, ExpressionProcessor):
result_expected = expression is not None and len(expression) > 0
if result_expected:
resultVariable = ExpressionProcessor(response, expression).get()
if resultVariable is None:
print "Expression %s did not match anything in the response" % expression
sys.exit(1)
print "Result: %s" % resultVariable
return resultVariable
def process_json(response, jsonPathExpression):
return process(response, jsonPathExpression, JsonPathResult)
def process_xml(response, xPathExpression):
return process(response, xPathExpression, XmlPathResult)
if isJson():
content_type = 'application/json'
elif isXml():
content_type = 'application/xml'
else:
print 'Could not determine Webhook format, neither JsonPath nor XPath expression was provided'
sys.exit(1)
uri = urlparse(URL)
host = '%s://%s' % (uri.scheme, uri.netloc)
context = uri.path
if uri.query:
context = '%s?%s' % (context, uri.query)
server = { 'url': host, 'username': username, 'password': password, 'proxyHost': proxyHost, 'proxyPort': proxyPort}
request = HttpRequest(server, username, password)
if body is None:
body = ""
i = [ count for count,item in enumerate(phase.getTasks()) if task == item ]
if len(phase.getTasks()[i[0]-1].comments) > 0:
body ='{state:3,work_notes:\'' + phase.getTasks()[i[0]-1].comments[-1].getText() + '\'}'
response = request.doRequest(method = method, context = context, body = body, contentType = content_type)
if response.isSuccessful():
print "Http Status: %s" % response.status
if isJson():
result = process_json(response.response, jsonPathExpression)
result2 = process_json(response.response, jsonPathExpression2)
result3 = process_json(response.response, jsonPathExpression3)
if isXml():
result = process_xml(response.response, xPathExpression)
result2 = process_xml(response.response, xPathExpression2)
result3 = process_xml(response.response, xPathExpression3)
else:
print "Failed to connect at %s." % URL
response.errorDump()
sys.exit(1)