Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / XML
Tip/Trick

Creating a Issue in Jira using Rest Api

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
5 Oct 2014CPOL2 min read 46.2K   1.8K   12   7
Creating a Issue in Jira using Rest API and Microsoft XML Core Services (MSXML)

Introduction

This article shows an approach to create an issue in Atlassian Jira. Jira is a Issue and Project tracking software following Agile. The code and technique described in this article can be used to programmatically connect to Jira and create an Issue. It can be implemented inside an Error Handler module to automatically add issues to a Jira account when the program encounters an error. The error details are sent to Jira as JSON and the inputs go into the respective fields under that project.

Background

In my opinion, this is the most simple and short approach to the problem. I have used the XMLHTTP service in MSXML. A basic idea about consuming a web service should be enough to understand the code presented in this article. Since this is a console application, some idea about that is also needed.  

Using the code

Well, first things first. Create a console apllication project and add the MSXML2 reference to your project. Right click on References and add it. You will find the dll inside the System32 folder.
The code below is in my Jira class. There are 4 properties here, which are pretty straightforward. We have the username, password and the resource url. The last one, JsonString will contain the the project parameters in json format.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSXML2;

namespace TryJira
{
    class Jira
    {
        public String JiraUserName { get; set; }
        public String JiraPassword { get; set; }
        public String JiraUrl { get; set; }
        public String JiraJson { get; set; }

Now create an XMLHTTP request object and lets call it JiraService.

C#
private XMLHTTP60 JiraService = new XMLHTTP60();

This method adds the issue to the project. I got the resource url from the Jira Rest Api documentation. Once we open the connection via POST, we set the necessary headers. The content-type is set to json because we will be sending the project parameters as a json string. Next we set the authorization header and the credentials for basic http authentication. The GetEncodedCredentials() returns the base 64 encoded credentials for Jira login. Next we use the send method to send the json string to the Jira project. 

C#
public String addJiraIssue()
       {
           JiraService.open("POST", JiraUrl + "/rest/api/2/issue/");
           JiraService.setRequestHeader("Content-Type", "application/json");
           JiraService.setRequestHeader("Accept", "application/json");
           JiraService.setRequestHeader("Authorization", "Basic " + GetEncodedCredentials());
           JiraService.send(JiraJson);
           String response = JiraService.responseText;
           JiraService.abort();
           return response;
       }

The method below encodes the Jira credentails to base 64. The "username:password" string is converted to bytes and the byte array is encoded to base 64. 

C#
private string GetEncodedCredentials()
        {
            string mergedCredentials = string.Format("{0}:{1}", JiraUserName, JiraPassword);
            byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
            return Convert.ToBase64String(byteCredentials);
        }
    }
}

This is inside the Program class which contains the Main() method. I have created a Jira object here, to set the properties.The json string is avery basic representation contains only a few fields. You can check out the rest at your Jira_Url/rest/api/2/field. The response received from Jira is a json string which is displayed on the console.  It contains the issue id and self fields, which can be used to get the location of the issue in Jira. 

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TryJira
{
    class Program
    {
        static void Main(string[] args)
        {
            Jira objJira = new Jira();
            objJira.JiraUrl = "@YOUR_JIRA_URL@";
            objJira.JiraJson = @"{""fields"":{""project"":{""key"": ""@KEY@""},""summary"": ""@SUMMARY@"",
                               ""description"": ""@DESCRIPTION@"",""issuetype"": {""name"": ""@TYPE@""}}}";
            objJira.JiraUserName="@JIRA_USERNAME@";
            objJira.JiraPassword="@JIRA_PASSWORD@";
            Console.WriteLine(objJira.addJiraIssue());
            Console.ReadKey();
        }
    }
}

This is how the issue looks like in the Jira project that I had created for testing purposes. Best of Luck!

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) ICRA Online Limited
India India
About Me:
I am a junior software developer who is still learning how to write code that won't give others a headache. I love biking and travelling. I am also a big foodie and a cinephile.

Coding Style:
I am not a ninja programmer and I don't want to be one. I am the slowest developer you may ever encounter. I take a lot of time to develop code and I spend even more time reviewing it. My codes are always full of comments and summaries. Sometimes there are more comments than actual lines of code.

Pet Peeve:
A great software with zero or very little documentation. That is simply a great idea wasted, not to mention the time and labor of the dev team. Normally I wouldn't care unless I have to maintain it.

Liberal Views:
Free sharing of knowledge by the developer community has always amazed me. I do what I can to contribute. I believe that the 'end-user' rules. because his disturbingly feeble-minded needs drive me to build something better.

Egoistic Views:
I take pride in what I develop and the way I do it but I still bow down to all who can provide a better solution.

Comments and Discussions

 
BugHi bro. I get an error that says "Unauthorized (401)" Pin
emmanbiii23-Jan-21 23:32
emmanbiii23-Jan-21 23:32 
QuestionHow To update the JIRA issue Pin
singh Sujeet R15-Jul-19 2:23
singh Sujeet R15-Jul-19 2:23 
QuestionIt is not working for me Pin
singh Sujeet R25-Jun-19 21:41
singh Sujeet R25-Jun-19 21:41 
QuestionXSRF check failed Pin
Member 1306042420-Mar-17 1:07
Member 1306042420-Mar-17 1:07 
AnswerRe: XSRF check failed Pin
Shubh Dasgupta12-Jul-17 22:00
Shubh Dasgupta12-Jul-17 22:00 
GeneralRe: XSRF check failed Pin
Member 1188400012-Jun-19 23:28
Member 1188400012-Jun-19 23:28 
SuggestionPlease refer to the alternative article instead of this one Pin
Abhijit Ghosh (Subho) 31-Oct-14 19:59
professionalAbhijit Ghosh (Subho) 31-Oct-14 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.