Click here to Skip to main content
13,196,946 members (55,185 online)
Click here to Skip to main content
Add your own
alternative version

Stats

19.7K views
973 downloads
11 bookmarked
Posted 5 Oct 2014

Creating a Issue in Jira using Rest Api

Rate this:
Please Sign up or sign in to vote.
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.

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.

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. 

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. 

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. 

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!

License

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

Share

About the Author

Abhijit Ghosh (Subho)
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionXSRF check failed Pin
Member 1306042420-Mar-17 1:07
memberMember 1306042420-Mar-17 1:07 
AnswerRe: XSRF check failed Pin
Shubh Dasgupta12-Jul-17 22:00
memberShubh Dasgupta12-Jul-17 22:00 
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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 5 Oct 2014
Article Copyright 2014 by Abhijit Ghosh (Subho)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid