I am working on automating Jira ticket generation through code whenever a form submission fails. Though getting SuccessStatusCode from response, I am unable to view the created issue anywhere (Backlog or Active) in Jira board. Below is the code:
static async Task generateJiraTicket()
{
string jiraUrl = "<jiraUrl>";
string username = "<email>";
string apiToken = "<generated_token>";
// Create JSON payload for the new issue
string issueData = @"
{
""fields"": {
""project"": { ""key"": ""<Key>"" },
""summary"": ""Sample Jira Ticket for Forms"",
""description"": ""Form submission failed."",
""issuetype"": { ""name"": ""Bug"" }
}
}";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(jiraUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{apiToken}")));
try
{
// Send a POST request to create the issue
HttpResponseMessage response = await client.PostAsync(jiraUrl, new StringContent(issueData, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Issue created successfully!");
Console.WriteLine($"Ticket URL: {response.Headers.Location}");
Console.WriteLine($"Response content: {responseBody}");
}
else
{
Console.WriteLine($"Failed to create issue. Status code: {response.StatusCode}");
string errorResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response content: {errorResponse}");
}
}catch (Exception ex)
{
Console.WriteLine($"Response content: {ex}");
}
}
}
Am I missing something? Can someone help on this?