Retrieving Historical Data with the GIT API

We previously looked at retrieving data from GIT using a webhook to get real-time events, but if your primary goal is to only retrieve data from a particular point in time or in a batch fashion, doing so via their REST API might be more useful to you.

The Setup

Before starting, you are going to need 4 pieces of information to connect to GIT;

  • The API URL ((https://api.github.com/)
  • The Name of your Repo
  • An Access Token generated from the REPO.
  • The user who owns the Access Token that you are connecting with.

To generate a Personal Access Token, you are going to need to log into GitHub, Navigate to Settings and then in that long list, go all the way down to Developer Settings, on the next page you will select “Personal Access Tokens” from where you will be able to generate your token from.

The Code

Similar to Azure DevOps, you are going to use the HttpClient (if using C#) and connect to Git to search for your issues data. The most common use case I always want to start with is searching for data (presumably because I don’t know all that exists there). As with Azure DevOps, your date searches need to be formatted in UTC.

string UtcStartDate = startDate.ToUniversalTime().ToString("yyyy-MM-ddT00:00:00Z");

using (HttpClient client = new HttpClient())
{

            client.BaseAddress = new Uri(_gitApiUri);
            client.DefaultRequestHeaders.UserAgent.ParseAdd("HttpClient");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _GitToken);

            // Use GitHub Search API
            string query = $"repo:{_GitUser}/{_RepoUri} is:issue created:>{UtcStartDate}";
            string encodedQuery = Uri.EscapeDataString(query);
            string url = $"search/issues?q={encodedQuery}";

            HttpResponseMessage response = await client.GetAsync(url);
}

Once done, you will receive a full list of all issues that match your search. What is nice about the Git Api, is that you receive all information about the issue when you perform a search (no just the Issue Id).

For more information on the Git API, you can access the full reference here.