Accessing Custom Fields with JIRA Webhooks

When I was starting to work with the JIRA Webhooks, one problem I started to run into was understanding what some of the fields actually were. I vaguely remembered this from my years on customizing JIRA, where all fields are given a very unique field name because the level of customization is so extensive.

Case in point, these two fields here

I have a good idea what customfield_10016 refers to, but customfield_10019? No idea.

What You Need

To accomplish this task, we are going to need to connect the JIRA Rest API. Similar to other REST APIs, JIRA is going to require that you have a proper Jira project Url, AccessToken and Email of the user that the token belongs to.

When creating your token, you can do this from your primary Atlassian account (not your JIRA project tenant) – https://id.atlassian.com/manage/api-tokens

Important: Ensure you are providing the correct JIRA project URL that lines up with the field you are requesting, otherwise you might be surprised on the results you get back.

The Code

To get access to the field data, you can use the following snippet to make a call (based on your passed-in data) where you can then get back a list of all fields for that project.

_authHeader = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{email}:{token}"));
using (HttpClient client = new HttpClient()) {
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _authHeader);

var response = await client.GetAsync($"{_jiraBaseUrl}/rest/api/3/field");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();

JArray fieldmapping = JArray.Parse(json.ToString());

var fieldMap = new Dictionary<string, string>();

foreach (JObject field in fieldmapping)
{
if (!_cachedSchema.ContainsKey(field["key"].ToString()))
{
_cachedSchema.Add(field["key"].ToString(), field["name"].ToString());
}
}

}

In the above example, I was saving the cached entries to a local dictionary object so I only had to run this operation once.

Going back to our customfield_10019 field, I was now able to see that this is the Rank field and have a better idea of how to handle that data.