How to use REST APIs to create and manage JIRA cloud tickets


Read this article to know how to make use of REST APIs to create JIRA tickets, set the severity, add an assignee, change status and more. Know the prerequisites and what all parameters you need to pass to make these API calls.

JIRA is an awesome solution for a wide variety of use cases including Project Management and ticket creation for issues. It enables complete timeline tracking, helps categorization of different varieties of tasks and allows designing of custom flow for any issue type. Sometimes, you have use cases where you want to integrate your application with JIRA and want to be able to create tickets, modify them or just track statistics of your JIRA tickets.

I have a similar use case recently I want to create tickets whenever an issue is Acknowledged in our URL Monitoring system. My application is written in Python 2.7 and while there is a library present in it for handling JIRA tickets, it seemed to have some issue with the latest updates that might have happened on the cloud JIRA that I am using. And even in the case if the library was working at this point in time, Python 2.7 support is stopping in December 2020 and hence it becomes important for me to go for a solution that doesn't stop working due to an incompatibility issue of the library. Hence, the best solution is to go for using REST APIs for my interaction with JIRA from my web app. That way I am safe even if when we shift to Python 3 for our application.

I am assuming you already have an account on JIRA and have a project created where you plan to create JIRA tickets through REST API. The first thing we need to do is to create an authentication token for your user which will be used when you call JIRA REST APIs for ticket creations. To do so:

  1. Go to your Account Settings page on JIRA.
  2. From the left sidebar, select the option "Security" and click on "Create and manage API tokens" link under the API Token section.
  3. Create JIRA API token
  4. Click on the Create API token button
  5. Enter a label to uniquely identify the where you are going to use the token or as per your wish.
  6. Copy JIRA API token
  7. A token will be generated which you can copy to your clipboard directly without displaying it on the screen. You must copy and save the token carefully because you won't be able to access it again. In case you lose it later on, you must create a new token.
Now that we have generated the API token necessary to connect with this JIRA project, now is the time to write a code that will make a REST POST call to create a new ticket with a heading, summary, and severity.

Create a JIRA ticket using REST API


I will be using Python's Request module to achieve this. The content part will go as the body of the post call. Here is the code that I used for this purpose. You can change the issue type and priority as per your requirement. Priority 1 refers to the "Highest" level of priority.
import requests
body = {
"fields": {
"project":
{
"key": "UM"
},
"summary": "Test 1",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
},"priority": {
"id": "2"
}
}
}
url = 'https://techulator.atlassian.net/rest/api/2/issue/'
headers = {"Content-Type": "application/json"}
r = requests.post(url, auth=('ankit@techulator.com', ''), headers=headers, data=json.dumps(body))
key = r.json()
print(key)

When I run this code, I get a JSON response with the JIRA ID for the bug I just created.
{u'self': u'https://techulator.atlassian.net/rest/api/2/issue/10000', u'id': u'10000', u'key': u'UM-1'}

Created JIRA ticket with REST
The ticket created is not assigned to anyone since I did not pass any such parameter. We can do further modifications to this ticket using PUT REST API calls.
Here are a few examples:

Assign the JIRA ticket to a user using REST


To assign a JIRA ticket to a user, the best way is to get the unique ID of a user. To get this ID, just visit the user profile by clicking on their name anywhere in the JIRA. When their profile is open, in the address bar you can find their profile ID. For example, my JIRA profile URL is https://techulator.atlassian.net/jira/people/5dff8218eb2ae70ea1de4f34 where the 5dff8218eb2ae70ea1de4f34 is my profile ID. We will use this ID to assign the ticket I just created to myself.
import requests
key= 'UM-1'
url = 'https://techulator.atlassian.net/rest/api/3/issue/{}/assignee'.format(key)
body = {
"name": "5dff8218eb2ae70ea1de4f34"
}
headers = {"Content-Type": "application/json"}
r = requests.put(url, auth=('ankit@techulator.com', ''), headers=headers, data=json.dumps(body))

You get an empty response for this but if you check the JIRA issue, you will see the this particular user whose ID you passed in the PUT call is now the assignee of the ticket.

Change status of JIRA ticket through REST API


This has to be done using a POST call. However, you need to know the status ID before you can use the POST call. To get the status ID, first use this GET call
import requests
key= 'UM-1'
url = 'https://techulator.atlassian.net/rest/api/2/issue/{}/transitions?expand=transitions.fields'.format(key)
r = requests.get(url, auth=('ankit@techulator.com', ''), headers=headers, data=json.dumps(body))

This will give you a list of status available in your workflow and the ID associated along with them. Extract the relevant ID can use it in POST call to change the status of a ticket:
import requests
key= 'UM-1'
url = 'https://techulator.atlassian.net/rest/api/2/issue/{}/transitions?expand=transitions.fields'.format(key)
print url
body = {"transition":
{
"id": "21"
}
}
headers = {"Content-Type": "application/json"}
r = requests.post(url, auth=('ankit@techulator.com', ''), headers=headers, data=json.dumps(body))

This call changed the status of my issue UM-1 from Backog to Selected for Development.

Similarly, many other things can be achieved by using the REST API on JIRA regarding your project workflow. Once you have got these basic API calls mentioned above, you can take a look at the JIRA Atlassian guidelines for REST APIs and create further required APIs.


More articles: Python programming JIRA

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: