I have been writing a script that will create a jira issue and I have been having issues getting it to work. My main issue is the urllib3 import not working in pycharm and I was looking for alternatives. Below is my code, username and password omitted of course. Thanks in advance for any advice!
from urllib3 import request, urlopen, URLError
import sys
import json
import base64
import os
import requests
def jira_rest_call(data):
# Set the root JIRA URL, and encode the username and password
url = '(omitted)/rest/api/2/issue'
base64string = base64.encodestring('%s:%s' % ('username', 'api-token')
).replace('\n', '')
# Build the request
restreq = requests.get(url)
restreq.add_header('Content-Type', 'application/json')
restreq.add_header("Authorization", "Basic %s" % base64string)
# Send the request and grab JSON response
response = urlopen(restreq, data)
# Load into a JSON object and return that to the calling function
return json.loads(response.read())
# Build the text for the JIRA ticket.
jira_summary = 'This is a test issue'
jira_description = 'This is a test description - Ben'
# Build the JSON to post to JIRA
json_data = '''
{
"fields":{
"project":{
"key":"ITSD"
},
"summary": "%s",
"components":[
{"name":"Internal IT automation"}
],
"issuetype":{
"name":"IT Incident"
},
"description": "%s"
}
} ''' % (jira_summary, jira_description)
json_response = jira_rest_call(json_data)
parent_key = json_response['key']
print("Created parent issue ", parent_key)