-
-
Save Amarlanda/b5765a9605f0bc2a03f092a7f0fe4f5a to your computer and use it in GitHub Desktop.
JIRA XLDepoy check and update transition request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import base64 | |
import httplib | |
import json | |
from urlparse import urlparse | |
class JiraCommunicator: | |
""" Jira Communicator using REST API """ | |
def __init__(self, endpoint='http://localhost:4516', username='admin', password='admin', apiVersion='latest'): | |
self.endpoint = endpoint | |
self.username = username | |
self.password = password | |
self.apiVersion = apiVersion | |
def issue_exists(self,jira): | |
print "Check Jira Issue [%s]" % jira | |
try: | |
self.do_get("/rest/api/%s/issue/%s" % (self.apiVersion,jira)) | |
print "Jira Issue [%s] exists" % jira | |
return True | |
except: | |
return False | |
def get_issue(self,jira): | |
return self.do_get("/rest/api/%s/issue/%s" % (self.apiVersion,jira)) | |
def do_get(self, path): | |
return self.do_it("GET", path, "") | |
def do_put(self, path, doc): | |
return self.do_it("PUT", path, doc) | |
def do_post(self, path, doc): | |
return self.do_it("POST", path, doc) | |
def do_delete(self, path): | |
return self.do_it("DELETE", path, "", False) | |
def do_it(self, verb, path, doc, parse_response=True): | |
#print "DO %s %s on %s " % (verb, path, self.endpoint) | |
parsed_url = urlparse(self.endpoint) | |
if parsed_url.scheme == "https": | |
conn = httplib.HTTPSConnection(parsed_url.hostname, parsed_url.port) | |
else: | |
conn = httplib.HTTPConnection(parsed_url.hostname, parsed_url.port) | |
try: | |
auth = base64.encodestring('%s:%s' % (self.username, self.password)).replace('\n', '') | |
headers = {"Authorization": "Basic %s" % auth} | |
conn.request(verb, path, doc, headers) | |
response = conn.getresponse() | |
#print response.status, response.reason | |
if response.status != 200 and response.status != 204: | |
raise Exception("Error when requesting XL Deploy Server [%s]:%s" % (response.status, response.reason)) | |
if parse_response: | |
data = str(response.read()) | |
decoded = json.loads(data) | |
return decoded | |
return None | |
finally: | |
conn.close() | |
def __str__(self): | |
return "[endpoint=%s, username=%s]" % (self.endpoint, self.username) | |
communicator = JiraCommunicator(url,username, password) | |
if not communicator.issue_exists(jira): | |
raise ValueError("[%s] Not Found in %s" % (jira, communicator)) | |
#print communicator.get_issue(jira) | |
print "Done." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import base64 | |
import httplib | |
import json | |
from urlparse import urlparse | |
class JiraCommunicator: | |
""" Jira Communicator using REST API """ | |
def __init__(self, endpoint='http://localhost:4516', username='admin', password='admin', apiVersion='latest'): | |
self.endpoint = endpoint | |
self.username = username | |
self.password = password | |
self.apiVersion = apiVersion | |
def issue_exists(self,jira): | |
print "Check Jira Issue [%s]" % jira | |
try: | |
self.do_get("/rest/api/%s/issue/%s" % (self.apiVersion,jira)) | |
print "Jira Issue [%s] exists" % jira | |
return True | |
except: | |
return False | |
def get_issue(self,jira): | |
return self.do_get("/rest/api/%s/issue/%s" % (self.apiVersion,jira)) | |
def get_transitions_issue(self,jira): | |
return self.do_get("/rest/api/%s/issue/%s/transitions" % (self.apiVersion,jira)) | |
def update_transition_issue(self,jira,doc): | |
return self.do_post_no_parse("/rest/api/%s/issue/%s/transitions" % (self.apiVersion,jira), doc) | |
def update_comment(self,jira,comment): | |
print "Update comment %s : %s " % (jira,comment) | |
commentData = { | |
"body": comment | |
} | |
return self.do_post_no_parse("/rest/api/%s/issue/%s/comment" % (self.apiVersion,jira), json.dumps(commentData) ) | |
def move_issue_to_transistion(self,jira, transition_name, transition_message): | |
jira_data_transitions = self.get_transitions_issue(jira) | |
next_transitions = filter(lambda t: t['name'] == transition_name, jira_data_transitions['transitions']) | |
if len(next_transitions) > 0: | |
next_transition = next_transitions[0] | |
else: | |
raise ValueError('Transistion [%s] not found' % next_transition_name) | |
print "Performing transition %s " % (next_transition_name) | |
transitionData = { | |
"update": { | |
"comment": [ | |
{ | |
"add": { | |
"body": transition_message | |
} | |
} | |
] | |
}, | |
"transition": { | |
"id": next_transition['id'] | |
} | |
} | |
self.update_transition_issue(jira,json.dumps(transitionData)) | |
print "----END OF ...." | |
self.update_comment(jira, "%s [Transition: %s]" % (transition_message,transition_name) ) | |
print "/END OF ...." | |
def do_get(self, path): | |
return self.do_it("GET", path, "") | |
def do_put(self, path, doc): | |
return self.do_it("PUT", path, doc) | |
def do_post(self, path, doc): | |
return self.do_it("POST", path, doc) | |
def do_post_no_parse(self, path, doc): | |
return self.do_it("POST", path, doc, False) | |
def do_delete(self, path): | |
return self.do_it("DELETE", path, "", False) | |
def do_it(self, verb, path, doc, parse_response=True): | |
#print "DO %s %s on %s " % (verb, path, self.endpoint) | |
parsed_url = urlparse(self.endpoint) | |
if parsed_url.scheme == "https": | |
conn = httplib.HTTPSConnection(parsed_url.hostname, parsed_url.port) | |
else: | |
conn = httplib.HTTPConnection(parsed_url.hostname, parsed_url.port) | |
try: | |
auth = base64.encodestring('%s:%s' % (self.username, self.password)).replace('\n', '') | |
headers = {"content-type": "application/json", "Authorization": "Basic %s" % auth} | |
conn.request(verb, path, doc, headers) | |
response = conn.getresponse() | |
#print response.status, response.reason | |
if response.status != 200 and response.status != 204 and response.status !=201: | |
raise Exception("Error when requesting XL Deploy Server [%s]:%s" % (response.status, response.reason)) | |
if parse_response: | |
data = str(response.read()) | |
decoded = json.loads(data) | |
return decoded | |
return None | |
finally: | |
conn.close() | |
def __str__(self): | |
return "[endpoint=%s, username=%s]" % (self.endpoint, self.username) | |
next_transition_name="Ready for PROD" | |
next_transition_message = "Automaticaly Commented by XLDeploy" | |
communicator = JiraCommunicator(url,username, password) | |
if not communicator.issue_exists(jira): | |
raise ValueError("[%s] Not Found in %s" % (jira, communicator)) | |
communicator.move_issue_to_transistion(jira,next_transition_name, next_transition_message) | |
print "Done." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!-- | |
Note: If you modify this file and automatic reloading is disabled in `planner.conf`, you must restart the XL Deploy server. | |
--> | |
<rules xmlns="http://www.xebialabs.com/xl-deploy/xl-rules"> | |
<rule name="xl.DeployToProduction.checkJira" scope="pre-plan"> | |
<conditions> | |
<expression>specification.deployedOrPreviousApplication.environment.requiresChangeTicketNumber</expression> | |
</conditions> | |
<steps> | |
<jython> | |
<description expression="true">"Check if the [%s] Ticket is valid" % (specification.deployedOrPreviousApplication.version.satisfiesChangeTicketNumber)</description> | |
<order>10</order> | |
<script-path>jira/check.py</script-path> | |
<jython-context> | |
<jira expression="true">specification.deployedOrPreviousApplication.version.satisfiesChangeTicketNumber</jira> | |
<url expression="true">specification.deployedOrPreviousApplication.environment.jiraUrl</url> | |
<username expression="true">specification.deployedOrPreviousApplication.environment.jiraUsername</username> | |
<password expression="true">specification.deployedOrPreviousApplication.environment.jiraPassword</password> | |
</jython-context> | |
</jython> | |
</steps> | |
</rule> | |
<rule name="xl.DeployToProduction.updateJira" scope="post-plan"> | |
<conditions> | |
<expression>specification.deployedOrPreviousApplication.environment.requiresChangeTicketNumber</expression> | |
</conditions> | |
<steps> | |
<jython> | |
<description expression="true">"update The [%s] Ticket status" % (specification.deployedOrPreviousApplication.version.satisfiesChangeTicketNumber)</description> | |
<order>10</order> | |
<script-path>jira/update.py</script-path> | |
<jython-context> | |
<jira expression="true">specification.deployedOrPreviousApplication.version.satisfiesChangeTicketNumber</jira> | |
<url expression="true">specification.deployedOrPreviousApplication.environment.jiraUrl</url> | |
<username expression="true">specification.deployedOrPreviousApplication.environment.jiraUsername</username> | |
<password expression="true">specification.deployedOrPreviousApplication.environment.jiraPassword</password> | |
</jython-context> | |
</jython> | |
</steps> | |
</rule> | |
</rules> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment