APIs and JIRA – Chapter 9
Heard of APIs? They are everywhere now, some are available for free while some come with a price. APIs (Application Programming Interface) refer to the program that enables you to perform an action on the product platform; something like using the services of that product. Of course, a lot of emphasis is made on ensuring that only authorized users avail the services and communication type is fast and light weight. REST APIs are the widely used ones since it provides an easy way to communicate between systems or with cloud services. REST (Representational State Transfer Technology) is a light weight approach for communication (meaning it consumes less bandwidth) usually used in web services development. In this chapter, we will learn on how to experiment with APIs that are available with the Jira product. Jira offers a number of APIs to perform multiple operations in Jira without logging into the tool. For example–
* you want to create a bug in Jira when you encounter an error with the pipeline execution in Jenkins; or
* you want to update status of a task based on some action performed outside Jira.
To perform an action in Jira, we will refer to URI paths. The Jira REST APIs use JSON as its communication format with standard HTTP methods like GET, POST, etc. Lets take a look at the URI structure-
“http://hostname:portnumber/rest/api/api-version/function”
The above structure refers to –
a. Jira hostname, followed by port number
b. Key words – /rest/api/ and api-version, in case you do not know the number, then its recommended you use – /rest/api/latest
c. Function – this refers to the activity that needs to be actioned
There is an API guide from Jira wherein they publish the latest library of functions. We will look at few examples in this chapter.
API calls can be done through various languages; for our learning we will use Microsoft PowerShell Scripting. You can perform actions in Jira with the help of URI paths.
Pre-requisites: Ensure that you have access to Jira projects
Example #1 -> Retrieve existing Jira Projects
Step 1 -> Open PowerShell ISE on your system as shown below-
Note– PowerShell ISE is an Integrated Scripting Environment, a Windows application that helps users to create, test and execute their scripts.
Step 2 -> Lets create a script to retrieve the project names with their properties for the Jira instance-
We first initialize the variables and then make an API call. The “Invoke-RESTMethod” sends HTTP requests to the REST web services that returns data that was requested. Save the file with the name “script-find-jira-projects.ps1“.
Step 3 -> After saving the script, run the script by either clicking on “F5” or by clicking the Run button as shown below-
The script will first ask for username and password and then call the API function for the Jira instance that’s locally installed on port 8090. Look out for the results in the bottom window in the ISE-
You would see that the script fetches the project list with their attributes. Remember in our previous chapters we had created a project TRIAL-PROJECT1, the same gets displayed in the result section. The API calls the function “project” from Jira server. Similar to this function, there is another function called “search” that displays all the bugs/tasks in the projects.
Wasn’t that easy ?? 🙂 Lets try out one more example.
Example # 2-> Creating Bug in Jira Project
Step 1 -> Create a new file in PowerShell ISE with the name “script-create-jira-ticket.ps1“-
Step 2 -> Save and run the script to view the results-
Once the bug is created in Jira, the script returns the bug ID, like for this example, TRIAL-3 is created in Jira.
Step 3 -> To verify the bug, access JIRA tool and check for the TRIAL-3 bug details.
Lets summarize-
(i). APIs are a convenient way to interact with a product like Jira. The API functions enables developers to strengthen their delivery pipeline like creating bugs when pipeline fails or making a check if a particular project /bug exists or not.
(ii). While Example 1 focused on retrieving project information using the API method “project” with HTTP GET method; Example 2 focused on creating a new bug in Jira project using the API method “issue” with HTTP POST method.
Try out these API functions, these are easy are use!
Happy Experimenting !!