What follows is a sample Python file for your reference.
#The script file creates a JIRA ticket when a request is created with Request Type as 'Bug' and Impact as 'High'. The request ID, subject, description, due by time and priority of the request is passed to the JIRA for the ticket creation. Upon successful creation of the JIRA ticket, the JIRA ticket ID and ticket URL is fetched from the response to be added as a note in the ServiceDesk Plus request. The custom fields 'JIRA_ISSUE_ID' and 'JIRA_ISSUE_URL' are also updated with corresponding values.
#Requirements - Requests module to be installed
#To be set up by the user: url,jirausername, jirapassword, Jira_Server, Port_Number
#Command - 'py CreateJiraTicket.py $COMPLETE_JSON_FILE'
import requests
import sys
import json
import datetime
#To open and access the json file
file_Path = sys.argv[1]
with open(file_Path) as data_file:
data = json.load(data_file)
#Getting required values
request_obj = data['request']
workorderid = request_obj['WORKORDERID']
subject = request_obj['SUBJECT']
desc = request_obj['SHORTDESCRIPTION']
priority = request_obj['PRIORITY']
duebytime = request_obj['DUEBYTIME']
#Tranform time from millisec to date format
duebydate = datetime.datetime.fromtimestamp(int(duebytime) / 1e3).strftime('%d %b %Y, %H:%M:%S')
#Creating the json object to be provided as input for the API call
jsonData ='''{
"fields": {
"summary": "'''+subject+'''",
"description": "'''+desc+'''",
"issuetype": {
"name": "BUG"
},
"project": {
"key": "ServiceDesk Plus issues"
},
"duedate": "'''+duebydate+'''",
"priority": {
"name": "'''+priority+'''"
},
"customfield_10003": "'''+workorderid+'''"
}
}'''
jirausername = 'jirausername'
jirapassword = 'jirapassword'
string = username + ":" + password
stringbytes = bytes(string,"utf-8")
base64string = base64.b64encode(stringbytes)
base64string = base64string.decode("utf-8")
#Specifying the HTTP headers necessary for submitting the API call
headers = ''' {
"X-Version" : "1",
'Content-Type':'application/json',
"Accept" : "application/json",
"Authorization" : "Basic " + base64string
}'''
#Constructing the url for the API call and submitting that to Jira server
with requests.Session() as s:
url = "[Jira_Server]:[Port_Number]/rest/api/2/issue/"
r = s.post(url,verify=True, data=jsonData,headers=headers)
#If the call returns success, create a note and update custom fields
if(r.status_code == 202):
responseobj=r.json()
jiraissueid = responseobj['id']
jiraissueurl = responseobj['self']
note={}
note["notestext"] = "Jira Request Created with ID: "+jiraissueid+"</br> Issue Link: "+jiraissueurl
noteObject={}
noteObject["notes"]=note
addNoteJson={}
addNoteJson['INPUT_DATA']=[]
addNoteJson['INPUT_DATA'].append(noteObject)
addNoteJson["OPERATIONNAME"]="ADD_NOTE"
updateReqArray={}
updateReqArray['JIRA_ISSUE_ID']=jiraissueid
updateReqArray['JIRA_ISSUE_URL']=jiraissueurl
updateFieldsJson={"INPUT_DATA":[]}
updateFieldsJson['INPUT_DATA'].append(updateReqArray)
updateFieldsJson["OPERATIONNAME"]="EDIT_REQUEST"
resultjson={}
resultjson["result"] = "success"
resultjson["message"] = "A Jira Request has been Created. Note with the Issue ID and URL has been added."
resultjson["operation"] = []
resultjson["operation"].append(addNoteJson)
resultjson["operation"].append(updateFieldsJson)
#Returning the JSON
print(resultjson)
else:
print("Problem submitting request")
print(r.json())