Jira using C#

Jassim Al Rahma 1,521 Reputation points
2023-02-05T21:38:32.59+00:00

Hi,

Did anyone try to create an Issue on Jira using C# or nuget?

Can you please share your experience?

Thanks,

Jassim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,229 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-06T00:12:16.4733333+00:00

    You can use Jira Rest API.

    For example to create a ticket, you can use POST to Issue endpoint.

    Here is a class I created as JiraHelper, which has a method CreateTaskTicket, which accepts the following parameters:

    • jiraUri: This is the uri of your Jira, something like https://example.atlassian.net/
    • userName: This is the username that you use to login in Jira, something like user1@example.com
    • apiKey: This is the API key that you created for login. You cannot use your password, you first need to create an API key. To do so take a look at here: Api Tokens
    • projectKey: This is the key of your project. You can see it usually as initial of the tickets, or see it in the projects list, the key column.
    • summary: The title that you want to show for the ticket
    • description: The description test.

    Then to use the method, it's enough to call it like this:

    var result = await JiraHelper.CreateTaskTicket(
        uri, userName, apiKey, projectKey,
        "Hello, world!", 
        "This is a ticket created from C# code!");
    

    In case of a success, the result contains:

    • Id: Id of the task
    • Key: Key of the task
    • Self: Url of the task

    Here is the class JiraHelper

    using System.Net.Http.Headers;
    using System.Net.Http.Json;
    public class JiraHelper
    {
        private static HttpClient client = new HttpClient();
        public static async Task<JiraTicketResponse> CreateTaskTicket(Uri jiraUri, string userName, string apiKey, string projectKey, string summary, string description)
        {
            var authData = System.Text.Encoding.UTF8.GetBytes($"{userName}:{apiKey}");
            var basicAuthentication = Convert.ToBase64String(authData);
            client.BaseAddress = jiraUri;
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuthentication);
            var data = new
            {
                fields = new
                {
                    issuetype = new { id = 10002 /*Task*/ },
                    summary = summary,
                    project = new { key = projectKey /*Key of your project*/},
                    description = new
                    {
                        version = 1,
                        type = "doc",
                        content = new[] {
                            new {
                                type = "paragraph",
                                content = new []{
                                    new {
                                        type = "text",
                                        text =  description
                                    }
                                }
                            }
                        }
                    }
                }
            };
            var result = await client.PostAsJsonAsync("/rest/api/3/issue", data);
            if (result.StatusCode == System.Net.HttpStatusCode.Created)
                return await result.Content.ReadFromJsonAsync<JiraTicketResponse>();
            else
                throw new Exception(await result.Content.ReadAsStringAsync());
        }
    }
    public class JiraTicketResponse
    {
        public int Id { get; set; }
        public string Key { get; set; }
        public Uri Self { get; set; }
    }
    
    1 person found this answer helpful.