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!
![Image 1](/KB/cs/825377/Jira_Screen-r-700.png)