Jira API examples · GitHub
Jira API examples · GitHub
Star
1
2 # get all Custom fields on Jira server
3 curl -D- -u user:password -X GET -H "Content-Type: application/json" https://jira.instance.co
4
5 # get JSON output of an issue
6 curl -D- -u user:password -X GET -H "Content-Type: application/json" https://jira.instance.co
7
8 # make 2 issues linked to each other
9 curl -u user:password -w '<%{http_code}>' -H 'Content-Type: application/json' https://jira.in
10
11 # get all custom field Select child values
12 $ curl -u user:pw -X GET https://jira.instance.com/rest/jiracustomfieldeditorplugin/1.1/user/
13 [{"optionvalue":"Product1","id":14042,"sequence":0,"disabled":false},{"optionvalue":"Product2
14
15 # create new issue
16 api_data="{\"fields\":{\"project\":{\"key\":\"${project}\"},\"summary\":\"my summary\",\"desc
17
18 curl -u "${jira_user}":"${jira_pw}" -w "<%{http_code}>" -H "Content-Type: application/json"
19
20 # update existing ticket, add attachment
21 curl -D- -u "${jira_user}":"${jira_pw}" -w "<%{http_code}>" -X POST -H "X-Atlassian-Token: no
22
23 # update existing ticket, change assignee
24 curl -D- -u "$user":"$pw" -X PUT -H "Content-Type: application/json" https://$server/rest/ap
25
26 # update existing ticket, edit customfield option value
27 curl -D- -u "$user":"$pw" -X PUT -H "Content-Type: application/json" https://$server/rest/ap
1 import sys
https://gist.github.com/perfecto25/3f542358086fcc8e2f63f396da08ded9 1/5
6/26/22, 8:55 PM Jira API examples · GitHub
p y
2 import json
3 import requests
4
5 --------------------------------------------------------------------------------------------
6 ## Get Jira details
7
8 def get_jira(jira_server, jira_user, jira_pw, jira_key):
9 print('getting jira issue %s' % jira_key)
10 url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key
11
12 try:
13 req = requests.get(url, auth=(jira_user, jira_pw), verify=False)
14
15 if not req.status_code in range(200,206):
16 print('Error connecting to Jira.. check config file')
17 sys.exit()
18 jira = req.json()
19 return jira['key']
20 except requests.exceptions.Timeout:
21 print('Timeout trying to connect to jira')
22 except requests.exceptions.RequestException as exep:
23 # catastrophic error. bail.
24 print('error connecting to jira: ' + str(exep))
25
26 >>> get_jira(jira_server, jira_user, jira_pw, 'SYS-123')
27
28
29
30 --------------------------------------------------------------------------------------------
31 ## Create a new Jira
32
33 def create_jira(jira_server, jira_user, jira_pw, data):
34 print('creating new Jira issue')
35 url = 'https://' + jira_server + '/rest/api/2/issue/'
36
37 headers = {'Content-type': 'application/json'}
38
39 try:
40 req = requests.post(url, auth=(jira_user, jira_pw), data=data, headers=headers, verif
41
42 # check return
43 if not req.status_code in range(200,206):
44 print('Error connecting to Jira.. check config file')
45 sys.exit()
46 jira = req.json()
47 return jira['key']
48 except requests.exceptions.Timeout:
49 print('Timeout trying to connect to jira')
https://gist.github.com/perfecto25/3f542358086fcc8e2f63f396da08ded9 2/5
6/26/22, 8:55 PM Jira API examples · GitHub
50 except requests.exceptions.RequestException as exep:
51 # catastrophic error. bail.
52 print('error connecting to jira: ' + str(exep))
53 except:
54 print('error creating new Jira ticket')
55
56 >>> data = data = '{"fields":{"project":{"key":"SYS"}, "summary": "my Summary","assignee":{"n
57 >>> create_jira(jira_server, jira_user, jira_pw, data)
58
59
60
61 --------------------------------------------------------------------------------------------
62 ## Update existing Jira
63
64 def update_jira(jira_server, jira_user, jira_pw, data, jira_key):
65 print('updating Jira issue %s' % jira_key)
66 headers = {'Content-type': 'application/json'}
67 url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key
68
69 try:
70 req = requests.put(url, auth=(jira_user, jira_pw), data=data, headers=headers, verify
71 print req.status_code
72
73 # check return
74 if not req.status_code in range(200,206):
75 print('Error connecting to Jira.. check config file')
76 sys.exit()
77 else:
78 print('Jira ticket %s updated successfully' % jira_key)
79 except requests.exceptions.Timeout:
80 print('Timeout trying to connect to jira')
81 except requests.exceptions.RequestException as exep:
82 # catastrophic error. bail.
83 print('error connecting to jira: ' + str(exep))
84
85 >>> data = {"fields": {"assignee":{"name":"Fred"}}}
86 >>> update_jira(jira_server, jira_user, jira_pw, data, 'SYS-123')
87
88 --------------------------------------------------------------------------------------------
89 ## Add an Attachment
90
91 def jira_add_attachment(jira_server, jira_user, jira_pw, jira_key, attachment, filename):
92 print('adding attachment to jira: %s' % jira_key)
93 headers = {'X-Atlassian-Token': 'nocheck'}
94 url = 'https://' + jira_server + '/rest/api/2/issue/' + jira_key + '/attachments'
95
96 try:
97 req = requests.post(url, auth=(jira_user, jira_pw), files={'file':(filename, attachme
98 print req.status_code
https://gist.github.com/perfecto25/3f542358086fcc8e2f63f396da08ded9 3/5
6/26/22, 8:55 PM Jira API examples · GitHub
99
100 # check return
101 if not req.status_code in range(200,206):
102 print('Error connecting to Jira.. check config file')
103 sys.exit()
104 else:
105 print('Jira ticket %s updated successfully' % jira_key)
106 except requests.exceptions.Timeout:
107 print('Timeout trying to connect to jira')
108 except requests.exceptions.RequestException as exep:
109 # catastrophic error. bail.
110 print('error connecting to jira: ' + str(exep))
111
112 attachments = {}
113 for filename in glob.glob("reports/*.zip"):
114 print filename
115 with open(filename) as f:
116 attachments[filename] = f.read()
117 jira_add_attachment(jira_server, jira_user, jira_pw, jira_key, attachments[filename]
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.
easiest way to create and update Jira w Python is to use JIRA module,
import sys
from jira import JIRA
from jira.exceptions import JIRAError
https://gist.github.com/perfecto25/3f542358086fcc8e2f63f396da08ded9 4/5
6/26/22, 8:55 PM Jira API examples · GitHub
server = jira01.company.com
user = 'admin'
pw = 'mypasswd'
# 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)
https://gist.github.com/perfecto25/3f542358086fcc8e2f63f396da08ded9 5/5