Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Last active January 30, 2023 09:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save perfecto25/3f542358086fcc8e2f63f396da08ded9 to your computer and use it in GitHub Desktop.
Save perfecto25/3f542358086fcc8e2f63f396da08ded9 to your computer and use it in GitHub Desktop.
Jira API examples
# get all Custom fields on Jira server
curl -D- -u user:password -X GET -H "Content-Type: application/json" https://jira.instance.com/rest/api/2/field
# get JSON output of an issue
curl -D- -u user:password -X GET -H "Content-Type: application/json" https://jira.instance.com/rest/api/2/search?jql=key=INFO-68
# make 2 issues linked to each other
curl -u user:password -w '<%{http_code}>' -H 'Content-Type: application/json' https://jira.instance.com/rest/api/2/issueLink -X POST --data '{"type":{"name":"Related Incident"},"inwardIssue":{"key":"ISS-123"},"outwardIssue":{"key":"SYS-456"}}'
# get all custom field Select child values
$ curl -u user:pw -X GET https://jira.instance.com/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoptions/12600
[{"optionvalue":"Product1","id":14042,"sequence":0,"disabled":false},{"optionvalue":"Product2","id":14043,"sequence":1,"disabled":false},{"optionvalue":"Product3","id":14044,"sequence":2,"disabled":false},{"optionvalue":"Product4","id":14045,"sequence":3,"disabled":false}]
# create new issue
api_data="{\"fields\":{\"project\":{\"key\":\"${project}\"},\"summary\":\"my summary\",\"description\": \"my description\",\"issuetype\":{\"name\":\"${issuetype}\"}}}"
curl -u "${jira_user}":"${jira_pw}" -w "<%{http_code}>" -H "Content-Type: application/json" https://${jira_server}/rest/api/2/issue/ -X POST --data "${api_data}")
# update existing ticket, add attachment
curl -D- -u "${jira_user}":"${jira_pw}" -w "<%{http_code}>" -X POST -H "X-Atlassian-Token: nocheck" -F "file=@/tmp/attachment_file.txt" https://${jira_server}/rest/api/2/issue/${jira_key}/attachments)
# update existing ticket, change assignee
curl -D- -u "$user":"$pw" -X PUT -H "Content-Type: application/json" https://$server/rest/api/2/issue/$jira_key --data '{"fields": {"assignee":{"name":"$assignee"}}}'
# update existing ticket, edit customfield option value
curl -D- -u "$user":"$pw" -X PUT -H "Content-Type: application/json" https://$server/rest/api/2/issue/$jira_key --data '{"customfield_12345":{"value":"myValue"}}'
import sys
import json
import requests
------------------------------------------------------------------------------------------------
## Get Jira details
def get_jira(jira_server, jira_user, jira_pw, jira_key):
print('getting jira issue %s' % jira_key)
url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key
try:
req = requests.get(url, auth=(jira_user, jira_pw), verify=False)
if not req.status_code in range(200,206):
print('Error connecting to Jira.. check config file')
sys.exit()
jira = req.json()
return jira['key']
except requests.exceptions.Timeout:
print('Timeout trying to connect to jira')
except requests.exceptions.RequestException as exep:
# catastrophic error. bail.
print('error connecting to jira: ' + str(exep))
>>> get_jira(jira_server, jira_user, jira_pw, 'SYS-123')
------------------------------------------------------------------------------------------------
## Create a new Jira
def create_jira(jira_server, jira_user, jira_pw, data):
print('creating new Jira issue')
url = 'https://' + jira_server + '/rest/api/2/issue/'
headers = {'Content-type': 'application/json'}
try:
req = requests.post(url, auth=(jira_user, jira_pw), data=data, headers=headers, verify=False)
# check return
if not req.status_code in range(200,206):
print('Error connecting to Jira.. check config file')
sys.exit()
jira = req.json()
return jira['key']
except requests.exceptions.Timeout:
print('Timeout trying to connect to jira')
except requests.exceptions.RequestException as exep:
# catastrophic error. bail.
print('error connecting to jira: ' + str(exep))
except:
print('error creating new Jira ticket')
>>> data = data = '{"fields":{"project":{"key":"SYS"}, "summary": "my Summary","assignee":{"name":"Joe.Shmoe"},"description":"my Desc","issuetype":{"name":"Task"}}}'
>>> create_jira(jira_server, jira_user, jira_pw, data)
------------------------------------------------------------------------------------------------
## Update existing Jira
def update_jira(jira_server, jira_user, jira_pw, data, jira_key):
print('updating Jira issue %s' % jira_key)
headers = {'Content-type': 'application/json'}
url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key
try:
req = requests.put(url, auth=(jira_user, jira_pw), data=data, headers=headers, verify=False)
print req.status_code
# check return
if not req.status_code in range(200,206):
print('Error connecting to Jira.. check config file')
sys.exit()
else:
print('Jira ticket %s updated successfully' % jira_key)
except requests.exceptions.Timeout:
print('Timeout trying to connect to jira')
except requests.exceptions.RequestException as exep:
# catastrophic error. bail.
print('error connecting to jira: ' + str(exep))
>>> data = {"fields": {"assignee":{"name":"Fred"}}}
>>> update_jira(jira_server, jira_user, jira_pw, data, 'SYS-123')
------------------------------------------------------------------------------------------------
## Add an Attachment
def jira_add_attachment(jira_server, jira_user, jira_pw, jira_key, attachment, filename):
print('adding attachment to jira: %s' % jira_key)
headers = {'X-Atlassian-Token': 'nocheck'}
url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key + '/attachments'
try:
req = requests.post(url, auth=(jira_user, jira_pw), files={'file':(filename, attachment)}, data={'description':'my Attachment'}, headers=headers, verify=False)
print req.status_code
# check return
if not req.status_code in range(200,206):
print('Error connecting to Jira.. check config file')
sys.exit()
else:
print('Jira ticket %s updated successfully' % jira_key)
except requests.exceptions.Timeout:
print('Timeout trying to connect to jira')
except requests.exceptions.RequestException as exep:
# catastrophic error. bail.
print('error connecting to jira: ' + str(exep))
attachments = {}
for filename in glob.glob("reports/*.zip"):
print filename
with open(filename) as f:
attachments[filename] = f.read()
jira_add_attachment(jira_server, jira_user, jira_pw, jira_key, attachments[filename], filename)
@zhao1532072926
Copy link

Can I set the reporter when I create a new issue? For example, the admin create an issue and it's reporter is an another user.

@perfecto25
Copy link
Author

perfecto25 commented Feb 8, 2018

easiest way to create and update Jira w Python is to use JIRA module,

pip install jira

import sys
from jira import JIRA
from jira.exceptions import JIRAError

def jira_connect(server, user, pw):
    ''' connects to jira server and returns jira object '''
    try:
        log.info("Connecting to JIRA: %s" % server)
        jira_options = {'server': 'https://'+server, 'verify': False}
        jira = JIRA(options=jira_options, basic_auth=(user, pw))
        return jira
    except Exception, e:
        log.error("Failed to connect to JIRA: %s" % e)
        sys.exit(1)


server = jira01.company.com
user = 'admin'
pw = 'mypasswd'

jira = jira_connect(server, user, pw)

# update assignee
issue = jira.issue('PROJ-123')

try:
  issue.update(fields={ 'assignee': 'New Guy' })
except JIRAError, exep:
  print("Failed to update Assignee %s" % exep)
  sys.exit(1)




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment