Retrieving Historical Data with the JIRA Api

Similar to AzureDevOps and Git, you can retrieve data from Jira using its own API. I had initially presumed that all work issue data would have been more closely aligned to Git, but instead found that the data and implementation were somewhere in the middle of AzureDevOps and Git.

The code implementation for querying for Jira data is very similar to how you use Wiql with Azure DevOps while the base connection to Jira is pretty much the same as when you connect with Git.

            string authHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_JiraUser}:{_JiraToken}"));

            string jiraBaseUrl = $"{_JiraApiUri}search?jql=";

            var client = new HttpClient();
            client.BaseAddress = new Uri(jiraBaseUrl);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            string jql = $"updated >= \"{DateOnly.FromDateTime(startDate)}\" AND updated <= \"{DateOnly.FromDateTime(endDate.Date)}\"";

            HttpResponseMessage response = await client.GetAsync(jiraBaseUrl + jql + "&maxResults=500");
            string responseData = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseData);

            if (response.IsSuccessStatusCode)
            {

                var data = await response.Content.ReadAsStringAsync();
                Console.WriteLine(data);
            }
            else
            {
                Console.WriteLine("Error: " + response.StatusCode);
            }

JQL is the structure used by Jira for querying data within its system. What is interesting here is that JIRA is not fond of time zones or the 12-hour clock when querying for data, and as such, only supports hours and minutes in its query. To get around this (if you’re only looking for items on a certain day), you can use the DateOnly function in C# to easily remove the time component from your query.

When you retrieve your results, they are again a mix of AzureDevOps and GIT because from GIT you don’t have to make a call to get all the data, but like AzureDevOps, the initial structure is a bit different.

In querying for this data, I did not even realize I was encroaching on the MaxResults that would be returned in my query, and added another parameter to the JQL query that allowed for over 50 results to be returned (maxResults=500). Once you dive into the issues, you’ll notice a new json parameter “expand” not there previously when receiving data via webhook. This field denotes additional data that could be queried for in the search results by adding the keyword “expand” to your Jql query.

For example, &expand=edit, would return metadata information on which fields are editable and how.