以下是一个Python样例以供参阅

 

#当入站请求类型为“Bug”,影响为“High”,则通过此脚本文件在JIRA中创建一个工单。请求的ID、主题、描述、处理时间、优先级将作为创建参数传递给JIRA。当JIRA工单创建成功后,JIRA的工单ID、工单URL将作为响应结果,作为注释添加到ServiceDesk Plus对应的请求下。 同时自定义字段“JIRA_ISSUE_ID”、“JIRA_ISSUE_URL”也将更新为相应的值。

 

#前提要求 - Requests模块已安装

 

#用户需设置内容: url,jirausername, jirapassword, Jira_Server, Port_Number

 

#命令 - 'py CreateJiraTicket.py $COMPLETE_JSON_FILE'

 

import requests

import sys

import json

import datetime

 

#打开及访问Json文件

file_Path = sys.argv[1]

with open(file_Path) as data_file:

 

data = json.load(data_file)

 

#获得相应的值

request_obj = data['request']

workorderid = request_obj['WORKORDERID']

subject = request_obj['SUBJECT']

desc = request_obj['SHORTDESCRIPTION']

priority = request_obj['PRIORITY']

duebytime = request_obj['DUEBYTIME']

 

#将时间格式从毫秒转换为日期格式

duebydate = datetime.datetime.fromtimestamp(int(duebytime) / 1e3).strftime('%d %b %Y, %H:%M:%S')

 

 

#创建API调用所需输入的的json对象

 

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")

 

 

#指定必要的HTTP headers来提交API申请

 

headers = ''' {

        "X-Version" : "1",

        'Content-Type':'application/json',

        "Accept" : "application/json",

        "Authorization" : "Basic " + base64string

    }'''

 

 

#组装URL以调用API,将相应参数提交到Jira服务器

 

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(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)

 

 

#返回JSON

 

 

print(resultjson)

else:

print("Problem submitting request")

  print(r.json())

全球超过3/5的世界五百强企业在使用ServiceDesk Plus赋能IT服务管理
 
让IT服务变得简单高效!