Serving DFW, our agency has travel agent Dallas waiting to take your call. Need Caribbean, Mexico, or Bahamas, river boat cruises, group discounts, give us a call! Premier Craft specializes in sheet metal forming, fabrication and design work. They are skilled in waterjet, cnc cutting, plasma, sheet metal, metal fabrication, architectural copper, decorative copper, custom fabrication, metal fabrication, gutter, heads, downspout, metal forming, roofing metal, shear, break metal, brake metal. Used Mixer Trucks new construction Edmond automotive locksmith okc, LockWorks OKC provides automotive and motorcycle locksmith services.
Looking to book your all-inclusive beach destination wedding in the Bahamas, Carribean or Mexico? Look no further for a Travel Agency Dallas. We handle river cruises, honeymoons, Disney vacations and so much more! As a Sandals and Beaches Resorts Preferred agency, we love the Caribbean.

Thursday, January 9, 2014

How to Post JIRA Issue Using C#/SharePoint/asp.net

Using the JIRA web service API,  you can use the POST REST method and submit an issue to a project.

View fields available in your environment when creating an Improvement type issue with project name PRJT:

https://xxx.jira.com/rest/api/2/issue/createmeta?projectKeys=PRJT&issuetypeNames=Improvement&expand=projects.issuetypes.fields
Make sure and replace xxx and/or the domain with your JIRA instance.  This will show you all fields that can be managed through the web service.



Create asp.net 4.5 Console Application Project in Visual Studio 2012.

Download NuGet Packages:
  • Json.NET
  • Microsoft ASP.NET Web API 2 Client
  • Microsoft HTTP Client Libraries


Here is the C# code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;


namespace ConsoleApplication5
{
    public class Issue
    {
        public Fields fields { get; set; }
        public Issue()
        {
            fields = new Fields();
        }
    }

    public class Fields
    {
        public Project project { get; set; }
        public string summary { get; set; }
        public string description { get; set; }
        public IssueType issuetype { get; set; }
        public Fields()
        {
            project = new Project();
            issuetype = new IssueType();
        }
    }

    public class Project
    {
        public string key { get; set; }
    }

    public class IssueType
    {
        public string name { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
           
            var data = new Issue();
            data.fields.project.key = "PRJT";
            data.fields.summary = "test";
            data.fields.description = "test";
            data.fields.issuetype.name = "Improvement";
           
            string postUrl = "https://xxx.jira.com/rest/api/latest/";

            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

            client.BaseAddress = new System.Uri(postUrl);
            byte[] cred = UTF8Encoding.UTF8.GetBytes("user:password");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();

            //System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<string>(data, jsonFormatter);
            System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data, jsonFormatter);
            System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
          
            if (response.IsSuccessStatusCode)
            {
                string result = response.Content.ReadAsStringAsync().Result;
                Console.Write(result);
            }
            else
            {
                Console.Write(response.StatusCode.ToString());
                Console.ReadLine();
            }

        }
    }
}


1 comment:

  1. This comment has been removed by the author.

    ReplyDelete