Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlcommunity/0df267983037e87f20f3 to your computer and use it in GitHub Desktop.
Save xlcommunity/0df267983037e87f20f3 to your computer and use it in GitHub Desktop.
Python script to count releases, templates and triggers in XL Release 4.7.x and up
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
import httplib
import unittest
import urllib2
import base64
import sys
import json
# Parameters to be changed
username = "admin"
password = "admin"
baseUrl = "http://localhost:5516" # Or HTTPS
numberByPage = 1000
# set these to a file path to use client auth, e.g. if XL Release is running using mutual SSL:
#pemFile = '/path/to/my/key.pem'
#clientCertFile = '/path/to/my/cert.pem'
pemFile = None
clientCertFile = None
forceSslv3 = False
# see https://stackoverflow.com/questions/17927339/forcing-mechanize-to-use-sslv3
if forceSslv3:
import ssl
from ssl import PROTOCOL_SSLv23, PROTOCOL_SSLv3, CERT_NONE, SSLSocket
def monkey_wrap_socket(sock, keyfile=None, certfile=None,
server_side=False, cert_reqs=CERT_NONE,
ssl_version=PROTOCOL_SSLv23, ca_certs=None,
do_handshake_on_connect=True,
suppress_ragged_eofs=True, ciphers=None):
ssl_version = PROTOCOL_SSLv3
# for Python 2.6 or lower, remove the "ciphers" argument
return SSLSocket(sock, keyfile=keyfile, certfile=certfile,
server_side=server_side, cert_reqs=cert_reqs,
ssl_version=ssl_version, ca_certs=ca_certs,
do_handshake_on_connect=do_handshake_on_connect,
suppress_ragged_eofs=suppress_ragged_eofs,
ciphers=ciphers)
ssl.wrap_socket = monkey_wrap_socket
# see http://www.osmonov.com/2009/04/client-certificates-with-urllib2.html
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)
if pemFile is not None and clientCertFile is not None:
cert_handler = HTTPSClientAuthHandler(pemFile, clientCertFile)
opener = urllib2.build_opener(cert_handler)
urllib2.install_opener(opener)
def releases_search_req(uri, username, password, data):
print "."
req = urllib2.Request(uri, data, headers={'Content-Type': 'application/json'})
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(req)
def templates_search_req(uri, username, password):
print "."
req = urllib2.Request(uri, headers={'Content-Type': 'application/json'})
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(req)
def releases_resp_by_page_active(p):
url = "%s/releases/search?numberbypage=%s&page=%s" % (baseUrl, numberByPage, p)
data = '{"active": "true", "onlyMine": "false", "onlyFlagged": "false", "planned": "true", "completed": "false", "filter": ""}'
return releases_search_req(url, username, password,data).read()
def releases_resp_by_page_completed(p):
url = "%s/releases/search?numberbypage=%s&page=%s" % (baseUrl, numberByPage, p)
data = '{"active": "false", "onlyMine": "false", "onlyFlagged": "false", "planned": "false", "completed": "true", "filter": ""}'
return releases_search_req(url, username, password,data).read()
def templates_resp_by_page(p):
url = "%s/releases/templates?numberbypage=%s&page=%s" % (baseUrl, numberByPage, p)
return templates_search_req(url, username, password).read()
def templates_resp_by_page2():
url = "%s/api/v1/templates" % (baseUrl)
return templates_search_req(url, username, password).read()
def count_results(respByPage):
goOn = True
page = 0
amount = 0
while goOn:
j = json.loads(respByPage(page))
amount += len(j['cis'])
page += 1
goOn = len(j['cis']) > 0
return amount
def triggers(respByPage):
j = json.loads(respByPage())
for ci in j:
if len(ci['releaseTriggers']) > 0:
print "Template with id %s has : %s triggers" % (ci['id'], len(ci['releaseTriggers']))
print " Triggers are : "
for trigger in ci['releaseTriggers']:
print " %s trigger (enabled: %s)" % (trigger['type'], trigger['enabled'])
releases_count_active = count_results(releases_resp_by_page_active)
releases_count_completed = count_results(releases_resp_by_page_completed)
templates_count = count_results(templates_resp_by_page)
print "Amount of templates: %s" % templates_count
print "Amount of active releases: %s" % releases_count_active
print "Amount of completed releases: %s" % releases_count_completed
triggers(templates_resp_by_page2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment