Introduction
I have been doing a lot of work integrating with various systems, which leads to the need to utilize many varying api’s. One common data point I inevitably need to pull from the target system is a list of all users. I have recently been working with the JIRA REST API and unfortunately there is no single method to get a list of all users. In this post I will provide a simple example in C# utilizing the /rest/api/2/user/search method to gather the list of users.
Prerequisites
- You will need access to a JIRA instance with the REST API enabled
- Visual Studio. Visual Studio Community 2015
- Create a new console application
- Install Atlassian.SDK nuget package
Visual Studio
First create a simple user object to model the json data being returned from the api.
public class User { public bool Active { get; set; } public string DisplayName { get; set; } public string EmailAddress { get; set; } public string Key { get; set; } public string Locale { get; set; } public string Name { get; set; } public string Self { get; set; } public string TimeZone { get; set; } }
Next we create a simple wrapper around the jira client provided by the Atlassian SDK.
The Jira client has built in functions mostly for getting issues or projects from the api. Luckily it exposes the underlying rest client so you can execute any request you want against the jira api. In the GetAllUsers method I am making a request to user/search?username={item} while iterating through the alphabet. This request will search the username, name or email address of the user object in jira. Since a username will likely contain more than one letter, as the results come back for each request there will be duplicates, so we have to check to make sure the user in the result set is not already in our list. Clearly this is not going to be the most performant method, however there is no other way to gather a full list of all users. Finally, we can create the jira helper wrapper and invoke the GetAllUsers method.
class Program { static void Main(string[] args) { var helper = new JiraApiHelper(); var users = helper.GetAllUsers(); } }
Conclusion
As I stated above this solution is not going to be performant, especially if the Jira instance has a large number of users. However, if the need is to get the entire universe of users for the Jira instance then this is one approach that accomplishes that goal.